blob: f341fd42a61310f4db584ef072e9dabf656dbdf0 [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);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003802 if (vim_strchr((char_u *)"+-*/%.", *eq) != NULL)
3803 {
3804 if (eq[0] == '.' && eq[1] == '.')
3805 ++eq;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003806 ++eq;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003807 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003808 }
3809 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003810 {
3811 eap->cmdidx = CMD_eval;
3812 return eap->cmd;
3813 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003814 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003815 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003816 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003817 return eap->cmd;
3818 }
3819 }
3820
3821 // Recognize an assignment if we recognize the variable name:
3822 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003823 // "@r = expr"
3824 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003825 // "var = expr" where "var" is a variable name or we are skipping
3826 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003827 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003828 oplen = assignment_len(skipwhite(p), &heredoc);
3829 if (oplen > 0)
3830 {
3831 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3832 || *eap->cmd == '&'
3833 || *eap->cmd == '$'
3834 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003835 || (eap->skip && IS_WHITE_OR_NUL(
3836 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003837 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003838 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003839 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003840 return eap->cmd;
3841 }
3842 }
3843
Bram Moolenaar9510d222022-09-11 15:14:05 +01003844 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003845 // "w:varname: number = 123".
3846 if (eap->cmd[1] == ':' && *p == ':')
3847 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003848 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003849 return eap->cmd;
3850 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003851 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003852
Bram Moolenaar31d99482022-05-26 22:24:43 +01003853 // 1234->func() is a method call
3854 if (number_method(eap->cmd))
3855 {
3856 eap->cmdidx = CMD_eval;
3857 return eap->cmd;
3858 }
3859
Bram Moolenaar7b829262021-10-13 15:04:34 +01003860 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3861 // an expression. A global/substitute/list command needs to use a
3862 // longer name.
3863 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3864 {
3865 eap->cmdidx = CMD_eval;
3866 return eap->cmd;
3867 }
3868
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003869 // If it is an ID it might be a variable with an operator on the next
3870 // line, if the variable exists it can't be an Ex command.
3871 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003872 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003873 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3874 {
3875 eap->cmdidx = CMD_eval;
3876 return eap->cmd;
3877 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003878
3879 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003880 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3881 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003882 {
3883 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3884 return eap->cmd + 2;
3885 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 }
3887#endif
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 /*
3890 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 */
3892 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003893 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 ++p;
Doug Kearnsea842022024-09-29 17:17:41 +02003896 if (full != NULL)
3897 *full = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899 else
3900 {
3901 while (ASCII_ISALPHA(*p))
3902 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003903 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003904 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003906 while (ASCII_ISALNUM(*p))
3907 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 }
3909 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3910 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003911 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 ++p;
3913 while (ASCII_ISALPHA(*p))
3914 ++p;
3915 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003916
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003917 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003918 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 ++p;
3920 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003921 // The "d" command can directly be followed by 'l' or 'p' flag, when
3922 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003923 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003924 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003925 // Check for ":dl", ":dell", etc. to ":deletel": that's
3926 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003927 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003928 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003929 break;
3930 if (i == len - 1)
3931 {
3932 --len;
3933 if (p[-1] == 'l')
3934 eap->flags |= EXFLAG_LIST;
3935 else
3936 eap->flags |= EXFLAG_PRINT;
3937 }
3938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003940 if (ASCII_ISLOWER(eap->cmd[0]))
3941 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003942 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003943 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003944
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003945 if (command_count != (int)CMD_SIZE)
3946 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003947 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003948 getout(1);
3949 }
3950
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003951 // Use a precomputed index for fast look-up in cmdnames[]
3952 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003953 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3954 if (ASCII_ISLOWER(c2))
3955 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3956 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003957 else if (ASCII_ISUPPER(eap->cmd[0]))
3958 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003960 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
3962 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3963 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3964 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3965 (size_t)len) == 0)
3966 {
3967#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003968 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 *full = TRUE;
3970#endif
3971 break;
3972 }
3973
Bram Moolenaar204852a2022-03-05 12:56:44 +00003974 // :Print and :mode are not supported in Vim9 script.
3975 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003976 if (vim9 && eap->cmdidx != CMD_SIZE)
3977 {
3978 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3979 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003980 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003981 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003982 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003983 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003984 eap->cmdidx = CMD_SIZE;
3985 }
3986 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003987
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003988 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003989 // 'cpoptions'.
3990 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3991 p = eap->cmd;
3992
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003993 // Look for a user defined command as a last resort. Let ":Print" be
3994 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003995 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3996 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003998 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 while (ASCII_ISALNUM(*p))
4000 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004001 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01004003 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 eap->cmdidx = CMD_SIZE;
4005 }
4006
Bram Moolenaar204852a2022-03-05 12:56:44 +00004007 // ":fina" means ":finally" in legacy script, for backwards compatibility.
4008 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02004009 eap->cmdidx = CMD_finally;
4010
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004011#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004012 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004013 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02004014 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004015 && (eap->cmdidx < 0 ||
4016 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004017 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004018 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4019
4020 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004021 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004022 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004023 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004024#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004025
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 return p;
4027}
4028
4029#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004030typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004031{
4032 char *name;
4033 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004034 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004035} cmdmod_info_T;
4036
4037static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004038 {"aboveleft", 3, FALSE},
4039 {"belowright", 3, FALSE},
4040 {"botright", 2, FALSE},
4041 {"browse", 3, FALSE},
4042 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004043 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004044 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004045 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004046 {"keepalt", 5, FALSE},
4047 {"keepjumps", 5, FALSE},
4048 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004049 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004050 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004051 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004052 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004053 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004054 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004055 {"rightbelow", 6, FALSE},
4056 {"sandbox", 3, FALSE},
4057 {"silent", 3, FALSE},
4058 {"tab", 3, TRUE},
4059 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004060 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004061 {"verbose", 4, TRUE},
4062 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004063 {"vim9cmd", 4, FALSE}
4064}; // cmdmod_info_tab
4065
Bram Moolenaared53fb92007-11-24 20:50:24 +00004066/*
4067 * Return length of a command modifier (including optional count).
4068 * Return zero when it's not a modifier.
4069 */
4070 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004071modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004072{
Christian Brabandt70a11a62024-07-26 19:13:55 +02004073 int i, j;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004074 char_u *p = cmd;
4075
4076 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004077 p = skipwhite(skipdigits(cmd + 1));
Christian Brabandt70a11a62024-07-26 19:13:55 +02004078 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004079 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004080 for (j = 0; p[j] != NUL; ++j)
4081 if (p[j] != cmdmod_info_tab[i].name[j])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004082 break;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004083 if (!ASCII_ISALPHA(p[j]) && j >= cmdmod_info_tab[i].minlen
4084 && (p == cmd || cmdmod_info_tab[i].has_count))
4085 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004086 }
4087 return 0;
4088}
4089
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090/*
4091 * Return > 0 if an Ex command "name" exists.
4092 * Return 2 if there is an exact match.
4093 * Return 3 if there is an ambiguous match.
4094 */
4095 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004096cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097{
4098 exarg_T ea;
4099 int full = FALSE;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004100 int i;
4101 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004102 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
Christian Brabandt70a11a62024-07-26 19:13:55 +02004104 // Check command modifiers.
4105 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004107 for (j = 0; name[j] != NUL; ++j)
4108 if (name[j] != cmdmod_info_tab[i].name[j])
4109 break;
4110 if (name[j] == NUL && j >= cmdmod_info_tab[i].minlen)
4111 return (cmdmod_info_tab[i].name[j] == NUL ? 2 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004114 // Check built-in commands and user defined commands.
4115 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004116 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004118 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004120 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004122 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4123 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004124 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004125 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4127}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004128
4129/*
4130 * "fullcommand" function
4131 */
4132 void
4133f_fullcommand(typval_T *argvars, typval_T *rettv)
4134{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004135 exarg_T ea;
4136 char_u *name;
4137 char_u *p;
4138 int vim9script = in_vim9script();
4139 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004140
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004141 rettv->v_type = VAR_STRING;
4142 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004143
Bram Moolenaaraa534142022-09-15 21:46:02 +01004144 if (in_vim9script()
4145 && (check_for_string_arg(argvars, 0) == FAIL
4146 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004147 return;
4148
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004149 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004150 if (name == NULL)
4151 return;
4152
Bram Moolenaaraa534142022-09-15 21:46:02 +01004153 if (argvars[1].v_type != VAR_UNKNOWN)
4154 {
4155 vim9script = tv_get_bool(&argvars[1]);
4156 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4157 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4158 }
4159
zeertzjqc024ed92022-01-04 16:22:52 +00004160 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004161 name++;
4162 name = skip_range(name, TRUE, NULL);
4163
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004164 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4165 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004166 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004167 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004168 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004169 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004170 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004171 goto theend;
4172
4173 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004174 {
4175 int res;
4176
4177 ++emsg_silent;
4178 res = not_in_vim9(&ea);
4179 --emsg_silent;
4180
4181 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004182 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004183 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004184
4185 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004186 ? get_user_command_name(ea.useridx, ea.cmdidx)
4187 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004188theend:
4189 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191#endif
4192
Bram Moolenaard0190392019-08-23 21:17:35 +02004193 cmdidx_T
4194excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195{
Bram Moolenaard0190392019-08-23 21:17:35 +02004196 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004198 if (!one_letter_cmd(cmd, &idx))
4199 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4200 idx = (cmdidx_T)((int)idx + 1))
4201 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4202 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
Bram Moolenaard0190392019-08-23 21:17:35 +02004204 return idx;
4205}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
Bram Moolenaard0190392019-08-23 21:17:35 +02004207 long
4208excmd_get_argt(cmdidx_T idx)
4209{
4210 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211}
4212
4213/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004214 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 *
4216 * Backslashed delimiters after / or ? will be skipped, and commands will
4217 * not be expanded between /'s and ?'s or after "'".
4218 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004219 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 * Returns the "cmd" pointer advanced to beyond the range.
4221 */
4222 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004223skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004224 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004225 int skip_star, // skip "*" used for Visual range
4226 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004228 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004229 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004231 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004233 if (*cmd == '\\')
4234 {
4235 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4236 ++cmd;
4237 else
4238 break;
4239 }
4240 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004242 char_u *p = cmd;
4243
4244 // a quote is only valid at the start or after a separator
4245 while (p > cmd_start)
4246 {
4247 --p;
4248 if (!VIM_ISWHITE(*p))
4249 break;
4250 }
4251 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4252 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (*++cmd == NUL && ctx != NULL)
4254 *ctx = EXPAND_NOTHING;
4255 }
4256 else if (*cmd == '/' || *cmd == '?')
4257 {
4258 delim = *cmd++;
4259 while (*cmd != NUL && *cmd != delim)
4260 if (*cmd++ == '\\' && *cmd != NUL)
4261 ++cmd;
4262 if (*cmd == NUL && ctx != NULL)
4263 *ctx = EXPAND_NOTHING;
4264 }
4265 if (*cmd != NUL)
4266 ++cmd;
4267 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004268
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004269 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004270 while (*cmd == ':')
4271 cmd = skipwhite(cmd + 1);
4272
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004273 // Skip "*" used for Visual range.
4274 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4275 cmd = skipwhite(cmd + 1);
4276
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 return cmd;
4278}
4279
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004280 static void
4281addr_error(cmd_addr_T addr_type)
4282{
4283 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004284 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004285 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004286 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004287}
4288
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004290 * Return the default address for an address type.
4291 */
4292 static linenr_T
4293default_address(exarg_T *eap)
4294{
4295 linenr_T lnum = 0;
4296
4297 switch (eap->addr_type)
4298 {
4299 case ADDR_LINES:
4300 case ADDR_OTHER:
4301 // Default is the cursor line number. Avoid using an invalid
4302 // line number though.
4303 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4304 lnum = curbuf->b_ml.ml_line_count;
4305 else
4306 lnum = curwin->w_cursor.lnum;
4307 break;
4308 case ADDR_WINDOWS:
4309 lnum = CURRENT_WIN_NR;
4310 break;
4311 case ADDR_ARGUMENTS:
4312 lnum = curwin->w_arg_idx + 1;
4313 if (lnum > ARGCOUNT)
4314 lnum = ARGCOUNT;
4315 break;
4316 case ADDR_LOADED_BUFFERS:
4317 case ADDR_BUFFERS:
4318 lnum = curbuf->b_fnum;
4319 break;
4320 case ADDR_TABS:
4321 lnum = CURRENT_TAB_NR;
4322 break;
4323 case ADDR_TABS_RELATIVE:
4324 case ADDR_UNSIGNED:
4325 lnum = 1;
4326 break;
4327 case ADDR_QUICKFIX:
4328#ifdef FEAT_QUICKFIX
4329 lnum = qf_get_cur_idx(eap);
4330#endif
4331 break;
4332 case ADDR_QUICKFIX_VALID:
4333#ifdef FEAT_QUICKFIX
4334 lnum = qf_get_cur_valid_idx(eap);
4335#endif
4336 break;
4337 case ADDR_NONE:
4338 // Will give an error later if a range is found.
4339 break;
4340 }
4341 return lnum;
4342}
4343
4344/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004345 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 *
4347 * Set ptr to the next character after the part that was interpreted.
4348 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004349 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 *
4351 * Return MAXLNUM when no Ex address was found.
4352 */
4353 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004354get_address(
4355 exarg_T *eap UNUSED,
4356 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004357 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004358 int skip, // only skip the address, don't use it
4359 int silent, // no errors or side effects
4360 int to_other_file, // flag: may jump to other file
4361 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362{
4363 int c;
4364 int i;
4365 long n;
4366 char_u *cmd;
4367 pos_T pos;
4368 pos_T *fp;
4369 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004370 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371
4372 cmd = skipwhite(*ptr);
4373 lnum = MAXLNUM;
4374 do
4375 {
4376 switch (*cmd)
4377 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004378 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004379 ++cmd;
4380 switch (addr_type)
4381 {
4382 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004383 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 lnum = curwin->w_cursor.lnum;
4385 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004387 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004388 break;
4389 case ADDR_ARGUMENTS:
4390 lnum = curwin->w_arg_idx + 1;
4391 break;
4392 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004393 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004394 lnum = curbuf->b_fnum;
4395 break;
4396 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004397 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004398 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004399 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004400 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004401 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004402 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004403 cmd = NULL;
4404 goto error;
4405 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004406 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004407#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004408 lnum = qf_get_cur_idx(eap);
4409#endif
4410 break;
4411 case ADDR_QUICKFIX_VALID:
4412#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004413 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004414#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004415 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004416 }
4417 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004419 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 ++cmd;
4421 switch (addr_type)
4422 {
4423 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004424 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 lnum = curbuf->b_ml.ml_line_count;
4426 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004427 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004428 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 break;
4430 case ADDR_ARGUMENTS:
4431 lnum = ARGCOUNT;
4432 break;
4433 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004434 buf = lastbuf;
4435 while (buf->b_ml.ml_mfp == NULL)
4436 {
4437 if (buf->b_prev == NULL)
4438 break;
4439 buf = buf->b_prev;
4440 }
4441 lnum = buf->b_fnum;
4442 break;
4443 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004444 lnum = lastbuf->b_fnum;
4445 break;
4446 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004447 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004449 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004450 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004451 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004452 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004453 cmd = NULL;
4454 goto error;
4455 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004456 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004457#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004458 lnum = qf_get_size(eap);
4459 if (lnum == 0)
4460 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004461#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004462 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004463 case ADDR_QUICKFIX_VALID:
4464#ifdef FEAT_QUICKFIX
4465 lnum = qf_get_valid_size(eap);
4466 if (lnum == 0)
4467 lnum = 1;
4468#endif
4469 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004470 }
4471 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004473 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004474 if (*++cmd == NUL)
4475 {
4476 cmd = NULL;
4477 goto error;
4478 }
4479 if (addr_type != ADDR_LINES)
4480 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004481 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004482 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004483 goto error;
4484 }
4485 if (skip)
4486 ++cmd;
4487 else
4488 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004489 // Only accept a mark in another file when it is
4490 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4492 ++cmd;
4493 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004494 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 lnum = curwin->w_cursor.lnum;
4496 else
4497 {
4498 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 {
4500 cmd = NULL;
4501 goto error;
4502 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 lnum = fp->lnum;
4504 }
4505 }
4506 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
4508 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004509 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 c = *cmd++;
4511 if (addr_type != ADDR_LINES)
4512 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004513 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004514 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004515 goto error;
4516 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004517 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004519 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004520 if (*cmd == c)
4521 ++cmd;
4522 }
4523 else
4524 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004525 int flags;
4526
4527 pos = curwin->w_cursor; // save curwin->w_cursor
4528
4529 // When '/' or '?' follows another address, start from
4530 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004531 if (lnum > 0 && lnum != MAXLNUM)
4532 curwin->w_cursor.lnum =
4533 lnum > curbuf->b_ml.ml_line_count
4534 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004535
4536 // Start a forward search at the end of the line (unless
4537 // before the first line).
4538 // Start a backward search at the start of the line.
4539 // This makes sure we never match in the current
4540 // line, and can match anywhere in the
4541 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004542 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004543 curwin->w_cursor.col = MAXCOL;
4544 else
4545 curwin->w_cursor.col = 0;
4546 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004547 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004548 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004549 {
4550 curwin->w_cursor = pos;
4551 cmd = NULL;
4552 goto error;
4553 }
4554 lnum = curwin->w_cursor.lnum;
4555 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004556 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004557 cmd += searchcmdlen;
4558 }
4559 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004561 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004562 ++cmd;
4563 if (addr_type != ADDR_LINES)
4564 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004565 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004566 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004567 goto error;
4568 }
4569 if (*cmd == '&')
4570 i = RE_SUBST;
4571 else if (*cmd == '?' || *cmd == '/')
4572 i = RE_SEARCH;
4573 else
4574 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004575 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 cmd = NULL;
4577 goto error;
4578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004580 if (!skip)
4581 {
4582 /*
4583 * When search follows another address, start from
4584 * there.
4585 */
4586 if (lnum != MAXLNUM)
4587 pos.lnum = lnum;
4588 else
4589 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004591 /*
4592 * Start the search just like for the above
4593 * do_search().
4594 */
4595 if (*cmd != '?')
4596 pos.col = MAXCOL;
4597 else
4598 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004599 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004600 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004601 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004602 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004603 lnum = pos.lnum;
4604 else
4605 {
4606 cmd = NULL;
4607 goto error;
4608 }
4609 }
4610 ++cmd;
4611 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
4613 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004614 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004615 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 }
4617
4618 for (;;)
4619 {
4620 cmd = skipwhite(cmd);
4621 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4622 break;
4623
4624 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004625 {
4626 switch (addr_type)
4627 {
4628 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004629 case ADDR_OTHER:
4630 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004631 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004632 break;
4633 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004634 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004635 break;
4636 case ADDR_ARGUMENTS:
4637 lnum = curwin->w_arg_idx + 1;
4638 break;
4639 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004640 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004641 lnum = curbuf->b_fnum;
4642 break;
4643 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004644 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004645 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004646 case ADDR_TABS_RELATIVE:
4647 lnum = 1;
4648 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004649 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004650#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004651 lnum = qf_get_cur_idx(eap);
4652#endif
4653 break;
4654 case ADDR_QUICKFIX_VALID:
4655#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004656 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004657#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004658 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004659 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004660 case ADDR_UNSIGNED:
4661 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004662 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004663 }
4664 }
4665
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004667 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 else
4669 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004670 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 n = 1;
4672 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004673 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004674 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004676 if (n == MAXLNUM)
4677 {
4678 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004679 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004680 goto error;
4681 }
4682 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004683
4684 if (addr_type == ADDR_TABS_RELATIVE)
4685 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004686 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004687 cmd = NULL;
4688 goto error;
4689 }
4690 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004691 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004692 lnum = compute_buffer_local_count(
4693 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004695 {
4696#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004697 // Relative line addressing: need to adjust for lines in a
4698 // closed fold after the first address.
4699 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4700 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004701 (void)hasFolding(lnum, NULL, &lnum);
4702#endif
4703 if (i == '-')
4704 lnum -= n;
4705 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004706 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004707 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004708 {
4709 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004710 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004711 goto error;
4712 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004713 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004714 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
4717 } while (*cmd == '/' || *cmd == '?');
4718
4719error:
4720 *ptr = cmd;
4721 return lnum;
4722}
4723
4724/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004725 * Set eap->line1 and eap->line2 to the whole range.
4726 * Used for commands with the EX_DFLALL flag and no range given.
4727 */
4728 static void
4729address_default_all(exarg_T *eap)
4730{
4731 eap->line1 = 1;
4732 switch (eap->addr_type)
4733 {
4734 case ADDR_LINES:
4735 case ADDR_OTHER:
4736 eap->line2 = curbuf->b_ml.ml_line_count;
4737 break;
4738 case ADDR_LOADED_BUFFERS:
4739 {
4740 buf_T *buf = firstbuf;
4741
4742 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4743 buf = buf->b_next;
4744 eap->line1 = buf->b_fnum;
4745 buf = lastbuf;
4746 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4747 buf = buf->b_prev;
4748 eap->line2 = buf->b_fnum;
4749 }
4750 break;
4751 case ADDR_BUFFERS:
4752 eap->line1 = firstbuf->b_fnum;
4753 eap->line2 = lastbuf->b_fnum;
4754 break;
4755 case ADDR_WINDOWS:
4756 eap->line2 = LAST_WIN_NR;
4757 break;
4758 case ADDR_TABS:
4759 eap->line2 = LAST_TAB_NR;
4760 break;
4761 case ADDR_TABS_RELATIVE:
4762 eap->line2 = 1;
4763 break;
4764 case ADDR_ARGUMENTS:
4765 if (ARGCOUNT == 0)
4766 eap->line1 = eap->line2 = 0;
4767 else
4768 eap->line2 = ARGCOUNT;
4769 break;
4770 case ADDR_QUICKFIX_VALID:
4771#ifdef FEAT_QUICKFIX
4772 eap->line2 = qf_get_valid_size(eap);
4773 if (eap->line2 == 0)
4774 eap->line2 = 1;
4775#endif
4776 break;
4777 case ADDR_NONE:
4778 case ADDR_UNSIGNED:
4779 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004780 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004781 break;
4782 }
4783}
4784
4785
4786/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004787 * Get flags from an Ex command argument.
4788 */
4789 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004790get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004791{
4792 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4793 {
4794 if (*eap->arg == 'l')
4795 eap->flags |= EXFLAG_LIST;
4796 else if (*eap->arg == 'p')
4797 eap->flags |= EXFLAG_PRINT;
4798 else
4799 eap->flags |= EXFLAG_NR;
4800 eap->arg = skipwhite(eap->arg + 1);
4801 }
4802}
4803
4804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 * Function called for command which is Not Implemented. NI!
4806 */
4807 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004808ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809{
4810 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004811 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004812 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813}
4814
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004815#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816/*
4817 * Function called for script command which is Not Implemented. NI!
4818 * Skips over ":perl <<EOF" constructs.
4819 */
4820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004821ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
4823 if (!eap->skip)
4824 ex_ni(eap);
4825 else
4826 vim_free(script_get(eap, eap->arg));
4827}
4828#endif
4829
4830/*
4831 * Check range in Ex command for validity.
4832 * Return NULL when valid, error message when invalid.
4833 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004834 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004835invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004837 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004838
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if ( eap->line1 < 0
4840 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004841 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004842 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004843
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004844 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004845 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004846 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004847 {
4848 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004849 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004850#ifdef FEAT_DIFF
4851 + (eap->cmdidx == CMD_diffget)
4852#endif
4853 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004854 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004855 break;
4856 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004857 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004858 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004859 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004860 break;
4861 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004862 // Only a boundary check, not whether the buffers actually
4863 // exist.
4864 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004865 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004866 break;
4867 case ADDR_LOADED_BUFFERS:
4868 buf = firstbuf;
4869 while (buf->b_ml.ml_mfp == NULL)
4870 {
4871 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004872 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004873 buf = buf->b_next;
4874 }
4875 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004876 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004877 buf = lastbuf;
4878 while (buf->b_ml.ml_mfp == NULL)
4879 {
4880 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004881 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004882 buf = buf->b_prev;
4883 }
4884 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004885 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004886 break;
4887 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004888 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004889 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004890 break;
4891 case ADDR_TABS:
4892 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004893 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004894 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004895 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004896 case ADDR_OTHER:
4897 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004898 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004899 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004900#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004901 // No error for value that is too big, will use the last entry.
4902 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004903 {
4904 if (eap->addr_count == 0)
4905 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004906 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004907 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004908#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004909 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004910 case ADDR_QUICKFIX_VALID:
4911#ifdef FEAT_QUICKFIX
4912 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4913 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004914 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004915#endif
4916 break;
4917 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004918 case ADDR_NONE:
4919 // Will give an error elsewhere.
4920 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004921 }
4922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 return NULL;
4924}
4925
4926/*
4927 * Correct the range for zero line number, if required.
4928 */
4929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004930correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004932 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 {
4934 if (eap->line1 == 0)
4935 eap->line1 = 1;
4936 if (eap->line2 == 0)
4937 eap->line2 = 1;
4938 }
4939}
4940
Bram Moolenaar748bf032005-02-02 23:04:36 +00004941#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004942/*
4943 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4944 * pattern. Otherwise return eap->arg.
4945 */
4946 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004947skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004948{
4949 char_u *p = eap->arg;
4950
Bram Moolenaara37420f2006-02-04 22:37:47 +00004951 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4952 || eap->cmdidx == CMD_vimgrepadd
4953 || eap->cmdidx == CMD_lvimgrepadd
4954 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004955 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004956 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004957 if (p == NULL)
4958 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004959 }
4960 return p;
4961}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004962
4963/*
4964 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4965 * in the command line, so that things like % get expanded.
4966 */
4967 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004968replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004969{
4970 char_u *new_cmdline;
4971 char_u *program;
4972 char_u *pos;
4973 char_u *ptr;
4974 int len;
4975 int i;
4976
4977 /*
4978 * Don't do it when ":vimgrep" is used for ":grep".
4979 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004980 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4981 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4982 || eap->cmdidx == CMD_grepadd
4983 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004984 && !grep_internal(eap->cmdidx))
4985 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004986 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4987 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004988 {
4989 if (*curbuf->b_p_gp == NUL)
4990 program = p_gp;
4991 else
4992 program = curbuf->b_p_gp;
4993 }
4994 else
4995 {
4996 if (*curbuf->b_p_mp == NUL)
4997 program = p_mp;
4998 else
4999 program = curbuf->b_p_mp;
5000 }
5001
5002 p = skipwhite(p);
5003
5004 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5005 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005006 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005007 i = 1;
5008 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
5009 ++i;
5010 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005011 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005012 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005013 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005014 ptr = new_cmdline;
5015 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5016 {
5017 i = (int)(pos - program);
5018 STRNCPY(ptr, program, i);
5019 STRCPY(ptr += i, p);
5020 ptr += len;
5021 program = pos + 2;
5022 }
5023 STRCPY(ptr, program);
5024 }
5025 else
5026 {
John Marriotted908f72024-04-18 22:46:56 +02005027 size_t program_len = STRLEN(program);
5028
5029 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005030 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005031 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005032 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005033 STRCPY(new_cmdline + program_len, " ");
5034 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005035 }
5036 msg_make(p);
5037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005038 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005039 vim_free(*cmdlinep);
5040 *cmdlinep = new_cmdline;
5041 p = new_cmdline;
5042 }
5043 return p;
5044}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005045#endif
5046
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047/*
5048 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005049 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 * Return FAIL for failure, OK otherwise.
5051 */
5052 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005053expand_filename(
5054 exarg_T *eap,
5055 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005056 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005058 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005060 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 char_u *p;
5062 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005063 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
Bram Moolenaar748bf032005-02-02 23:04:36 +00005065#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005066 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005067 p = skip_grep_pat(eap);
5068#else
5069 p = eap->arg;
5070#endif
5071
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 /*
5073 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5074 * the file name contains a wildcard it should not cause expanding.
5075 * (it will be expanded anyway if there is a wildcard before replacing).
5076 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005077 has_wildcards = mch_has_wildcard(p);
5078 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005080#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005081 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005082 if (p[0] == '`' && p[1] == '=')
5083 {
5084 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005085 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005086 if (*p == '`')
5087 ++p;
5088 continue;
5089 }
5090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 /*
5092 * Quick check if this cannot be the start of a special string.
5093 * Also removes backslash before '%', '#' and '<'.
5094 */
5095 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5096 {
5097 ++p;
5098 continue;
5099 }
5100
5101 /*
5102 * Try to find a match at this position.
5103 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005104 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005105 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005106 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005108 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 {
5110 p += srclen;
5111 continue;
5112 }
5113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005114 // Wildcards won't be expanded below, the replacement is taken
5115 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005116 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5117 {
5118 char_u *l = repl;
5119
5120 repl = expand_env_save(repl);
5121 vim_free(l);
5122 }
5123
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005124 // Need to escape white space et al. with a backslash.
5125 // Don't do this for:
5126 // - replacement that already has been escaped: "##"
5127 // - shell commands (may have to use quotes instead).
5128 // - non-unix systems when there is a single argument (spaces don't
5129 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005131 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 && eap->cmdidx != CMD_grep
5134 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005135 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005136 && eap->cmdidx != CMD_lgrep
5137 && eap->cmdidx != CMD_lgrepadd
5138 && eap->cmdidx != CMD_lmake
5139 && eap->cmdidx != CMD_make
5140 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005142 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143#endif
5144 )
5145 {
5146 char_u *l;
5147#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005148 // Don't escape a backslash here, because rem_backslash() doesn't
5149 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 static char_u *nobslash = (char_u *)" \t\"|";
5151# define ESCAPE_CHARS nobslash
5152#else
5153# define ESCAPE_CHARS escape_chars
5154#endif
5155
5156 for (l = repl; *l; ++l)
5157 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5158 {
5159 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5160 if (l != NULL)
5161 {
5162 vim_free(repl);
5163 repl = l;
5164 }
5165 break;
5166 }
5167 }
5168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005169 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005170 if ((eap->usefilter || eap->cmdidx == CMD_bang
5171 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005172 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
5174 char_u *l;
5175
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005176 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 if (l != NULL)
5178 {
5179 vim_free(repl);
5180 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182 }
5183
Mike Williams51024bb2024-05-30 07:46:30 +02005184 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 vim_free(repl);
5186 if (p == NULL)
5187 return FAIL;
5188 }
5189
5190 /*
5191 * One file argument: Expand wildcards.
5192 * Don't do this with ":r !command" or ":w !command".
5193 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005194 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
5196 /*
5197 * May do this twice:
5198 * 1. Replace environment variables.
5199 * 2. Replace any other wildcards, remove backslashes.
5200 */
5201 for (n = 1; n <= 2; ++n)
5202 {
5203 if (n == 2)
5204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 /*
5206 * Halve the number of backslashes (this is Vi compatible).
5207 * For Unix and OS/2, when wildcards are expanded, this is
5208 * done by ExpandOne() below.
5209 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005210#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 if (!has_wildcards)
5212#endif
5213 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
5215
5216 if (has_wildcards)
5217 {
5218 if (n == 1)
5219 {
5220 /*
5221 * First loop: May expand environment variables. This
5222 * can be done much faster with expand_env() than with
5223 * something else (e.g., calling a shell).
5224 * After expanding environment variables, check again
5225 * if there are still wildcards present.
5226 */
5227 if (vim_strchr(eap->arg, '$') != NULL
5228 || vim_strchr(eap->arg, '~') != NULL)
5229 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005230 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005231 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 has_wildcards = mch_has_wildcard(NameBuff);
5233 p = NameBuff;
5234 }
5235 else
5236 p = NULL;
5237 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005238 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 {
5240 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005241 int options = WILD_LIST_NOTFOUND
5242 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243
5244 ExpandInit(&xpc);
5245 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005246 if (p_wic)
5247 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005249 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 if (p == NULL)
5251 return FAIL;
5252 }
5253 if (p != NULL)
5254 {
John Marriotted908f72024-04-18 22:46:56 +02005255 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005257 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 vim_free(p);
5259 }
5260 }
5261 }
5262 }
5263 return OK;
5264}
5265
5266/*
5267 * Replace part of the command line, keeping eap->cmd, eap->arg and
5268 * eap->nextcmd correct.
5269 * "src" points to the part that is to be replaced, of length "srclen".
5270 * "repl" is the replacement string.
5271 * Returns a pointer to the character after the replaced string.
5272 * Returns NULL for failure.
5273 */
5274 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005275repl_cmdline(
5276 exarg_T *eap,
5277 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005278 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005279 char_u *repl,
5280 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281{
John Marriotted908f72024-04-18 22:46:56 +02005282 size_t repllen;
5283 size_t taillen;
5284 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005286 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
5288 /*
5289 * The new command line is build in new_cmdline[].
5290 * First allocate it.
5291 * Careful: a "+cmd" argument may have been NUL terminated.
5292 */
John Marriotted908f72024-04-18 22:46:56 +02005293 repllen = STRLEN(repl);
5294 taillen = STRLEN(src + srclen);
5295 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005296 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005297 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005298 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005299 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301 /*
5302 * Copy the stuff before the expanded part.
5303 * Copy the expanded stuff.
5304 * Copy what came after the expanded part.
5305 * Copy the next commands, if there are any.
5306 */
John Marriotted908f72024-04-18 22:46:56 +02005307 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5308 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005309
John Marriotted908f72024-04-18 22:46:56 +02005310 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5311 new_cmdlinelen += repllen; // remember the end of the string
5312 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5313 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005315 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
John Marriotted908f72024-04-18 22:46:56 +02005317 new_cmdlinelen += taillen + 1;
5318 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5319 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5322 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5323 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5324 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5325 vim_free(*cmdlinep);
5326 *cmdlinep = new_cmdline;
5327
5328 return src;
5329}
5330
5331/*
5332 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005333 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 */
5335 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005336separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337{
5338 char_u *p;
5339
Bram Moolenaar86b68352004-12-27 21:59:20 +00005340#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005341 p = skip_grep_pat(eap);
5342#else
5343 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005344#endif
5345
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005346 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
5348 if (*p == Ctrl_V)
5349 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005350 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005351 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005353 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005354 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005355 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 break;
5357 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005358
5359#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005360 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005361 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005362 {
5363 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005364 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005365 if (*p == NUL) // stop at NUL after CTRL-V
5366 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005367 }
5368#endif
5369
Bram Moolenaareda29c92022-10-02 12:59:00 +01005370 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005371 // :@" and :*" do not start a comment!
5372 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005373 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005374 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005375 && !(eap->argt & EX_NOTRLCOM)
5376 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5377 || p != eap->arg)
5378 && (eap->cmdidx != CMD_redir
5379 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005380 || (*p == '#'
5381 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005382 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005383 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005384 || (*p == '|'
5385 && eap->cmdidx != CMD_append
5386 && eap->cmdidx != CMD_change
5387 && eap->cmdidx != CMD_insert)
5388 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
5390 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005391 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 * AND 'b' is present in 'cpoptions'.
5393 */
5394 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005395 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005397 if (!keep_backslash)
5398 {
5399 STRMOVE(p - 1, p); // remove the '\'
5400 --p;
5401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 }
5403 else
5404 {
5405 eap->nextcmd = check_nextcmd(p);
5406 *p = NUL;
5407 break;
5408 }
5409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005411
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005412 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 del_trailing_spaces(eap->arg);
5414}
5415
5416/*
5417 * get + command from ex argument
5418 */
5419 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005420getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421{
5422 char_u *arg = *argp;
5423 char_u *command = NULL;
5424
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005425 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 {
5427 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005428 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 command = dollar_command;
5430 else
5431 {
5432 command = arg;
5433 arg = skip_cmd_arg(command, TRUE);
5434 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005435 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 }
5437
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005438 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 *argp = arg;
5440 }
5441 return command;
5442}
5443
5444/*
5445 * Find end of "+command" argument. Skip over "\ " and "\\".
5446 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005447 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005448skip_cmd_arg(
5449 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005450 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
5452 while (*p && !vim_isspace(*p))
5453 {
5454 if (*p == '\\' && p[1] != NUL)
5455 {
5456 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005457 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 else
5459 ++p;
5460 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005461 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
5463 return p;
5464}
5465
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005466 int
5467get_bad_opt(char_u *p, exarg_T *eap)
5468{
5469 if (STRICMP(p, "keep") == 0)
5470 eap->bad_char = BAD_KEEP;
5471 else if (STRICMP(p, "drop") == 0)
5472 eap->bad_char = BAD_DROP;
5473 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5474 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005475 else
5476 return FAIL;
5477 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005478}
5479
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005481 * Function given to ExpandGeneric() to obtain the list of bad= names.
5482 */
5483 static char_u *
5484get_bad_name(expand_T *xp UNUSED, int idx)
5485{
5486 // Note: Keep this in sync with getargopt.
5487 static char *(p_bad_values[]) =
5488 {
5489 "?",
5490 "keep",
5491 "drop",
5492 };
5493
John Marriotted908f72024-04-18 22:46:56 +02005494 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5495 return NULL;
5496
5497 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005498}
5499
5500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 * Get "++opt=arg" argument.
5502 * Return FAIL or OK.
5503 */
5504 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005505getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
5507 char_u *arg = eap->arg + 2;
5508 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005509 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005512 // Note: Keep this in sync with get_argopt_name.
5513
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005514 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5516 {
5517 if (*arg == 'n')
5518 {
5519 arg += 2;
5520 eap->force_bin = FORCE_NOBIN;
5521 }
5522 else
5523 eap->force_bin = FORCE_BIN;
5524 if (!checkforcmd(&arg, "binary", 3))
5525 return FAIL;
5526 eap->arg = skipwhite(arg);
5527 return OK;
5528 }
5529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005530 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005531 if (STRNCMP(arg, "edit", 4) == 0)
5532 {
5533 eap->read_edit = TRUE;
5534 eap->arg = skipwhite(arg + 4);
5535 return OK;
5536 }
5537
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 if (STRNCMP(arg, "ff", 2) == 0)
5539 {
5540 arg += 2;
5541 pp = &eap->force_ff;
5542 }
5543 else if (STRNCMP(arg, "fileformat", 10) == 0)
5544 {
5545 arg += 10;
5546 pp = &eap->force_ff;
5547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 else if (STRNCMP(arg, "enc", 3) == 0)
5549 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005550 if (STRNCMP(arg, "encoding", 8) == 0)
5551 arg += 8;
5552 else
5553 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 pp = &eap->force_enc;
5555 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005556 else if (STRNCMP(arg, "bad", 3) == 0)
5557 {
5558 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005559 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
5562 if (pp == NULL || *arg != '=')
5563 return FAIL;
5564
5565 ++arg;
5566 *pp = (int)(arg - eap->cmd);
5567 arg = skip_cmd_arg(arg, FALSE);
5568 eap->arg = skipwhite(arg);
5569 *arg = NUL;
5570
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 if (pp == &eap->force_ff)
5572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5574 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005575 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005577 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005579 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5581 *p = TOLOWER_ASC(*p);
5582 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005583 else
5584 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005585 // Check ++bad= argument. Must be a single-byte character, "keep" or
5586 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005587 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005588 return FAIL;
5589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
5591 return OK;
5592}
5593
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005594/*
5595 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5596 */
5597 static char_u *
5598get_argopt_name(expand_T *xp UNUSED, int idx)
5599{
5600 // Note: Keep this in sync with getargopt.
5601 static char *(p_opt_values[]) =
5602 {
5603 "fileformat=",
5604 "encoding=",
5605 "binary",
5606 "nobinary",
5607 "bad=",
5608 "edit",
5609 };
5610
John Marriotted908f72024-04-18 22:46:56 +02005611 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5612 return NULL;
5613
5614 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005615}
5616
5617/*
5618 * Command-line expansion for ++opt=name.
5619 */
5620 int
5621expand_argopt(
5622 char_u *pat,
5623 expand_T *xp,
5624 regmatch_T *rmp,
5625 char_u ***matches,
5626 int *numMatches)
5627{
5628 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5629 {
5630 char_u *(*cb)(expand_T *, int) = NULL;
5631
5632 char_u *name_end = xp->xp_pattern - 1;
5633 if (name_end - xp->xp_line >= 2
5634 && STRNCMP(name_end - 2, "ff", 2) == 0)
5635 cb = get_fileformat_name;
5636 else if (name_end - xp->xp_line >= 10
5637 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5638 cb = get_fileformat_name;
5639 else if (name_end - xp->xp_line >= 3
5640 && STRNCMP(name_end - 3, "enc", 3) == 0)
5641 cb = get_encoding_name;
5642 else if (name_end - xp->xp_line >= 8
5643 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5644 cb = get_encoding_name;
5645 else if (name_end - xp->xp_line >= 3
5646 && STRNCMP(name_end - 3, "bad", 3) == 0)
5647 cb = get_bad_name;
5648
5649 if (cb != NULL)
5650 {
5651 return ExpandGeneric(
5652 pat,
5653 xp,
5654 rmp,
5655 matches,
5656 numMatches,
5657 cb,
5658 FALSE);
5659 }
5660 return FAIL;
5661 }
5662
5663 // Special handling of "ff" which acts as a short form of
5664 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005665 if (xp->xp_pattern_len == 2
5666 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005667 {
5668 *matches = ALLOC_MULT(char_u *, 1);
5669 if (*matches == NULL)
5670 return FAIL;
5671 *numMatches = 1;
5672 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5673 return OK;
5674 }
5675
5676 return ExpandGeneric(
5677 pat,
5678 xp,
5679 rmp,
5680 matches,
5681 numMatches,
5682 get_argopt_name,
5683 FALSE);
5684}
5685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005687ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005690 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 * directory for security reasons.
5692 */
5693 if (secure)
5694 {
5695 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005696 eap->errmsg =
5697 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
5699 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005700 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 else
5702 do_augroup(eap->arg, eap->forceit);
5703}
5704
5705/*
5706 * ":doautocmd": Apply the automatic commands to the current buffer.
5707 */
5708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005709ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005711 char_u *arg = eap->arg;
5712 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005713 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005714
Bram Moolenaar1610d052016-06-09 22:53:01 +02005715 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005716 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005717 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005718 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721/*
5722 * :[N]bunload[!] [N] [bufname] unload buffer
5723 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5724 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5725 */
5726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005727ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005729 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005730 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 eap->errmsg = do_bufdel(
5732 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5733 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5734 : DOBUF_UNLOAD, eap->arg,
5735 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5736}
5737
5738/*
5739 * :[N]buffer [N] to buffer N
5740 * :[N]sbuffer [N] to buffer N
5741 */
5742 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005743ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005745 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005746 return;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01005747 do_exbuffer(eap);
5748}
5749
5750/*
5751 * ":buffer" command and alike.
5752 */
5753 static void
5754do_exbuffer(exarg_T *eap)
5755{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005757 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 else
5759 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005760 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5762 else
5763 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005764 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005765 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767}
5768
5769/*
5770 * :[N]bmodified [N] to next mod. buffer
5771 * :[N]sbmodified [N] to next mod. buffer
5772 */
5773 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005774ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005777 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005778 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779}
5780
5781/*
5782 * :[N]bnext [N] to next buffer
5783 * :[N]sbnext [N] split and to next buffer
5784 */
5785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005786ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005788 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005789 return;
5790
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005792 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005793 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794}
5795
5796/*
5797 * :[N]bNext [N] to previous buffer
5798 * :[N]bprevious [N] to previous buffer
5799 * :[N]sbNext [N] split and to previous buffer
5800 * :[N]sbprevious [N] split and to previous buffer
5801 */
5802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005803ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005805 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005806 return;
5807
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005809 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005810 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811}
5812
5813/*
5814 * :brewind to first buffer
5815 * :bfirst to first buffer
5816 * :sbrewind split and to first buffer
5817 * :sbfirst split and to first buffer
5818 */
5819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005820ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005822 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005823 return;
5824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005826 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005827 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828}
5829
5830/*
5831 * :blast to last buffer
5832 * :sblast split and to last buffer
5833 */
5834 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005835ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005837 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005838 return;
5839
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005841 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005842 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
Bram Moolenaar7a092242020-04-16 22:10:49 +02005845/*
5846 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005847 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005850ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005852 int comment_char = '"';
5853
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005854 if (in_vim9script())
5855 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005856 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005857}
5858
5859/*
5860 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5861 * to "cmd_start" or has a white space character before it.
5862 */
5863 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005864ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005865{
5866 int c = *cmd;
5867
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005868 if (c == NUL || c == '|' || c == '\n')
5869 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005870 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005871 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5872 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005873 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005874 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875}
5876
5877#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5878 || defined(PROTO)
5879/*
5880 * Return the next command, after the first '|' or '\n'.
5881 * Return NULL if not found.
5882 */
5883 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005884find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885{
5886 while (*p != '|' && *p != '\n')
5887 {
5888 if (*p == NUL)
5889 return NULL;
5890 ++p;
5891 }
5892 return (p + 1);
5893}
5894#endif
5895
5896/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005897 * Check if *p is a separator between Ex commands, skipping over white space.
5898 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 */
5900 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005901check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005903 char_u *s = skipwhite(p);
5904
5905 if (*s == '|' || *s == '\n')
5906 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 else
5908 return NULL;
5909}
5910
5911/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005912 * If "eap->nextcmd" is not set, check for a next command at "p".
5913 */
5914 void
5915set_nextcmd(exarg_T *eap, char_u *arg)
5916{
5917 char_u *p = check_nextcmd(arg);
5918
5919 if (eap->nextcmd == NULL)
5920 eap->nextcmd = p;
5921 else if (p != NULL)
5922 // cannot use "| command" inside a {} block
5923 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5924}
5925
5926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 * - if there are more files to edit
5928 * - and this is the last window
5929 * - and forceit not used
5930 * - and not repeated twice on a row
5931 * return FAIL and give error message if 'message' TRUE
5932 * return OK otherwise
5933 */
5934 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005935check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005936 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005937 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
5939 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5940
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005941 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005942 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 {
5944 if (message)
5945 {
5946#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005947 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5948 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005950 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005952 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5953 NGETTEXT("%d more file to edit. Quit anyway?",
5954 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5956 return OK;
5957 return FAIL;
5958 }
5959#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005960 semsg(NGETTEXT(e_nr_more_file_to_edit,
5961 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005962 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 }
5964 return FAIL;
5965 }
5966 return OK;
5967}
5968
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969/*
5970 * Function given to ExpandGeneric() to obtain the list of command names.
5971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005973get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
5975 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005976 return expand_user_command_name(idx);
Christian Brabandt70a11a62024-07-26 19:13:55 +02005977 // the following are no real commands
5978 if (STRNCMP(cmdnames[idx].cmd_name, "{", 1) == 0 ||
5979 STRNCMP(cmdnames[idx].cmd_name, "}", 1) == 0)
5980 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 return cmdnames[idx].cmd_name;
5982}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005985ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986{
Bram Moolenaare6850792010-05-14 15:28:44 +02005987 if (*eap->arg == NUL)
5988 {
5989#ifdef FEAT_EVAL
5990 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5991 char_u *p = NULL;
5992
5993 if (expr != NULL)
5994 {
5995 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005996 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005997 --emsg_off;
5998 vim_free(expr);
5999 }
6000 if (p != NULL)
6001 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006002 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02006003 vim_free(p);
6004 }
6005 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006006 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02006007#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006008 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02006009#endif
6010 }
6011 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00006012 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006013
6014#ifdef FEAT_VTP
6015 else if (has_vtp_working())
6016 {
6017 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006018 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006019 redrawcmd();
6020 }
6021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022}
6023
6024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006025ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
6027 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006028 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 do_highlight(eap->arg, eap->forceit, FALSE);
6030}
6031
6032
6033/*
6034 * Call this function if we thought we were going to exit, but we won't
6035 * (because of an error). May need to restore the terminal mode.
6036 */
6037 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006038not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
6040 exiting = FALSE;
6041 settmode(TMODE_RAW);
6042}
6043
Bram Moolenaar3552e742021-05-29 12:21:58 +02006044 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006045before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6046{
6047 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6048
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006049 // Bail out when autocommands closed the window.
6050 // Refuse to quit when the buffer in the last window is being closed (can
6051 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006052 if (!win_valid(wp)
6053 || curbuf_locked()
6054 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6055 return TRUE;
6056
6057 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6058 {
6059 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006060 // Refuse to quit when locked or when the window was closed or the
6061 // buffer in the last window is being closed (can only happen in
6062 // autocommands).
6063 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006064 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6065 return TRUE;
6066 }
6067
6068 return FALSE;
6069}
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006072 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006073 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006074 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006076 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006077ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006079 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006080
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (cmdwin_type != 0)
6082 {
6083 cmdwin_result = Ctrl_C;
6084 return;
6085 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006086 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006087 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006088 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006089 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006090 return;
6091 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006092 if (eap->addr_count > 0)
6093 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006094 int wnr = eap->line2;
6095
6096 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6097 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006098 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006099 }
6100 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006101 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006104 if (curbuf_locked())
6105 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006106
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006107 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006108 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006109 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110
6111#ifdef FEAT_NETBEANS_INTG
6112 netbeansForcedQuit = eap->forceit;
6113#endif
6114
6115 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006116 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 */
6118 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6119 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006120 if ((!buf_hide(wp->w_buffer)
6121 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006122 | (eap->forceit ? CCGD_FORCEIT : 0)
6123 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006125 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {
6127 not_exiting();
6128 }
6129 else
6130 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006131 // quit last window
6132 // Note: only_one_window() returns true, even so a help window is
6133 // still open. In that case only quit, if no address has been
6134 // specified. Example:
6135 // :h|wincmd w|1q - don't quit
6136 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006137 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006139 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006140#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006142#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006143 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006144 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 }
6146}
6147
6148/*
6149 * ":cquit".
6150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006152ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006154 // this does not always pass on the exit code to the Manx compiler. why?
6155 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156}
6157
6158/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006159 * Do preparations for "qall" and "wqall".
6160 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006162 int
6163before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (cmdwin_type != 0)
6166 {
6167 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006168 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 else
6170 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006171 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006173
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006174 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006175 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006176 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006177 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006178 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006179 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006180
6181 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006182 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006183
Bram Moolenaar411da642023-05-10 16:53:27 +01006184 return OK;
6185}
6186
6187/*
6188 * ":qall": try to quit all windows
6189 */
6190 static void
6191ex_quit_all(exarg_T *eap)
6192{
6193 if (before_quit_all(eap) == FAIL)
6194 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006196 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 getout(0);
6198 not_exiting();
6199}
6200
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201/*
6202 * ":close": close current window, unless it is the last one
6203 */
6204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006205ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006207 win_T *win;
6208 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006210 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006212 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006213 {
6214 if (eap->addr_count == 0)
6215 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006216 else
6217 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006218 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006219 {
6220 winnr++;
6221 if (winnr == eap->line2)
6222 break;
6223 }
6224 if (win == NULL)
6225 win = lastwin;
6226 ex_win_close(eap->forceit, win, NULL);
6227 }
6228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229}
6230
Bram Moolenaar4033c552017-09-16 20:54:51 +02006231#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232/*
6233 * ":pclose": Close any preview window.
6234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006236ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006237{
6238 win_T *win;
6239
Bram Moolenaar79648732019-07-18 21:43:07 +02006240 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006241 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006242 if (win->w_p_pvw)
6243 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006244 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006245 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006246 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006247# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006248 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006249 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006250# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006251}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006252#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006253
Bram Moolenaarf740b292006-02-16 22:11:02 +00006254/*
6255 * Close window "win" and take care of handling closing the last window for a
6256 * modified buffer.
6257 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006259ex_win_close(
6260 int forceit,
6261 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006262 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263{
6264 int need_hide;
6265 buf_T *buf = win->w_buffer;
6266
Bram Moolenaarcf844172020-06-26 19:44:06 +02006267 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006268 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006269 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006270 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006271 return;
6272 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006273 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006274 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006275
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006277 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006279#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006280 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006282# ifdef FEAT_TERMINAL
6283 if (term_job_running(buf->b_term))
6284 {
6285 if (term_confirm_stop(buf) == FAIL)
6286 return;
6287 // Manually kill the terminal here because this command will
6288 // hide it otherwise.
6289 free_terminal(buf);
6290 need_hide = FALSE;
6291 }
6292 else
6293# endif
6294 {
6295 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006296
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006297 set_bufref(&bufref, buf);
6298 dialog_changed(buf, FALSE);
6299 if (bufref_valid(&bufref) && bufIsChanged(buf))
6300 return;
6301 need_hide = FALSE;
6302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 }
6304 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006307 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 return;
6309 }
6310 }
6311
Bram Moolenaar4033c552017-09-16 20:54:51 +02006312#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006314#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006315
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006316 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006317 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006318 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006319 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006320 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321}
6322
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006324 * Handle the argument for a tabpage related ex command.
6325 * Returns a tabpage number.
6326 * When an error is encountered then eap->errmsg is set.
6327 */
6328 static int
6329get_tabpage_arg(exarg_T *eap)
6330{
6331 int tab_number;
6332 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6333
6334 if (eap->arg && *eap->arg != NUL)
6335 {
6336 char_u *p = eap->arg;
6337 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006338 int relative = 0; // argument +N/-N means: go to N places to the
6339 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006340
6341 if (*p == '-')
6342 {
6343 relative = -1;
6344 p++;
6345 }
6346 else if (*p == '+')
6347 {
6348 relative = 1;
6349 p++;
6350 }
6351
6352 p_save = p;
6353 tab_number = getdigits(&p);
6354
6355 if (relative == 0)
6356 {
6357 if (STRCMP(p, "$") == 0)
6358 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006359 else if (STRCMP(p, "#") == 0)
6360 if (valid_tabpage(lastused_tabpage))
6361 tab_number = tabpage_index(lastused_tabpage);
6362 else
6363 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006364 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6365 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006366 tab_number = 0;
6367 goto theend;
6368 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006369 else if (p == p_save || *p_save == '-' || *p != NUL
6370 || tab_number > LAST_TAB_NR)
6371 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006372 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006373 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006374 goto theend;
6375 }
6376 }
6377 else
6378 {
6379 if (*p_save == NUL)
6380 tab_number = 1;
6381 else if (p == p_save || *p_save == '-' || *p != NUL
6382 || tab_number == 0)
6383 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006384 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006385 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006386 goto theend;
6387 }
6388 tab_number = tab_number * relative + tabpage_index(curtab);
6389 if (!unaccept_arg0 && relative == -1)
6390 --tab_number;
6391 }
6392 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006393 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006394 }
6395 else if (eap->addr_count > 0)
6396 {
6397 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006398 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006399 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006400 tab_number = 0;
6401 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006402 else
6403 {
6404 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006405 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006406 {
zeertzjq076faac2024-03-25 16:41:06 +01006407 char_u *cmdp = eap->cmd;
6408
6409 while (--cmdp > *eap->cmdlinep
6410 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6411 ;
6412 if (*cmdp == '-')
6413 {
6414 --tab_number;
6415 if (tab_number < unaccept_arg0)
6416 eap->errmsg = _(e_invalid_range);
6417 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006418 }
6419 }
6420 }
6421 else
6422 {
6423 switch (eap->cmdidx)
6424 {
6425 case CMD_tabnext:
6426 tab_number = tabpage_index(curtab) + 1;
6427 if (tab_number > LAST_TAB_NR)
6428 tab_number = 1;
6429 break;
6430 case CMD_tabmove:
6431 tab_number = LAST_TAB_NR;
6432 break;
6433 default:
6434 tab_number = tabpage_index(curtab);
6435 }
6436 }
6437
6438theend:
6439 return tab_number;
6440}
6441
6442/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006443 * ":tabclose": close current tab page, unless it is the last one.
6444 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 */
6446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006447ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006449 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006450 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006451
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006452 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006453 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006454 cmdwin_result = K_IGNORE;
6455 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006456 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006457
6458 if (first_tabpage->tp_next == NULL)
6459 {
6460 emsg(_(e_cannot_close_last_tab_page));
6461 return;
6462 }
6463
6464 if (window_layout_locked(CMD_tabclose))
6465 return;
6466
6467 tab_number = get_tabpage_arg(eap);
6468 if (eap->errmsg != NULL)
6469 return;
6470
6471 tp = find_tabpage(tab_number);
6472 if (tp == NULL)
6473 {
6474 beep_flush();
6475 return;
6476 }
6477 if (tp != curtab)
6478 {
6479 tabpage_close_other(tp, eap->forceit);
6480 return;
6481 }
6482 else if (!text_locked() && !curbuf_locked())
6483 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006484}
6485
6486/*
6487 * ":tabonly": close all tab pages except the current one
6488 */
6489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006490ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006491{
6492 tabpage_T *tp;
6493 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006494 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006495
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006496 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006497 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006498 cmdwin_result = K_IGNORE;
6499 return;
6500 }
6501
6502 if (first_tabpage->tp_next == NULL)
6503 {
6504 msg(_("Already only one tab page"));
6505 return;
6506 }
6507
6508 if (window_layout_locked(CMD_tabonly))
6509 return;
6510
6511 tab_number = get_tabpage_arg(eap);
6512 if (eap->errmsg != NULL)
6513 return;
6514
6515 goto_tabpage(tab_number);
6516 // Repeat this up to a 1000 times, because autocommands may
6517 // mess up the lists.
6518 for (done = 0; done < 1000; ++done)
6519 {
6520 FOR_ALL_TABPAGES(tp)
6521 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006522 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006523 tabpage_close_other(tp, eap->forceit);
6524 // if we failed to close it quit
6525 if (valid_tabpage(tp))
6526 done = 1000;
6527 // start over, "tp" is now invalid
6528 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006529 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006530 if (first_tabpage->tp_next == NULL)
6531 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534
6535/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006536 * Close the current tab page.
6537 */
6538 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006539tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006540{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006541 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006542 return;
6543
Jim Zhoubcf66e02025-03-16 20:24:57 +01006544 trigger_tabclosedpre(curtab, TRUE);
6545
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006546 // First close all the windows but the current one. If that worked then
6547 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006548 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006549 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006550 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006551 ex_win_close(forceit, curwin, NULL);
6552# ifdef FEAT_GUI
6553 need_mouse_correct = TRUE;
6554# endif
6555}
6556
6557/*
6558 * Close tab page "tp", which is not the current tab page.
6559 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006560 * Also takes care of the tab pages line disappearing when closing the
6561 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006562 */
6563 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006564tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006565{
6566 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006567 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006568
Jim Zhoubcf66e02025-03-16 20:24:57 +01006569 trigger_tabclosedpre(tp, TRUE);
6570
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006571 // Limit to 1000 windows, autocommands may add a window while we close
6572 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006573 while (++done < 1000)
6574 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006575 wp = tp->tp_firstwin;
6576 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006577
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006578 // Autocommands may delete the tab page under our fingers and we may
6579 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006580 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006581 break;
6582 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006583
Bram Moolenaar29323592016-07-24 22:04:11 +02006584 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006585}
6586
6587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 * ":only".
6589 */
6590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006591ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006593 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595# ifdef FEAT_GUI
6596 need_mouse_correct = TRUE;
6597# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006598 if (eap->addr_count > 0)
6599 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006600 win_T *wp;
6601 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006602 for (wp = firstwin; --wnr > 0; )
6603 {
6604 if (wp->w_next == NULL)
6605 break;
6606 else
6607 wp = wp->w_next;
6608 }
6609 win_goto(wp);
6610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 close_others(TRUE, eap->forceit);
6612}
6613
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006615ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006617 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006618 if (eap->skip)
6619 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006620
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006621 if (window_layout_locked(CMD_hide))
6622 return;
6623#ifdef FEAT_GUI
6624 need_mouse_correct = TRUE;
6625#endif
6626 if (eap->addr_count == 0)
6627 win_close(curwin, FALSE); // don't free buffer
6628 else
6629 {
6630 int winnr = 0;
6631 win_T *win;
6632
6633 FOR_ALL_WINDOWS(win)
6634 {
6635 winnr++;
6636 if (winnr == eap->line2)
6637 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006639 if (win == NULL)
6640 win = lastwin;
6641 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 }
6643}
6644
6645/*
6646 * ":stop" and ":suspend": Suspend Vim.
6647 */
dbivolaruab16ad32021-12-29 19:41:47 +00006648 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006649ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
6651 /*
6652 * Disallow suspending for "rvim".
6653 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006654 if (check_restricted())
6655 return;
6656
6657 if (!eap->forceit)
6658 autowrite_all();
6659 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6660 windgoto((int)Rows - 1, 0);
6661 out_char('\n');
6662 out_flush();
6663 stoptermcap();
6664 out_flush(); // needed for SUN to restore xterm buffer
6665 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6666 ui_suspend(); // call machine specific function
6667 maketitle();
6668 resettitle(); // force updating the title
6669 starttermcap();
6670 scroll_start(); // scroll screen before redrawing
6671 redraw_later_clear();
6672 shell_resized(); // may have resized window
6673 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674}
6675
6676/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006677 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 */
6679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006680ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006682#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006683 if (not_in_vim9(eap) == FAIL)
6684 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 if (cmdwin_type != 0)
6687 {
6688 cmdwin_result = Ctrl_C;
6689 return;
6690 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006691 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006692 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006693 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006694 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006695 return;
6696 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006699 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 */
6701 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6702 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006703
6704 // Write the buffer for ":wq" or when it was changed.
6705 // Trigger QuitPre and ExitPre.
6706 // Check if we can exit now, after autocommands have changed things.
6707 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6708 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006710 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 {
6712 not_exiting();
6713 }
6714 else
6715 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006716 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006718 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719# ifdef FEAT_GUI
6720 need_mouse_correct = TRUE;
6721# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006722 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006723 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 }
6725}
6726
6727/*
6728 * ":print", ":list", ":number".
6729 */
6730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006731ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006733 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006734 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006735 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006737 for ( ;!got_int; ui_breakcheck())
6738 {
6739 print_line(eap->line1,
6740 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6741 || (eap->flags & EXFLAG_NR)),
6742 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6743 if (++eap->line1 > eap->line2)
6744 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006745 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006746 }
6747 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006748 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006749 curwin->w_cursor.lnum = eap->line2;
6750 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 }
6752
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754}
6755
6756#ifdef FEAT_BYTEOFF
6757 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006758ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759{
6760 goto_byte(eap->line2);
6761}
6762#endif
6763
6764/*
6765 * ":shell".
6766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006768ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769{
6770 do_shell(NULL, 0);
6771}
6772
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006773#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006775static int drop_busy = FALSE;
6776static int drop_filec;
6777static char_u **drop_filev = NULL;
6778static int drop_split;
6779static void (*drop_callback)(void *);
6780static void *drop_cookie;
6781
6782 static void
6783handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 exarg_T ea;
6786 int save_msg_scroll = msg_scroll;
6787
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006788 // Setting the argument list may cause screen updates and being called
6789 // recursively. Avoid that by setting drop_busy.
6790 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006792 // Check whether the current buffer is changed. If so, we will need
6793 // to split the current window or data could be lost.
6794 // We don't need to check if the 'hidden' option is set, as in this
6795 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006796 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 {
6798 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006799 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 --emsg_off;
6801 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006802 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 if (win_split(0, 0) == FAIL)
6805 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006806 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006808 // When splitting the window, create a new alist. Otherwise the
6809 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 alist_unlink(curwin->w_alist);
6811 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 }
6813
6814 /*
6815 * Set up the new argument list.
6816 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006817 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
6819 /*
6820 * Move to the first file.
6821 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006822 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006823 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 ea.cmd = (char_u *)"next";
6825 do_argfile(&ea, 0);
6826
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006827 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6828 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 need_start_insertmode = FALSE;
6830
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006831 // Restore msg_scroll, otherwise a following command may cause scrolling
6832 // unexpectedly. The screen will be redrawn by the caller, thus
6833 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006835
6836 if (drop_callback != NULL)
6837 drop_callback(drop_cookie);
6838
6839 drop_filev = NULL;
6840 drop_busy = FALSE;
6841}
6842
6843/*
6844 * Handle a file drop. The code is here because a drop is *nearly* like an
6845 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006846 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006847 * code is very similar to :args and hence needs access to a lot of the static
6848 * functions in this file.
6849 *
6850 * The "filev" list must have been allocated using alloc(), as should each item
6851 * in the list. This function takes over responsibility for freeing the "filev"
6852 * list.
6853 */
6854 void
6855handle_drop(
6856 int filec, // the number of files dropped
6857 char_u **filev, // the list of files dropped
6858 int split, // force splitting the window
6859 void (*callback)(void *), // to be called after setting the argument
6860 // list
6861 void *cookie) // argument for "callback" (allocated)
6862{
6863 // Cannot handle recursive drops, finish the pending one.
6864 if (drop_busy)
6865 {
6866 FreeWild(filec, filev);
6867 vim_free(cookie);
6868 return;
6869 }
6870
6871 // When calling handle_drop() more than once in a row we only use the last
6872 // one.
6873 if (drop_filev != NULL)
6874 {
6875 FreeWild(drop_filec, drop_filev);
6876 vim_free(drop_cookie);
6877 }
6878
6879 drop_filec = filec;
6880 drop_filev = filev;
6881 drop_split = split;
6882 drop_callback = callback;
6883 drop_cookie = cookie;
6884
6885 // Postpone this when:
6886 // - editing the command line
6887 // - not possible to change the current buffer
6888 // - updating the screen
6889 // As it may change buffers and window structures that are in use and cause
6890 // freed memory to be used.
6891 if (text_locked() || curbuf_locked() || updating_screen)
6892 return;
6893
6894 handle_drop_internal();
6895}
6896
6897/*
6898 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6899 * reset: Handle a postponed drop.
6900 */
6901 void
6902handle_any_postponed_drop(void)
6903{
6904 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006905 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006906 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907}
6908#endif
6909
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 * ":preserve".
6912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006914ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006916 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 ml_preserve(curbuf, TRUE);
6918}
6919
6920/*
6921 * ":recover".
6922 */
6923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006924ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006926 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006928 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6929 | CCGD_MULTWIN
6930 | (eap->forceit ? CCGD_FORCEIT : 0)
6931 | CCGD_EXCMD)
6932
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 && (*eap->arg == NUL
6934 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006935 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 recoverymode = FALSE;
6937}
6938
6939/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006940 * Command modifier used in a wrong way. Also for other commands that can't
6941 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 */
6943 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006944ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006946 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947}
6948
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006949#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006950
6951// callback function for 'findfunc'
6952static callback_T ffu_cb;
6953
6954 static callback_T *
6955get_findfunc_callback(void)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006956{
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006957 return *curbuf->b_p_ffu != NUL ? &curbuf->b_ffu_cb : &ffu_cb;
6958}
6959
zeertzjq6eda2692024-11-03 09:23:33 +01006960/*
6961 * Call 'findfunc' to obtain a list of file names.
6962 */
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006963 static list_T *
6964call_findfunc(char_u *pat, int cmdcomplete)
6965{
6966 typval_T args[3];
6967 callback_T *cb;
6968 typval_T rettv;
6969 int retval;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006970 sctx_T saved_sctx = current_sctx;
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006971 sctx_T *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006972
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006973 args[0].v_type = VAR_STRING;
6974 args[0].vval.v_string = pat;
6975 args[1].v_type = VAR_BOOL;
6976 args[1].vval.v_number = cmdcomplete;
6977 args[2].v_type = VAR_UNKNOWN;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006978
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006979 // Lock the text to prevent weird things from happening. Also disallow
6980 // switching to another window, it should not be needed and may end up in
6981 // Insert mode in another buffer.
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006982 ++textlock;
6983
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006984 ctx = get_option_sctx("findfunc");
6985 if (ctx != NULL)
6986 current_sctx = *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006987
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006988 cb = get_findfunc_callback();
6989 retval = call_callback(cb, -1, &rettv, 2, args);
6990
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006991 current_sctx = saved_sctx;
6992
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006993 --textlock;
6994
6995 list_T *retlist = NULL;
6996
6997 if (retval == OK)
6998 {
6999 if (rettv.v_type == VAR_LIST)
7000 retlist = list_copy(rettv.vval.v_list, FALSE, FALSE, get_copyID());
7001 else
7002 emsg(_(e_invalid_return_type_from_findfunc));
7003
7004 clear_tv(&rettv);
7005 }
7006
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007007 return retlist;
7008}
7009
7010/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007011 * Find file names matching "pat" using 'findfunc' and return it in "files".
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007012 * Used for expanding the :find, :sfind and :tabfind command argument.
7013 * Returns OK on success and FAIL otherwise.
7014 */
7015 int
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007016expand_findfunc(char_u *pat, char_u ***files, int *numMatches)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007017{
7018 list_T *l;
7019 int len;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007020
7021 *numMatches = 0;
7022 *files = NULL;
7023
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007024 l = call_findfunc(pat, VVAL_TRUE);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007025
7026 if (l == NULL)
7027 return FAIL;
7028
7029 len = list_len(l);
7030 if (len == 0) // empty List
7031 return FAIL;
7032
7033 *files = ALLOC_MULT(char_u *, len);
7034 if (*files == NULL)
7035 return FAIL;
7036
7037 // Copy all the List items
7038 listitem_T *li;
7039 int idx = 0;
7040 FOR_ALL_LIST_ITEMS(l, li)
7041 {
7042 if (li->li_tv.v_type == VAR_STRING)
7043 {
7044 (*files)[idx] = vim_strsave(li->li_tv.vval.v_string);
7045 idx++;
7046 }
7047 }
7048
7049 *numMatches = idx;
7050 list_free(l);
7051
7052 return OK;
7053}
7054
7055/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007056 * Use 'findfunc' to find file 'findarg'. The 'count' argument is used to find
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007057 * the n'th matching file.
7058 */
7059 static char_u *
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007060findfunc_find_file(char_u *findarg, int findarg_len, int count)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007061{
7062 list_T *fname_list;
7063 char_u *ret_fname = NULL;
7064 char_u cc;
7065 int fname_count;
7066
7067 cc = findarg[findarg_len];
7068 findarg[findarg_len] = NUL;
7069
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007070 fname_list = call_findfunc(findarg, VVAL_FALSE);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007071 fname_count = list_len(fname_list);
7072
7073 if (fname_count == 0)
7074 semsg(_(e_cant_find_file_str_in_path), findarg);
7075 else
7076 {
7077 if (count > fname_count)
7078 semsg(_(e_no_more_file_str_found_in_path), findarg);
7079 else
7080 {
7081 listitem_T *li = list_find(fname_list, count - 1);
7082 if (li != NULL && li->li_tv.v_type == VAR_STRING)
7083 ret_fname = vim_strsave(li->li_tv.vval.v_string);
7084 }
7085 }
7086
7087 if (fname_list != NULL)
7088 list_free(fname_list);
7089
7090 findarg[findarg_len] = cc;
7091
7092 return ret_fname;
7093}
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007094
7095/*
7096 * Process the 'findfunc' option value.
7097 * Returns NULL on success and an error message on failure.
7098 */
7099 char *
7100did_set_findfunc(optset_T *args UNUSED)
7101{
7102 int retval;
7103
zeertzjq6eda2692024-11-03 09:23:33 +01007104 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007105 // buffer-local option set
7106 retval = option_set_callback_func(curbuf->b_p_ffu, &curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007107 else
7108 {
7109 // global option set
7110 retval = option_set_callback_func(p_ffu, &ffu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01007111 // when using :set, free the local callback
7112 if (!(args->os_flags & OPT_GLOBAL))
7113 free_callback(&curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007114 }
7115
7116 if (retval == FAIL)
7117 return e_invalid_argument;
7118
7119 // If the option value starts with <SID> or s:, then replace that with
7120 // the script identifier.
7121 char_u **varp = (char_u **)args->os_varp;
7122 char_u *name = get_scriptlocal_funcname(*varp);
7123 if (name != NULL)
7124 {
7125 free_string_option(*varp);
7126 *varp = name;
7127 }
7128
7129 return NULL;
7130}
7131
7132# if defined(EXITFREE) || defined(PROTO)
7133 void
7134free_findfunc_option(void)
7135{
7136 free_callback(&ffu_cb);
7137}
7138# endif
7139
7140/*
7141 * Mark the global 'findfunc' callback with "copyID" so that it is not
7142 * garbage collected.
7143 */
7144 int
7145set_ref_in_findfunc(int copyID UNUSED)
7146{
7147 int abort = FALSE;
7148 abort = set_ref_in_callback(&ffu_cb, copyID);
7149 return abort;
7150}
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007151#endif
7152
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153/*
7154 * :sview [+command] file split window with new file, read-only
7155 * :split [[+command] file] split window with current or new file
7156 * :vsplit [[+command] file] split window vertically with current or new file
7157 * :new [[+command] file] split window with no or new file
7158 * :vnew [[+command] file] split vertically window with no or new file
7159 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007160 *
7161 * :tabedit open new Tab page with empty window
7162 * :tabedit [+command] file open new Tab page and edit "file"
7163 * :tabnew [[+command] file] just like :tabedit
7164 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 */
7166 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007167ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007169 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007171#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007172 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02007173 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007174#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02007175 int use_tab = eap->cmdidx == CMD_tabedit
7176 || eap->cmdidx == CMD_tabfind
7177 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007179 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007180 return;
7181
Bram Moolenaar4033c552017-09-16 20:54:51 +02007182#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
Bram Moolenaar4033c552017-09-16 20:54:51 +02007186#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007187 // A ":split" in the quickfix window works like ":new". Don't want two
7188 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02007189 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 {
7191 if (eap->cmdidx == CMD_split)
7192 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 if (eap->cmdidx == CMD_vsplit)
7194 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007198 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007200 char_u *file_to_find = NULL;
7201 char *search_ctx = NULL;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007202
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007203 if (*get_findfunc() != NUL)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007204 {
7205#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007206 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007207 eap->addr_count > 0 ? eap->line2 : 1);
7208#endif
7209 }
7210 else
7211 {
7212 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7213 FNAME_MESS, TRUE, curbuf->b_ffname,
7214 &file_to_find, &search_ctx);
7215 vim_free(file_to_find);
7216 vim_findfile_cleanup(search_ctx);
7217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 if (fname == NULL)
7219 goto theend;
7220 eap->arg = fname;
7221 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007222# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007223 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 && eap->cmdidx != CMD_new)
7226 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007227 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007228# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007229 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007230# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007231 au_has_group((char_u *)"FileExplorer"))
7232 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007233 // No browsing supported but we do have the file explorer:
7234 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007235 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007236 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007237 }
7238 else
7239 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02007240 fname = do_browse(0, (char_u *)(use_tab
7241 ? _("Edit File in new tab page")
7242 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007244 if (fname == NULL)
7245 goto theend;
7246 eap->arg = fname;
7247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007249 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007252 /*
7253 * Either open new tab page or split the window.
7254 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007255 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007256 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007257 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007258 : eap->addr_count == 0 ? 0
7259 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007260 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007261 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007262
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007263 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007264 if (curwin != old_curwin
7265 && win_valid(old_curwin)
7266 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007267 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007268 old_curwin->w_alt_fnum = curbuf->b_fnum;
7269 }
7270 }
7271 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007274 // Reset 'scrollbind' when editing another file, but keep it when
7275 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007276 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007277 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 else
7279 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 do_exedit(eap, old_curwin);
7281 }
7282
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007283# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007284 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287theend:
7288 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007290
7291/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007292 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007293 */
7294 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007295tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007296{
7297 exarg_T ea;
7298
Bram Moolenaara80faa82020-04-12 19:37:17 +02007299 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007300 ea.cmdidx = CMD_tabnew;
7301 ea.cmd = (char_u *)"tabn";
7302 ea.arg = (char_u *)"";
7303 ex_splitview(&ea);
7304}
7305
7306/*
7307 * :tabnext command
7308 */
7309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007310ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007311{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007312 int tab_number;
7313
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007314 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007315 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007316 switch (eap->cmdidx)
7317 {
7318 case CMD_tabfirst:
7319 case CMD_tabrewind:
7320 goto_tabpage(1);
7321 break;
7322 case CMD_tablast:
7323 goto_tabpage(9999);
7324 break;
7325 case CMD_tabprevious:
7326 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007327 if (eap->arg && *eap->arg != NUL)
7328 {
7329 char_u *p = eap->arg;
7330 char_u *p_save = p;
7331
7332 tab_number = getdigits(&p);
7333 if (p == p_save || *p_save == '-' || *p != NUL
7334 || tab_number == 0)
7335 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007336 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007337 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007338 return;
7339 }
7340 }
7341 else
7342 {
7343 if (eap->addr_count == 0)
7344 tab_number = 1;
7345 else
7346 {
7347 tab_number = eap->line2;
7348 if (tab_number < 1)
7349 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007350 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007351 return;
7352 }
7353 }
7354 }
7355 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007356 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007357 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007358 tab_number = get_tabpage_arg(eap);
7359 if (eap->errmsg == NULL)
7360 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007361 break;
7362 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007363}
7364
7365/*
7366 * :tabmove command
7367 */
7368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007369ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007370{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007371 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007372
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007373 tab_number = get_tabpage_arg(eap);
7374 if (eap->errmsg == NULL)
7375 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007376}
7377
7378/*
7379 * :tabs command: List tabs and their contents.
7380 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007382ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383{
7384 tabpage_T *tp;
7385 win_T *wp;
7386 int tabcount = 1;
7387
7388 msg_start();
7389 msg_scroll = TRUE;
7390 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7391 {
7392 msg_putchar('\n');
7393 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007394 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007395 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007396 ui_breakcheck();
7397
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007398 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007399 wp = firstwin;
7400 else
7401 wp = tp->tp_firstwin;
7402 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7403 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007404 msg_putchar('\n');
7405 msg_putchar(wp == curwin ? '>' : ' ');
7406 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007407 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7408 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007409 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007410 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007411 else
7412 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7413 IObuff, IOSIZE, TRUE);
7414 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007415 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007416 ui_breakcheck();
7417 }
7418 }
7419}
7420
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421/*
7422 * ":mode": Set screen mode.
7423 * If no argument given, just get the screen size and redraw.
7424 */
7425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007426ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428 if (*eap->arg == NUL)
7429 shell_resized();
7430 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007431 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432}
7433
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434/*
7435 * ":resize".
7436 * set, increment or decrement current window height
7437 */
7438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007439ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
7441 int n;
7442 win_T *wp = curwin;
7443
7444 if (eap->addr_count > 0)
7445 {
7446 n = eap->line2;
7447 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7448 ;
7449 }
7450
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007451# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007455 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 {
7457 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007458 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007459 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007461 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 }
7463 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 {
7465 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007466 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007467 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007469 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 }
7471}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
7473/*
7474 * ":find [+command] <file>" command.
7475 */
7476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007477ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478{
Colin Kennedy21570352024-03-03 16:16:47 +01007479 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007480 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007481
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007482 char_u *fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007484 char_u *file_to_find = NULL;
7485 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007487 if (*get_findfunc() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 {
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007489#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007490 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007491 eap->addr_count > 0 ? eap->line2 : 1);
7492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007494 else
7495 {
7496 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7497 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
7498 if (eap->addr_count > 0)
7499 {
7500 // Repeat finding the file "count" times. This matters when it appears
7501 // several times in the path.
7502 count = eap->line2;
7503 while (fname != NULL && --count > 0)
7504 {
7505 vim_free(fname);
7506 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7507 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
7508 }
7509 }
7510 VIM_CLEAR(file_to_find);
7511 vim_findfile_cleanup(search_ctx);
7512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007514 if (fname == NULL)
7515 return;
7516
7517 eap->arg = fname;
7518 do_exedit(eap, NULL);
7519 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520}
7521
7522/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007523 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007524 */
7525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007526ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007527{
7528 regmatch_T regmatch;
7529 char_u *p;
7530
Bram Moolenaar65088802021-03-13 21:07:21 +01007531#ifdef FEAT_EVAL
7532 if (not_in_vim9(eap) == FAIL)
7533 return;
7534#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007535 curwin->w_cursor.lnum = eap->line2;
7536 beginline(BL_SOL | BL_FIX);
7537 if (*eap->arg == '/')
7538 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007539 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007540 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007541 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007542 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007543 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007544 if (regmatch.regprog != NULL)
7545 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007546 // make a copy of the line, when searching for a mark it might be
7547 // flushed
7548 char_u *line = vim_strsave(ml_get_curline());
7549
Bram Moolenaardf177f62005-02-22 08:39:57 +00007550 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007551 if (vim_regexec(&regmatch, line, (colnr_T)0))
7552 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007553 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007554 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007555 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007556 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007557 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007558 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007559 eap->arg += STRLEN(eap->arg);
7560 }
7561 check_cursor();
7562
7563 eap->cmdidx = CMD_visual;
7564 do_exedit(eap, NULL);
7565}
7566
7567/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007568 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 */
7570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007571ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007573 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7574
Colin Kennedy21570352024-03-03 16:16:47 +01007575 // Exclude commands which keep the window's current buffer
7576 if (
7577 eap->cmdidx != CMD_badd
7578 && eap->cmdidx != CMD_balt
7579 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007580 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7581 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007582 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007583
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 do_exedit(eap, NULL);
7585}
7586
7587/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007588 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007591do_exedit(
7592 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007593 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594{
7595 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007597 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007599 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7600 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007601 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 /*
7603 * ":vi" command ends Ex mode.
7604 */
7605 if (exmode_active && (eap->cmdidx == CMD_visual
7606 || eap->cmdidx == CMD_view))
7607 {
7608 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007609 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007611 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007612 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007613 if (global_busy)
7614 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007615 if (eap->nextcmd != NULL)
7616 {
7617 stuffReadbuff(eap->nextcmd);
7618 eap->nextcmd = NULL;
7619 }
7620
7621 if (exmode_was != EXMODE_VIM)
7622 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007623 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007624 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007625 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007626 no_wait_return = 0;
7627 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007628 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007629 msg_scroll = 0;
7630#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007631 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007632 hold_gui_events = 0;
7633#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007634 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007635 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007636
7637 main_loop(FALSE, TRUE);
7638
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007639 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007640 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007641 no_wait_return = save_nwr;
7642 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007643#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007644 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007645#endif
7646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 }
7650
7651 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007652 || eap->cmdidx == CMD_tabnew
7653 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007654 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007656 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 setpcmark();
7658 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007659 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7660 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007662 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 || *eap->arg != NUL
7664#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007665 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666#endif
7667 )
7668 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007669 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7670 // Only ":edit" or ":script" can bring us here, others are stopped
7671 // earlier.
7672 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007673 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007674
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 n = readonlymode;
7676 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7677 readonlymode = TRUE;
7678 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007679 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7680 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007681 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7682 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7684 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007685 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7687 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7688 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007689 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007691 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007692 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007693 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7694 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007695 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007697 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 if (old_curwin != NULL)
7699 {
7700 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007701 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007703#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007704 cleanup_T cs;
7705
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007706 // Reset the error/interrupt/exception state here so that
7707 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007708 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007709#endif
7710#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007712#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007713 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007714
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007715#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007716 // Restore the error/interrupt/exception state if not
7717 // discarded by a new aborting error, interrupt, or
7718 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007719 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 }
7722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 }
7724 else if (readonlymode && curbuf->b_nwindows == 1)
7725 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007726 // When editing an already visited buffer, 'readonly' won't be set
7727 // but the previous value is kept. With ":view" and ":sview" we
7728 // want the file to be readonly, except when another window is
7729 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 curbuf->b_p_ro = TRUE;
7731 }
7732 readonlymode = n;
7733 }
7734 else
7735 {
7736 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007737 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 if (n != curwin->w_arg_idx_invalid)
7741 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 }
7743
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 /*
7745 * if ":split file" worked, set alternate file name in old window to new
7746 * file
7747 */
7748 if (old_curwin != NULL
7749 && *eap->arg != NUL
7750 && curwin != old_curwin
7751 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007752 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007753 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755
7756 ex_no_reprint = TRUE;
7757}
7758
7759#ifndef FEAT_GUI
7760/*
7761 * ":gui" and ":gvim" when there is no GUI.
7762 */
7763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007764ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007766 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767}
7768#endif
7769
Bram Moolenaar4f974752019-02-17 17:44:42 +01007770#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007772ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
7774 gui_make_tearoff(eap->arg);
7775}
7776#endif
7777
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007778#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7779 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007781ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007783# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7784 if (gui.in_use)
7785 gui_make_popup(eap->arg, eap->forceit);
7786# ifdef FEAT_TERM_POPUP_MENU
7787 else
7788# endif
7789# endif
7790# ifdef FEAT_TERM_POPUP_MENU
7791 pum_make_popup(eap->arg, eap->forceit);
7792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793}
7794#endif
7795
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007797ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798{
7799 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007800 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007802 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803}
7804
7805/*
7806 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7807 * offset.
7808 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007811ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007814 win_T *save_curwin = curwin;
7815 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 long topline;
7817 long y;
7818 linenr_T old_linenr = curwin->w_cursor.lnum;
7819
7820 setpcmark();
7821
7822 /*
7823 * determine max topline
7824 */
7825 if (curwin->w_p_scb)
7826 {
7827 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007828 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 {
7830 if (wp->w_p_scb && wp->w_buffer)
7831 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007832 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 if (topline > y)
7834 topline = y;
7835 }
7836 }
7837 if (topline < 1)
7838 topline = 1;
7839 }
7840 else
7841 {
7842 topline = 1;
7843 }
7844
7845
7846 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007847 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007849 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {
7851 if (curwin->w_p_scb)
7852 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007853 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 y = topline - curwin->w_topline;
7855 if (y > 0)
7856 scrollup(y, TRUE);
7857 else
7858 scrolldown(-y, TRUE);
7859 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007860 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 }
7864 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007865 curwin = save_curwin;
7866 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 if (curwin->w_p_scb)
7868 {
7869 did_syncbind = TRUE;
7870 checkpcmark();
7871 if (old_linenr != curwin->w_cursor.lnum)
7872 {
7873 char_u ctrl_o[2];
7874
7875 ctrl_o[0] = Ctrl_O;
7876 ctrl_o[1] = 0;
7877 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7878 }
7879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880}
7881
7882
7883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007884ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007886 int i;
7887 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7888 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007890 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007892 do_bang(1, eap, FALSE, FALSE, TRUE);
7893 return;
7894 }
7895
7896 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7897 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898
7899#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007900 if (cmdmod.cmod_flags & CMOD_BROWSE)
7901 {
7902 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007904 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7905 NULL, NULL, NULL, curbuf);
7906 if (browseFile != NULL)
7907 {
7908 i = readfile(browseFile, NULL,
7909 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7910 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 }
7912 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007913 i = OK;
7914 }
7915 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007917 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007919 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 return;
7921 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007922 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 }
7924 else
7925 {
7926 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7927 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7928 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007929 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
7931 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007932 if (i != OK)
7933 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007934#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007935 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007937 semsg(_(e_cant_open_file_str), eap->arg);
7938 }
7939 else
7940 {
7941 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007942 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007943 // Delete the empty line that remains. Historically ex does
7944 // this but vi doesn't.
7945 if (eap->line2 == 0)
7946 lnum = curbuf->b_ml.ml_line_count;
7947 else
7948 lnum = 1;
7949 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007950 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007951 ml_delete(lnum);
7952 if (curwin->w_cursor.lnum > 1
7953 && curwin->w_cursor.lnum >= lnum)
7954 --curwin->w_cursor.lnum;
7955 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007956 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007957 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007958 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 }
7960}
7961
Bram Moolenaarea408852005-06-25 22:49:46 +00007962static char_u *prev_dir = NULL;
7963
7964#if defined(EXITFREE) || defined(PROTO)
7965 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007966free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007967{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007968 VIM_CLEAR(prev_dir);
7969 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007970}
7971#endif
7972
Bram Moolenaarf4258302013-06-02 18:20:17 +02007973/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007974 * Get the previous directory for the given chdir scope.
7975 */
7976 static char_u *
7977get_prevdir(cdscope_T scope)
7978{
7979 if (scope == CDSCOPE_WINDOW)
7980 return curwin->w_prevdir;
7981 else if (scope == CDSCOPE_TABPAGE)
7982 return curtab->tp_prevdir;
7983 return prev_dir;
7984}
7985
7986/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007987 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007988 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7989 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007990 */
7991 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007992post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007993{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007994 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007995 // Clear tab local directory for both :cd and :tcd
7996 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007997 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007998 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007999 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008000 char_u *pdir = get_prevdir(scope);
8001
8002 // If still in the global directory, need to remember current
8003 // directory as the global directory.
8004 if (globaldir == NULL && pdir != NULL)
8005 globaldir = vim_strsave(pdir);
8006
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008007 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02008008 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008009 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008010 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008011 curtab->tp_localdir = vim_strsave(NameBuff);
8012 else
8013 curwin->w_localdir = vim_strsave(NameBuff);
8014 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02008015 }
8016 else
8017 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008018 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01008019 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008020 }
8021
zeertzjq64be6aa2021-11-19 11:59:08 +00008022 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008023 shorten_fnames(TRUE);
8024}
8025
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008026/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008027 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
8028 */
8029 void
8030trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
8031{
8032#ifdef FEAT_EVAL
8033 dict_T *v_event;
8034 save_v_event_T save_v_event;
8035
8036 v_event = get_v_event(&save_v_event);
8037 (void)dict_add_string(v_event, "directory", new_dir);
8038 dict_set_items_ro(v_event);
8039#endif
8040 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
8041#ifdef FEAT_EVAL
8042 restore_v_event(v_event, &save_v_event);
8043#endif
8044}
8045
8046/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008047 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02008048 * chdir() function.
8049 * scope == CDSCOPE_WINDOW: changes the window-local directory
8050 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
8051 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008052 * Returns TRUE if the directory is successfully changed.
8053 */
8054 int
8055changedir_func(
8056 char_u *new_dir,
8057 int forceit,
8058 cdscope_T scope)
8059{
Bram Moolenaar002bc792020-06-05 22:33:42 +02008060 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008061 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008062 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00008063 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008064 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008065
Bram Moolenaar7cc96922020-01-31 22:41:38 +01008066 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008067 return FALSE;
8068
8069 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
8070 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00008071 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008072 return FALSE;
8073 }
8074
8075 // ":cd -": Change to previous directory
8076 if (STRCMP(new_dir, "-") == 0)
8077 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008078 pdir = get_prevdir(scope);
8079 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008080 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008081 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008082 return FALSE;
8083 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02008084 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008085 }
8086
8087 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008088 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02008089 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008090 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02008091 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008092
Bakudankun29f3a452021-12-11 12:28:08 +00008093 // For UNIX ":cd" means: go to home directory.
8094 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008095#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008096 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00008097#else
8098 if (*new_dir == NUL && p_cdh)
8099#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008100 {
8101 // use NameBuff for home directory name
8102# ifdef VMS
8103 char_u *p;
8104
8105 p = mch_getenv((char_u *)"SYS$LOGIN");
8106 if (p == NULL || *p == NUL) // empty is the same as not set
8107 NameBuff[0] = NUL;
8108 else
8109 vim_strncpy(NameBuff, p, MAXPATHL - 1);
8110# else
8111 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8112# endif
8113 new_dir = NameBuff;
8114 }
zeertzjqf38aad82021-12-30 13:45:57 +00008115 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008116 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
8117 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00008118 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008119 if (scope == CDSCOPE_WINDOW)
8120 acmd_fname = (char_u *)"window";
8121 else if (scope == CDSCOPE_TABPAGE)
8122 acmd_fname = (char_u *)"tabpage";
8123 else
8124 acmd_fname = (char_u *)"global";
8125 trigger_DirChangedPre(acmd_fname, new_dir);
8126
8127 if (vim_chdir(new_dir))
8128 {
8129 emsg(_(e_command_failed));
8130 vim_free(pdir);
8131 return FALSE;
8132 }
Richard Doty3d0abad2021-12-29 14:39:08 +00008133 }
zeertzjq73257142022-02-02 13:16:37 +00008134
8135 if (scope == CDSCOPE_WINDOW)
8136 pp = &curwin->w_prevdir;
8137 else if (scope == CDSCOPE_TABPAGE)
8138 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008139 else
zeertzjq73257142022-02-02 13:16:37 +00008140 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008141 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00008142 *pp = pdir;
8143
8144 post_chdir(scope);
8145
8146 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008147 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008148 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00008149 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008150}
Bram Moolenaarea408852005-06-25 22:49:46 +00008151
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008153 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008155 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008156ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01008158 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159
8160 new_dir = eap->arg;
8161#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00008162 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
8163 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008165 ex_pwd(NULL);
8166 return;
8167 }
8168#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008169
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008170 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008171
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008172 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8173 scope = CDSCOPE_WINDOW;
8174 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
8175 scope = CDSCOPE_TABPAGE;
8176
8177 if (changedir_func(new_dir, eap->forceit, scope))
8178 {
8179 // Echo the new current directory if the command was typed.
8180 if (KeyTyped || p_verbose >= 5)
8181 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 }
8183}
8184
8185/*
8186 * ":pwd".
8187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008189ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190{
8191 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8192 {
8193#ifdef BACKSLASH_IN_FILENAME
8194 slash_adjust(NameBuff);
8195#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02008196 if (p_verbose > 0)
8197 {
8198 char *context = "global";
8199
Bram Moolenaar05268152021-11-18 18:53:45 +00008200 if (last_chdir_reason != NULL)
8201 context = last_chdir_reason;
8202 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02008203 context = "window";
8204 else if (curtab->tp_localdir != NULL)
8205 context = "tabpage";
8206 smsg("[%s] %s", context, (char *)NameBuff);
8207 }
8208 else
8209 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 }
8211 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00008212 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213}
8214
8215/*
8216 * ":=".
8217 */
8218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008219ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008221 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008222 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223}
8224
8225 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008226ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008228 int n;
8229 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230
8231 if (cursor_valid())
8232 {
8233 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8234 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00008235 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008237
8238 len = eap->line2;
8239 switch (*eap->arg)
8240 {
8241 case 'm': break;
8242 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008243 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008244 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008245
8246 // Hide the cursor if invoked with !
8247 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248}
8249
8250/*
8251 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008252 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 */
8254 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008255do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256{
Bram Moolenaar52953192019-08-16 10:27:13 +02008257 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008258 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008259# ifdef ELAPSED_FUNC
8260 elapsed_T start_tv;
8261
8262 // Remember at what time we started, so that we know how much longer we
8263 // should wait after waiting for a bit.
8264 ELAPSED_INIT(start_tv);
8265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008267 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008268 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008269 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008270 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008271
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008272 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008273 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008275 wait_now = msec - done > 1000L ? 1000L : msec - done;
8276#ifdef FEAT_TIMERS
8277 {
8278 long due_time = check_due_timer();
8279
8280 if (due_time > 0 && due_time < wait_now)
8281 wait_now = due_time;
8282 }
8283#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008284#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008285 if (has_any_channel() && wait_now > 20L)
8286 wait_now = 20L;
8287#endif
8288#ifdef FEAT_SOUND
8289 if (has_any_sound_callback() && wait_now > 20L)
8290 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008291#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008292 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008293
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008294#ifdef FEAT_JOB_CHANNEL
8295 if (has_any_channel())
8296 ui_breakcheck_force(TRUE);
8297 else
8298#endif
8299 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008300#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008301 // Process the netbeans and clientserver messages that may have been
8302 // received in the call to ui_breakcheck() when the GUI is in use. This
8303 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008304 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008305#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008306
8307# ifdef ELAPSED_FUNC
8308 // actual time passed
8309 done = ELAPSED_FUNC(start_tv);
8310# else
8311 // guestimate time passed (will actually be more)
8312 done += wait_now;
8313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008315
8316 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8317 // input buffer, otherwise a following call to input() fails.
8318 if (got_int)
8319 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008320
8321 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008322 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323}
8324
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325/*
8326 * ":winsize" command (obsolete).
8327 */
8328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008329ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330{
8331 int w, h;
8332 char_u *arg = eap->arg;
8333 char_u *p;
8334
Keith Thompson184f71c2024-01-04 21:19:04 +01008335 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008336 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008337 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008338 return;
8339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 w = getdigits(&arg);
8341 arg = skipwhite(arg);
8342 p = arg;
8343 h = getdigits(&arg);
8344 if (*p != NUL && *arg == NUL)
8345 set_shellsize(w, h, TRUE);
8346 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008347 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348}
8349
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008351ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
8353 int xchar = NUL;
8354 char_u *p;
8355
8356 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8357 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008358 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 if (eap->arg[1] == NUL)
8360 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008361 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 return;
8363 }
8364 xchar = eap->arg[1];
8365 p = eap->arg + 2;
8366 }
8367 else
8368 p = eap->arg + 1;
8369
Bram Moolenaar63b91732021-08-05 20:40:03 +02008370 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008372 if (*p != NUL && *p != (
8373#ifdef FEAT_EVAL
8374 in_vim9script() ? '#' :
8375#endif
8376 '"')
8377 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008378 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008379 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008381 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008382 postponed_split_flags = cmdmod.cmod_split;
8383 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8385 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008386 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 }
8388}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389
Bram Moolenaar843ee412004-06-30 16:16:41 +00008390#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391/*
8392 * ":winpos".
8393 */
8394 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008395ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396{
8397 int x, y;
8398 char_u *arg = eap->arg;
8399 char_u *p;
8400
8401 if (*arg == NUL)
8402 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008403# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008404# ifdef VIMDLL
8405 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8406 mch_get_winpos(&x, &y) != FAIL)
8407# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008409# else
8410 if (mch_get_winpos(&x, &y) != FAIL)
8411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {
8413 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008414 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 }
8416 else
8417# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008418 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 }
8420 else
8421 {
8422 x = getdigits(&arg);
8423 arg = skipwhite(arg);
8424 p = arg;
8425 y = getdigits(&arg);
8426 if (*p == NUL || *arg != NUL)
8427 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008428 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 return;
8430 }
8431# ifdef FEAT_GUI
8432 if (gui.in_use)
8433 gui_mch_set_winpos(x, y);
8434 else if (gui.starting)
8435 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008436 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 gui_win_x = x;
8438 gui_win_y = y;
8439 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008440# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 else
8442# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008443# endif
8444# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008445 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446# endif
8447# ifdef HAVE_TGETENT
8448 if (*T_CWP)
8449 term_set_winpos(x, y);
8450# endif
8451 }
8452}
8453#endif
8454
8455/*
8456 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8457 */
8458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008459ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461 oparg_T oa;
8462
8463 clear_oparg(&oa);
8464 oa.regname = eap->regname;
8465 oa.start.lnum = eap->line1;
8466 oa.end.lnum = eap->line2;
8467 oa.line_count = eap->line2 - eap->line1 + 1;
8468 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008470 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {
8472 setpcmark();
8473 curwin->w_cursor.lnum = eap->line1;
8474 beginline(BL_SOL | BL_FIX);
8475 }
8476
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008477 if (VIsual_active)
8478 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008479
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 switch (eap->cmdidx)
8481 {
8482 case CMD_delete:
8483 oa.op_type = OP_DELETE;
8484 op_delete(&oa);
8485 break;
8486
8487 case CMD_yank:
8488 oa.op_type = OP_YANK;
8489 (void)op_yank(&oa, FALSE, TRUE);
8490 break;
8491
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008492 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008493 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008495 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8496#else
8497 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008499 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 oa.op_type = OP_RSHIFT;
8501 else
8502 oa.op_type = OP_LSHIFT;
8503 op_shift(&oa, FALSE, eap->amount);
8504 break;
8505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008507 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508}
8509
8510/*
8511 * ":put".
8512 */
8513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008514ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008516 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 if (eap->line2 == 0)
8518 {
8519 eap->line2 = 1;
8520 eap->forceit = TRUE;
8521 }
8522 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008523 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008524 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008525 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526}
8527
8528/*
64-bitmane08f10a2025-03-18 22:14:34 +01008529 * ":iput".
8530 */
8531 static void
8532ex_iput(exarg_T *eap)
8533{
8534 // ":0iput" works like ":1iput!".
8535 if (eap->line2 == 0)
8536 {
8537 eap->line2 = 1;
8538 eap->forceit = TRUE;
8539 }
8540 curwin->w_cursor.lnum = eap->line2;
8541 check_cursor_col();
8542 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
8543 PUT_LINE|PUT_CURSLINE
8544 |PUT_FIXINDENT);
8545}
8546
8547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 * Handle ":copy" and ":move".
8549 */
8550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008551ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
8553 long n;
8554
Bram Moolenaar491799b2020-08-01 19:23:43 +02008555#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008556 if (not_in_vim9(eap) == FAIL)
8557 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008558#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008559 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008560 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 {
8562 eap->nextcmd = NULL;
8563 return;
8564 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008565 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566
8567 /*
8568 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8569 */
8570 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8571 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008572 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 return;
8574 }
8575
8576 if (eap->cmdidx == CMD_move)
8577 {
8578 if (do_move(eap->line1, eap->line2, n) == FAIL)
8579 return;
8580 }
8581 else
8582 ex_copy(eap->line1, eap->line2, n);
8583 u_clearline();
8584 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008585 ex_may_print(eap);
8586}
8587
8588/*
8589 * Print the current line if flags were given to the Ex command.
8590 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008591 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008592ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008593{
8594 if (eap->flags != 0)
8595 {
8596 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8597 (eap->flags & EXFLAG_LIST));
8598 ex_no_reprint = TRUE;
8599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600}
8601
8602/*
8603 * ":smagic" and ":snomagic".
8604 */
8605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008606ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008608 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008610 magic_overruled = eap->cmdidx == CMD_smagic
8611 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008612 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008613 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614}
8615
8616/*
8617 * ":join".
8618 */
8619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008620ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621{
8622 curwin->w_cursor.lnum = eap->line1;
8623 if (eap->line1 == eap->line2)
8624 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008625 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 return;
8627 if (eap->line2 == curbuf->b_ml.ml_line_count)
8628 {
8629 beep_flush();
8630 return;
8631 }
8632 ++eap->line2;
8633 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008634 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008636 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637}
8638
8639/*
8640 * ":[addr]@r" or ":[addr]*r": execute register
8641 */
8642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008643ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644{
8645 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008646 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647
8648 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008649 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
8651#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008652 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653#endif
8654
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008655 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 c = *eap->arg;
8657 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8658 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008659 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008660 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8661 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008664 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008667 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008669 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008671 /*
8672 * Execute from the typeahead buffer.
8673 * Continue until the stuff buffer is empty and all added characters
8674 * have been consumed.
8675 */
8676 while (!stuff_empty() || typebuf.tb_len > prev_len)
8677 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8678
8679 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680}
8681
8682/*
8683 * ":!".
8684 */
8685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008686ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
8688 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8689}
8690
8691/*
8692 * ":undo".
8693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008695ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008697 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008698 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008699 else
8700 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701}
8702
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008703#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008705ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008706{
8707 char_u hash[UNDO_HASH_SIZE];
8708
8709 u_compute_hash(hash);
8710 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8711}
8712
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008714ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008715{
8716 char_u hash[UNDO_HASH_SIZE];
8717
8718 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008719 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008720}
8721#endif
8722
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723/*
8724 * ":redo".
8725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008727ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
8729 u_redo(1);
8730}
8731
8732/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008733 * ":earlier" and ":later".
8734 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008736ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008737{
8738 long count = 0;
8739 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008740 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008741 char_u *p = eap->arg;
8742
8743 if (*p == NUL)
8744 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008745 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008746 {
8747 count = getdigits(&p);
8748 switch (*p)
8749 {
8750 case 's': ++p; sec = TRUE; break;
8751 case 'm': ++p; sec = TRUE; count *= 60; break;
8752 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008753 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8754 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008755 }
8756 }
8757
8758 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008759 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008760 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008761 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8762 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008763}
8764
8765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 * ":redir": start/stop redirection.
8767 */
8768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008769ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770{
8771 char *mode;
8772 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008773 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774
Bram Moolenaarba768492016-07-08 15:32:54 +02008775#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008776 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008777 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008778 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008779 return;
8780 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008781#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008782
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 if (STRICMP(eap->arg, "END") == 0)
8784 close_redir();
8785 else
8786 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008787 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008789 ++arg;
8790 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008792 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 mode = "a";
8794 }
8795 else
8796 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008797 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798
8799 close_redir();
8800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008801 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008802 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 if (fname == NULL)
8804 return;
8805#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008806 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 {
8808 char_u *browseFile;
8809
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008810 browseFile = do_browse(BROWSE_SAVE,
8811 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008812 fname, NULL, NULL,
8813 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008815 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 vim_free(fname);
8817 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008818 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 }
8820#endif
8821
8822 redir_fd = open_exfile(fname, eap->forceit, mode);
8823 vim_free(fname);
8824 }
8825#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008826 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008828 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008830 ++arg;
8831 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008833 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008834 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008836 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008838 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008839 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008840 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008841 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008843 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008844 if (*arg == '>')
8845 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008846 // Make register empty when not using @A-@Z and the
8847 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008848 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008849 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008851 }
8852 if (*arg != NUL)
8853 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008854 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008855 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008856 }
8857 }
8858 else if (*arg == '=' && arg[1] == '>')
8859 {
8860 int append;
8861
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008862 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008863 close_redir();
8864 arg += 2;
8865
8866 if (*arg == '>')
8867 {
8868 ++arg;
8869 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 }
8871 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008872 append = FALSE;
8873
8874 if (var_redir_start(skipwhite(arg), append) == OK)
8875 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 }
8877#endif
8878
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008879 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008882 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008884
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008885 // Make sure redirection is not off. Can happen for cmdline completion
8886 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008887 if (redir_fd != NULL
8888#ifdef FEAT_EVAL
8889 || redir_reg || redir_vname
8890#endif
8891 )
8892 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893}
8894
8895/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008896 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008898 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008899ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008901 redraw_cmd(eap->forceit);
8902}
8903
8904/*
8905 * ":redraw": force redraw, with clear if "clear" is TRUE.
8906 */
8907 void
8908redraw_cmd(int clear)
8909{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008910 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008912
8913 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008915
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008916 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008918 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 if (need_maketitle)
8920 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008921#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8922# ifdef VIMDLL
8923 if (!gui.in_use)
8924# endif
8925 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008926#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008927 RedrawingDisabled = save_RedrawingDisabled;
8928 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929
Bram Moolenaar5017c662022-04-07 18:06:08 +01008930 // After drawing the statusline screen_attr may still be set.
8931 screen_stop_highlight();
8932
Bram Moolenaara2a89732022-08-31 14:46:18 +01008933 // Reset msg_didout, so that a message that's there is overwritten.
8934 msg_didout = FALSE;
8935 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008937 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008938 need_wait_return = FALSE;
8939
Bram Moolenaara653e532022-04-19 11:38:24 +01008940 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008941 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008942 redrawcmdline();
8943
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 out_flush();
8945}
8946
8947/*
8948 * ":redrawstatus": force redraw of status line(s)
8949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008951ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 if (eap->forceit)
8954 status_redraw_all();
8955 else
8956 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008957 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008958 return; // redraw later
8959
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008960 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008961 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008962
8963 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008964 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008965
zeertzjq320d9102022-09-20 17:12:13 +01008966 if (State & MODE_CMDLINE)
8967 redraw_statuslines();
8968 else
8969 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008970 RedrawingDisabled = save_RedrawingDisabled;
8971 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973}
8974
Bram Moolenaare12bab32019-01-08 22:02:56 +01008975/*
8976 * ":redrawtabline": force redraw of the tabline
8977 */
8978 static void
8979ex_redrawtabline(exarg_T *eap UNUSED)
8980{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008981 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008982 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008983
8984 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008985 p_lz = FALSE;
8986
8987 draw_tabline();
8988
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008989 RedrawingDisabled = save_RedrawingDisabled;
8990 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008991 out_flush();
8992}
8993
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008995close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996{
8997 if (redir_fd != NULL)
8998 {
8999 fclose(redir_fd);
9000 redir_fd = NULL;
9001 }
9002#ifdef FEAT_EVAL
9003 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009004 if (redir_vname)
9005 {
9006 var_redir_stop();
9007 redir_vname = 0;
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009#endif
9010}
9011
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02009012#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009013 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02009014vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009015{
9016 if (vim_mkdir(name, prot) != 0)
9017 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00009018 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009019 return FAIL;
9020 }
9021 return OK;
9022}
9023#endif
9024
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025/*
9026 * Open a file for writing for an Ex command, with some checks.
9027 * Return file descriptor, or NULL on failure.
9028 */
9029 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009030open_exfile(
9031 char_u *fname,
9032 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009033 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034{
9035 FILE *fd;
9036
9037#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009038 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 if (mch_isdir(fname))
9040 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01009041 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 return NULL;
9043 }
9044#endif
9045 if (!forceit && *mode != 'a' && vim_fexists(fname))
9046 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00009047 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 return NULL;
9049 }
9050
9051 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00009052 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053
9054 return fd;
9055}
9056
9057/*
9058 * ":mark" and ":k".
9059 */
9060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009061ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
9063 pos_T pos;
9064
Bram Moolenaar10b94212021-02-19 21:42:57 +01009065#ifdef FEAT_EVAL
9066 if (not_in_vim9(eap) == FAIL)
9067 return;
9068#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009069 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009071 emsg(_(e_argument_required));
9072 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009074
9075 if (eap->arg[1] != NUL) // more than one character?
9076 {
9077 semsg(_(e_trailing_characters_str), eap->arg);
9078 return;
9079 }
9080
9081 pos = curwin->w_cursor; // save curwin->w_cursor
9082 curwin->w_cursor.lnum = eap->line2;
9083 beginline(BL_WHITE | BL_FIX);
9084 if (setmark(*eap->arg) == FAIL) // set mark
9085 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
9086 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087}
9088
9089/*
9090 * Update w_topline, w_leftcol and the cursor position.
9091 */
9092 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009093update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009095 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 update_topline();
9097 if (!curwin->w_p_wrap)
9098 validate_cursor();
9099 update_curswant();
9100}
9101
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009103 * Save the current State and go to Normal mode.
9104 * Return TRUE if the typeahead could be saved.
9105 */
9106 int
9107save_current_state(save_state_T *sst)
9108{
9109 sst->save_msg_scroll = msg_scroll;
9110 sst->save_restart_edit = restart_edit;
9111 sst->save_msg_didout = msg_didout;
9112 sst->save_State = State;
9113 sst->save_insertmode = p_im;
9114 sst->save_finish_op = finish_op;
9115 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009116 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009117 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009118
Bram Moolenaar4324d872020-12-01 20:12:24 +01009119 msg_scroll = FALSE; // no msg scrolling in Normal mode
9120 restart_edit = 0; // don't go to Insert mode
9121 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01009122
Bram Moolenaara452b802020-12-01 21:47:59 +01009123 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01009124 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009125
9126 /*
9127 * Save the current typeahead. This is required to allow using ":normal"
9128 * from an event handler and makes sure we don't hang when the argument
9129 * ends with half a command.
9130 */
9131 save_typeahead(&sst->tabuf);
9132 return sst->tabuf.typebuf_valid;
9133}
9134
9135 void
9136restore_current_state(save_state_T *sst)
9137{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009138 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02009139 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009140
9141 msg_scroll = sst->save_msg_scroll;
9142 restart_edit = sst->save_restart_edit;
9143 p_im = sst->save_insertmode;
9144 finish_op = sst->save_finish_op;
9145 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009146 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009147 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009148 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01009149 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009150
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009151 // Restore the state (needed when called from a function executed for
9152 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009153 State = sst->save_State;
9154#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009155 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009156#endif
9157}
9158
9159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 * ":normal[!] {commands}": Execute normal mode commands.
9161 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009162 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009163ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009165 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 char_u *arg = NULL;
9167 int l;
9168 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009170 if (ex_normal_lock > 0)
9171 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00009172 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009173 return;
9174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 if (ex_normal_busy >= p_mmd)
9176 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009177 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 return;
9179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 /*
9182 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9183 * this for the K_SPECIAL leading byte, otherwise special keys will not
9184 * work.
9185 */
9186 if (has_mbyte)
9187 {
9188 int len = 0;
9189
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009190 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 for (p = eap->arg; *p != NUL; ++p)
9192 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009193#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009194 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01009196#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009197 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009198 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009199#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 )
9203 len += 2;
9204 }
9205 if (len > 0)
9206 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009207 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 if (arg != NULL)
9209 {
9210 len = 0;
9211 for (p = eap->arg; *p != NUL; ++p)
9212 {
9213 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01009214#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 if (*p == CSI)
9216 {
9217 arg[len++] = KS_EXTRA;
9218 arg[len++] = (int)KE_CSI;
9219 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009220#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009221 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 {
9223 arg[len++] = *++p;
9224 if (*p == K_SPECIAL)
9225 {
9226 arg[len++] = KS_SPECIAL;
9227 arg[len++] = KE_FILLER;
9228 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009229#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 else if (*p == CSI)
9231 {
9232 arg[len++] = KS_EXTRA;
9233 arg[len++] = (int)KE_CSI;
9234 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 }
9237 arg[len] = NUL;
9238 }
9239 }
9240 }
9241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009243 ++ex_normal_busy;
9244 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 {
9246 /*
9247 * Repeat the :normal command for each line in the range. When no
9248 * range given, execute it just once, without positioning the cursor
9249 * first.
9250 */
9251 do
9252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 if (eap->addr_count != 0)
9254 {
9255 curwin->w_cursor.lnum = eap->line1++;
9256 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02009257 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 }
9259
Bram Moolenaar13505972019-01-24 15:04:48 +01009260 exec_normal_cmd(arg != NULL
9261 ? arg
9262 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 }
9264 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9265 }
9266
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009267 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 update_topline_cursor();
9269
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009270 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009272 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009273#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009274 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009275#endif
9276
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278}
9279
9280/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009281 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 */
9283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009284ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009286 if (eap->forceit)
9287 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009288 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009289 if (!curwin->w_cursor.lnum)
9290 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009291 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009292 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009293#ifdef FEAT_TERMINAL
9294 // Ignore this when running in an active terminal.
9295 if (term_job_running(curbuf->b_term))
9296 return;
9297#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009298
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009299 // Ignore the command when already in Insert mode. Inserting an
9300 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009301 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009302 return;
9303
Bram Moolenaar64969662005-12-14 21:59:55 +00009304 if (eap->cmdidx == CMD_startinsert)
9305 restart_edit = 'a';
9306 else if (eap->cmdidx == CMD_startreplace)
9307 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009309 restart_edit = 'V';
9310
9311 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009313 if (eap->cmdidx == CMD_startinsert)
9314 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009315 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009317
9318 if (VIsual_active)
9319 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320}
9321
9322/*
9323 * ":stopinsert"
9324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009326ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327{
9328 restart_edit = 0;
9329 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009330 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009333/*
9334 * Execute normal mode command "cmd".
9335 * "remap" can be REMAP_NONE or REMAP_YES.
9336 */
9337 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009338exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009339{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009340 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009341 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009342 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009343}
Bram Moolenaar25281632016-01-21 23:32:32 +01009344
Bram Moolenaar25281632016-01-21 23:32:32 +01009345/*
9346 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009347 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009348 */
9349 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009350exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009351{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009352 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009353 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009354
Bram Moolenaar905dd902019-04-07 14:21:47 +02009355 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9356 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009357 clear_oparg(&oa);
9358 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009359 while ((!stuff_empty()
9360 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009361 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009362 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009363 {
9364 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009365#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009366 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009367 && oa.op_type == OP_NOP && oa.regname == NUL
9368 && !VIsual_active)
9369 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009370 // If terminal_loop() returns OK we got a key that is handled
9371 // in Normal model. With FAIL we first need to position the
9372 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009373 if (terminal_loop(TRUE) == OK)
9374 normal_cmd(&oa, TRUE);
9375 }
9376 else
9377#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009378 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009379 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009380 }
9381}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009382
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383#ifdef FEAT_FIND_ID
9384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009385ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
9387 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9388 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009389 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390}
9391
Bram Moolenaar4033c552017-09-16 20:54:51 +02009392#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393/*
9394 * ":psearch"
9395 */
9396 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009397ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398{
9399 g_do_tagpreview = p_pvh;
9400 ex_findpat(eap);
9401 g_do_tagpreview = 0;
9402}
9403#endif
9404
9405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009406ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
9408 int whole = TRUE;
9409 long n;
9410 char_u *p;
9411 int action;
9412
9413 switch (cmdnames[eap->cmdidx].cmd_name[2])
9414 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009415 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9417 action = ACTION_GOTO;
9418 else
9419 action = ACTION_SHOW;
9420 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009421 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 action = ACTION_SHOW_ALL;
9423 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009424 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 action = ACTION_GOTO;
9426 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009427 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 action = ACTION_SPLIT;
9429 break;
9430 }
9431
9432 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009433 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 {
9435 n = getdigits(&eap->arg);
9436 eap->arg = skipwhite(eap->arg);
9437 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009438 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 {
9440 whole = FALSE;
9441 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009442 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 if (*p)
9444 {
9445 *p++ = NUL;
9446 p = skipwhite(p);
9447
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009448 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009449 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009450 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009452 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 }
9454 }
9455 if (!eap->skip)
9456 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9457 whole, !eap->forceit,
9458 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009459 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460}
9461#endif
9462
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463
Bram Moolenaar4033c552017-09-16 20:54:51 +02009464#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465/*
9466 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9467 */
9468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009469ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009471 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9473}
9474
9475/*
9476 * ":pedit"
9477 */
9478 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009479ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480{
9481 win_T *curwin_save = curwin;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009482 prepare_preview_window();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009484 // Edit the file.
9485 do_exedit(eap, NULL);
9486
9487 back_to_current_window(curwin_save);
9488}
9489
9490/*
9491 * ":pbuffer"
9492 */
9493 static void
9494ex_pbuffer(exarg_T *eap)
9495{
9496 win_T *curwin_save = curwin;
9497 prepare_preview_window();
9498
9499 // Go to the buffer.
9500 do_exbuffer(eap);
9501
9502 back_to_current_window(curwin_save);
9503}
9504
9505 static void
9506prepare_preview_window(void)
9507{
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009508 if (ERROR_IF_ANY_POPUP_WINDOW)
9509 return;
9510
Bram Moolenaar026587b2019-08-17 15:08:00 +02009511 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009513 prepare_tagpreview(TRUE, TRUE, FALSE);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009514}
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009515
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009516 static void
9517back_to_current_window(win_T *curwin_save)
9518{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 if (curwin != curwin_save && win_valid(curwin_save))
9520 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009521 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009523 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 win_enter(curwin_save, TRUE);
9525 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009526# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009527 else if (WIN_IS_POPUP(curwin))
9528 {
9529 // can't keep focus in popup window
9530 win_enter(firstwin, TRUE);
9531 }
9532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 g_do_tagpreview = 0;
9534}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536
9537/*
9538 * ":stag", ":stselect" and ":stjump".
9539 */
9540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009541ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542{
9543 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009544 postponed_split_flags = cmdmod.cmod_split;
9545 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9547 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009548 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550
9551/*
9552 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9553 */
9554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009555ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556{
9557 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9558}
9559
9560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009561ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
9563 int cmd;
9564
9565 switch (name[1])
9566 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009567 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009569 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009571 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009573 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009575 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009577 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009579 case 'f': // ":tfirst"
9580 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009582 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009584 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009586 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009588 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 return;
9590 }
9591#endif
9592 cmd = DT_TAG;
9593 break;
9594 }
9595
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009596 if (name[0] == 'l')
9597 {
9598#ifndef FEAT_QUICKFIX
9599 ex_ni(eap);
9600 return;
9601#else
9602 cmd = DT_LTAG;
9603#endif
9604 }
9605
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9607 eap->forceit, TRUE);
9608}
9609
John Marriotted908f72024-04-18 22:46:56 +02009610enum {
9611 SPEC_PERC = 0,
9612 SPEC_HASH,
9613 SPEC_CWORD, // cursor word
9614 SPEC_CCWORD, // cursor WORD
9615 SPEC_CEXPR, // expr under cursor
9616 SPEC_CFILE, // cursor path name
9617 SPEC_SFILE, // ":so" file name
9618 SPEC_SLNUM, // ":so" file line number
9619 SPEC_STACK, // call stack
9620 SPEC_SCRIPT, // script file name
9621 SPEC_AFILE, // autocommand file name
9622 SPEC_ABUF, // autocommand buffer number
9623 SPEC_AMATCH, // autocommand match name
9624 SPEC_SFLNUM, // script file line number
9625 SPEC_SID // script ID: <SNR>123_
9626#ifdef FEAT_CLIENTSERVER
9627 , SPEC_CLIENT
9628#endif
9629};
9630
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009632 * Check "str" for starting with a special cmdline variable.
9633 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9634 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9635 */
9636 int
Mike Williams51024bb2024-05-30 07:46:30 +02009637find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009638{
John Marriotted908f72024-04-18 22:46:56 +02009639 // must be sorted by the 'value' field because it is used by bsearch()!
9640 static keyvalue_T spec_str_tab[] = {
9641 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9642 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9643 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9644 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9645 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9646 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9647 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009648#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009649 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009650#endif
John Marriotted908f72024-04-18 22:46:56 +02009651 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9652 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9653 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9654 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9655 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9656 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009657 };
John Marriotted908f72024-04-18 22:46:56 +02009658 keyvalue_T target;
9659 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009660
John Marriotted908f72024-04-18 22:46:56 +02009661 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009662 {
John Marriotted908f72024-04-18 22:46:56 +02009663 case '%':
9664 *usedlen = 1;
9665 return SPEC_PERC;
9666
9667 case '#':
9668 *usedlen = 1;
9669 return SPEC_HASH;
9670
9671 case '<':
9672 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +01009673 target.value.string = src + 1; // skip over '<'
9674 target.value.length = 0; // not used, see cmp_keyvalue_value_n()
John Marriotted908f72024-04-18 22:46:56 +02009675
John Marriott8d4477e2024-11-02 15:59:01 +01009676 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab,
9677 ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]),
9678 cmp_keyvalue_value_n);
John Marriotted908f72024-04-18 22:46:56 +02009679 if (entry == NULL)
9680 return -1;
9681
John Marriott8d4477e2024-11-02 15:59:01 +01009682 *usedlen = entry->value.length + 1;
John Marriotted908f72024-04-18 22:46:56 +02009683 return entry->key;
9684
9685 default:
9686 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009687 }
John Marriotted908f72024-04-18 22:46:56 +02009688
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009689 return -1;
9690}
9691
9692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 * Evaluate cmdline variables.
9694 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009695 * change "%" to curbuf->b_ffname
9696 * "#" to curwin->w_alt_fnum
9697 * "%%" to curwin->w_alt_fnum in Vim9 script
9698 * "<cword>" to word under the cursor
9699 * "<cWORD>" to WORD under the cursor
9700 * "<cexpr>" to C-expression under the cursor
9701 * "<cfile>" to path name under the cursor
9702 * "<sfile>" to sourced file name
9703 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009704 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009705 * "<slnum>" to sourced file line number
9706 * "<afile>" to file name for autocommand
9707 * "<abuf>" to buffer number for autocommand
9708 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 *
9710 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9711 * "" for error without a message) and NULL is returned.
9712 * Returns an allocated string if a valid match was found.
9713 * Returns NULL if no match was found. "usedlen" then still contains the
9714 * number of characters to skip.
9715 */
9716 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009717eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009718 char_u *src, // pointer into commandline
9719 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009720 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009721 linenr_T *lnump, // line number for :e command, or NULL
9722 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009723 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009724 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009725 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726{
9727 int i;
9728 char_u *s;
9729 char_u *result;
9730 char_u *resultbuf = NULL;
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +01009731 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009733 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009735 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738
9739 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009740 if (escaped != NULL)
9741 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742
9743 /*
9744 * Check if there is something to do.
9745 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009746 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009747 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 {
9749 *usedlen = 1;
9750 return NULL;
9751 }
9752
9753 /*
9754 * Skip when preceded with a backslash "\%" and "\#".
9755 * Note: In "\\%" the % is also not recognized!
9756 */
9757 if (src > srcstart && src[-1] == '\\')
9758 {
9759 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009760 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 return NULL;
9762 }
9763
9764 /*
9765 * word or WORD under cursor
9766 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009767 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9768 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009770 resultlen = find_ident_under_cursor(&result,
9771 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9772 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9773 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 if (resultlen == 0)
9775 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009776 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 return NULL;
9778 }
9779 }
9780
9781 /*
9782 * '#': Alternate file name
9783 * '%': Current file name
9784 * File name under the cursor
9785 * File name for autocommand
9786 * and following modifiers
9787 */
9788 else
9789 {
Mike Williams51024bb2024-05-30 07:46:30 +02009790 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009791
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 switch (spec_idx)
9793 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009794 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009795#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009796 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009799 // '%': current file
9800 if (curbuf->b_fname == NULL)
9801 {
9802 result = (char_u *)"";
9803 valid = 0; // Must have ":p:h" to be valid
9804 }
9805 else
9806 {
9807 result = curbuf->b_fname;
9808 tilde_file = STRCMP(result, "~") == 0;
9809 }
9810 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009812#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009813 // "%%" alternate file
9814 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009815#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009816 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009817 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009818 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009820 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 result = arg_all();
9822 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009823 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009824 if (escaped != NULL)
9825 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 break;
9828 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009829 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009830 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009831 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009833 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009834 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009835 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009836 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009838 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009840 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009841 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009842 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009843 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009844 return NULL;
9845 }
9846#ifdef FEAT_EVAL
9847 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9848 (long)i);
9849 if (result == NULL)
9850 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009851 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009852 return NULL;
9853 }
9854#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009855 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 }
9859 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009860 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009861 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9862 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009863 buf = buflist_findnr(i);
9864 if (buf == NULL)
9865 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009866 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009867 return NULL;
9868 }
9869 if (lnump != NULL)
9870 *lnump = ECMD_LAST;
9871 if (buf->b_fname == NULL)
9872 {
9873 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009874 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009875 }
9876 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009877 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009878 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009879 tilde_file = STRCMP(result, "~") == 0;
9880 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 break;
9883
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009884 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009885 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 if (result == NULL)
9887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009888 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 return NULL;
9890 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009891 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009894 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009896 if (result != NULL && !autocmd_fname_full)
9897 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009898 // Still need to turn the fname into a full path. It is
9899 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009900 autocmd_fname_full = TRUE;
9901 result = FullName_save(autocmd_fname, FALSE);
9902 vim_free(autocmd_fname);
9903 autocmd_fname = result;
9904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 if (result == NULL)
9906 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009907 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 return NULL;
9909 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009910 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 break;
9912
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009913 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 if (autocmd_bufnr <= 0)
9915 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009916 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 return NULL;
9918 }
9919 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9920 result = strbuf;
9921 break;
9922
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009923 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 result = autocmd_match;
9925 if (result == NULL)
9926 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009927 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 return NULL;
9929 }
9930 break;
9931
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009932 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009933 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 if (result == NULL)
9935 {
LemonBoy6013d002022-04-09 21:42:10 +01009936 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9937 return NULL;
9938 }
9939 resultbuf = result; // remember allocated string
9940 break;
9941 case SPEC_STACK: // call stack
9942 result = estack_sfile(ESTACK_STACK);
9943 if (result == NULL)
9944 {
9945 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9946 return NULL;
9947 }
9948 resultbuf = result; // remember allocated string
9949 break;
9950 case SPEC_SCRIPT: // script file name
9951 result = estack_sfile(ESTACK_SCRIPT);
9952 if (result == NULL)
9953 {
9954 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 return NULL;
9956 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009957 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009959
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009960 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009961 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009962 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009963 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009964 return NULL;
9965 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009966 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009967 result = strbuf;
9968 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009969
9970#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009971 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009972 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009973 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009974 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009975 return NULL;
9976 }
9977 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009978 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009979 result = strbuf;
9980 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009981
Bram Moolenaar90944302020-08-01 20:45:11 +02009982 case SPEC_SID:
9983 if (current_sctx.sc_sid <= 0)
9984 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009985 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009986 return NULL;
9987 }
9988 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9989 result = strbuf;
9990 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009991#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009992
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009993#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009994 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009995 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9996 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 result = strbuf;
9998 break;
9999#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010000
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010001 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010002 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010003 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 }
10005
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +010010006 resultlen = STRLEN(result); // length of new string
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010007 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 {
10009 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +010010011 resultlen = s - result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 else if (!skip_mod)
10014 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010015 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 &resultlen);
10017 if (result == NULL)
10018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010019 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 return NULL;
10021 }
10022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 }
10024
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010025 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010027 if (empty_is_error)
10028 {
10029 if (valid != VALID_HEAD + VALID_PATH)
10030 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
10031 else
10032 *errormsg = _(e_evaluates_to_an_empty_string);
10033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 result = NULL;
10035 }
10036 else
10037 result = vim_strnsave(result, resultlen);
10038 vim_free(resultbuf);
10039 return result;
10040}
10041
10042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 * Expand the <sfile> string in "arg".
10044 *
10045 * Returns an allocated string, or NULL for any error.
10046 */
10047 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010048expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010050 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +020010052 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +020010054 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +020010056 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +020010057 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 char_u *p;
10059
John Marriotted908f72024-04-18 22:46:56 +020010060 resultlen = STRLEN(arg);
10061 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 if (result == NULL)
10063 return NULL;
10064
10065 for (p = result; *p; )
10066 {
10067 if (STRNCMP(p, "<sfile>", 7) != 0)
10068 ++p;
10069 else
10070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010071 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +010010072 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 if (errormsg != NULL)
10074 {
10075 if (*errormsg)
10076 emsg(errormsg);
10077 vim_free(result);
10078 return NULL;
10079 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010080 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 {
10082 p += srclen;
10083 continue;
10084 }
John Marriotted908f72024-04-18 22:46:56 +020010085 repllen = STRLEN(repl);
10086 resultlen += repllen - srclen;
10087 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 if (newres == NULL)
10089 {
10090 vim_free(repl);
10091 vim_free(result);
10092 return NULL;
10093 }
John Marriotted908f72024-04-18 22:46:56 +020010094 len = p - result;
10095 mch_memmove(newres, result, len);
10096 STRCPY(newres + len, repl);
10097 len += repllen;
10098 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 vim_free(repl);
10100 vim_free(result);
10101 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010102 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 }
10104 }
10105
10106 return result;
10107}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010110/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010111 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010112 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010115dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 if (fname == NULL)
10118 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010119 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120}
10121#endif
10122
10123/*
10124 * ":behave {mswin,xterm}"
10125 */
10126 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010127ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128{
10129 if (STRCMP(eap->arg, "mswin") == 0)
10130 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010131 set_option_value_give_err((char_u *)"selection",
10132 0L, (char_u *)"exclusive", 0);
10133 set_option_value_give_err((char_u *)"selectmode",
10134 0L, (char_u *)"mouse,key", 0);
10135 set_option_value_give_err((char_u *)"mousemodel",
10136 0L, (char_u *)"popup", 0);
10137 set_option_value_give_err((char_u *)"keymodel",
10138 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 }
10140 else if (STRCMP(eap->arg, "xterm") == 0)
10141 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010142 set_option_value_give_err((char_u *)"selection",
10143 0L, (char_u *)"inclusive", 0);
10144 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
10145 set_option_value_give_err((char_u *)"mousemodel",
10146 0L, (char_u *)"extend", 0);
10147 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 }
10149 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010150 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151}
10152
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153static int filetype_detect = FALSE;
10154static int filetype_plugin = FALSE;
10155static int filetype_indent = FALSE;
10156
10157/*
10158 * ":filetype [plugin] [indent] {on,off,detect}"
10159 * on: Load the filetype.vim file to install autocommands for file types.
10160 * off: Load the ftoff.vim file to remove all autocommands for file types.
10161 * plugin on: load filetype.vim and ftplugin.vim
10162 * plugin off: load ftplugof.vim
10163 * indent on: load filetype.vim and indent.vim
10164 * indent off: load indoff.vim
10165 */
10166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010167ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168{
10169 char_u *arg = eap->arg;
10170 int plugin = FALSE;
10171 int indent = FALSE;
10172
10173 if (*eap->arg == NUL)
10174 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010175 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010176 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 filetype_detect ? "ON" : "OFF",
10178 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10179 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10180 return;
10181 }
10182
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010183 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 for (;;)
10185 {
10186 if (STRNCMP(arg, "plugin", 6) == 0)
10187 {
10188 plugin = TRUE;
10189 arg = skipwhite(arg + 6);
10190 continue;
10191 }
10192 if (STRNCMP(arg, "indent", 6) == 0)
10193 {
10194 indent = TRUE;
10195 arg = skipwhite(arg + 6);
10196 continue;
10197 }
10198 break;
10199 }
10200 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10201 {
10202 if (*arg == 'o' || !filetype_detect)
10203 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010204 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 filetype_detect = TRUE;
10206 if (plugin)
10207 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010208 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 filetype_plugin = TRUE;
10210 }
10211 if (indent)
10212 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010213 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 filetype_indent = TRUE;
10215 }
10216 }
10217 if (*arg == 'd')
10218 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010219 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010220 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 }
10222 }
10223 else if (STRCMP(arg, "off") == 0)
10224 {
10225 if (plugin || indent)
10226 {
10227 if (plugin)
10228 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010229 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 filetype_plugin = FALSE;
10231 }
10232 if (indent)
10233 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010234 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 filetype_indent = FALSE;
10236 }
10237 }
10238 else
10239 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010240 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 filetype_detect = FALSE;
10242 }
10243 }
10244 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010245 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246}
10247
10248/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010249 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 */
10251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010252ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253{
zeertzjq5bf6c212024-03-31 18:41:27 +020010254 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010255 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010256
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010257 char_u *arg = eap->arg;
10258 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10259 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010260
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010261 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10262 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +020010263 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010267ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268{
10269#ifdef FEAT_DIGRAPHS
10270 if (*eap->arg != NUL)
10271 putdigraph(eap->arg);
10272 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010273 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274#else
mityu61065042021-07-19 20:07:21 +020010275 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276#endif
10277}
10278
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010279#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10280 void
10281set_no_hlsearch(int flag)
10282{
10283 no_hlsearch = flag;
10284# ifdef FEAT_EVAL
10285 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10286# endif
10287}
10288
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289/*
10290 * ":nohlsearch"
10291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010293ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010295 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +010010296 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298#endif
10299
10300#ifdef FEAT_CRYPT
10301/*
10302 * ":X": Get crypt key
10303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010305ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010307 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010308 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309}
10310#endif
10311
10312#ifdef FEAT_FOLDING
10313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010314ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315{
10316 if (foldManualAllowed(TRUE))
10317 foldCreate(eap->line1, eap->line2);
10318}
10319
10320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010321ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322{
10323 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10324 eap->forceit, FALSE);
10325}
10326
10327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010328ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329{
10330 linenr_T lnum;
10331
Bram Moolenaar4facea32019-10-12 20:17:40 +020010332# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010333 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010334# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010335
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010336 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10338 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10339 ml_setmarked(lnum);
10340
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010341 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010343 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010344# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010345 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010346# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347}
10348#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010350#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010351/*
10352 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10353 * instead of a quickfix command.
10354 */
10355 int
10356is_loclist_cmd(int cmdidx)
10357{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010358 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010359 return FALSE;
10360 return cmdnames[cmdidx].cmd_name[0] == 'l';
10361}
10362#endif
10363
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010364 int
10365get_pressedreturn(void)
10366{
10367 return ex_pressedreturn;
10368}
10369
10370 void
10371set_pressedreturn(int val)
10372{
10373 ex_pressedreturn = val;
10374}