blob: b87e3c76eaf032436f0756a9d1dba87fda2bb337 [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 Moolenaare4ce8252019-08-04 15:30:16 +0200292# define ex_eval ex_ni
293# define ex_execute ex_ni
294# define ex_finally ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000295# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200296# define ex_finish ex_ni
297# define ex_function ex_ni
298# define ex_if ex_ni
299# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200300# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200301# define ex_lockvar ex_ni
302# define ex_oldfiles ex_ni
303# define ex_options ex_ni
304# define ex_packadd ex_ni
305# define ex_packloadall ex_ni
306# define ex_return ex_ni
307# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308# define ex_throw ex_ni
309# define ex_try ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000310# define ex_type ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200312# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313# define ex_import ex_ni
314# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200316#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317# define ex_loadview ex_ni
318#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200319#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# define ex_viminfo ex_ni
321#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100322static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100323static void ex_filetype(exarg_T *eap);
324static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000326# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327# define ex_diffpatch ex_ni
328# define ex_diffgetput ex_ni
329# define ex_diffsplit ex_ni
330# define ex_diffthis ex_ni
331# define ex_diffupdate ex_ni
332#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#else
337# define ex_nohlsearch ex_ni
338# define ex_match ex_ni
339#endif
340#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#else
343# define ex_X ex_ni
344#endif
345#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100346static void ex_fold(exarg_T *eap);
347static void ex_foldopen(exarg_T *eap);
348static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#else
350# define ex_fold ex_ni
351# define ex_foldopen ex_ni
352# define ex_folddo ex_ni
353#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100354#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355# define ex_language ex_ni
356#endif
357#ifndef FEAT_SIGNS
358# define ex_sign ex_ni
359#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000360#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200361# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000362# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200363# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365
Bram Moolenaar05159a02005-02-26 23:04:13 +0000366#ifndef FEAT_PROFILE
367# define ex_profile ex_ni
368#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200369#ifndef FEAT_TERMINAL
370# define ex_terminal ex_ni
371#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200372#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
373# define ex_xrestore ex_ni
374#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100375#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200376# define ex_popupclear ex_ni
377#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379/*
380 * Declare cmdnames[].
381 */
382#define DO_DECLARE_EXCMD
383#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200384#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static char_u dollar_command[2] = {'$', 0};
387
388
389#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100390// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391typedef struct
392{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100393 char_u *line; // command line
394 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395} wcmd_T;
396
397/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000402struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100404 garray_T *lines_gap; // growarray with line info
405 int current_line; // last read line from growarray
406 int repeating; // TRUE when looping a second time
407 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100408 char_u *(*lc_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 void *cookie;
410};
411
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200412static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100413static int store_loop_line(garray_T *gap, char_u *line);
414static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000415
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100416// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000417struct dbg_stuff
418{
419 int trylevel;
420 int force_abort;
421 except_T *caught_stack;
422 char_u *vv_exception;
423 char_u *vv_throwpoint;
424 int did_emsg;
425 int got_int;
426 int did_throw;
427 int need_rethrow;
428 int check_cstack;
429 except_T *current_exception;
430};
431
Bram Moolenaared203462004-06-16 11:19:22 +0000432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100433save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000434{
435 dsp->trylevel = trylevel; trylevel = 0;
436 dsp->force_abort = force_abort; force_abort = FALSE;
437 dsp->caught_stack = caught_stack; caught_stack = NULL;
438 dsp->vv_exception = v_exception(NULL);
439 dsp->vv_throwpoint = v_throwpoint(NULL);
440
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100441 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000442 dsp->did_emsg = did_emsg; did_emsg = FALSE;
443 dsp->got_int = got_int; got_int = FALSE;
444 dsp->did_throw = did_throw; did_throw = FALSE;
445 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
446 dsp->check_cstack = check_cstack; check_cstack = FALSE;
447 dsp->current_exception = current_exception; current_exception = NULL;
448}
449
450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100451restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000452{
453 suppress_errthrow = FALSE;
454 trylevel = dsp->trylevel;
455 force_abort = dsp->force_abort;
456 caught_stack = dsp->caught_stack;
457 (void)v_exception(dsp->vv_exception);
458 (void)v_throwpoint(dsp->vv_throwpoint);
459 did_emsg = dsp->did_emsg;
460 got_int = dsp->got_int;
461 did_throw = dsp->did_throw;
462 need_rethrow = dsp->need_rethrow;
463 check_cstack = dsp->check_cstack;
464 current_exception = dsp->current_exception;
465}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468/*
Colin Kennedy65e580b2024-03-26 18:29:30 +0100469 * Check if ffname differs from fnum.
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200470 * fnum is a buffer number. 0 == current buffer, 1-or-more must be a valid
471 * buffer ID.
Colin Kennedy65e580b2024-03-26 18:29:30 +0100472 * 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{
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200478 if (fnum != 0)
479 {
480 if (fnum == curbuf->b_fnum)
481 return FALSE;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100482
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200483 return TRUE;
484 }
Colin Kennedy65e580b2024-03-26 18:29:30 +0100485
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200486 if (ffname == NULL)
487 return TRUE;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100488
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200489 if (*ffname == NUL)
490 return FALSE;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100491
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200492 // TODO: Need a reliable way to know whether a buffer is meant to live
493 // on-disk !curbuf->b_dev_valid is not always available (example: missing
494 // on Windows)
495 if (curbuf->b_sfname != NULL
496 && *curbuf->b_sfname != NUL)
497 // This occurs with unsaved buffers. In which case `ffname` actually
498 // corresponds to curbuf->b_sfname
499 return fnamecmp(ffname, curbuf->b_sfname) != 0;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100500
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200501 return otherfile(ffname);
Colin Kennedy65e580b2024-03-26 18:29:30 +0100502}
503
504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
506 * command is given.
507 */
508 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100509do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100510 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511{
512 int save_msg_scroll;
513 int prev_msg_row;
514 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100515 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000516
517 if (improved)
518 exmode_active = EXMODE_VIM;
519 else
520 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100521 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100522 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100524 // When using ":global /pat/ visual" and then "Q" we return to continue
525 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if (global_busy)
527 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
529 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100530 ++RedrawingDisabled; // don't redisplay the window
531 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100533 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 ++hold_gui_events;
535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536
Bram Moolenaar32526b32019-01-19 17:43:09 +0100537 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 while (exmode_active)
539 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100540 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000541 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
542 {
543 exmode_active = FALSE;
544 break;
545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 msg_scroll = TRUE;
547 need_wait_return = FALSE;
548 ex_pressedreturn = FALSE;
549 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100550 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 prev_msg_row = msg_row;
552 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (improved)
554 {
555 cmdline_row = msg_row;
556 do_cmdline(NULL, getexline, NULL, 0);
557 }
558 else
559 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
560 lines_left = Rows - 1;
561
Bram Moolenaardf177f62005-02-22 08:39:57 +0000562 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100563 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000565 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000566 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000567 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000569 if (ex_pressedreturn)
570 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100571 // go up one line, to overwrite the ":<CR>" line, so the
572 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000573 msg_row = prev_msg_row;
574 if (prev_msg_row == Rows - 1)
575 msg_row--;
576 }
577 msg_col = 0;
578 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
579 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100582 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000583 {
584 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000585 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000586 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000587 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 }
590
591#ifdef FEAT_GUI
592 --hold_gui_events;
593#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100594 if (RedrawingDisabled > 0)
595 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100597 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 need_wait_return = FALSE;
599 msg_scroll = save_msg_scroll;
600}
601
602/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200603 * Print the executed command for when 'verbose' is set.
604 * When "lnum" is 0 only print the command.
605 */
606 static void
607msg_verbose_cmd(linenr_T lnum, char_u *cmd)
608{
609 ++no_wait_return;
610 verbose_enter_scroll();
611
612 if (lnum == 0)
613 smsg(_("Executing: %s"), cmd);
614 else
615 smsg(_("line %ld: %s"), (long)lnum, cmd);
616 if (msg_silent == 0)
617 msg_puts("\n"); // don't overwrite this
618
619 verbose_leave_scroll();
620 --no_wait_return;
621}
622
623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 * Execute a simple command line. Used for translated commands like "*".
625 */
626 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100627do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 return do_cmdline(cmd, NULL, NULL,
630 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
631}
632
633/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100634 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
635 * This allows for using a range without ":" in Vim9 script.
636 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200637 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100638do_cmd_argument(char_u *cmd)
639{
640 return do_cmdline(cmd, NULL, NULL,
641 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
642}
643
644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 * do_cmdline(): execute one Ex command line
646 *
647 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100648 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 * 2. Split up in parts separated with '|'.
650 *
651 * This function can be called recursively!
652 *
653 * flags:
654 * DOCMD_VERBOSE - The command will be included in the error message.
655 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100656 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 * DOCMD_KEYTYPED - Don't reset KeyTyped.
658 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
659 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
660 *
661 * return FAIL if cmdline could not be executed, OK otherwise
662 */
663 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100664do_cmdline(
665 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200666 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100667 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100668 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100670 char_u *next_cmdline; // next cmd to execute
671 char_u *cmdline_copy = NULL; // copy of cmd line
672 int used_getline = FALSE; // used "fgetline" to obtain command
673 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100675 int count = 0; // line number count
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100676 int did_inc_RedrawingDisabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 int retval = OK;
678#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100679 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100680 garray_T lines_ga; // keep lines for ":while"/":for"
681 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200682 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100683 char_u *fname = NULL; // function or script name
684 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
685 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
686 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200688 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200689 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100691 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200692 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000694 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000696 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100698# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699# define cmd_cookie cookie
700#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100701 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200702#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100703 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
704 // location for storing error messages to be converted to an exception.
705 // This ensures that the do_errthrow() call in do_one_cmd() does not
706 // combine the messages stored by an earlier invocation of do_one_cmd()
707 // with the command name of the later one. This would happen when
708 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 saved_msg_list = msg_list;
710 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711#endif
712
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100713 // It's possible to create an endless loop with ":execute", catch that
714 // here. The value of 200 allows nested function calls, ":source", etc.
715 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100716 if (call_depth >= 200
717#ifdef FEAT_EVAL
718 && call_depth >= p_mfd
719#endif
720 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000722 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100724 // When converting to an exception, we do not include the command name
725 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100726 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 msg_list = saved_msg_list;
728#endif
729 return FAIL;
730 }
731 ++call_depth;
732
733#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100734 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000736 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100738 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100740 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100741 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000742 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 ++ex_nesting_level;
744
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100745 // Get the function or script name and the address where the next breakpoint
746 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000747 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {
749 fname = func_name(real_cookie);
750 breakpoint = func_breakpoint(real_cookie);
751 dbg_tick = func_dbg_tick(real_cookie);
752 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100755 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 breakpoint = source_breakpoint(real_cookie);
757 dbg_tick = source_dbg_tick(real_cookie);
758 }
759
760 /*
761 * Initialize "force_abort" and "suppress_errthrow" at the top level.
762 */
763 if (!recursive)
764 {
765 force_abort = FALSE;
766 suppress_errthrow = FALSE;
767 }
768
769 /*
770 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000771 * exception handling (used when debugging). Otherwise clear it to avoid
772 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000774 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000775 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000776 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200777 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
779 initial_trylevel = trylevel;
780
781 /*
782 * "did_throw" will be set to TRUE when an exception is being thrown.
783 */
784 did_throw = FALSE;
785#endif
786 /*
787 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000788 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 * If force_abort is set, we cancel everything.
790 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100791#ifdef FEAT_EVAL
792 did_emsg_cumul += did_emsg;
793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 did_emsg = FALSE;
795
796 /*
797 * KeyTyped is only set when calling vgetc(). Reset it here when not
798 * calling vgetc() (sourced command lines).
799 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100800 if (!(flags & DOCMD_KEYTYPED)
801 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 KeyTyped = FALSE;
803
804 /*
805 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000806 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 * - for multiple commands on one line, separated with '|'
808 * - when repeating until there are no more lines (for ":source")
809 */
810 next_cmdline = cmdline;
811 do
812 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815#endif
816
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100817 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (next_cmdline == NULL
819#ifdef FEAT_EVAL
820 && !force_abort
821 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#endif
824 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100825 {
826#ifdef FEAT_EVAL
827 did_emsg_cumul += did_emsg;
828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
832 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000833 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100834 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 * 3. If a line is given: Make a copy, so we can mess with it.
836 */
837
838#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100839 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000840 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100842 // Each '|' separated command is stored separately in lines_ga, to
843 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100844 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100846 // Check if a function has returned or, unless it has an unclosed
847 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000851 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000852 func_line_end(real_cookie);
853# endif
854 if (func_has_ended(real_cookie))
855 {
856 retval = FAIL;
857 break;
858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000861 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100862 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000863 script_line_end();
864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100866 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 {
869 retval = FAIL;
870 break;
871 }
872
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (breakpoint != NULL && dbg_tick != NULL
875 && *dbg_tick != debug_tick)
876 {
877 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100878 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100879 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 *dbg_tick = debug_tick;
881 }
882
883 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100884 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100886 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100888 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100890 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100891 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100893 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100894 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 *dbg_tick = debug_tick;
896 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000898 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000899 {
900 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100901 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100902 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903 script_line_start();
904 }
905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#endif
908
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100909 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (next_cmdline == NULL)
911 {
912 /*
913 * Need to set msg_didout for the first line after an ":if",
914 * otherwise the ":if" will be overwritten.
915 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100916 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100918 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919#ifdef FEAT_EVAL
920 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
921#else
922 0
923#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100924 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
925 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100927 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100928 // returned for the end of a sourced file or executed function
929 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (KeyTyped && !(flags & DOCMD_REPEAT))
931 need_wait_return = FALSE;
932 retval = FAIL;
933 break;
934 }
935 used_getline = TRUE;
936
937 /*
938 * Keep the first typed line. Clear it when more lines are typed.
939 */
940 if (flags & DOCMD_KEEPLINE)
941 {
942 vim_free(repeat_cmdline);
943 if (count == 0)
944 repeat_cmdline = vim_strsave(next_cmdline);
945 else
946 repeat_cmdline = NULL;
947 }
948 }
949
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100950 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 else if (cmdline_copy == NULL)
952 {
953 next_cmdline = vim_strsave(next_cmdline);
954 if (next_cmdline == NULL)
955 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200956 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 retval = FAIL;
958 break;
959 }
960 }
961 cmdline_copy = next_cmdline;
962
963#ifdef FEAT_EVAL
964 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200965 * Inside a while/for loop, and when the command looks like a ":while"
966 * or ":for", the line is stored, because we may need it later when
967 * looping.
968 *
969 * When there is a '|' and another command, it is stored separately,
970 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000971 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200972 *
973 * Pass a different "fgetline" function to do_one_cmd() below,
974 * that it stores lines in or reads them from "lines_ga". Makes it
975 * possible to define a function inside a while/for loop and handles
976 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200978 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200980 cmd_getline = get_loop_line;
981 cmd_cookie = (void *)&cmd_loop_cookie;
982 cmd_loop_cookie.lines_gap = &lines_ga;
983 cmd_loop_cookie.current_line = current_line;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100984 cmd_loop_cookie.lc_getline = fgetline;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200985 cmd_loop_cookie.cookie = cookie;
986 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
987
988 // Save the current line when encountering it the first time.
989 if (current_line == lines_ga.ga_len
990 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
992 retval = FAIL;
993 break;
994 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200995 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200997 else
998 {
999 cmd_getline = fgetline;
1000 cmd_cookie = cookie;
1001 }
1002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 did_endif = FALSE;
1004#endif
1005
1006 if (count++ == 0)
1007 {
1008 /*
1009 * All output from the commands is put below each other, without
1010 * waiting for a return. Don't do this when executing commands
1011 * from a script or when being called recursive (e.g. for ":e
1012 * +command file").
1013 */
1014 if (!(flags & DOCMD_NOWAIT) && !recursive)
1015 {
1016 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001017 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001019 msg_scroll = TRUE; // put messages below each other
1020 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001022 did_inc_RedrawingDisabled = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024 }
1025
Bram Moolenaar823654b2020-05-29 23:03:09 +02001026 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001027 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
1029 /*
1030 * 2. Execute one '|' separated command.
1031 * do_one_cmd() will return NULL if there is no trailing '|'.
1032 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1033 */
1034 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001035 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036#ifdef FEAT_EVAL
1037 &cstack,
1038#endif
1039 cmd_getline, cmd_cookie);
1040 --recursive;
1041
1042#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001044 // Use "current_line" from "cmd_loop_cookie", it may have been
1045 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047#endif
1048
1049 if (next_cmdline == NULL)
1050 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001051 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001052
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 /*
1054 * If the command was typed, remember it for the ':' register.
1055 * Do this AFTER executing the command to make :@: work.
1056 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001057 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 && new_last_cmdline != NULL)
1059 {
1060 vim_free(last_cmdline);
1061 last_cmdline = new_last_cmdline;
1062 new_last_cmdline = NULL;
1063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 }
1065 else
1066 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001067 // need to copy the command after the '|' to cmdline_copy, for the
1068 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001069 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 next_cmdline = cmdline_copy;
1071 }
1072
1073
1074#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001075 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001077 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001079 {
1080 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
1086 ++current_line;
1087
1088 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001089 * An ":endwhile", ":endfor" and ":continue" is handled here.
1090 * If we were executing commands, jump back to the ":while" or
1091 * ":for".
1092 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001098 // Jump back to the matching ":while" or ":for". Be careful
1099 // not to use a cs_line[] from an entry that isn't a ":while"
1100 // or ":for": It would make "current_line" invalid and can
1101 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (!did_emsg && !got_int && !did_throw
1103 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 && (cstack.cs_flags[cstack.cs_idx]
1105 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 && cstack.cs_line[cstack.cs_idx] >= 0
1107 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1108 {
1109 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001110 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001112 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001114 // Check for the next breakpoint at or after the ":while"
1115 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001116 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 {
1118 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001119 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 fname,
1121 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1122 *dbg_tick = debug_tick;
1123 }
1124 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001125 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001127 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001129 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1130 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132 }
1133
1134 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001140 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 }
1142 }
1143
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001144 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001145 if (breakpoint != NULL && has_watchexpr())
1146 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001147 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001148 *dbg_tick = debug_tick;
1149 }
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 /*
1152 * When not inside any ":while" loop, clear remembered lines.
1153 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001154 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 {
1156 if (lines_ga.ga_len > 0)
1157 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001158 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1160 free_cmdlines(&lines_ga);
1161 }
1162 current_line = 0;
1163 }
1164
1165 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001166 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1167 * being restored at the ":endtry". Reset them here and set the
1168 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1169 * This includes the case where a missing ":endif", ":endwhile" or
1170 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001174 cstack.cs_lflags &= ~CSL_HAD_FINA;
1175 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1176 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 did_throw ? (void *)current_exception : NULL);
1178 did_emsg = got_int = did_throw = FALSE;
1179 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1180 }
1181
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001182 // Update global "trylevel" for recursive calls to do_cmdline() from
1183 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 trylevel = initial_trylevel + cstack.cs_trylevel;
1185
1186 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001187 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 * files) is aborted because of an error, an interrupt, or an uncaught
1189 * exception, cancel everything. If it is left normally, reset
1190 * force_abort to get the non-EH compatible abortion behavior for
1191 * the rest of the script.
1192 */
1193 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1194 force_abort = FALSE;
1195
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001196 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001198#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
1200 }
1201 /*
1202 * Continue executing command lines when:
1203 * - no CTRL-C typed, no aborting error, no exception thrown or try
1204 * conditionals need to be checked for executing finally clauses or
1205 * catching an interrupt exception
1206 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001207 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 * looping for ":source" command or function call.
1209 */
1210 while (!((got_int
1211#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001212 || (did_emsg && (force_abort || in_vim9script()))
1213 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214#endif
1215 )
1216#ifdef FEAT_EVAL
1217 && cstack.cs_trylevel == 0
1218#endif
1219 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001220 && !(did_emsg
1221#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001222 // Keep going when inside try/catch, so that the error can be
1223 // deal with, except when it is a syntax error, it may cause
1224 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001225 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1226#endif
1227 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001228 && (getline_equal(fgetline, cookie, getexmodeline)
1229 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 && (next_cmdline != NULL
1231#ifdef FEAT_EVAL
1232 || cstack.cs_idx >= 0
1233#endif
1234 || (flags & DOCMD_REPEAT)));
1235
1236 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001237 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238#ifdef FEAT_EVAL
1239 free_cmdlines(&lines_ga);
1240 ga_clear(&lines_ga);
1241
1242 if (cstack.cs_idx >= 0)
1243 {
1244 /*
1245 * If a sourced file or executed function ran to its end, report the
1246 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001247 * In Vim9 script do not give a second error, executing aborts after
1248 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001250 if (!got_int && !did_throw && !aborting()
1251 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001252 && ((getline_equal(fgetline, cookie, getsourceline)
1253 && !source_finished(fgetline, cookie))
1254 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 && !func_has_ended(real_cookie))))
1256 {
1257 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001258 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001260 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001261 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001262 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001264 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266
1267 /*
1268 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1269 * ":endtry" in a sourced file or executed function. If the try
1270 * conditional is in its finally clause, ignore anything pending.
1271 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001272 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 */
1274 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001275 {
1276 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1277
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001278 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001279 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001280 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1281 &cstack.cs_looplevel);
1282 }
1283 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 trylevel = initial_trylevel;
1285 }
1286
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001287 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1288 // lack was reported above and the error message is to be converted to an
1289 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001290 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 ? (char_u *)"endfunction" : (char_u *)NULL);
1292
1293 if (trylevel == 0)
1294 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001295 // Just in case did_throw got set but current_exception wasn't.
1296 if (current_exception == NULL)
1297 did_throw = FALSE;
1298
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 /*
1300 * When an exception is being thrown out of the outermost try
1301 * conditional, discard the uncaught exception, disable the conversion
1302 * of interrupts or errors to exceptions, and ensure that no more
1303 * commands are executed.
1304 */
1305 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001306 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307
1308 /*
1309 * On an interrupt or an aborting error not converted to an exception,
1310 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001311 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 * when force_abort is set and did_emsg unset in case of an interrupt
1313 * from a finally clause after an error.
1314 */
1315 else if (got_int || (did_emsg && force_abort))
1316 suppress_errthrow = TRUE;
1317 }
1318
1319 /*
1320 * The current cstack will be freed when do_cmdline() returns. An uncaught
1321 * exception will have to be rethrown in the previous cstack. If a function
1322 * has just returned or a script file was just finished and the previous
1323 * cstack belongs to the same function or, respectively, script file, it
1324 * will have to be checked for finally clauses to be executed due to the
1325 * ":return" or ":finish". This is done in do_one_cmd().
1326 */
1327 if (did_throw)
1328 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 && ex_nesting_level > func_level(real_cookie) + 1))
1333 {
1334 if (!did_throw)
1335 check_cstack = TRUE;
1336 }
1337 else
1338 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001339 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001340 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 --ex_nesting_level;
1342 /*
1343 * Go to debug mode when returning from a function in which we are
1344 * single-stepping.
1345 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001346 if ((getline_equal(fgetline, cookie, getsourceline)
1347 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001349 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 ? (char_u *)_("End of sourced file")
1351 : (char_u *)_("End of function"));
1352 }
1353
1354 /*
1355 * Restore the exception environment (done after returning from the
1356 * debugger).
1357 */
1358 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001359 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
1361 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001362
1363 // Cleanup if "cs_emsg_silent_list" remains.
1364 if (cstack.cs_emsg_silent_list != NULL)
1365 {
1366 eslist_T *elem, *temp;
1367
1368 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1369 {
1370 temp = elem->next;
1371 vim_free(elem);
1372 }
1373 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001374#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
1376 /*
1377 * If there was too much output to fit on the command line, ask the user to
1378 * hit return before redrawing the screen. With the ":global" command we do
1379 * this only once after the command is finished.
1380 */
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001381 if (did_inc_RedrawingDisabled)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001383 if (RedrawingDisabled > 0)
1384 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 --no_wait_return;
1386 msg_scroll = FALSE;
1387
1388 /*
1389 * When just finished an ":if"-":else" which was typed, no need to
1390 * wait for hit-return. Also for an error situation.
1391 */
1392 if (retval == FAIL
1393#ifdef FEAT_EVAL
1394 || (did_endif && KeyTyped && !did_emsg)
1395#endif
1396 )
1397 {
1398 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001399 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401 else if (need_wait_return)
1402 {
1403 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001404 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 * here should not overwrite the command that may be shown before
1406 * doing that.
1407 */
1408 msg_didout |= msg_didout_before_start;
1409 wait_return(FALSE);
1410 }
1411 }
1412
Bram Moolenaar2a942252012-11-28 23:03:07 +01001413#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001414 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001415#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 /*
1417 * Reset if_level, in case a sourced script file contains more ":if" than
1418 * ":endif" (could be ":if x | foo | endif").
1419 */
1420 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001421#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 --call_depth;
1424 return retval;
1425}
1426
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001427#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001428/*
1429 * Handle when "did_throw" is set after executing commands.
1430 */
1431 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001432handle_did_throw(void)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001433{
1434 char *p = NULL;
1435 msglist_T *messages = NULL;
ichizok7e5fe382023-04-15 13:17:50 +01001436 ESTACK_CHECK_DECLARATION;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001437
1438 /*
1439 * If the uncaught exception is a user exception, report it as an
1440 * error. If it is an error exception, display the saved error
1441 * message now. For an interrupt exception, do nothing; the
1442 * interrupt message is given elsewhere.
1443 */
1444 switch (current_exception->type)
1445 {
1446 case ET_USER:
1447 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001448 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001449 current_exception->value);
1450 p = (char *)vim_strsave(IObuff);
1451 break;
1452 case ET_ERROR:
1453 messages = current_exception->messages;
1454 current_exception->messages = NULL;
1455 break;
1456 case ET_INTERRUPT:
1457 break;
1458 }
1459
1460 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1461 current_exception->throw_lnum);
ichizok7e5fe382023-04-15 13:17:50 +01001462 ESTACK_CHECK_SETUP;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001463 current_exception->throw_name = NULL;
1464
1465 discard_current_exception(); // uses IObuff if 'verbose'
LemonBoy749ba0f2024-07-04 19:23:16 +02001466
1467 // If "silent!" is active the uncaught exception is not fatal.
1468 if (emsg_silent == 0)
1469 {
1470 suppress_errthrow = TRUE;
1471 force_abort = TRUE;
1472 }
Bram Moolenaar620c9592021-07-31 21:32:31 +02001473
1474 if (messages != NULL)
1475 {
1476 do
1477 {
1478 msglist_T *next = messages->next;
1479 int save_compiling = estack_compiling;
1480
1481 estack_compiling = messages->msg_compiling;
1482 emsg(messages->msg);
1483 vim_free(messages->msg);
1484 vim_free(messages->sfile);
1485 vim_free(messages);
1486 messages = next;
1487 estack_compiling = save_compiling;
1488 }
1489 while (messages != NULL);
1490 }
1491 else if (p != NULL)
1492 {
1493 emsg(p);
1494 vim_free(p);
1495 }
1496 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01001497 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001498 estack_pop();
1499}
1500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001502 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 */
1504 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001505get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001507 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 wcmd_T *wp;
1509 char_u *line;
1510
1511 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1512 {
1513 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001514 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001516 // First time inside the ":while"/":for": get line normally.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001517 if (cp->lc_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001518 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001520 line = cp->lc_getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001521 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 ++cp->current_line;
1523
1524 return line;
1525 }
1526
1527 KeyTyped = FALSE;
1528 ++cp->current_line;
1529 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001530 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 return vim_strsave(wp->line);
1532}
1533
1534/*
1535 * Store a line in "gap" so that a ":while" loop can execute it again.
1536 */
1537 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001538store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
1540 if (ga_grow(gap, 1) == FAIL)
1541 return FAIL;
1542 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001543 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 return OK;
1546}
1547
1548/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001549 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 */
1551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001552free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
1554 while (gap->ga_len > 0)
1555 {
1556 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1557 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 }
1559}
1560#endif
1561
1562/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001563 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1564 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001567getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001568 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001569 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001570 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001573 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001574 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001576 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1577 // function that's originally used to obtain the lines. This may be
1578 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001579 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001580 cp = (struct loop_cookie *)cookie;
1581 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001583 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 cp = cp->cookie;
1585 }
1586 return gp == func;
1587#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001588 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589#endif
1590}
1591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001593 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 * getline function. Otherwise return "cookie".
1595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001597getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001598 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001599 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600{
Bram Moolenaar13505972019-01-24 15:04:48 +01001601#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001602 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001603 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001605 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1606 // cookie that's originally used to obtain the lines. This may be nested
1607 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001608 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001609 cp = (struct loop_cookie *)cookie;
1610 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001612 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 cp = cp->cookie;
1614 }
1615 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001616#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001619}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001621#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001622/*
1623 * Get the next line source line without advancing.
1624 */
1625 char_u *
1626getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001627 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001628 void *cookie) // argument for fgetline()
1629{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001630 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001631 struct loop_cookie *cp;
1632 wcmd_T *wp;
1633
1634 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1635 // cookie that's originally used to obtain the lines. This may be nested
1636 // several levels.
1637 gp = fgetline;
1638 cp = (struct loop_cookie *)cookie;
1639 while (gp == get_loop_line)
1640 {
1641 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1642 {
1643 // executing lines a second time, use the stored copy
1644 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1645 return wp->line;
1646 }
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001647 gp = cp->lc_getline;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001648 cp = cp->cookie;
1649 }
1650 if (gp == getsourceline)
1651 return source_nextline(cp);
1652 return NULL;
1653}
1654#endif
1655
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001656
1657/*
1658 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1659 * ":bwipeout", etc.
1660 * Returns the buffer number.
1661 */
1662 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001663compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001664{
1665 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001666 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001667 int count = offset;
1668
1669 buf = firstbuf;
1670 while (buf->b_next != NULL && buf->b_fnum < lnum)
1671 buf = buf->b_next;
1672 while (count != 0)
1673 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001674 count += (offset < 0) ? 1 : -1;
1675 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1676 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001678 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001679 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001680 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001681 while (buf->b_ml.ml_mfp == NULL)
1682 {
1683 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1684 if (nextbuf == NULL)
1685 break;
1686 buf = nextbuf;
1687 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001688 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001689 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001690 if (addr_type == ADDR_LOADED_BUFFERS)
1691 while (buf->b_ml.ml_mfp == NULL)
1692 {
1693 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1694 if (nextbuf == NULL)
1695 break;
1696 buf = nextbuf;
1697 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001698 return buf->b_fnum;
1699}
1700
Bram Moolenaarb7316892019-05-01 18:08:42 +02001701/*
1702 * Return the window number of "win".
1703 * When "win" is NULL return the number of windows.
1704 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001705 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001706current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001707{
1708 win_T *wp;
1709 int nr = 0;
1710
Bram Moolenaar29323592016-07-24 22:04:11 +02001711 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001712 {
1713 ++nr;
1714 if (wp == win)
1715 break;
1716 }
1717 return nr;
1718}
1719
1720 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001721current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001722{
1723 tabpage_T *tp;
1724 int nr = 0;
1725
Bram Moolenaar29323592016-07-24 22:04:11 +02001726 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001727 {
1728 ++nr;
1729 if (tp == tab)
1730 break;
1731 }
1732 return nr;
1733}
1734
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001735 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001736comment_start(char_u *p, int starts_with_colon UNUSED)
1737{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001738 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001739 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001740 return *p == '"';
1741}
1742
Bram Moolenaarf240e182014-11-27 18:33:02 +01001743# define CURRENT_WIN_NR current_win_nr(curwin)
1744# define LAST_WIN_NR current_win_nr(NULL)
1745# define CURRENT_TAB_NR current_tab_nr(curtab)
1746# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001747
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748/*
1749 * Execute one Ex command.
1750 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001751 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1752 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 *
1754 * 1. skip comment lines and leading space
1755 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001756 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001757 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001758 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001759 * 6. parse arguments
1760 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001762 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 *
1764 * This function may be called recursively!
1765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001767do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001768 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001769 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001771 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001773 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001774 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001776 char_u *p;
1777 linenr_T lnum;
1778 long n;
1779 char *errormsg = NULL; // error message
1780 char_u *after_modifier = NULL;
1781 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001782 cmdmod_T save_cmdmod;
1783 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001784 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001785 int ni; // set when Not Implemented
1786 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001787 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001788 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001789#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001790 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001791#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001792 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001793 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
Bram Moolenaara80faa82020-04-12 19:37:17 +02001795 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 ea.line1 = 1;
1797 ea.line2 = 1;
1798#ifdef FEAT_EVAL
1799 ++ex_nesting_level;
1800#endif
1801
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001802 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (quitmore
1804#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001805 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001806 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001807#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001808 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001809 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 --quitmore;
1811
1812 /*
1813 * Reset browse, confirm, etc.. They are restored when returning, for
1814 * recursive calls.
1815 */
1816 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001818 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001819 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1820 goto doend;
1821
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001822/*
1823 * 1. Skip comment lines and leading white space and colons.
1824 * 2. Handle command modifiers.
1825 */
1826 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001828 ea.cmdlinep = cmdlinep;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001829 ea.ea_getline = fgetline;
Bram Moolenaareffed932018-08-14 13:38:17 +02001830 ea.cookie = cookie;
1831#ifdef FEAT_EVAL
1832 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001833 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001835 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001836 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001837 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001838 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
1840#ifdef FEAT_EVAL
1841 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1842 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1843#else
1844 ea.skip = (if_level > 0);
1845#endif
1846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001848 * 3. Skip over the range to find the command. Let "p" point to after it.
1849 *
1850 * We need the command to know what kind of range it uses.
1851 */
1852 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001853
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001854 // In Vim9 script a colon is required before the range. This may also be
1855 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001856 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001857 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001858 {
1859 may_have_range = FALSE;
1860 for (p = ea.cmd; p >= *cmdlinep; --p)
1861 {
1862 if (*p == ':')
1863 may_have_range = TRUE;
1864 if (p < ea.cmd && !VIM_ISWHITE(*p))
1865 break;
1866 }
1867 }
1868 else
1869 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001870 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001871 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001873#ifdef FEAT_EVAL
1874 // Handle ":export" - it functions almost like a command modifier.
1875 // ":export var Name: type"
1876 // ":export def Name(..."
1877 // etc.
1878 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1879 is_export = TRUE;
1880#endif
1881
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001882 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001883 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001884 if (ea.cmd == cmd + 1 && *cmd == '$')
1885 // should be "$VAR = val"
1886 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001887#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001888 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001889#else
1890 p = find_ex_command(&ea, NULL, NULL, NULL);
1891#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001892 if (ea.cmdidx == CMD_SIZE)
1893 {
1894 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1895
1896 // If a ':' before the range is missing, give a clearer error
1897 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001898 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001899 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001900 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001901 goto doend;
1902 }
1903 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001904 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001907
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001908#ifdef FEAT_EVAL
1909# ifdef FEAT_PROFILE
1910 // Count this line for profiling if skip is TRUE.
1911 if (do_profiling == PROF_YES
1912 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1913 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1914 {
1915 int skip = did_emsg || got_int || did_throw;
1916
1917 if (ea.cmdidx == CMD_catch)
1918 skip = !skip && !(cstack->cs_idx >= 0
1919 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1920 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1921 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1922 skip = skip || !(cstack->cs_idx >= 0
1923 && !(cstack->cs_flags[cstack->cs_idx]
1924 & (CSF_ACTIVE | CSF_TRUE)));
1925 else if (ea.cmdidx == CMD_finally)
1926 skip = FALSE;
1927 else if (ea.cmdidx != CMD_endif
1928 && ea.cmdidx != CMD_endfor
1929 && ea.cmdidx != CMD_endtry
1930 && ea.cmdidx != CMD_endwhile)
1931 skip = ea.skip;
1932
1933 if (!skip)
1934 {
1935 if (getline_equal(fgetline, cookie, get_func_line))
1936 func_line_exec(getline_cookie(fgetline, cookie));
1937 else if (getline_equal(fgetline, cookie, getsourceline))
1938 script_line_exec();
1939 }
1940 }
1941# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001942#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001943
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001944 ea.cmd = cmd;
1945
1946#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001947 // May go to debug mode. If this happens and the ">quit" debug command is
1948 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001949 dbg_check_breakpoint(&ea);
1950 if (!ea.skip && got_int)
1951 {
1952 ea.skip = TRUE;
1953 (void)do_intthrow(cstack);
1954 }
1955#endif
1956
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001957/*
1958 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 *
1960 * where 'addr' is:
1961 *
1962 * % (entire file)
1963 * $ [+-NUM]
1964 * 'x [+-NUM] (where x denotes a currently defined mark)
1965 * . [+-NUM]
1966 * [+-NUM]..
1967 * NUM
1968 *
1969 * The ea.cmd pointer is updated to point to the first character following the
1970 * range spec. If an initial address is found, but no second, the upper bound
1971 * is equal to the lower.
1972 */
1973
Bram Moolenaar25190db2019-05-04 15:05:28 +02001974 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001975 if (!IS_USER_CMDIDX(ea.cmdidx))
1976 {
1977 if (ea.cmdidx != CMD_SIZE)
1978 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1979 else
1980 ea.addr_type = ADDR_LINES;
1981
Bram Moolenaar25190db2019-05-04 15:05:28 +02001982 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001983 if (ea.cmdidx == CMD_wincmd && p != NULL)
1984 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001985#ifdef FEAT_QUICKFIX
1986 // :.cc in quickfix window uses line number
1987 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1988 ea.addr_type = ADDR_OTHER;
1989#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001990 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001991
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001992 if (!may_have_range)
1993 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001994 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1995 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001998 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 */
2000
2001 /*
2002 * Skip ':' and any white space
2003 */
2004 ea.cmd = skipwhite(ea.cmd);
2005 while (*ea.cmd == ':')
2006 ea.cmd = skipwhite(ea.cmd + 1);
2007
2008 /*
2009 * If we got a line, but no command, then go to the line.
2010 * If we find a '|' or '\n' we set ea.nextcmd.
2011 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002012 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
2013 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
2015 /*
2016 * strange vi behaviour:
2017 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01002018 * ":3|..." prints line 3 (not in Vim9 script)
2019 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002021 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002023 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 goto doend;
2025 }
2026
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002027 // If this looks like an undefined user command and there are CmdUndefined
2028 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002029 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2030 && ASCII_ISUPPER(*ea.cmd)
2031 && has_cmdundefined())
2032 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002033 int ret;
2034
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002035 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002036 while (ASCII_ISALNUM(*p))
2037 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002038 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002039 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2040 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002041 // If the autocommands did something and didn't cause an error, try
2042 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002043 p = (ret
2044#ifdef FEAT_EVAL
2045 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002046#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002048 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002049
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (p == NULL)
2051 {
2052 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002053 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 goto doend;
2055 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002056 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002057 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2058 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 {
2060 errormsg = uc_fun_cmd();
2061 goto doend;
2062 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 if (ea.cmdidx == CMD_SIZE)
2065 {
2066 if (!ea.skip)
2067 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002068 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002071 // If the modifier was parsed OK the error must be in the
2072 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002073 if (after_modifier != NULL)
2074 append_command(after_modifier);
2075 else
2076 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002077 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002078 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002079 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002080 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082 goto doend;
2083 }
2084
Bram Moolenaar958636c2014-10-21 20:01:58 +02002085 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2086 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002087#ifdef HAVE_EX_SCRIPT_NI
2088 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2089#endif
2090 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091
2092#ifndef FEAT_EVAL
2093 /*
2094 * When the expression evaluation is disabled, recognize the ":if" and
2095 * ":endif" commands and ignore everything in between it.
2096 */
2097 if (ea.cmdidx == CMD_if)
2098 ++if_level;
2099 if (if_level)
2100 {
2101 if (ea.cmdidx == CMD_endif)
2102 --if_level;
2103 goto doend;
2104 }
2105
2106#endif
2107
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002109 if (*p == '!' && ea.cmdidx != CMD_substitute
2110 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
2112 ++p;
2113 ea.forceit = TRUE;
2114 }
2115 else
2116 ea.forceit = FALSE;
2117
2118/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002119 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002121 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002122 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
2124 if (!ea.skip)
2125 {
2126#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002127 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002129 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002130 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 goto doend;
2132 }
2133#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002134 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002135 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002136 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002137 goto doend;
2138 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002139 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002141 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002142 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 goto doend;
2144 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002145
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002146 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002148 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2149 {
2150 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002151 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002152 goto doend;
2153 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002154 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2155 {
2156 // Command not allowed when text is locked
2157 errormsg = _(get_text_locked_msg());
2158 goto doend;
2159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002161
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002162 // Disallow editing another buffer when "curbuf_lock" is set.
2163 // Do allow ":checktime" (it is postponed).
2164 // Do allow ":edit" (check for an argument later).
2165 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002166 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002167 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002168 && ea.cmdidx != CMD_edit
2169 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002170 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002171 && curbuf_locked())
2172 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002174 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002176 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 goto doend;
2178 }
2179 }
2180
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002181 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002183 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 goto doend;
2185 }
2186
2187 /*
2188 * Don't complain about the range if it is not used
2189 * (could happen if line_count is accidentally set to 0).
2190 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002191 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
2193 /*
2194 * If the range is backwards, ask for confirmation and, if given, swap
2195 * ea.line1 & ea.line2 so it's forwards again.
2196 * When global command is busy, don't ask, will fail below.
2197 */
2198 if (!global_busy && ea.line1 > ea.line2)
2199 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002200 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002202 if (sourcing || exmode_active)
2203 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002204 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002205 goto doend;
2206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 if (ask_yesno((char_u *)
2208 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002209 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 }
2211 lnum = ea.line1;
2212 ea.line1 = ea.line2;
2213 ea.line2 = lnum;
2214 }
2215 if ((errormsg = invalid_range(&ea)) != NULL)
2216 goto doend;
2217 }
2218
Bram Moolenaarb7316892019-05-01 18:08:42 +02002219 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2220 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 ea.line2 = 1;
2222
2223 correct_range(&ea);
2224
2225#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002226 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002227 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002229 // Put the first line at the start of a closed fold, put the last line
2230 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 (void)hasFolding(ea.line1, &ea.line1, NULL);
2232 (void)hasFolding(ea.line2, NULL, &ea.line2);
2233 }
2234#endif
2235
2236#ifdef FEAT_QUICKFIX
2237 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002238 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 * option here, so things like % get expanded.
2240 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002241 p = replace_makeprg(&ea, p, cmdlinep);
2242 if (p == NULL)
2243 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244#endif
2245
2246 /*
2247 * Skip to start of argument.
2248 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2249 */
2250 if (ea.cmdidx == CMD_bang)
2251 ea.arg = p;
2252 else
2253 ea.arg = skipwhite(p);
2254
Bram Moolenaar379fb762018-08-30 15:58:28 +02002255 // ":file" cannot be run with an argument when "curbuf_lock" is set
2256 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2257 goto doend;
2258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 /*
2260 * Check for "++opt=val" argument.
2261 * Must be first, allow ":w ++enc=utf8 !cmd"
2262 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002263 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2265 if (getargopt(&ea) == FAIL && !ni)
2266 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002267 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 goto doend;
2269 }
2270
2271 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2272 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002273 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002275 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002277 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 goto doend;
2279 }
2280 ea.arg = skipwhite(ea.arg + 1);
2281 ea.append = TRUE;
2282 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002283 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 ++ea.arg;
2286 ea.usefilter = TRUE;
2287 }
2288 }
2289
2290 if (ea.cmdidx == CMD_read)
2291 {
2292 if (ea.forceit)
2293 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002294 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 ea.forceit = FALSE;
2296 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002297 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
2299 ++ea.arg;
2300 ea.usefilter = TRUE;
2301 }
2302 }
2303
2304 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2305 {
2306 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002307 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
2309 ++ea.arg;
2310 ++ea.amount;
2311 }
2312 ea.arg = skipwhite(ea.arg);
2313 }
2314
2315 /*
2316 * Check for "+command" argument, before checking for next command.
2317 * Don't do this for ":read !cmd" and ":write !cmd".
2318 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002319 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2321
2322 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002323 * For commands that do not use '|' inside their argument: Check for '|' to
2324 * separate commands and '"' or '#' to start comments.
2325 *
2326 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002327 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002328 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002329 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002331 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2332 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002333 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002334 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002335 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002336 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002337 || ea.cmdidx == CMD_global
2338 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002339 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002340#ifdef FEAT_EVAL
2341 || inside_block(&ea)
2342#endif
2343 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 for (p = ea.arg; *p; ++p)
2346 {
Mohamed Akramf3daa452024-07-06 17:12:09 +02002347 // Remove one backslash before a newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002349 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002350 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
2352 ea.nextcmd = p + 1;
2353 *p = NUL;
2354 break;
2355 }
2356 }
2357 }
2358
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002359 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002360 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002362 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002363 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002365 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002366 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002367 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002369#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002370 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002371 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002373 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002374 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
2376#endif
64-bitmane08f10a2025-03-18 22:14:34 +01002377 if (valid_yank_reg(*ea.arg, (!IS_USER_CMDIDX(ea.cmdidx)
2378 && ea.cmdidx != CMD_put && ea.cmdidx != CMD_iput)))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002379 {
2380 ea.regname = *ea.arg++;
2381#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002382 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002383 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2384 {
kuuote08d7b1c2021-10-04 22:17:36 +01002385 if (!ea.skip)
2386 {
2387 set_expr_line(vim_strsave(ea.arg), &ea);
2388 did_set_expr_line = TRUE;
2389 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002390 ea.arg += STRLEN(ea.arg);
2391 }
2392#endif
2393 ea.arg = skipwhite(ea.arg);
2394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396
2397 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002398 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 * count, it's a buffer name.
2400 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002401 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002402 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002403 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002405 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002407 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002409 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 goto doend;
2411 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002412 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
2414 ea.line2 = n;
2415 if (ea.addr_count == 0)
2416 ea.addr_count = 1;
2417 }
2418 else
2419 {
2420 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002421 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002422 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002423 else
2424 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 ++ea.addr_count;
2426 /*
2427 * Be vi compatible: no error message for out of range.
2428 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002429 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 ea.line2 = curbuf->b_ml.ml_line_count;
2431 }
2432 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002433
2434 /*
2435 * Check for flags: 'l', 'p' and '#'.
2436 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002437 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002438 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002439 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2440 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002442 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002443 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 goto doend;
2445 }
2446
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002447 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002449 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 goto doend;
2451 }
2452
2453#ifdef FEAT_EVAL
2454 /*
2455 * Skip the command when it's not going to be executed.
2456 * The commands like :if, :endif, etc. always need to be executed.
2457 * Also make an exception for commands that handle a trailing command
2458 * themselves.
2459 */
2460 if (ea.skip)
2461 {
2462 switch (ea.cmdidx)
2463 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002464 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_while:
2466 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002467 case CMD_for:
2468 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 case CMD_if:
2470 case CMD_elseif:
2471 case CMD_else:
2472 case CMD_endif:
2473 case CMD_try:
2474 case CMD_catch:
2475 case CMD_finally:
2476 case CMD_endtry:
2477 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002478 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 break;
2480
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002481 // Commands that handle '|' themselves. Check: A command should
2482 // either have the EX_TRLBAR flag, appear in this list or appear in
2483 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 case CMD_aboveleft:
2485 case CMD_and:
2486 case CMD_belowright:
2487 case CMD_botright:
2488 case CMD_browse:
2489 case CMD_call:
2490 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002491 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 case CMD_delfunction:
2493 case CMD_djump:
2494 case CMD_dlist:
2495 case CMD_dsearch:
2496 case CMD_dsplit:
2497 case CMD_echo:
2498 case CMD_echoerr:
2499 case CMD_echomsg:
2500 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002501 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002503 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002504 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 case CMD_help:
2506 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002507 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 case CMD_ijump:
2509 case CMD_ilist:
2510 case CMD_isearch:
2511 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002512 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 case CMD_keepjumps:
2514 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002515 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 case CMD_leftabove:
2517 case CMD_let:
2518 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002519 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002520 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002522 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002523 case CMD_noautocmd:
2524 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 case CMD_perl:
2526 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002527 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002528 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002529 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 case CMD_return:
2531 case CMD_rightbelow:
2532 case CMD_ruby:
2533 case CMD_silent:
2534 case CMD_smagic:
2535 case CMD_snomagic:
2536 case CMD_substitute:
2537 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002538 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 case CMD_tcl:
2540 case CMD_throw:
2541 case CMD_tilde:
2542 case CMD_topleft:
2543 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002544 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002545 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 case CMD_verbose:
2547 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002548 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 break;
2550
2551 default: goto doend;
2552 }
2553 }
2554#endif
2555
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002556 if ((ea.argt & EX_XFILE)
2557 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2558 goto doend;
2559
2560#ifdef FEAT_EVAL
2561 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002563 emsg(_(e_invalid_command_after_export));
2564 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 /*
2569 * Accept buffer name. Cannot be used at the same time with a buffer
2570 * number. Don't do this for a user command.
2571 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002572 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002573 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
2575 /*
2576 * :bdelete, :bwipeout and :bunload take several arguments, separated
2577 * by spaces: find next space (skipping over escaped characters).
2578 * The others take one argument: ignore trailing spaces.
2579 */
2580 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2581 || ea.cmdidx == CMD_bunload)
2582 p = skiptowhite_esc(ea.arg);
2583 else
2584 {
2585 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002586 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 --p;
2588 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002589 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002590 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002591 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 goto doend;
2593 ea.addr_count = 1;
2594 ea.arg = skipwhite(p);
2595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002597 // The :try command saves the emsg_silent flag, reset it here when
2598 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002599 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002600 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002601 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002602 if (emsg_silent < 0)
2603 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002604 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002605 }
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002608 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
Bram Moolenaar958636c2014-10-21 20:01:58 +02002611 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
2613 /*
2614 * Execute a user-defined command.
2615 */
2616 do_ucmd(&ea);
2617 }
2618 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
2620 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002621 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2624 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002625 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
2627
2628#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002629 // A command will reset "is_export" when exporting an item. If it is still
LemonBoy90c27b22023-08-27 19:28:15 +02002630 // set something went wrong or the command was never executed.
2631 if (!ea.skip && is_export)
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002632 {
2633 if (errormsg == NULL)
2634 errormsg = _(e_export_with_invalid_argument);
2635 is_export = FALSE;
2636 }
2637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002639 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002640 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002641 && current_sctx.sc_sid > 0
2642 && ea.cmdidx != CMD_endif
2643 && (cstack->cs_idx < 0
2644 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002645 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 /*
2648 * If the command just executed called do_cmdline(), any throw or ":return"
2649 * or ":finish" encountered there must also check the cstack of the still
2650 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2651 * exception, or reanimate a returned function or finished script file and
2652 * return or finish it again.
2653 */
2654 if (need_rethrow)
2655 do_throw(cstack);
2656 else if (check_cstack)
2657 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002658 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002660 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 && current_func_returned())
2662 do_return(&ea, TRUE, FALSE, NULL);
2663 }
2664 need_rethrow = check_cstack = FALSE;
2665#endif
2666
2667doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002668 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002669 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002671 curwin->w_cursor.col = 0;
2672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
2674 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2675 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002676 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002678 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
2680 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002681 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002683 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
2685 emsg(errormsg);
2686 }
2687#ifdef FEAT_EVAL
2688 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002689 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2690 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002691
2692 if (did_set_expr_line)
2693 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002694 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695#endif
2696
Bram Moolenaare1004402020-10-24 20:49:43 +02002697 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002699 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002700 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002702 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 ea.nextcmd = NULL;
2704
2705#ifdef FEAT_EVAL
2706 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002707 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708#endif
2709
2710 return ea.nextcmd;
2711}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002713static char ex_error_buf[MSG_BUF_LEN];
2714
2715/*
2716 * Return an error message with argument included.
2717 * Uses a static buffer, only the last error will be kept.
2718 * "msg" will be translated, caller should use N_().
2719 */
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02002720 char *
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002721ex_errmsg(char *msg, char_u *arg)
2722{
2723 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2724 return ex_error_buf;
2725}
2726
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727/*
zeertzjq7d664bf2024-07-14 10:22:54 +02002728 * The "+" string used in place of an empty command in Ex mode.
2729 * This string is used in pointer comparison.
2730 */
2731static char exmode_plus[] = "+";
2732
2733/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002734 * Handle a range without a command.
2735 * Returns an error message on failure.
2736 */
2737 char *
2738ex_range_without_command(exarg_T *eap)
2739{
2740 char *errormsg = NULL;
2741
zeertzjq7d664bf2024-07-14 10:22:54 +02002742 if ((*eap->cmd == '|' ||
2743 (exmode_active && eap->cmd != (char_u *)exmode_plus + 1))
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002744#ifdef FEAT_EVAL
2745 && !in_vim9script()
2746#endif
2747 )
2748 {
2749 eap->cmdidx = CMD_print;
2750 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2751 if ((errormsg = invalid_range(eap)) == NULL)
2752 {
2753 correct_range(eap);
2754 ex_print(eap);
2755 }
2756 }
2757 else if (eap->addr_count != 0)
2758 {
2759 if (eap->line2 > curbuf->b_ml.ml_line_count)
2760 {
2761 // With '-' in 'cpoptions' a line number past the file is an
2762 // error, otherwise put it at the end of the file.
2763 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2764 eap->line2 = -1;
2765 else
2766 eap->line2 = curbuf->b_ml.ml_line_count;
2767 }
2768
2769 if (eap->line2 < 0)
2770 errormsg = _(e_invalid_range);
2771 else
2772 {
2773 if (eap->line2 == 0)
2774 curwin->w_cursor.lnum = 1;
2775 else
2776 curwin->w_cursor.lnum = eap->line2;
2777 beginline(BL_SOL | BL_FIX);
2778 }
2779 }
2780 return errormsg;
2781}
2782
2783/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002784 * Check for an Ex command with optional tail.
2785 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002786 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002787 */
2788 static int
2789checkforcmd_opt(
2790 char_u **pp, // start of command
2791 char *cmd, // name of command
2792 int len, // required length
2793 int noparen)
2794{
2795 int i;
2796
2797 for (i = 0; cmd[i] != NUL; ++i)
2798 if (((char_u *)cmd)[i] != (*pp)[i])
2799 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002800 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002801 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002802 {
2803 *pp = skipwhite(*pp + i);
2804 return TRUE;
2805 }
2806 return FALSE;
2807}
2808
2809/*
2810 * Check for an Ex command with optional tail.
2811 * If there is a match advance "pp" to the argument and return TRUE.
2812 */
2813 int
2814checkforcmd(
2815 char_u **pp, // start of command
2816 char *cmd, // name of command
2817 int len) // required length
2818{
2819 return checkforcmd_opt(pp, cmd, len, FALSE);
2820}
2821
2822/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002823 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002824 * If there is a match advance "pp" to the argument and return TRUE.
2825 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002826 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002827checkforcmd_noparen(
2828 char_u **pp, // start of command
2829 char *cmd, // name of command
2830 int len) // required length
2831{
2832 return checkforcmd_opt(pp, cmd, len, TRUE);
2833}
2834
2835/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 * Parse and skip over command modifiers:
2837 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002838 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002840 * When "skip_only" is TRUE the global variables are not changed, except for
2841 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002842 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2843 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002844 * Call apply_cmdmod() to get the side effects of the modifiers:
2845 * - Increment "sandbox" for ":sandbox"
2846 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002847 * - set msg_silent for ":silent"
2848 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002849 * Return FAIL when the command is not to be executed.
2850 * May set "errormsg" to an error message.
2851 */
2852 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002853parse_command_modifiers(
2854 exarg_T *eap,
2855 char **errormsg,
2856 cmdmod_T *cmod,
2857 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002858{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002859 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002860 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002861 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002862 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002863 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002864 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002865
Bram Moolenaare1004402020-10-24 20:49:43 +02002866 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002867 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002868
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002869 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2870 {
2871 // The automatically inserted Visual area range is skipped, so that
2872 // typing ":cmdmod cmd" in Visual mode works without having to move the
zeertzjqd9be94c2024-07-14 10:20:20 +02002873 // range to after the modifiers. The command will be "'<,'>cmdmod cmd",
2874 // parse "cmdmod cmd" and then put back "'<,'>" before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002875 eap->cmd += 5;
2876 cmd_start = eap->cmd;
2877 has_visual_range = TRUE;
2878 }
2879
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002880 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002881 for (;;)
2882 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002883 char_u *p;
2884
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002886 {
2887 if (*eap->cmd == ':')
2888 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002890 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002891
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002892 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002893 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002894 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2895 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002896 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2897 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002898 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002899 if (!skip_only)
2900 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002901 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 }
2903
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002904 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002905 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002906 {
2907 // a comment ends at a NL
zeertzjqf7b86092024-09-05 17:26:30 +02002908 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2909 if (eap->nextcmd != NULL)
2910 ++eap->nextcmd;
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002911 if (vim9script)
2912 {
2913 if (has_cmdmod(cmod, FALSE))
2914 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002915#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002916 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2917 && eap->cmd[2] != '{')
2918 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002919#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002920 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002922 }
zeertzjqf7b86092024-09-05 17:26:30 +02002923 if (*eap->cmd == '\n')
zeertzjq2432b4a2024-09-03 22:58:30 +02002924 {
2925 eap->nextcmd = eap->cmd + 1;
2926 return FAIL;
2927 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002928 if (*eap->cmd == NUL)
2929 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002930 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002931 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002932 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002933 if (vim9script && has_cmdmod(cmod, FALSE))
2934 *errormsg = _(e_command_modifier_without_command);
2935 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002936 return FAIL;
2937 }
2938
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002939 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002940
2941 // In Vim9 script a variable can shadow a command modifier:
2942 // verbose = 123
2943 // verbose += 123
2944 // silent! verbose = func()
2945 // verbose.member = 2
2946 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002947 // But not:
2948 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002949 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002950 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002951 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002952
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002953 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002954 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002955 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002956 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2957 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002958 break;
2959 }
2960
Bram Moolenaareffed932018-08-14 13:38:17 +02002961 switch (*p)
2962 {
John Marriotted908f72024-04-18 22:46:56 +02002963 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002964 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002965 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002966 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002967 continue;
2968
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002969 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002970 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002971 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002972 continue;
2973 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002974 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002975 {
2976#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002977 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002978#endif
2979 continue;
2980 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002981 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002982 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002983 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002984 continue;
2985
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002986 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 break;
2988#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002989 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002990#endif
2991 continue;
2992
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002993 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002995 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002996 continue;
2997 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002998 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003000 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003001 continue;
3002 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003003 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003005 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003006 continue;
3007 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003008 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003010 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003011 continue;
3012
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003013 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003014 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003015 char_u *reg_pat;
3016 char_u *nulp = NULL;
3017 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003018
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003019 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003020 || *p == NUL
3021 || (ends_excmd(*p)
3022#ifdef FEAT_EVAL
3023 // in ":filter #pat# cmd" # does not
3024 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003025 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003026#endif
3027 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003028 break;
3029 if (*p == '!')
3030 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003031 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003032 p = skipwhite(p + 1);
3033 if (*p == NUL || ends_excmd(*p))
3034 break;
3035 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003036#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003037 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003038 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003039 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003040#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003041 if (skip_only)
3042 p = skip_vimgrep_pat(p, NULL, NULL);
3043 else
3044 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003045 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3046 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003047 if (p == NULL || *p == NUL)
3048 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003049 if (!skip_only)
3050 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003051 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003052 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003053 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003054 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003055 // restore the character overwritten by NUL
3056 if (nulp != NULL)
3057 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003058 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003059 eap->cmd = p;
3060 continue;
3061 }
3062
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003063 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3064 {
3065 cmod->cmod_split |= WSP_HOR;
3066 continue;
3067 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003068 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003069 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003070 || *p == NUL || ends_excmd(*p))
3071 break;
3072 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003073 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003074 continue;
3075
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003076 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003077 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003078 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003079 continue;
3080 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003081 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3082 {
3083 if (ends_excmd2(p, eap->cmd))
3084 {
3085 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003086 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003087 return FAIL;
3088 }
3089 cmod->cmod_flags |= CMOD_LEGACY;
3090 continue;
3091 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003092
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003093 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003094 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003095 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003096 continue;
3097
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003098 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003099 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003100 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003101 continue;
3102 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003103 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003104 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003105 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003106 continue;
3107
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003108 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003109 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003110 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003111 continue;
3112
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003113 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003114 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003115 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003116 continue;
3117 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003118 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003119 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003120 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003121 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3122 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003123 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003124 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003125 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003126 }
3127 continue;
3128
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003129 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003130 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003131 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003132 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003133 long tabnr = get_address(eap, &eap->cmd,
3134 ADDR_TABS, eap->skip,
3135 skip_only, FALSE, 1);
3136 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003137 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003138 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003139 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003140 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3141 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003142 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003143 return FAIL;
3144 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003145 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003146 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003147 }
3148 eap->cmd = p;
3149 continue;
3150 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003151 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003152 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003153 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003154 continue;
3155
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003156 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003157 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003158 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003159 continue;
3160
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003161 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003162 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003163 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003164 continue;
3165 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003166 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003167 {
3168 if (ends_excmd2(p, eap->cmd))
3169 {
3170 *errormsg =
3171 _(e_vim9cmd_must_be_followed_by_command);
3172 return FAIL;
3173 }
3174 cmod->cmod_flags |= CMOD_VIM9CMD;
3175 continue;
3176 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003177 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003178 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003179 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003180 {
zeertzjqcd749632022-06-14 13:30:35 +01003181 // zero means not set, one is verbose == 0, etc.
3182 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003183 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003184 else
zeertzjqcd749632022-06-14 13:30:35 +01003185 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003186 eap->cmd = p;
3187 continue;
3188 }
3189 break;
3190 }
3191
Bram Moolenaar1501b632022-03-27 16:56:21 +01003192 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003193 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003194 if (eap->cmd > cmd_start)
3195 {
3196 // Move the '<,'> range to after the modifiers and insert a colon.
3197 // Since the modifiers have been parsed put the colon on top of the
3198 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3199 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003200 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003201 {
3202 size_t len = STRLEN(cmd_start);
3203
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003204 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003205 // "'<,'>mods" -> "mods *+
3206 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003207 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003208 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003209 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003210 }
3211 else
3212 {
3213 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3214 eap->cmd -= 5;
3215 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3216 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003217 }
3218 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003219 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003220 // Special case: change empty command to "+".
3221 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003222 eap->cmd = (char_u *)"'<,'>+";
3223 else
3224 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003225 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003226 else if (use_plus_cmd)
zeertzjq7d664bf2024-07-14 10:22:54 +02003227 eap->cmd = (char_u *)exmode_plus;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003228
Bram Moolenaareffed932018-08-14 13:38:17 +02003229 return OK;
3230}
3231
3232/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003233 * Return TRUE if "cmod" has anything set.
3234 */
3235 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003236has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003237{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003238 return (cmod->cmod_flags != 0 && (!ignore_silent
3239 || (cmod->cmod_flags
3240 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003241 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003242 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003243 || cmod->cmod_tab != 0
3244 || cmod->cmod_filter_regmatch.regprog != NULL;
3245}
3246
Bram Moolenaar8991be22022-02-14 21:51:46 +00003247#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003248/*
3249 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3250 */
3251 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003252cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003253{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003254 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003255 {
3256 emsg(_(e_misplaced_command_modifier));
3257 return TRUE;
3258 }
3259 return FALSE;
3260}
Dominique Pelle748b3082022-01-08 12:41:16 +00003261#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003262
3263/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003264 * Apply the command modifiers. Saves current state in "cmdmod", call
3265 * undo_cmdmod() later.
3266 */
3267 void
3268apply_cmdmod(cmdmod_T *cmod)
3269{
3270#ifdef HAVE_SANDBOX
3271 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3272 {
3273 ++sandbox;
3274 cmod->cmod_did_sandbox = TRUE;
3275 }
3276#endif
zeertzjqcd749632022-06-14 13:30:35 +01003277 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003278 {
3279 if (cmod->cmod_verbose_save == 0)
3280 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003281 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003282 }
3283
3284 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3285 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003286 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003287 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003288 cmod->cmod_save_msg_scroll = msg_scroll;
3289 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003290 if (cmod->cmod_flags & CMOD_SILENT)
3291 ++msg_silent;
3292 if (cmod->cmod_flags & CMOD_UNSILENT)
3293 msg_silent = 0;
3294
3295 if (cmod->cmod_flags & CMOD_ERRSILENT)
3296 {
3297 ++emsg_silent;
3298 ++cmod->cmod_did_esilent;
3299 }
3300
Bram Moolenaare1004402020-10-24 20:49:43 +02003301 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003302 {
3303 // Set 'eventignore' to "all".
3304 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003305 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003306 set_string_option_direct((char_u *)"ei", -1,
3307 (char_u *)"all", OPT_FREE, SID_NONE);
3308 }
3309}
3310
3311/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003312 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003313 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003314 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003315undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003316{
Bram Moolenaare1004402020-10-24 20:49:43 +02003317 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003318 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003319 p_verbose = cmod->cmod_verbose_save - 1;
3320 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003321 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003322
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003323#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003324 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003325 {
3326 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003327 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003328 }
3329#endif
3330
Bram Moolenaare1004402020-10-24 20:49:43 +02003331 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003332 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003333 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003334 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3335 OPT_FREE, SID_NONE);
3336 free_string_option(cmod->cmod_save_ei);
3337 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003338 }
3339
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003340 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003341
Bram Moolenaare1004402020-10-24 20:49:43 +02003342 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003343 {
3344 // messages could be enabled for a serious error, need to check if the
3345 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003346 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3347 msg_silent = cmod->cmod_save_msg_silent - 1;
3348 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003349 if (emsg_silent < 0)
3350 emsg_silent = 0;
3351 // Restore msg_scroll, it's set by file I/O commands, even when no
3352 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003353 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003354
3355 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3356 // somewhere in the line. Put it back in the first column.
3357 if (redirecting())
3358 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003359
Bram Moolenaare1004402020-10-24 20:49:43 +02003360 cmod->cmod_save_msg_silent = 0;
3361 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003362 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003363}
3364
3365/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003366 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003367 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003368 * Return FAIL and set "errormsg" or return OK.
3369 */
3370 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003371parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003372{
3373 int address_count = 1;
3374 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003375 int need_check_cursor = FALSE;
3376 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003377
3378 // Repeat for all ',' or ';' separated addresses.
3379 for (;;)
3380 {
3381 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003382 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003383 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003384 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003385 eap->addr_count == 0, address_count++);
3386 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003387 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003388 if (lnum == MAXLNUM)
3389 {
3390 if (*eap->cmd == '%') // '%' - all lines
3391 {
3392 ++eap->cmd;
3393 switch (eap->addr_type)
3394 {
3395 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003396 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003397 eap->line1 = 1;
3398 eap->line2 = curbuf->b_ml.ml_line_count;
3399 break;
3400 case ADDR_LOADED_BUFFERS:
3401 {
3402 buf_T *buf = firstbuf;
3403
3404 while (buf->b_next != NULL
3405 && buf->b_ml.ml_mfp == NULL)
3406 buf = buf->b_next;
3407 eap->line1 = buf->b_fnum;
3408 buf = lastbuf;
3409 while (buf->b_prev != NULL
3410 && buf->b_ml.ml_mfp == NULL)
3411 buf = buf->b_prev;
3412 eap->line2 = buf->b_fnum;
3413 break;
3414 }
3415 case ADDR_BUFFERS:
3416 eap->line1 = firstbuf->b_fnum;
3417 eap->line2 = lastbuf->b_fnum;
3418 break;
3419 case ADDR_WINDOWS:
3420 case ADDR_TABS:
3421 if (IS_USER_CMDIDX(eap->cmdidx))
3422 {
3423 eap->line1 = 1;
3424 eap->line2 = eap->addr_type == ADDR_WINDOWS
3425 ? LAST_WIN_NR : LAST_TAB_NR;
3426 }
3427 else
3428 {
3429 // there is no Vim command which uses '%' and
3430 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003431 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003432 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003433 }
3434 break;
3435 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003436 case ADDR_UNSIGNED:
3437 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003438 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003439 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003440 case ADDR_ARGUMENTS:
3441 if (ARGCOUNT == 0)
3442 eap->line1 = eap->line2 = 0;
3443 else
3444 {
3445 eap->line1 = 1;
3446 eap->line2 = ARGCOUNT;
3447 }
3448 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003449 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003450#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003451 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003452 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003453 if (eap->line2 == 0)
3454 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003455#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003456 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003457 case ADDR_NONE:
3458 // Will give an error later if a range is found.
3459 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003460 }
3461 ++eap->addr_count;
3462 }
3463 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3464 {
3465 pos_T *fp;
3466
3467 // '*' - visual area
3468 if (eap->addr_type != ADDR_LINES)
3469 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003470 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003471 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003472 }
3473
3474 ++eap->cmd;
3475 if (!eap->skip)
3476 {
3477 fp = getmark('<', FALSE);
3478 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003479 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003480 eap->line1 = fp->lnum;
3481 fp = getmark('>', FALSE);
3482 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003483 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003484 eap->line2 = fp->lnum;
3485 ++eap->addr_count;
3486 }
3487 }
3488 }
3489 else
3490 eap->line2 = lnum;
3491 eap->addr_count++;
3492
3493 if (*eap->cmd == ';')
3494 {
3495 if (!eap->skip)
3496 {
3497 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003498
Bram Moolenaar0e717042020-04-27 19:29:01 +02003499 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003500 // accept zero as address, so 0;/PATTERN/ works correctly
3501 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003502 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003503 if (eap->line2 > 0)
3504 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003505 else
3506 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003507 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003508 }
3509 }
3510 else if (*eap->cmd != ',')
3511 break;
3512 ++eap->cmd;
3513 }
3514
3515 // One address given: set start and end lines.
3516 if (eap->addr_count == 1)
3517 {
3518 eap->line1 = eap->line2;
3519 // ... but only implicit: really no address given
3520 if (lnum == MAXLNUM)
3521 eap->addr_count = 0;
3522 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003523 ret = OK;
3524
3525theend:
3526 if (need_check_cursor)
3527 check_cursor();
3528 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003529}
3530
3531/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003532 * Append "cmd" to the error message in IObuff.
3533 * Takes care of limiting the length and handling 0xa0, which would be
3534 * invisible otherwise.
3535 */
3536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003537append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003538{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003539 size_t len = STRLEN(IObuff);
3540 char_u *s = cmd;
3541 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003542
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003543 if (len > IOSIZE - 100)
3544 {
3545 // Not enough space, truncate and put in "...".
3546 d = IObuff + IOSIZE - 100;
3547 d -= mb_head_off(IObuff, d);
3548 STRCPY(d, "...");
3549 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003550 STRCAT(IObuff, ": ");
3551 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003552 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003553 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003554 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003555 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003556 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003557 STRCPY(d, "<a0>");
3558 d += 4;
3559 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003560 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3561 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003562 else
3563 MB_COPY_CHAR(s, d);
3564 }
3565 *d = NUL;
3566}
3567
Dominique Pelle748b3082022-01-08 12:41:16 +00003568#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003569/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003570 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3571 * the name. Otherwise just return "start".
3572 */
3573 char_u *
3574skip_option_env_lead(char_u *start)
3575{
3576 char_u *name = start;
3577
3578 if (*start == '&')
3579 {
3580 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3581 name += 3;
3582 else
3583 name += 1;
3584 }
3585 else if (*start == '$')
3586 name += 1;
3587 return name;
3588}
Dominique Pelle748b3082022-01-08 12:41:16 +00003589#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003590
3591/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003592 * Return TRUE and set "*idx" if "p" points to a one letter command.
3593 * If not in Vim9 script:
Doug Kearnsea842022024-09-29 17:17:41 +02003594 * - The 'k' command can directly be followed by any character
3595 * but :keepa[lt] is another command, as are :keepj[umps],
3596 * :kee[pmarks] and :keepp[atterns].
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003597 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3598 * but :sre[wind] is another command, as are :scr[iptnames],
3599 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3600 */
3601 static int
3602one_letter_cmd(char_u *p, cmdidx_T *idx)
3603{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003604 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003605 return FALSE;
Doug Kearnsea842022024-09-29 17:17:41 +02003606 if (p[0] == 'k'
3607 && (p[1] != 'e' || (p[1] == 'e' && p[2] != 'e')))
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003608 {
3609 *idx = CMD_k;
3610 return TRUE;
3611 }
3612 if (p[0] == 's'
3613 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3614 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3615 || p[1] == 'g'
3616 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3617 || p[1] == 'I'
3618 || (p[1] == 'r' && p[2] != 'e')))
3619 {
3620 *idx = CMD_substitute;
3621 return TRUE;
3622 }
3623 return FALSE;
3624}
3625
Dominique Pellee764d1b2023-03-12 21:20:59 +00003626#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003627/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003628 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3629 * call.
3630 */
3631 int
3632number_method(char_u *cmd)
3633{
3634 char_u *p = skipdigits(cmd);
3635
3636 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3637}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003638#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003639
3640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003642 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003643 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003644 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 *
3646 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3647 * assignment without "let". Sets eap->cmdidx to the command while returning
3648 * "eap->cmd".
3649 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 * Returns NULL for an ambiguous user command.
3651 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 char_u *
3653find_ex_command(
3654 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003655 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003656 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003657 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 int len;
3660 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003661 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003662#ifndef FEAT_EVAL
3663 int vim9 = FALSE;
3664#else
3665 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 /*
3668 * Recognize a Vim9 script function/method call and assignment:
3669 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3670 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003671 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003672 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003674 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003676 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003677 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3678 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003680 int oplen;
3681 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003682 char_u *swp;
3683
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003684 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003685 || (eap->cmd[0] == '$'
3686 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003687 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003688 && (valid_yank_reg(eap->cmd[1], FALSE)
3689 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003690 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003691 if (*eap->cmd == '&')
3692 {
3693 p = eap->cmd + 1;
3694 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3695 p += 2;
3696 p = to_name_end(p, FALSE);
3697 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003698 else if (*eap->cmd == '$')
3699 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003700 else
3701 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003702 if (ends_excmd(*skipwhite(p)))
3703 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003704 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3705 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003706 eap->cmdidx = CMD_eval;
3707 return eap->cmd;
3708 }
3709 // "&option" can be followed by "->" or "=", check below
3710 }
3711
3712 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003714 if (
3715 // "(..." is an expression.
3716 // "funcname(" is always a function call.
3717 *p == '('
3718 || (p == eap->cmd
3719 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003720 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003721 *eap->cmd == '{'
3722 // "'string'->func()" is an expression.
3723 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003724 // '"string"->func()' is an expression.
3725 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003726 // '$"string"->func()' is an expression.
3727 // "$'string'->func()" is an expression.
3728 || (eap->cmd[0] == '$'
3729 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3730 // '0z1234->func()' is an expression.
3731 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003732 // "g:varname" is an expression.
3733 || eap->cmd[1] == ':'
3734 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003735 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003736 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003737 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003738 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3739 {
3740 // "{" by itself is the start of a block.
3741 eap->cmdidx = CMD_block;
3742 return eap->cmd + 1;
3743 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003744 eap->cmdidx = CMD_eval;
3745 return eap->cmd;
3746 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003747
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003748 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003749 // "varname[]" is an expression.
3750 *p == '['
3751 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003752 || (*p == '.'
3753 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3754 // g:[key] is an expression
3755 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003756 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003757 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003758
3759 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003760 // Skip over the whole thing, it can be:
3761 // name.member = val
3762 // name[a : b] = val
3763 // name[idx] = val
3764 // name[idx].member = val
3765 // etc.
3766 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003767 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003768 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003769 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003770 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003771 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003772 || (after[0] == '.' && after[1] == '.'
3773 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003774 eap->cmdidx = CMD_var;
3775 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003776 --emsg_silent;
3777 return eap->cmd;
3778 }
3779
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003780 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3781 // an assignment.
3782 // If there is no line break inside the "[...]" then "p" is
3783 // advanced to after the "]" by to_name_const_end(): check if a "="
3784 // follows.
3785 // If "[...]" has a line break "p" still points at the "[" and it
3786 // can't be an assignment.
3787 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003788 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003789 char_u *eq;
3790
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003791 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003792 if (p == eap->cmd && *p == '[')
3793 {
3794 int count = 0;
3795 int semicolon = FALSE;
3796
3797 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3798 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003799 eq = p;
3800 if (eq != NULL)
3801 {
3802 eq = skipwhite(eq);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003803 if (vim_strchr((char_u *)"+-*/%.", *eq) != NULL)
3804 {
3805 if (eq[0] == '.' && eq[1] == '.')
3806 ++eq;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003807 ++eq;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003808 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003809 }
3810 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003811 {
3812 eap->cmdidx = CMD_eval;
3813 return eap->cmd;
3814 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003815 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003816 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003817 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003818 return eap->cmd;
3819 }
3820 }
3821
3822 // Recognize an assignment if we recognize the variable name:
3823 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003824 // "@r = expr"
3825 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003826 // "var = expr" where "var" is a variable name or we are skipping
3827 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003828 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003829 oplen = assignment_len(skipwhite(p), &heredoc);
3830 if (oplen > 0)
3831 {
3832 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3833 || *eap->cmd == '&'
3834 || *eap->cmd == '$'
3835 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003836 || (eap->skip && IS_WHITE_OR_NUL(
3837 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003838 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003839 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003840 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003841 return eap->cmd;
3842 }
3843 }
3844
Bram Moolenaar9510d222022-09-11 15:14:05 +01003845 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003846 // "w:varname: number = 123".
3847 if (eap->cmd[1] == ':' && *p == ':')
3848 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003849 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003850 return eap->cmd;
3851 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003852 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003853
Bram Moolenaar31d99482022-05-26 22:24:43 +01003854 // 1234->func() is a method call
3855 if (number_method(eap->cmd))
3856 {
3857 eap->cmdidx = CMD_eval;
3858 return eap->cmd;
3859 }
3860
Bram Moolenaar7b829262021-10-13 15:04:34 +01003861 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3862 // an expression. A global/substitute/list command needs to use a
3863 // longer name.
3864 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3865 {
3866 eap->cmdidx = CMD_eval;
3867 return eap->cmd;
3868 }
3869
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003870 // If it is an ID it might be a variable with an operator on the next
3871 // line, if the variable exists it can't be an Ex command.
3872 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003873 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003874 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3875 {
3876 eap->cmdidx = CMD_eval;
3877 return eap->cmd;
3878 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003879
3880 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003881 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3882 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003883 {
3884 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3885 return eap->cmd + 2;
3886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 }
3888#endif
3889
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 /*
3891 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 */
3893 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003894 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 ++p;
Doug Kearnsea842022024-09-29 17:17:41 +02003897 if (full != NULL)
3898 *full = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
3900 else
3901 {
3902 while (ASCII_ISALPHA(*p))
3903 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003904 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003905 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003907 while (ASCII_ISALNUM(*p))
3908 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 }
3910 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3911 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003912 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 ++p;
3914 while (ASCII_ISALPHA(*p))
3915 ++p;
3916 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003917
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003918 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003919 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 ++p;
3921 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003922 // The "d" command can directly be followed by 'l' or 'p' flag, when
3923 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003924 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003925 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003926 // Check for ":dl", ":dell", etc. to ":deletel": that's
3927 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003928 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003929 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003930 break;
3931 if (i == len - 1)
3932 {
3933 --len;
3934 if (p[-1] == 'l')
3935 eap->flags |= EXFLAG_LIST;
3936 else
3937 eap->flags |= EXFLAG_PRINT;
3938 }
3939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003941 if (ASCII_ISLOWER(eap->cmd[0]))
3942 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003943 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003944 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003945
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003946 if (command_count != (int)CMD_SIZE)
3947 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003948 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003949 getout(1);
3950 }
3951
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003952 // Use a precomputed index for fast look-up in cmdnames[]
3953 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003954 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3955 if (ASCII_ISLOWER(c2))
3956 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3957 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003958 else if (ASCII_ISUPPER(eap->cmd[0]))
3959 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003961 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
3963 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3964 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3965 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3966 (size_t)len) == 0)
3967 {
3968#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003969 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 *full = TRUE;
3971#endif
3972 break;
3973 }
3974
Bram Moolenaar204852a2022-03-05 12:56:44 +00003975 // :Print and :mode are not supported in Vim9 script.
3976 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003977 if (vim9 && eap->cmdidx != CMD_SIZE)
3978 {
3979 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3980 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003981 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003982 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003983 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003984 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003985 eap->cmdidx = CMD_SIZE;
3986 }
3987 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003988
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003989 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003990 // 'cpoptions'.
3991 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3992 p = eap->cmd;
3993
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003994 // Look for a user defined command as a last resort. Let ":Print" be
3995 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003996 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3997 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003999 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 while (ASCII_ISALNUM(*p))
4001 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004002 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01004004 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 eap->cmdidx = CMD_SIZE;
4006 }
4007
Bram Moolenaar204852a2022-03-05 12:56:44 +00004008 // ":fina" means ":finally" in legacy script, for backwards compatibility.
4009 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02004010 eap->cmdidx = CMD_finally;
4011
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004012#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004013 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004014 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02004015 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004016 && (eap->cmdidx < 0 ||
4017 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004018 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004019 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4020
4021 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004022 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004023 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004024 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004025#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004026
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 return p;
4028}
4029
4030#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004031typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004032{
4033 char *name;
4034 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004035 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004036} cmdmod_info_T;
4037
4038static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004039 {"aboveleft", 3, FALSE},
4040 {"belowright", 3, FALSE},
4041 {"botright", 2, FALSE},
4042 {"browse", 3, FALSE},
4043 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004044 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004045 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004046 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004047 {"keepalt", 5, FALSE},
4048 {"keepjumps", 5, FALSE},
4049 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004050 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004051 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004052 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004053 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004054 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004055 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004056 {"rightbelow", 6, FALSE},
4057 {"sandbox", 3, FALSE},
4058 {"silent", 3, FALSE},
4059 {"tab", 3, TRUE},
4060 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004061 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004062 {"verbose", 4, TRUE},
4063 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004064 {"vim9cmd", 4, FALSE}
4065}; // cmdmod_info_tab
4066
Bram Moolenaared53fb92007-11-24 20:50:24 +00004067/*
4068 * Return length of a command modifier (including optional count).
4069 * Return zero when it's not a modifier.
4070 */
4071 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004072modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004073{
Christian Brabandt70a11a62024-07-26 19:13:55 +02004074 int i, j;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004075 char_u *p = cmd;
4076
4077 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004078 p = skipwhite(skipdigits(cmd + 1));
Christian Brabandt70a11a62024-07-26 19:13:55 +02004079 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004080 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004081 for (j = 0; p[j] != NUL; ++j)
4082 if (p[j] != cmdmod_info_tab[i].name[j])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004083 break;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004084 if (!ASCII_ISALPHA(p[j]) && j >= cmdmod_info_tab[i].minlen
4085 && (p == cmd || cmdmod_info_tab[i].has_count))
4086 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004087 }
4088 return 0;
4089}
4090
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091/*
4092 * Return > 0 if an Ex command "name" exists.
4093 * Return 2 if there is an exact match.
4094 * Return 3 if there is an ambiguous match.
4095 */
4096 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004097cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098{
4099 exarg_T ea;
4100 int full = FALSE;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004101 int i;
4102 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004103 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104
Christian Brabandt70a11a62024-07-26 19:13:55 +02004105 // Check command modifiers.
4106 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004108 for (j = 0; name[j] != NUL; ++j)
4109 if (name[j] != cmdmod_info_tab[i].name[j])
4110 break;
4111 if (name[j] == NUL && j >= cmdmod_info_tab[i].minlen)
4112 return (cmdmod_info_tab[i].name[j] == NUL ? 2 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004115 // Check built-in commands and user defined commands.
4116 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004117 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004119 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004121 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004123 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4124 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004125 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004126 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4128}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004129
4130/*
4131 * "fullcommand" function
4132 */
4133 void
4134f_fullcommand(typval_T *argvars, typval_T *rettv)
4135{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004136 exarg_T ea;
4137 char_u *name;
4138 char_u *p;
4139 int vim9script = in_vim9script();
4140 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004141
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004142 rettv->v_type = VAR_STRING;
4143 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004144
Bram Moolenaaraa534142022-09-15 21:46:02 +01004145 if (in_vim9script()
4146 && (check_for_string_arg(argvars, 0) == FAIL
4147 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004148 return;
4149
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004150 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004151 if (name == NULL)
4152 return;
4153
Bram Moolenaaraa534142022-09-15 21:46:02 +01004154 if (argvars[1].v_type != VAR_UNKNOWN)
4155 {
4156 vim9script = tv_get_bool(&argvars[1]);
4157 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4158 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4159 }
4160
zeertzjqc024ed92022-01-04 16:22:52 +00004161 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004162 name++;
4163 name = skip_range(name, TRUE, NULL);
4164
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004165 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4166 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004167 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004168 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004169 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004170 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004171 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004172 goto theend;
4173
4174 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004175 {
4176 int res;
4177
4178 ++emsg_silent;
4179 res = not_in_vim9(&ea);
4180 --emsg_silent;
4181
4182 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004183 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004184 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004185
4186 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004187 ? get_user_command_name(ea.useridx, ea.cmdidx)
4188 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004189theend:
4190 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004191}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192#endif
4193
Bram Moolenaard0190392019-08-23 21:17:35 +02004194 cmdidx_T
4195excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
Bram Moolenaard0190392019-08-23 21:17:35 +02004197 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004199 if (!one_letter_cmd(cmd, &idx))
4200 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4201 idx = (cmdidx_T)((int)idx + 1))
4202 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4203 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
Bram Moolenaard0190392019-08-23 21:17:35 +02004205 return idx;
4206}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
Bram Moolenaard0190392019-08-23 21:17:35 +02004208 long
4209excmd_get_argt(cmdidx_T idx)
4210{
4211 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212}
4213
4214/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004215 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 *
4217 * Backslashed delimiters after / or ? will be skipped, and commands will
4218 * not be expanded between /'s and ?'s or after "'".
4219 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004220 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 * Returns the "cmd" pointer advanced to beyond the range.
4222 */
4223 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004224skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004225 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004226 int skip_star, // skip "*" used for Visual range
4227 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004229 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004230 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004232 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004234 if (*cmd == '\\')
4235 {
4236 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4237 ++cmd;
4238 else
4239 break;
4240 }
4241 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004243 char_u *p = cmd;
4244
4245 // a quote is only valid at the start or after a separator
4246 while (p > cmd_start)
4247 {
4248 --p;
4249 if (!VIM_ISWHITE(*p))
4250 break;
4251 }
4252 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4253 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 if (*++cmd == NUL && ctx != NULL)
4255 *ctx = EXPAND_NOTHING;
4256 }
4257 else if (*cmd == '/' || *cmd == '?')
4258 {
4259 delim = *cmd++;
4260 while (*cmd != NUL && *cmd != delim)
4261 if (*cmd++ == '\\' && *cmd != NUL)
4262 ++cmd;
4263 if (*cmd == NUL && ctx != NULL)
4264 *ctx = EXPAND_NOTHING;
4265 }
4266 if (*cmd != NUL)
4267 ++cmd;
4268 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004269
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004270 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004271 while (*cmd == ':')
4272 cmd = skipwhite(cmd + 1);
4273
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004274 // Skip "*" used for Visual range.
4275 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4276 cmd = skipwhite(cmd + 1);
4277
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 return cmd;
4279}
4280
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004281 static void
4282addr_error(cmd_addr_T addr_type)
4283{
4284 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004285 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004286 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004287 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004288}
4289
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004291 * Return the default address for an address type.
4292 */
4293 static linenr_T
4294default_address(exarg_T *eap)
4295{
4296 linenr_T lnum = 0;
4297
4298 switch (eap->addr_type)
4299 {
4300 case ADDR_LINES:
4301 case ADDR_OTHER:
4302 // Default is the cursor line number. Avoid using an invalid
4303 // line number though.
4304 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4305 lnum = curbuf->b_ml.ml_line_count;
4306 else
4307 lnum = curwin->w_cursor.lnum;
4308 break;
4309 case ADDR_WINDOWS:
4310 lnum = CURRENT_WIN_NR;
4311 break;
4312 case ADDR_ARGUMENTS:
4313 lnum = curwin->w_arg_idx + 1;
4314 if (lnum > ARGCOUNT)
4315 lnum = ARGCOUNT;
4316 break;
4317 case ADDR_LOADED_BUFFERS:
4318 case ADDR_BUFFERS:
4319 lnum = curbuf->b_fnum;
4320 break;
4321 case ADDR_TABS:
4322 lnum = CURRENT_TAB_NR;
4323 break;
4324 case ADDR_TABS_RELATIVE:
4325 case ADDR_UNSIGNED:
4326 lnum = 1;
4327 break;
4328 case ADDR_QUICKFIX:
4329#ifdef FEAT_QUICKFIX
4330 lnum = qf_get_cur_idx(eap);
4331#endif
4332 break;
4333 case ADDR_QUICKFIX_VALID:
4334#ifdef FEAT_QUICKFIX
4335 lnum = qf_get_cur_valid_idx(eap);
4336#endif
4337 break;
4338 case ADDR_NONE:
4339 // Will give an error later if a range is found.
4340 break;
4341 }
4342 return lnum;
4343}
4344
4345/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004346 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 *
4348 * Set ptr to the next character after the part that was interpreted.
4349 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004350 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 *
4352 * Return MAXLNUM when no Ex address was found.
4353 */
4354 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004355get_address(
4356 exarg_T *eap UNUSED,
4357 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004358 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004359 int skip, // only skip the address, don't use it
4360 int silent, // no errors or side effects
4361 int to_other_file, // flag: may jump to other file
4362 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363{
4364 int c;
4365 int i;
4366 long n;
4367 char_u *cmd;
4368 pos_T pos;
4369 pos_T *fp;
4370 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004371 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
4373 cmd = skipwhite(*ptr);
4374 lnum = MAXLNUM;
4375 do
4376 {
4377 switch (*cmd)
4378 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004379 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004380 ++cmd;
4381 switch (addr_type)
4382 {
4383 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004384 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 lnum = curwin->w_cursor.lnum;
4386 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004387 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004388 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004389 break;
4390 case ADDR_ARGUMENTS:
4391 lnum = curwin->w_arg_idx + 1;
4392 break;
4393 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004394 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 lnum = curbuf->b_fnum;
4396 break;
4397 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004398 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004399 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004400 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004401 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004402 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004403 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004404 cmd = NULL;
4405 goto error;
4406 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004407 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004408#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004409 lnum = qf_get_cur_idx(eap);
4410#endif
4411 break;
4412 case ADDR_QUICKFIX_VALID:
4413#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004414 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004415#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004416 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 }
4418 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004420 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004421 ++cmd;
4422 switch (addr_type)
4423 {
4424 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004425 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 lnum = curbuf->b_ml.ml_line_count;
4427 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004428 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004429 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004430 break;
4431 case ADDR_ARGUMENTS:
4432 lnum = ARGCOUNT;
4433 break;
4434 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004435 buf = lastbuf;
4436 while (buf->b_ml.ml_mfp == NULL)
4437 {
4438 if (buf->b_prev == NULL)
4439 break;
4440 buf = buf->b_prev;
4441 }
4442 lnum = buf->b_fnum;
4443 break;
4444 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004445 lnum = lastbuf->b_fnum;
4446 break;
4447 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004448 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004450 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004451 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004452 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004453 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004454 cmd = NULL;
4455 goto error;
4456 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004457 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004458#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004459 lnum = qf_get_size(eap);
4460 if (lnum == 0)
4461 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004462#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004463 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004464 case ADDR_QUICKFIX_VALID:
4465#ifdef FEAT_QUICKFIX
4466 lnum = qf_get_valid_size(eap);
4467 if (lnum == 0)
4468 lnum = 1;
4469#endif
4470 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004471 }
4472 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004474 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004475 if (*++cmd == NUL)
4476 {
4477 cmd = NULL;
4478 goto error;
4479 }
4480 if (addr_type != ADDR_LINES)
4481 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004482 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004483 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004484 goto error;
4485 }
4486 if (skip)
4487 ++cmd;
4488 else
4489 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004490 // Only accept a mark in another file when it is
4491 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4493 ++cmd;
4494 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004495 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004496 lnum = curwin->w_cursor.lnum;
4497 else
4498 {
4499 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 {
4501 cmd = NULL;
4502 goto error;
4503 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004504 lnum = fp->lnum;
4505 }
4506 }
4507 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508
4509 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004510 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004511 c = *cmd++;
4512 if (addr_type != ADDR_LINES)
4513 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004514 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004515 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004516 goto error;
4517 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004518 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004519 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004520 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004521 if (*cmd == c)
4522 ++cmd;
4523 }
4524 else
4525 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004526 int flags;
4527
4528 pos = curwin->w_cursor; // save curwin->w_cursor
4529
4530 // When '/' or '?' follows another address, start from
4531 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004532 if (lnum > 0 && lnum != MAXLNUM)
4533 curwin->w_cursor.lnum =
4534 lnum > curbuf->b_ml.ml_line_count
4535 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004536
4537 // Start a forward search at the end of the line (unless
4538 // before the first line).
4539 // Start a backward search at the start of the line.
4540 // This makes sure we never match in the current
4541 // line, and can match anywhere in the
4542 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004543 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004544 curwin->w_cursor.col = MAXCOL;
4545 else
4546 curwin->w_cursor.col = 0;
4547 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004548 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004549 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004550 {
4551 curwin->w_cursor = pos;
4552 cmd = NULL;
4553 goto error;
4554 }
4555 lnum = curwin->w_cursor.lnum;
4556 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004557 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004558 cmd += searchcmdlen;
4559 }
4560 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004562 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 ++cmd;
4564 if (addr_type != ADDR_LINES)
4565 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004566 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004567 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004568 goto error;
4569 }
4570 if (*cmd == '&')
4571 i = RE_SUBST;
4572 else if (*cmd == '?' || *cmd == '/')
4573 i = RE_SEARCH;
4574 else
4575 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004576 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004577 cmd = NULL;
4578 goto error;
4579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004581 if (!skip)
4582 {
4583 /*
4584 * When search follows another address, start from
4585 * there.
4586 */
4587 if (lnum != MAXLNUM)
4588 pos.lnum = lnum;
4589 else
4590 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004592 /*
4593 * Start the search just like for the above
4594 * do_search().
4595 */
4596 if (*cmd != '?')
4597 pos.col = MAXCOL;
4598 else
4599 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004600 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004601 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004602 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004603 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004604 lnum = pos.lnum;
4605 else
4606 {
4607 cmd = NULL;
4608 goto error;
4609 }
4610 }
4611 ++cmd;
4612 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613
4614 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004615 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004616 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618
4619 for (;;)
4620 {
4621 cmd = skipwhite(cmd);
4622 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4623 break;
4624
4625 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004626 {
4627 switch (addr_type)
4628 {
4629 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004630 case ADDR_OTHER:
4631 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004632 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004633 break;
4634 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004635 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004636 break;
4637 case ADDR_ARGUMENTS:
4638 lnum = curwin->w_arg_idx + 1;
4639 break;
4640 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004641 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004642 lnum = curbuf->b_fnum;
4643 break;
4644 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004645 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004646 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004647 case ADDR_TABS_RELATIVE:
4648 lnum = 1;
4649 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004650 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004651#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004652 lnum = qf_get_cur_idx(eap);
4653#endif
4654 break;
4655 case ADDR_QUICKFIX_VALID:
4656#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004657 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004658#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004659 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004660 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004661 case ADDR_UNSIGNED:
4662 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004663 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004664 }
4665 }
4666
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004668 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 else
4670 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004671 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 n = 1;
4673 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004674 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004675 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004677 if (n == MAXLNUM)
4678 {
4679 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004680 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004681 goto error;
4682 }
4683 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004684
4685 if (addr_type == ADDR_TABS_RELATIVE)
4686 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004687 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004688 cmd = NULL;
4689 goto error;
4690 }
4691 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004692 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004693 lnum = compute_buffer_local_count(
4694 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004696 {
4697#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004698 // Relative line addressing: need to adjust for lines in a
4699 // closed fold after the first address.
4700 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4701 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004702 (void)hasFolding(lnum, NULL, &lnum);
4703#endif
4704 if (i == '-')
4705 lnum -= n;
4706 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004707 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004708 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004709 {
4710 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004711 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004712 goto error;
4713 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004714 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004715 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 }
4718 } while (*cmd == '/' || *cmd == '?');
4719
4720error:
4721 *ptr = cmd;
4722 return lnum;
4723}
4724
4725/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004726 * Set eap->line1 and eap->line2 to the whole range.
4727 * Used for commands with the EX_DFLALL flag and no range given.
4728 */
4729 static void
4730address_default_all(exarg_T *eap)
4731{
4732 eap->line1 = 1;
4733 switch (eap->addr_type)
4734 {
4735 case ADDR_LINES:
4736 case ADDR_OTHER:
4737 eap->line2 = curbuf->b_ml.ml_line_count;
4738 break;
4739 case ADDR_LOADED_BUFFERS:
4740 {
4741 buf_T *buf = firstbuf;
4742
4743 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4744 buf = buf->b_next;
4745 eap->line1 = buf->b_fnum;
4746 buf = lastbuf;
4747 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4748 buf = buf->b_prev;
4749 eap->line2 = buf->b_fnum;
4750 }
4751 break;
4752 case ADDR_BUFFERS:
4753 eap->line1 = firstbuf->b_fnum;
4754 eap->line2 = lastbuf->b_fnum;
4755 break;
4756 case ADDR_WINDOWS:
4757 eap->line2 = LAST_WIN_NR;
4758 break;
4759 case ADDR_TABS:
4760 eap->line2 = LAST_TAB_NR;
4761 break;
4762 case ADDR_TABS_RELATIVE:
4763 eap->line2 = 1;
4764 break;
4765 case ADDR_ARGUMENTS:
4766 if (ARGCOUNT == 0)
4767 eap->line1 = eap->line2 = 0;
4768 else
4769 eap->line2 = ARGCOUNT;
4770 break;
4771 case ADDR_QUICKFIX_VALID:
4772#ifdef FEAT_QUICKFIX
4773 eap->line2 = qf_get_valid_size(eap);
4774 if (eap->line2 == 0)
4775 eap->line2 = 1;
4776#endif
4777 break;
4778 case ADDR_NONE:
4779 case ADDR_UNSIGNED:
4780 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004781 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004782 break;
4783 }
4784}
4785
4786
4787/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004788 * Get flags from an Ex command argument.
4789 */
4790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004791get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004792{
4793 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4794 {
4795 if (*eap->arg == 'l')
4796 eap->flags |= EXFLAG_LIST;
4797 else if (*eap->arg == 'p')
4798 eap->flags |= EXFLAG_PRINT;
4799 else
4800 eap->flags |= EXFLAG_NR;
4801 eap->arg = skipwhite(eap->arg + 1);
4802 }
4803}
4804
4805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 * Function called for command which is Not Implemented. NI!
4807 */
4808 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004809ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
4811 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004812 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004813 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814}
4815
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004816#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817/*
4818 * Function called for script command which is Not Implemented. NI!
4819 * Skips over ":perl <<EOF" constructs.
4820 */
4821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004822ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823{
4824 if (!eap->skip)
4825 ex_ni(eap);
4826 else
4827 vim_free(script_get(eap, eap->arg));
4828}
4829#endif
4830
4831/*
4832 * Check range in Ex command for validity.
4833 * Return NULL when valid, error message when invalid.
4834 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004835 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004836invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004838 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004839
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 if ( eap->line1 < 0
4841 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004842 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004843 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004844
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004845 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004846 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004847 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004848 {
4849 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004850 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004851#ifdef FEAT_DIFF
4852 + (eap->cmdidx == CMD_diffget)
4853#endif
4854 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004855 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004856 break;
4857 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004858 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004859 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004860 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004861 break;
4862 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004863 // Only a boundary check, not whether the buffers actually
4864 // exist.
4865 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004866 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004867 break;
4868 case ADDR_LOADED_BUFFERS:
4869 buf = firstbuf;
4870 while (buf->b_ml.ml_mfp == NULL)
4871 {
4872 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004873 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004874 buf = buf->b_next;
4875 }
4876 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004877 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004878 buf = lastbuf;
4879 while (buf->b_ml.ml_mfp == NULL)
4880 {
4881 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004882 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004883 buf = buf->b_prev;
4884 }
4885 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004886 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004887 break;
4888 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004889 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004890 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004891 break;
4892 case ADDR_TABS:
4893 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004894 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004895 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004896 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004897 case ADDR_OTHER:
4898 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004899 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004900 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004901#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004902 // No error for value that is too big, will use the last entry.
4903 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004904 {
4905 if (eap->addr_count == 0)
4906 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004907 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004908 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004909#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004910 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004911 case ADDR_QUICKFIX_VALID:
4912#ifdef FEAT_QUICKFIX
4913 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4914 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004915 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004916#endif
4917 break;
4918 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004919 case ADDR_NONE:
4920 // Will give an error elsewhere.
4921 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004922 }
4923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 return NULL;
4925}
4926
4927/*
4928 * Correct the range for zero line number, if required.
4929 */
4930 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004931correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004933 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
4935 if (eap->line1 == 0)
4936 eap->line1 = 1;
4937 if (eap->line2 == 0)
4938 eap->line2 = 1;
4939 }
4940}
4941
Bram Moolenaar748bf032005-02-02 23:04:36 +00004942#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004943/*
4944 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4945 * pattern. Otherwise return eap->arg.
4946 */
4947 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004948skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004949{
4950 char_u *p = eap->arg;
4951
Bram Moolenaara37420f2006-02-04 22:37:47 +00004952 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4953 || eap->cmdidx == CMD_vimgrepadd
4954 || eap->cmdidx == CMD_lvimgrepadd
4955 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004956 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004957 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004958 if (p == NULL)
4959 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004960 }
4961 return p;
4962}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004963
4964/*
4965 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4966 * in the command line, so that things like % get expanded.
4967 */
4968 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004969replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004970{
4971 char_u *new_cmdline;
4972 char_u *program;
4973 char_u *pos;
4974 char_u *ptr;
4975 int len;
4976 int i;
4977
4978 /*
4979 * Don't do it when ":vimgrep" is used for ":grep".
4980 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004981 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4982 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4983 || eap->cmdidx == CMD_grepadd
4984 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004985 && !grep_internal(eap->cmdidx))
4986 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004987 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4988 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004989 {
4990 if (*curbuf->b_p_gp == NUL)
4991 program = p_gp;
4992 else
4993 program = curbuf->b_p_gp;
4994 }
4995 else
4996 {
4997 if (*curbuf->b_p_mp == NUL)
4998 program = p_mp;
4999 else
5000 program = curbuf->b_p_mp;
5001 }
5002
5003 p = skipwhite(p);
5004
5005 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5006 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005007 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005008 i = 1;
5009 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
5010 ++i;
5011 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005012 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005013 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005014 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005015 ptr = new_cmdline;
5016 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5017 {
5018 i = (int)(pos - program);
5019 STRNCPY(ptr, program, i);
5020 STRCPY(ptr += i, p);
5021 ptr += len;
5022 program = pos + 2;
5023 }
5024 STRCPY(ptr, program);
5025 }
5026 else
5027 {
John Marriotted908f72024-04-18 22:46:56 +02005028 size_t program_len = STRLEN(program);
5029
5030 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005031 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005032 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005033 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005034 STRCPY(new_cmdline + program_len, " ");
5035 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005036 }
5037 msg_make(p);
5038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005039 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005040 vim_free(*cmdlinep);
5041 *cmdlinep = new_cmdline;
5042 p = new_cmdline;
5043 }
5044 return p;
5045}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005046#endif
5047
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048/*
5049 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005050 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 * Return FAIL for failure, OK otherwise.
5052 */
5053 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005054expand_filename(
5055 exarg_T *eap,
5056 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005057 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005059 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005061 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 char_u *p;
5063 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005064 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
Bram Moolenaar748bf032005-02-02 23:04:36 +00005066#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005067 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005068 p = skip_grep_pat(eap);
5069#else
5070 p = eap->arg;
5071#endif
5072
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 /*
5074 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5075 * the file name contains a wildcard it should not cause expanding.
5076 * (it will be expanded anyway if there is a wildcard before replacing).
5077 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005078 has_wildcards = mch_has_wildcard(p);
5079 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005081#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005082 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005083 if (p[0] == '`' && p[1] == '=')
5084 {
5085 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005086 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005087 if (*p == '`')
5088 ++p;
5089 continue;
5090 }
5091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 /*
5093 * Quick check if this cannot be the start of a special string.
5094 * Also removes backslash before '%', '#' and '<'.
5095 */
5096 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5097 {
5098 ++p;
5099 continue;
5100 }
5101
5102 /*
5103 * Try to find a match at this position.
5104 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005105 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005106 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005107 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005109 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 p += srclen;
5112 continue;
5113 }
5114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005115 // Wildcards won't be expanded below, the replacement is taken
5116 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005117 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5118 {
5119 char_u *l = repl;
5120
5121 repl = expand_env_save(repl);
5122 vim_free(l);
5123 }
5124
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005125 // Need to escape white space et al. with a backslash.
5126 // Don't do this for:
5127 // - replacement that already has been escaped: "##"
5128 // - shell commands (may have to use quotes instead).
5129 // - non-unix systems when there is a single argument (spaces don't
5130 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005132 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 && eap->cmdidx != CMD_grep
5135 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005136 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005137 && eap->cmdidx != CMD_lgrep
5138 && eap->cmdidx != CMD_lgrepadd
5139 && eap->cmdidx != CMD_lmake
5140 && eap->cmdidx != CMD_make
5141 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005143 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144#endif
5145 )
5146 {
5147 char_u *l;
5148#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005149 // Don't escape a backslash here, because rem_backslash() doesn't
5150 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 static char_u *nobslash = (char_u *)" \t\"|";
5152# define ESCAPE_CHARS nobslash
5153#else
5154# define ESCAPE_CHARS escape_chars
5155#endif
5156
5157 for (l = repl; *l; ++l)
5158 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5159 {
5160 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5161 if (l != NULL)
5162 {
5163 vim_free(repl);
5164 repl = l;
5165 }
5166 break;
5167 }
5168 }
5169
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005170 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005171 if ((eap->usefilter || eap->cmdidx == CMD_bang
5172 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005173 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
5175 char_u *l;
5176
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005177 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 if (l != NULL)
5179 {
5180 vim_free(repl);
5181 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
5183 }
5184
Mike Williams51024bb2024-05-30 07:46:30 +02005185 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 vim_free(repl);
5187 if (p == NULL)
5188 return FAIL;
5189 }
5190
5191 /*
5192 * One file argument: Expand wildcards.
5193 * Don't do this with ":r !command" or ":w !command".
5194 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005195 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
5197 /*
5198 * May do this twice:
5199 * 1. Replace environment variables.
5200 * 2. Replace any other wildcards, remove backslashes.
5201 */
5202 for (n = 1; n <= 2; ++n)
5203 {
5204 if (n == 2)
5205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 /*
5207 * Halve the number of backslashes (this is Vi compatible).
5208 * For Unix and OS/2, when wildcards are expanded, this is
5209 * done by ExpandOne() below.
5210 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005211#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (!has_wildcards)
5213#endif
5214 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
5216
5217 if (has_wildcards)
5218 {
5219 if (n == 1)
5220 {
5221 /*
5222 * First loop: May expand environment variables. This
5223 * can be done much faster with expand_env() than with
5224 * something else (e.g., calling a shell).
5225 * After expanding environment variables, check again
5226 * if there are still wildcards present.
5227 */
5228 if (vim_strchr(eap->arg, '$') != NULL
5229 || vim_strchr(eap->arg, '~') != NULL)
5230 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005231 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005232 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 has_wildcards = mch_has_wildcard(NameBuff);
5234 p = NameBuff;
5235 }
5236 else
5237 p = NULL;
5238 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005239 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005242 int options = WILD_LIST_NOTFOUND
5243 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245 ExpandInit(&xpc);
5246 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005247 if (p_wic)
5248 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005250 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 if (p == NULL)
5252 return FAIL;
5253 }
5254 if (p != NULL)
5255 {
John Marriotted908f72024-04-18 22:46:56 +02005256 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005258 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 vim_free(p);
5260 }
5261 }
5262 }
5263 }
5264 return OK;
5265}
5266
5267/*
5268 * Replace part of the command line, keeping eap->cmd, eap->arg and
5269 * eap->nextcmd correct.
5270 * "src" points to the part that is to be replaced, of length "srclen".
5271 * "repl" is the replacement string.
5272 * Returns a pointer to the character after the replaced string.
5273 * Returns NULL for failure.
5274 */
5275 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005276repl_cmdline(
5277 exarg_T *eap,
5278 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005279 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005280 char_u *repl,
5281 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282{
John Marriotted908f72024-04-18 22:46:56 +02005283 size_t repllen;
5284 size_t taillen;
5285 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005287 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
5289 /*
5290 * The new command line is build in new_cmdline[].
5291 * First allocate it.
5292 * Careful: a "+cmd" argument may have been NUL terminated.
5293 */
John Marriotted908f72024-04-18 22:46:56 +02005294 repllen = STRLEN(repl);
5295 taillen = STRLEN(src + srclen);
5296 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005297 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005298 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005299 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005300 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
5302 /*
5303 * Copy the stuff before the expanded part.
5304 * Copy the expanded stuff.
5305 * Copy what came after the expanded part.
5306 * Copy the next commands, if there are any.
5307 */
John Marriotted908f72024-04-18 22:46:56 +02005308 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5309 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005310
John Marriotted908f72024-04-18 22:46:56 +02005311 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5312 new_cmdlinelen += repllen; // remember the end of the string
5313 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5314 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005316 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
John Marriotted908f72024-04-18 22:46:56 +02005318 new_cmdlinelen += taillen + 1;
5319 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5320 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
5322 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5323 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5324 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5325 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5326 vim_free(*cmdlinep);
5327 *cmdlinep = new_cmdline;
5328
5329 return src;
5330}
5331
5332/*
5333 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005334 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 */
5336 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005337separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
5339 char_u *p;
5340
Bram Moolenaar86b68352004-12-27 21:59:20 +00005341#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005342 p = skip_grep_pat(eap);
5343#else
5344 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005345#endif
5346
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005347 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 {
5349 if (*p == Ctrl_V)
5350 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005351 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005352 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005354 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005355 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005356 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 break;
5358 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005359
5360#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005361 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005362 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005363 {
5364 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005365 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005366 if (*p == NUL) // stop at NUL after CTRL-V
5367 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005368 }
5369#endif
5370
Bram Moolenaareda29c92022-10-02 12:59:00 +01005371 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005372 // :@" and :*" do not start a comment!
5373 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005374 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005375 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005376 && !(eap->argt & EX_NOTRLCOM)
5377 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5378 || p != eap->arg)
5379 && (eap->cmdidx != CMD_redir
5380 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005381 || (*p == '#'
5382 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005383 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005384 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005385 || (*p == '|'
5386 && eap->cmdidx != CMD_append
5387 && eap->cmdidx != CMD_change
5388 && eap->cmdidx != CMD_insert)
5389 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
5391 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005392 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 * AND 'b' is present in 'cpoptions'.
5394 */
5395 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005396 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005398 if (!keep_backslash)
5399 {
5400 STRMOVE(p - 1, p); // remove the '\'
5401 --p;
5402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
5404 else
5405 {
5406 eap->nextcmd = check_nextcmd(p);
5407 *p = NUL;
5408 break;
5409 }
5410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005412
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005413 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 del_trailing_spaces(eap->arg);
5415}
5416
5417/*
5418 * get + command from ex argument
5419 */
5420 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005421getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
5423 char_u *arg = *argp;
5424 char_u *command = NULL;
5425
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005426 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {
5428 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005429 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 command = dollar_command;
5431 else
5432 {
5433 command = arg;
5434 arg = skip_cmd_arg(command, TRUE);
5435 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005436 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 }
5438
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005439 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 *argp = arg;
5441 }
5442 return command;
5443}
5444
5445/*
5446 * Find end of "+command" argument. Skip over "\ " and "\\".
5447 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005448 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005449skip_cmd_arg(
5450 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005451 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
5453 while (*p && !vim_isspace(*p))
5454 {
5455 if (*p == '\\' && p[1] != NUL)
5456 {
5457 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005458 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 else
5460 ++p;
5461 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005462 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
5464 return p;
5465}
5466
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005467 int
5468get_bad_opt(char_u *p, exarg_T *eap)
5469{
5470 if (STRICMP(p, "keep") == 0)
5471 eap->bad_char = BAD_KEEP;
5472 else if (STRICMP(p, "drop") == 0)
5473 eap->bad_char = BAD_DROP;
5474 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5475 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005476 else
5477 return FAIL;
5478 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005479}
5480
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005482 * Function given to ExpandGeneric() to obtain the list of bad= names.
5483 */
5484 static char_u *
5485get_bad_name(expand_T *xp UNUSED, int idx)
5486{
5487 // Note: Keep this in sync with getargopt.
5488 static char *(p_bad_values[]) =
5489 {
5490 "?",
5491 "keep",
5492 "drop",
5493 };
5494
John Marriotted908f72024-04-18 22:46:56 +02005495 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5496 return NULL;
5497
5498 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005499}
5500
5501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 * Get "++opt=arg" argument.
5503 * Return FAIL or OK.
5504 */
5505 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005506getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
5508 char_u *arg = eap->arg + 2;
5509 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005510 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005513 // Note: Keep this in sync with get_argopt_name.
5514
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005515 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5517 {
5518 if (*arg == 'n')
5519 {
5520 arg += 2;
5521 eap->force_bin = FORCE_NOBIN;
5522 }
5523 else
5524 eap->force_bin = FORCE_BIN;
5525 if (!checkforcmd(&arg, "binary", 3))
5526 return FAIL;
5527 eap->arg = skipwhite(arg);
5528 return OK;
5529 }
5530
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005531 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005532 if (STRNCMP(arg, "edit", 4) == 0)
5533 {
5534 eap->read_edit = TRUE;
5535 eap->arg = skipwhite(arg + 4);
5536 return OK;
5537 }
5538
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 if (STRNCMP(arg, "ff", 2) == 0)
5540 {
5541 arg += 2;
5542 pp = &eap->force_ff;
5543 }
5544 else if (STRNCMP(arg, "fileformat", 10) == 0)
5545 {
5546 arg += 10;
5547 pp = &eap->force_ff;
5548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 else if (STRNCMP(arg, "enc", 3) == 0)
5550 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005551 if (STRNCMP(arg, "encoding", 8) == 0)
5552 arg += 8;
5553 else
5554 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 pp = &eap->force_enc;
5556 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005557 else if (STRNCMP(arg, "bad", 3) == 0)
5558 {
5559 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005560 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
5563 if (pp == NULL || *arg != '=')
5564 return FAIL;
5565
5566 ++arg;
5567 *pp = (int)(arg - eap->cmd);
5568 arg = skip_cmd_arg(arg, FALSE);
5569 eap->arg = skipwhite(arg);
5570 *arg = NUL;
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 if (pp == &eap->force_ff)
5573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5575 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005576 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005578 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005580 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5582 *p = TOLOWER_ASC(*p);
5583 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005584 else
5585 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005586 // Check ++bad= argument. Must be a single-byte character, "keep" or
5587 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005588 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005589 return FAIL;
5590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591
5592 return OK;
5593}
5594
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005595/*
5596 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5597 */
5598 static char_u *
5599get_argopt_name(expand_T *xp UNUSED, int idx)
5600{
5601 // Note: Keep this in sync with getargopt.
5602 static char *(p_opt_values[]) =
5603 {
5604 "fileformat=",
5605 "encoding=",
5606 "binary",
5607 "nobinary",
5608 "bad=",
5609 "edit",
5610 };
5611
John Marriotted908f72024-04-18 22:46:56 +02005612 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5613 return NULL;
5614
5615 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005616}
5617
5618/*
5619 * Command-line expansion for ++opt=name.
5620 */
5621 int
5622expand_argopt(
5623 char_u *pat,
5624 expand_T *xp,
5625 regmatch_T *rmp,
5626 char_u ***matches,
5627 int *numMatches)
5628{
5629 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5630 {
5631 char_u *(*cb)(expand_T *, int) = NULL;
5632
5633 char_u *name_end = xp->xp_pattern - 1;
5634 if (name_end - xp->xp_line >= 2
5635 && STRNCMP(name_end - 2, "ff", 2) == 0)
5636 cb = get_fileformat_name;
5637 else if (name_end - xp->xp_line >= 10
5638 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5639 cb = get_fileformat_name;
5640 else if (name_end - xp->xp_line >= 3
5641 && STRNCMP(name_end - 3, "enc", 3) == 0)
5642 cb = get_encoding_name;
5643 else if (name_end - xp->xp_line >= 8
5644 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5645 cb = get_encoding_name;
5646 else if (name_end - xp->xp_line >= 3
5647 && STRNCMP(name_end - 3, "bad", 3) == 0)
5648 cb = get_bad_name;
5649
5650 if (cb != NULL)
5651 {
5652 return ExpandGeneric(
5653 pat,
5654 xp,
5655 rmp,
5656 matches,
5657 numMatches,
5658 cb,
5659 FALSE);
5660 }
5661 return FAIL;
5662 }
5663
5664 // Special handling of "ff" which acts as a short form of
5665 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005666 if (xp->xp_pattern_len == 2
5667 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005668 {
5669 *matches = ALLOC_MULT(char_u *, 1);
5670 if (*matches == NULL)
5671 return FAIL;
5672 *numMatches = 1;
5673 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5674 return OK;
5675 }
5676
5677 return ExpandGeneric(
5678 pat,
5679 xp,
5680 rmp,
5681 matches,
5682 numMatches,
5683 get_argopt_name,
5684 FALSE);
5685}
5686
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005688ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005691 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 * directory for security reasons.
5693 */
5694 if (secure)
5695 {
5696 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005697 eap->errmsg =
5698 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005701 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 else
5703 do_augroup(eap->arg, eap->forceit);
5704}
5705
5706/*
5707 * ":doautocmd": Apply the automatic commands to the current buffer.
5708 */
5709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005710ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005712 char_u *arg = eap->arg;
5713 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005714 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005715
Bram Moolenaar1610d052016-06-09 22:53:01 +02005716 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005717 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005718 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005719 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722/*
5723 * :[N]bunload[!] [N] [bufname] unload buffer
5724 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5725 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5726 */
5727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005728ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005730 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005731 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 eap->errmsg = do_bufdel(
5733 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5734 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5735 : DOBUF_UNLOAD, eap->arg,
5736 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5737}
5738
5739/*
5740 * :[N]buffer [N] to buffer N
5741 * :[N]sbuffer [N] to buffer N
5742 */
5743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005744ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005746 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005747 return;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01005748 do_exbuffer(eap);
5749}
5750
5751/*
5752 * ":buffer" command and alike.
5753 */
5754 static void
5755do_exbuffer(exarg_T *eap)
5756{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005758 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 else
5760 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005761 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5763 else
5764 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005765 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005766 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 }
5768}
5769
5770/*
5771 * :[N]bmodified [N] to next mod. buffer
5772 * :[N]sbmodified [N] to next mod. buffer
5773 */
5774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005775ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005778 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005779 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780}
5781
5782/*
5783 * :[N]bnext [N] to next buffer
5784 * :[N]sbnext [N] split and to next buffer
5785 */
5786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005787ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005789 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005790 return;
5791
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005793 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005794 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795}
5796
5797/*
5798 * :[N]bNext [N] to previous buffer
5799 * :[N]bprevious [N] to previous buffer
5800 * :[N]sbNext [N] split and to previous buffer
5801 * :[N]sbprevious [N] split and to previous buffer
5802 */
5803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005804ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005806 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005807 return;
5808
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005810 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005811 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812}
5813
5814/*
5815 * :brewind to first buffer
5816 * :bfirst to first buffer
5817 * :sbrewind split and to first buffer
5818 * :sbfirst split and to first buffer
5819 */
5820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005821ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005823 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005824 return;
5825
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005827 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005828 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829}
5830
5831/*
5832 * :blast to last buffer
5833 * :sblast split and to last buffer
5834 */
5835 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005836ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005838 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005839 return;
5840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005842 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005843 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845
Bram Moolenaar7a092242020-04-16 22:10:49 +02005846/*
5847 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005848 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005851ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005853 int comment_char = '"';
5854
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005855 if (in_vim9script())
5856 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005857 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005858}
5859
5860/*
5861 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5862 * to "cmd_start" or has a white space character before it.
5863 */
5864 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005865ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005866{
5867 int c = *cmd;
5868
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005869 if (c == NUL || c == '|' || c == '\n')
5870 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005871 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005872 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5873 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005874 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005875 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876}
5877
5878#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5879 || defined(PROTO)
5880/*
5881 * Return the next command, after the first '|' or '\n'.
5882 * Return NULL if not found.
5883 */
5884 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005885find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
5887 while (*p != '|' && *p != '\n')
5888 {
5889 if (*p == NUL)
5890 return NULL;
5891 ++p;
5892 }
5893 return (p + 1);
5894}
5895#endif
5896
5897/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005898 * Check if *p is a separator between Ex commands, skipping over white space.
5899 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 */
5901 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005902check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005904 char_u *s = skipwhite(p);
5905
5906 if (*s == '|' || *s == '\n')
5907 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 else
5909 return NULL;
5910}
5911
5912/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005913 * If "eap->nextcmd" is not set, check for a next command at "p".
5914 */
5915 void
5916set_nextcmd(exarg_T *eap, char_u *arg)
5917{
5918 char_u *p = check_nextcmd(arg);
5919
5920 if (eap->nextcmd == NULL)
5921 eap->nextcmd = p;
5922 else if (p != NULL)
5923 // cannot use "| command" inside a {} block
5924 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5925}
5926
5927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 * - if there are more files to edit
5929 * - and this is the last window
5930 * - and forceit not used
5931 * - and not repeated twice on a row
5932 * return FAIL and give error message if 'message' TRUE
5933 * return OK otherwise
5934 */
5935 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005936check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005937 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005938 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939{
5940 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5941
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005942 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005943 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {
5945 if (message)
5946 {
5947#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005948 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5949 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005951 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005953 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5954 NGETTEXT("%d more file to edit. Quit anyway?",
5955 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5957 return OK;
5958 return FAIL;
5959 }
5960#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005961 semsg(NGETTEXT(e_nr_more_file_to_edit,
5962 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005963 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 }
5965 return FAIL;
5966 }
5967 return OK;
5968}
5969
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970/*
5971 * Function given to ExpandGeneric() to obtain the list of command names.
5972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005974get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975{
5976 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005977 return expand_user_command_name(idx);
Christian Brabandt70a11a62024-07-26 19:13:55 +02005978 // the following are no real commands
5979 if (STRNCMP(cmdnames[idx].cmd_name, "{", 1) == 0 ||
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +02005980 STRNCMP(cmdnames[idx].cmd_name, "}", 1) == 0)
Christian Brabandt70a11a62024-07-26 19:13:55 +02005981 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 return cmdnames[idx].cmd_name;
5983}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005986ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987{
Bram Moolenaare6850792010-05-14 15:28:44 +02005988 if (*eap->arg == NUL)
5989 {
5990#ifdef FEAT_EVAL
5991 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5992 char_u *p = NULL;
5993
5994 if (expr != NULL)
5995 {
5996 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005997 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005998 --emsg_off;
5999 vim_free(expr);
6000 }
6001 if (p != NULL)
6002 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006003 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02006004 vim_free(p);
6005 }
6006 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006007 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02006008#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006009 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02006010#endif
6011 }
6012 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00006013 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006014
6015#ifdef FEAT_VTP
6016 else if (has_vtp_working())
6017 {
6018 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006019 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006020 redrawcmd();
6021 }
6022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023}
6024
6025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006026ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027{
6028 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006029 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 do_highlight(eap->arg, eap->forceit, FALSE);
6031}
6032
6033
6034/*
6035 * Call this function if we thought we were going to exit, but we won't
6036 * (because of an error). May need to restore the terminal mode.
6037 */
6038 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006039not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040{
6041 exiting = FALSE;
6042 settmode(TMODE_RAW);
6043}
6044
Bram Moolenaar3552e742021-05-29 12:21:58 +02006045 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006046before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6047{
6048 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6049
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006050 // Bail out when autocommands closed the window.
6051 // Refuse to quit when the buffer in the last window is being closed (can
6052 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006053 if (!win_valid(wp)
6054 || curbuf_locked()
6055 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6056 return TRUE;
6057
6058 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6059 {
6060 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006061 // Refuse to quit when locked or when the window was closed or the
6062 // buffer in the last window is being closed (can only happen in
6063 // autocommands).
6064 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006065 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6066 return TRUE;
6067 }
6068
6069 return FALSE;
6070}
6071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006073 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006074 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006075 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006077 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006078ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006080 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 if (cmdwin_type != 0)
6083 {
6084 cmdwin_result = Ctrl_C;
6085 return;
6086 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006087 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006088 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006089 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006090 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006091 return;
6092 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006093 if (eap->addr_count > 0)
6094 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006095 int wnr = eap->line2;
6096
6097 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6098 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006099 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006100 }
6101 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006102 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006103
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006104 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006105 if (curbuf_locked())
6106 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006107
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006108 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006109 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006110 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
6112#ifdef FEAT_NETBEANS_INTG
6113 netbeansForcedQuit = eap->forceit;
6114#endif
6115
6116 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006117 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 */
6119 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6120 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006121 if ((!buf_hide(wp->w_buffer)
6122 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006123 | (eap->forceit ? CCGD_FORCEIT : 0)
6124 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006126 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {
6128 not_exiting();
6129 }
6130 else
6131 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006132 // quit last window
6133 // Note: only_one_window() returns true, even so a help window is
6134 // still open. In that case only quit, if no address has been
6135 // specified. Example:
6136 // :h|wincmd w|1q - don't quit
6137 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006138 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006140 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006141#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006143#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006144 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006145 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 }
6147}
6148
6149/*
6150 * ":cquit".
6151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006153ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006155 // this does not always pass on the exit code to the Manx compiler. why?
6156 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157}
6158
6159/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006160 * Do preparations for "qall" and "wqall".
6161 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006163 int
6164before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 if (cmdwin_type != 0)
6167 {
6168 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006169 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 else
6171 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006172 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006174
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006175 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006176 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006177 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006178 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006179 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006180 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006181
6182 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006183 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006184
Bram Moolenaar411da642023-05-10 16:53:27 +01006185 return OK;
6186}
6187
6188/*
6189 * ":qall": try to quit all windows
6190 */
6191 static void
6192ex_quit_all(exarg_T *eap)
6193{
6194 if (before_quit_all(eap) == FAIL)
6195 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006197 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 getout(0);
6199 not_exiting();
6200}
6201
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202/*
6203 * ":close": close current window, unless it is the last one
6204 */
6205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006206ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006208 win_T *win;
6209 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006211 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006213 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006214 {
6215 if (eap->addr_count == 0)
6216 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006217 else
6218 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006219 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006220 {
6221 winnr++;
6222 if (winnr == eap->line2)
6223 break;
6224 }
6225 if (win == NULL)
6226 win = lastwin;
6227 ex_win_close(eap->forceit, win, NULL);
6228 }
6229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230}
6231
Bram Moolenaar4033c552017-09-16 20:54:51 +02006232#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006233/*
6234 * ":pclose": Close any preview window.
6235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006237ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006238{
6239 win_T *win;
6240
Bram Moolenaar79648732019-07-18 21:43:07 +02006241 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006242 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006243 if (win->w_p_pvw)
6244 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006245 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006246 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006247 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006248# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006249 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006250 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006251# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006252}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006253#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006254
Bram Moolenaarf740b292006-02-16 22:11:02 +00006255/*
6256 * Close window "win" and take care of handling closing the last window for a
6257 * modified buffer.
6258 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006260ex_win_close(
6261 int forceit,
6262 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006263 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
6265 int need_hide;
6266 buf_T *buf = win->w_buffer;
6267
Bram Moolenaarcf844172020-06-26 19:44:06 +02006268 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006269 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006270 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006271 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006272 return;
6273 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006274 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006275 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006276
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006278 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006280#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006281 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006283# ifdef FEAT_TERMINAL
6284 if (term_job_running(buf->b_term))
6285 {
6286 if (term_confirm_stop(buf) == FAIL)
6287 return;
6288 // Manually kill the terminal here because this command will
6289 // hide it otherwise.
6290 free_terminal(buf);
6291 need_hide = FALSE;
6292 }
6293 else
6294# endif
6295 {
6296 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006297
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006298 set_bufref(&bufref, buf);
6299 dialog_changed(buf, FALSE);
6300 if (bufref_valid(&bufref) && bufIsChanged(buf))
6301 return;
6302 need_hide = FALSE;
6303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 }
6305 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006308 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 return;
6310 }
6311 }
6312
Bram Moolenaar4033c552017-09-16 20:54:51 +02006313#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006315#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006316
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006317 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006318 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006319 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006320 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006321 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322}
6323
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006325 * Handle the argument for a tabpage related ex command.
6326 * Returns a tabpage number.
6327 * When an error is encountered then eap->errmsg is set.
6328 */
6329 static int
6330get_tabpage_arg(exarg_T *eap)
6331{
6332 int tab_number;
6333 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6334
6335 if (eap->arg && *eap->arg != NUL)
6336 {
6337 char_u *p = eap->arg;
6338 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006339 int relative = 0; // argument +N/-N means: go to N places to the
6340 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006341
6342 if (*p == '-')
6343 {
6344 relative = -1;
6345 p++;
6346 }
6347 else if (*p == '+')
6348 {
6349 relative = 1;
6350 p++;
6351 }
6352
6353 p_save = p;
6354 tab_number = getdigits(&p);
6355
6356 if (relative == 0)
6357 {
6358 if (STRCMP(p, "$") == 0)
6359 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006360 else if (STRCMP(p, "#") == 0)
6361 if (valid_tabpage(lastused_tabpage))
6362 tab_number = tabpage_index(lastused_tabpage);
6363 else
6364 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006365 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6366 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006367 tab_number = 0;
6368 goto theend;
6369 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006370 else if (p == p_save || *p_save == '-' || *p != NUL
6371 || tab_number > LAST_TAB_NR)
6372 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006373 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006374 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006375 goto theend;
6376 }
6377 }
6378 else
6379 {
6380 if (*p_save == NUL)
6381 tab_number = 1;
6382 else if (p == p_save || *p_save == '-' || *p != NUL
6383 || tab_number == 0)
6384 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006385 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006386 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006387 goto theend;
6388 }
6389 tab_number = tab_number * relative + tabpage_index(curtab);
6390 if (!unaccept_arg0 && relative == -1)
6391 --tab_number;
6392 }
6393 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006394 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006395 }
6396 else if (eap->addr_count > 0)
6397 {
6398 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006399 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006400 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006401 tab_number = 0;
6402 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006403 else
6404 {
6405 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006406 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006407 {
zeertzjq076faac2024-03-25 16:41:06 +01006408 char_u *cmdp = eap->cmd;
6409
6410 while (--cmdp > *eap->cmdlinep
6411 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6412 ;
6413 if (*cmdp == '-')
6414 {
6415 --tab_number;
6416 if (tab_number < unaccept_arg0)
6417 eap->errmsg = _(e_invalid_range);
6418 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006419 }
6420 }
6421 }
6422 else
6423 {
6424 switch (eap->cmdidx)
6425 {
6426 case CMD_tabnext:
6427 tab_number = tabpage_index(curtab) + 1;
6428 if (tab_number > LAST_TAB_NR)
6429 tab_number = 1;
6430 break;
6431 case CMD_tabmove:
6432 tab_number = LAST_TAB_NR;
6433 break;
6434 default:
6435 tab_number = tabpage_index(curtab);
6436 }
6437 }
6438
6439theend:
6440 return tab_number;
6441}
6442
6443/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006444 * ":tabclose": close current tab page, unless it is the last one.
6445 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 */
6447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006448ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006450 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006451 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006452
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006453 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006454 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006455 cmdwin_result = K_IGNORE;
6456 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006457 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006458
6459 if (first_tabpage->tp_next == NULL)
6460 {
6461 emsg(_(e_cannot_close_last_tab_page));
6462 return;
6463 }
6464
6465 if (window_layout_locked(CMD_tabclose))
6466 return;
6467
6468 tab_number = get_tabpage_arg(eap);
6469 if (eap->errmsg != NULL)
6470 return;
6471
6472 tp = find_tabpage(tab_number);
6473 if (tp == NULL)
6474 {
6475 beep_flush();
6476 return;
6477 }
6478 if (tp != curtab)
6479 {
6480 tabpage_close_other(tp, eap->forceit);
6481 return;
6482 }
6483 else if (!text_locked() && !curbuf_locked())
6484 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006485}
6486
6487/*
6488 * ":tabonly": close all tab pages except the current one
6489 */
6490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006491ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006492{
6493 tabpage_T *tp;
6494 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006495 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006496
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006497 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006498 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006499 cmdwin_result = K_IGNORE;
6500 return;
6501 }
6502
6503 if (first_tabpage->tp_next == NULL)
6504 {
6505 msg(_("Already only one tab page"));
6506 return;
6507 }
6508
6509 if (window_layout_locked(CMD_tabonly))
6510 return;
6511
6512 tab_number = get_tabpage_arg(eap);
6513 if (eap->errmsg != NULL)
6514 return;
6515
6516 goto_tabpage(tab_number);
6517 // Repeat this up to a 1000 times, because autocommands may
6518 // mess up the lists.
6519 for (done = 0; done < 1000; ++done)
6520 {
6521 FOR_ALL_TABPAGES(tp)
6522 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006523 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006524 tabpage_close_other(tp, eap->forceit);
6525 // if we failed to close it quit
6526 if (valid_tabpage(tp))
6527 done = 1000;
6528 // start over, "tp" is now invalid
6529 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006530 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006531 if (first_tabpage->tp_next == NULL)
6532 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535
6536/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006537 * Close the current tab page.
6538 */
6539 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006540tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006541{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006542 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006543 return;
6544
Jim Zhoubcf66e02025-03-16 20:24:57 +01006545 trigger_tabclosedpre(curtab, TRUE);
6546
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006547 // First close all the windows but the current one. If that worked then
6548 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006549 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006550 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006551 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006552 ex_win_close(forceit, curwin, NULL);
6553# ifdef FEAT_GUI
6554 need_mouse_correct = TRUE;
6555# endif
6556}
6557
6558/*
6559 * Close tab page "tp", which is not the current tab page.
6560 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006561 * Also takes care of the tab pages line disappearing when closing the
6562 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006563 */
6564 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006565tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006566{
6567 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006568 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006569
Jim Zhoubcf66e02025-03-16 20:24:57 +01006570 trigger_tabclosedpre(tp, TRUE);
6571
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006572 // Limit to 1000 windows, autocommands may add a window while we close
6573 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006574 while (++done < 1000)
6575 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006576 wp = tp->tp_firstwin;
6577 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006578
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006579 // Autocommands may delete the tab page under our fingers and we may
6580 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006581 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006582 break;
6583 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006584
Bram Moolenaar29323592016-07-24 22:04:11 +02006585 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006586}
6587
6588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 * ":only".
6590 */
6591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006592ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006594 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006595 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596# ifdef FEAT_GUI
6597 need_mouse_correct = TRUE;
6598# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006599 if (eap->addr_count > 0)
6600 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006601 win_T *wp;
6602 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006603 for (wp = firstwin; --wnr > 0; )
6604 {
6605 if (wp->w_next == NULL)
6606 break;
6607 else
6608 wp = wp->w_next;
6609 }
6610 win_goto(wp);
6611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 close_others(TRUE, eap->forceit);
6613}
6614
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006616ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006618 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006619 if (eap->skip)
6620 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006621
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006622 if (window_layout_locked(CMD_hide))
6623 return;
6624#ifdef FEAT_GUI
6625 need_mouse_correct = TRUE;
6626#endif
6627 if (eap->addr_count == 0)
6628 win_close(curwin, FALSE); // don't free buffer
6629 else
6630 {
6631 int winnr = 0;
6632 win_T *win;
6633
6634 FOR_ALL_WINDOWS(win)
6635 {
6636 winnr++;
6637 if (winnr == eap->line2)
6638 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006640 if (win == NULL)
6641 win = lastwin;
6642 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 }
6644}
6645
6646/*
6647 * ":stop" and ":suspend": Suspend Vim.
6648 */
dbivolaruab16ad32021-12-29 19:41:47 +00006649 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006650ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
6652 /*
6653 * Disallow suspending for "rvim".
6654 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006655 if (check_restricted())
6656 return;
6657
6658 if (!eap->forceit)
6659 autowrite_all();
6660 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6661 windgoto((int)Rows - 1, 0);
6662 out_char('\n');
6663 out_flush();
6664 stoptermcap();
6665 out_flush(); // needed for SUN to restore xterm buffer
6666 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6667 ui_suspend(); // call machine specific function
6668 maketitle();
6669 resettitle(); // force updating the title
6670 starttermcap();
6671 scroll_start(); // scroll screen before redrawing
6672 redraw_later_clear();
6673 shell_resized(); // may have resized window
6674 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675}
6676
6677/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006678 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 */
6680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006681ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006683#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006684 if (not_in_vim9(eap) == FAIL)
6685 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 if (cmdwin_type != 0)
6688 {
6689 cmdwin_result = Ctrl_C;
6690 return;
6691 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006692 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006693 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006694 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006695 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006696 return;
6697 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006700 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 */
6702 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6703 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006704
6705 // Write the buffer for ":wq" or when it was changed.
6706 // Trigger QuitPre and ExitPre.
6707 // Check if we can exit now, after autocommands have changed things.
6708 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6709 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006711 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 {
6713 not_exiting();
6714 }
6715 else
6716 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006717 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006719 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720# ifdef FEAT_GUI
6721 need_mouse_correct = TRUE;
6722# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006723 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006724 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 }
6726}
6727
6728/*
6729 * ":print", ":list", ":number".
6730 */
6731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006732ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006734 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006735 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006736 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006738 for ( ;!got_int; ui_breakcheck())
6739 {
6740 print_line(eap->line1,
6741 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6742 || (eap->flags & EXFLAG_NR)),
6743 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6744 if (++eap->line1 > eap->line2)
6745 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006746 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006747 }
6748 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006749 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006750 curwin->w_cursor.lnum = eap->line2;
6751 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 }
6753
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755}
6756
6757#ifdef FEAT_BYTEOFF
6758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006759ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
6761 goto_byte(eap->line2);
6762}
6763#endif
6764
6765/*
6766 * ":shell".
6767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006769ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770{
6771 do_shell(NULL, 0);
6772}
6773
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006774#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006776static int drop_busy = FALSE;
6777static int drop_filec;
6778static char_u **drop_filev = NULL;
6779static int drop_split;
6780static void (*drop_callback)(void *);
6781static void *drop_cookie;
6782
6783 static void
6784handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785{
6786 exarg_T ea;
6787 int save_msg_scroll = msg_scroll;
6788
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006789 // Setting the argument list may cause screen updates and being called
6790 // recursively. Avoid that by setting drop_busy.
6791 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006793 // Check whether the current buffer is changed. If so, we will need
6794 // to split the current window or data could be lost.
6795 // We don't need to check if the 'hidden' option is set, as in this
6796 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006797 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 {
6799 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006800 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 --emsg_off;
6802 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006803 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 if (win_split(0, 0) == FAIL)
6806 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006807 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006809 // When splitting the window, create a new alist. Otherwise the
6810 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 alist_unlink(curwin->w_alist);
6812 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 }
6814
6815 /*
6816 * Set up the new argument list.
6817 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006818 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819
6820 /*
6821 * Move to the first file.
6822 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006823 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006824 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 ea.cmd = (char_u *)"next";
6826 do_argfile(&ea, 0);
6827
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006828 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6829 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 need_start_insertmode = FALSE;
6831
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006832 // Restore msg_scroll, otherwise a following command may cause scrolling
6833 // unexpectedly. The screen will be redrawn by the caller, thus
6834 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006836
6837 if (drop_callback != NULL)
6838 drop_callback(drop_cookie);
6839
6840 drop_filev = NULL;
6841 drop_busy = FALSE;
6842}
6843
6844/*
6845 * Handle a file drop. The code is here because a drop is *nearly* like an
6846 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006847 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006848 * code is very similar to :args and hence needs access to a lot of the static
6849 * functions in this file.
6850 *
6851 * The "filev" list must have been allocated using alloc(), as should each item
6852 * in the list. This function takes over responsibility for freeing the "filev"
6853 * list.
6854 */
6855 void
6856handle_drop(
6857 int filec, // the number of files dropped
6858 char_u **filev, // the list of files dropped
6859 int split, // force splitting the window
6860 void (*callback)(void *), // to be called after setting the argument
6861 // list
6862 void *cookie) // argument for "callback" (allocated)
6863{
6864 // Cannot handle recursive drops, finish the pending one.
6865 if (drop_busy)
6866 {
6867 FreeWild(filec, filev);
6868 vim_free(cookie);
6869 return;
6870 }
6871
6872 // When calling handle_drop() more than once in a row we only use the last
6873 // one.
6874 if (drop_filev != NULL)
6875 {
6876 FreeWild(drop_filec, drop_filev);
6877 vim_free(drop_cookie);
6878 }
6879
6880 drop_filec = filec;
6881 drop_filev = filev;
6882 drop_split = split;
6883 drop_callback = callback;
6884 drop_cookie = cookie;
6885
6886 // Postpone this when:
6887 // - editing the command line
6888 // - not possible to change the current buffer
6889 // - updating the screen
6890 // As it may change buffers and window structures that are in use and cause
6891 // freed memory to be used.
6892 if (text_locked() || curbuf_locked() || updating_screen)
6893 return;
6894
6895 handle_drop_internal();
6896}
6897
6898/*
6899 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6900 * reset: Handle a postponed drop.
6901 */
6902 void
6903handle_any_postponed_drop(void)
6904{
6905 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006906 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006907 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908}
6909#endif
6910
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 * ":preserve".
6913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006915ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006917 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 ml_preserve(curbuf, TRUE);
6919}
6920
6921/*
6922 * ":recover".
6923 */
6924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006925ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006927 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006929 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6930 | CCGD_MULTWIN
6931 | (eap->forceit ? CCGD_FORCEIT : 0)
6932 | CCGD_EXCMD)
6933
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 && (*eap->arg == NUL
6935 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006936 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 recoverymode = FALSE;
6938}
6939
6940/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006941 * Command modifier used in a wrong way. Also for other commands that can't
6942 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 */
6944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006945ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006947 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948}
6949
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006950#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006951
6952// callback function for 'findfunc'
6953static callback_T ffu_cb;
6954
6955 static callback_T *
6956get_findfunc_callback(void)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006957{
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006958 return *curbuf->b_p_ffu != NUL ? &curbuf->b_ffu_cb : &ffu_cb;
6959}
6960
zeertzjq6eda2692024-11-03 09:23:33 +01006961/*
6962 * Call 'findfunc' to obtain a list of file names.
6963 */
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006964 static list_T *
6965call_findfunc(char_u *pat, int cmdcomplete)
6966{
6967 typval_T args[3];
6968 callback_T *cb;
6969 typval_T rettv;
6970 int retval;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006971 sctx_T saved_sctx = current_sctx;
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006972 sctx_T *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006973
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006974 args[0].v_type = VAR_STRING;
6975 args[0].vval.v_string = pat;
6976 args[1].v_type = VAR_BOOL;
6977 args[1].vval.v_number = cmdcomplete;
6978 args[2].v_type = VAR_UNKNOWN;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006979
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006980 // Lock the text to prevent weird things from happening. Also disallow
6981 // switching to another window, it should not be needed and may end up in
6982 // Insert mode in another buffer.
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006983 ++textlock;
6984
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006985 ctx = get_option_sctx("findfunc");
6986 if (ctx != NULL)
6987 current_sctx = *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006988
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006989 cb = get_findfunc_callback();
6990 retval = call_callback(cb, -1, &rettv, 2, args);
6991
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006992 current_sctx = saved_sctx;
6993
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006994 --textlock;
6995
6996 list_T *retlist = NULL;
6997
6998 if (retval == OK)
6999 {
7000 if (rettv.v_type == VAR_LIST)
7001 retlist = list_copy(rettv.vval.v_list, FALSE, FALSE, get_copyID());
7002 else
7003 emsg(_(e_invalid_return_type_from_findfunc));
7004
7005 clear_tv(&rettv);
7006 }
7007
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007008 return retlist;
7009}
7010
7011/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007012 * Find file names matching "pat" using 'findfunc' and return it in "files".
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007013 * Used for expanding the :find, :sfind and :tabfind command argument.
7014 * Returns OK on success and FAIL otherwise.
7015 */
7016 int
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007017expand_findfunc(char_u *pat, char_u ***files, int *numMatches)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007018{
7019 list_T *l;
7020 int len;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007021
7022 *numMatches = 0;
7023 *files = NULL;
7024
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007025 l = call_findfunc(pat, VVAL_TRUE);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007026
7027 if (l == NULL)
7028 return FAIL;
7029
7030 len = list_len(l);
7031 if (len == 0) // empty List
7032 return FAIL;
7033
7034 *files = ALLOC_MULT(char_u *, len);
7035 if (*files == NULL)
7036 return FAIL;
7037
7038 // Copy all the List items
7039 listitem_T *li;
7040 int idx = 0;
7041 FOR_ALL_LIST_ITEMS(l, li)
7042 {
7043 if (li->li_tv.v_type == VAR_STRING)
7044 {
7045 (*files)[idx] = vim_strsave(li->li_tv.vval.v_string);
7046 idx++;
7047 }
7048 }
7049
7050 *numMatches = idx;
7051 list_free(l);
7052
7053 return OK;
7054}
7055
7056/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007057 * Use 'findfunc' to find file 'findarg'. The 'count' argument is used to find
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007058 * the n'th matching file.
7059 */
7060 static char_u *
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007061findfunc_find_file(char_u *findarg, int findarg_len, int count)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007062{
7063 list_T *fname_list;
7064 char_u *ret_fname = NULL;
7065 char_u cc;
7066 int fname_count;
7067
7068 cc = findarg[findarg_len];
7069 findarg[findarg_len] = NUL;
7070
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007071 fname_list = call_findfunc(findarg, VVAL_FALSE);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007072 fname_count = list_len(fname_list);
7073
7074 if (fname_count == 0)
7075 semsg(_(e_cant_find_file_str_in_path), findarg);
7076 else
7077 {
7078 if (count > fname_count)
7079 semsg(_(e_no_more_file_str_found_in_path), findarg);
7080 else
7081 {
7082 listitem_T *li = list_find(fname_list, count - 1);
7083 if (li != NULL && li->li_tv.v_type == VAR_STRING)
7084 ret_fname = vim_strsave(li->li_tv.vval.v_string);
7085 }
7086 }
7087
7088 if (fname_list != NULL)
7089 list_free(fname_list);
7090
7091 findarg[findarg_len] = cc;
7092
7093 return ret_fname;
7094}
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007095
7096/*
7097 * Process the 'findfunc' option value.
7098 * Returns NULL on success and an error message on failure.
7099 */
7100 char *
7101did_set_findfunc(optset_T *args UNUSED)
7102{
7103 int retval;
7104
zeertzjq6eda2692024-11-03 09:23:33 +01007105 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007106 // buffer-local option set
7107 retval = option_set_callback_func(curbuf->b_p_ffu, &curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007108 else
7109 {
7110 // global option set
7111 retval = option_set_callback_func(p_ffu, &ffu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01007112 // when using :set, free the local callback
7113 if (!(args->os_flags & OPT_GLOBAL))
7114 free_callback(&curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007115 }
7116
7117 if (retval == FAIL)
7118 return e_invalid_argument;
7119
7120 // If the option value starts with <SID> or s:, then replace that with
7121 // the script identifier.
7122 char_u **varp = (char_u **)args->os_varp;
7123 char_u *name = get_scriptlocal_funcname(*varp);
7124 if (name != NULL)
7125 {
7126 free_string_option(*varp);
7127 *varp = name;
7128 }
7129
7130 return NULL;
7131}
7132
7133# if defined(EXITFREE) || defined(PROTO)
7134 void
7135free_findfunc_option(void)
7136{
7137 free_callback(&ffu_cb);
7138}
7139# endif
7140
7141/*
7142 * Mark the global 'findfunc' callback with "copyID" so that it is not
7143 * garbage collected.
7144 */
7145 int
7146set_ref_in_findfunc(int copyID UNUSED)
7147{
7148 int abort = FALSE;
7149 abort = set_ref_in_callback(&ffu_cb, copyID);
7150 return abort;
7151}
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007152#endif
7153
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154/*
7155 * :sview [+command] file split window with new file, read-only
7156 * :split [[+command] file] split window with current or new file
7157 * :vsplit [[+command] file] split window vertically with current or new file
7158 * :new [[+command] file] split window with no or new file
7159 * :vnew [[+command] file] split vertically window with no or new file
7160 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007161 *
7162 * :tabedit open new Tab page with empty window
7163 * :tabedit [+command] file open new Tab page and edit "file"
7164 * :tabnew [[+command] file] just like :tabedit
7165 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 */
7167 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007168ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007170 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007172#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007173 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02007174 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007175#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02007176 int use_tab = eap->cmdidx == CMD_tabedit
7177 || eap->cmdidx == CMD_tabfind
7178 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007180 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007181 return;
7182
Bram Moolenaar4033c552017-09-16 20:54:51 +02007183#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
Bram Moolenaar4033c552017-09-16 20:54:51 +02007187#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007188 // A ":split" in the quickfix window works like ":new". Don't want two
7189 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02007190 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 {
7192 if (eap->cmdidx == CMD_split)
7193 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 if (eap->cmdidx == CMD_vsplit)
7195 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007199 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007201 char_u *file_to_find = NULL;
7202 char *search_ctx = NULL;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007203
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007204 if (*get_findfunc() != NUL)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007205 {
7206#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007207 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007208 eap->addr_count > 0 ? eap->line2 : 1);
7209#endif
7210 }
7211 else
7212 {
7213 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7214 FNAME_MESS, TRUE, curbuf->b_ffname,
7215 &file_to_find, &search_ctx);
7216 vim_free(file_to_find);
7217 vim_findfile_cleanup(search_ctx);
7218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 if (fname == NULL)
7220 goto theend;
7221 eap->arg = fname;
7222 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007223# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007224 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 && eap->cmdidx != CMD_new)
7227 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007228 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007229# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007230 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007231# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007232 au_has_group((char_u *)"FileExplorer"))
7233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007234 // No browsing supported but we do have the file explorer:
7235 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007236 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007237 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007238 }
7239 else
7240 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02007241 fname = do_browse(0, (char_u *)(use_tab
7242 ? _("Edit File in new tab page")
7243 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007245 if (fname == NULL)
7246 goto theend;
7247 eap->arg = fname;
7248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007250 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007253 /*
7254 * Either open new tab page or split the window.
7255 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007256 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007257 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007258 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007259 : eap->addr_count == 0 ? 0
7260 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007261 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007262 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007263
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007264 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007265 if (curwin != old_curwin
7266 && win_valid(old_curwin)
7267 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007268 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007269 old_curwin->w_alt_fnum = curbuf->b_fnum;
7270 }
7271 }
7272 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7274 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007275 // Reset 'scrollbind' when editing another file, but keep it when
7276 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007277 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007278 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 else
7280 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 do_exedit(eap, old_curwin);
7282 }
7283
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007284# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007285 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288theend:
7289 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007291
7292/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007293 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007294 */
7295 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007296tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007297{
7298 exarg_T ea;
7299
Bram Moolenaara80faa82020-04-12 19:37:17 +02007300 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007301 ea.cmdidx = CMD_tabnew;
7302 ea.cmd = (char_u *)"tabn";
7303 ea.arg = (char_u *)"";
7304 ex_splitview(&ea);
7305}
7306
7307/*
7308 * :tabnext command
7309 */
7310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007311ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007312{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007313 int tab_number;
7314
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007315 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007316 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007317 switch (eap->cmdidx)
7318 {
7319 case CMD_tabfirst:
7320 case CMD_tabrewind:
7321 goto_tabpage(1);
7322 break;
7323 case CMD_tablast:
7324 goto_tabpage(9999);
7325 break;
7326 case CMD_tabprevious:
7327 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007328 if (eap->arg && *eap->arg != NUL)
7329 {
7330 char_u *p = eap->arg;
7331 char_u *p_save = p;
7332
7333 tab_number = getdigits(&p);
7334 if (p == p_save || *p_save == '-' || *p != NUL
7335 || tab_number == 0)
7336 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007337 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007338 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007339 return;
7340 }
7341 }
7342 else
7343 {
7344 if (eap->addr_count == 0)
7345 tab_number = 1;
7346 else
7347 {
7348 tab_number = eap->line2;
7349 if (tab_number < 1)
7350 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007351 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007352 return;
7353 }
7354 }
7355 }
7356 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007357 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007358 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007359 tab_number = get_tabpage_arg(eap);
7360 if (eap->errmsg == NULL)
7361 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007362 break;
7363 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007364}
7365
7366/*
7367 * :tabmove command
7368 */
7369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007370ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007371{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007372 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007373
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007374 tab_number = get_tabpage_arg(eap);
7375 if (eap->errmsg == NULL)
7376 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007377}
7378
7379/*
7380 * :tabs command: List tabs and their contents.
7381 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007383ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007384{
7385 tabpage_T *tp;
7386 win_T *wp;
7387 int tabcount = 1;
7388
7389 msg_start();
7390 msg_scroll = TRUE;
7391 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7392 {
7393 msg_putchar('\n');
7394 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007395 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007396 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007397 ui_breakcheck();
7398
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007399 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007400 wp = firstwin;
7401 else
7402 wp = tp->tp_firstwin;
7403 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7404 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007405 msg_putchar('\n');
7406 msg_putchar(wp == curwin ? '>' : ' ');
7407 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007408 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7409 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007410 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007411 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007412 else
7413 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7414 IObuff, IOSIZE, TRUE);
7415 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007416 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007417 ui_breakcheck();
7418 }
7419 }
7420}
7421
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422/*
7423 * ":mode": Set screen mode.
7424 * If no argument given, just get the screen size and redraw.
7425 */
7426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007427ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429 if (*eap->arg == NUL)
7430 shell_resized();
7431 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007432 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433}
7434
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435/*
7436 * ":resize".
7437 * set, increment or decrement current window height
7438 */
7439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007440ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
7442 int n;
7443 win_T *wp = curwin;
7444
7445 if (eap->addr_count > 0)
7446 {
7447 n = eap->line2;
7448 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7449 ;
7450 }
7451
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007452# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007456 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 {
7458 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007459 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007460 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007462 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 }
7464 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 {
7466 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007467 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007468 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007470 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 }
7472}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473
7474/*
7475 * ":find [+command] <file>" command.
7476 */
7477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007478ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479{
Colin Kennedy21570352024-03-03 16:16:47 +01007480 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007481 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007482
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007483 char_u *fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007485 char_u *file_to_find = NULL;
7486 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007488 if (*get_findfunc() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 {
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007490#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007491 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007492 eap->addr_count > 0 ? eap->line2 : 1);
7493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 }
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007495 else
7496 {
7497 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7498 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
7499 if (eap->addr_count > 0)
7500 {
7501 // Repeat finding the file "count" times. This matters when it appears
7502 // several times in the path.
7503 count = eap->line2;
7504 while (fname != NULL && --count > 0)
7505 {
7506 vim_free(fname);
7507 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7508 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
7509 }
7510 }
7511 VIM_CLEAR(file_to_find);
7512 vim_findfile_cleanup(search_ctx);
7513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007515 if (fname == NULL)
7516 return;
7517
7518 eap->arg = fname;
7519 do_exedit(eap, NULL);
7520 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521}
7522
7523/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007524 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007525 */
7526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007527ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007528{
7529 regmatch_T regmatch;
7530 char_u *p;
7531
Bram Moolenaar65088802021-03-13 21:07:21 +01007532#ifdef FEAT_EVAL
7533 if (not_in_vim9(eap) == FAIL)
7534 return;
7535#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007536 curwin->w_cursor.lnum = eap->line2;
7537 beginline(BL_SOL | BL_FIX);
7538 if (*eap->arg == '/')
7539 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007540 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007541 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007542 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007543 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007544 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007545 if (regmatch.regprog != NULL)
7546 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007547 // make a copy of the line, when searching for a mark it might be
7548 // flushed
7549 char_u *line = vim_strsave(ml_get_curline());
7550
Bram Moolenaardf177f62005-02-22 08:39:57 +00007551 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007552 if (vim_regexec(&regmatch, line, (colnr_T)0))
7553 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007554 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007555 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007556 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007557 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007558 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007559 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007560 eap->arg += STRLEN(eap->arg);
7561 }
7562 check_cursor();
7563
7564 eap->cmdidx = CMD_visual;
7565 do_exedit(eap, NULL);
7566}
7567
7568/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007569 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 */
7571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007572ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007574 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7575
Colin Kennedy21570352024-03-03 16:16:47 +01007576 // Exclude commands which keep the window's current buffer
7577 if (
7578 eap->cmdidx != CMD_badd
7579 && eap->cmdidx != CMD_balt
7580 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007581 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7582 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007583 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007584
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 do_exedit(eap, NULL);
7586}
7587
7588/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007589 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007592do_exedit(
7593 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007594 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
7596 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007598 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007600 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7601 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007602 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 /*
7604 * ":vi" command ends Ex mode.
7605 */
7606 if (exmode_active && (eap->cmdidx == CMD_visual
7607 || eap->cmdidx == CMD_view))
7608 {
7609 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007610 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007612 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007613 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007614 if (global_busy)
7615 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007616 if (eap->nextcmd != NULL)
7617 {
7618 stuffReadbuff(eap->nextcmd);
7619 eap->nextcmd = NULL;
7620 }
7621
7622 if (exmode_was != EXMODE_VIM)
7623 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007624 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007625 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007626 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007627 no_wait_return = 0;
7628 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007629 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007630 msg_scroll = 0;
7631#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007632 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007633 hold_gui_events = 0;
7634#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007635 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007636 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007637
7638 main_loop(FALSE, TRUE);
7639
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007640 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007641 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007642 no_wait_return = save_nwr;
7643 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007644#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007645 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007646#endif
7647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 }
7651
7652 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007653 || eap->cmdidx == CMD_tabnew
7654 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007655 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007657 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 setpcmark();
7659 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007660 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7661 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007663 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 || *eap->arg != NUL
7665#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007666 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667#endif
7668 )
7669 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007670 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7671 // Only ":edit" or ":script" can bring us here, others are stopped
7672 // earlier.
7673 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007674 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007675
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 n = readonlymode;
7677 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7678 readonlymode = TRUE;
7679 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007680 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7681 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007682 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7683 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7685 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007686 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7688 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7689 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007690 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007692 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007693 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007694 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7695 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007696 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007698 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (old_curwin != NULL)
7700 {
7701 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007702 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007704#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007705 cleanup_T cs;
7706
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007707 // Reset the error/interrupt/exception state here so that
7708 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007709 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007710#endif
7711#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007713#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007714 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007715
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007716#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007717 // Restore the error/interrupt/exception state if not
7718 // discarded by a new aborting error, interrupt, or
7719 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007720 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 }
7723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 }
7725 else if (readonlymode && curbuf->b_nwindows == 1)
7726 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007727 // When editing an already visited buffer, 'readonly' won't be set
7728 // but the previous value is kept. With ":view" and ":sview" we
7729 // want the file to be readonly, except when another window is
7730 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 curbuf->b_p_ro = TRUE;
7732 }
7733 readonlymode = n;
7734 }
7735 else
7736 {
7737 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007738 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 if (n != curwin->w_arg_idx_invalid)
7742 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 }
7744
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 /*
7746 * if ":split file" worked, set alternate file name in old window to new
7747 * file
7748 */
7749 if (old_curwin != NULL
7750 && *eap->arg != NUL
7751 && curwin != old_curwin
7752 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007753 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007754 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756
7757 ex_no_reprint = TRUE;
7758}
7759
7760#ifndef FEAT_GUI
7761/*
7762 * ":gui" and ":gvim" when there is no GUI.
7763 */
7764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007765ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007767 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768}
7769#endif
7770
Bram Moolenaar4f974752019-02-17 17:44:42 +01007771#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007773ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774{
7775 gui_make_tearoff(eap->arg);
7776}
7777#endif
7778
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007779#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7780 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007782ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007784# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7785 if (gui.in_use)
7786 gui_make_popup(eap->arg, eap->forceit);
7787# ifdef FEAT_TERM_POPUP_MENU
7788 else
7789# endif
7790# endif
7791# ifdef FEAT_TERM_POPUP_MENU
7792 pum_make_popup(eap->arg, eap->forceit);
7793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794}
7795#endif
7796
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007798ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
7800 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007801 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007803 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804}
7805
7806/*
7807 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7808 * offset.
7809 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007812ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007815 win_T *save_curwin = curwin;
7816 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 long topline;
7818 long y;
7819 linenr_T old_linenr = curwin->w_cursor.lnum;
7820
7821 setpcmark();
7822
7823 /*
7824 * determine max topline
7825 */
7826 if (curwin->w_p_scb)
7827 {
7828 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007829 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {
7831 if (wp->w_p_scb && wp->w_buffer)
7832 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007833 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 if (topline > y)
7835 topline = y;
7836 }
7837 }
7838 if (topline < 1)
7839 topline = 1;
7840 }
7841 else
7842 {
7843 topline = 1;
7844 }
7845
7846
7847 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007848 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007850 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 {
7852 if (curwin->w_p_scb)
7853 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007854 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 y = topline - curwin->w_topline;
7856 if (y > 0)
7857 scrollup(y, TRUE);
7858 else
7859 scrolldown(-y, TRUE);
7860 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007861 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007866 curwin = save_curwin;
7867 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 if (curwin->w_p_scb)
7869 {
7870 did_syncbind = TRUE;
7871 checkpcmark();
7872 if (old_linenr != curwin->w_cursor.lnum)
7873 {
7874 char_u ctrl_o[2];
7875
7876 ctrl_o[0] = Ctrl_O;
7877 ctrl_o[1] = 0;
7878 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7879 }
7880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881}
7882
7883
7884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007885ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007887 int i;
7888 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7889 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007891 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007893 do_bang(1, eap, FALSE, FALSE, TRUE);
7894 return;
7895 }
7896
7897 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7898 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899
7900#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007901 if (cmdmod.cmod_flags & CMOD_BROWSE)
7902 {
7903 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007905 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7906 NULL, NULL, NULL, curbuf);
7907 if (browseFile != NULL)
7908 {
7909 i = readfile(browseFile, NULL,
7910 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7911 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 }
7913 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007914 i = OK;
7915 }
7916 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007918 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007920 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 return;
7922 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007923 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 }
7925 else
7926 {
7927 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7928 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7929 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007930 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931
7932 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007933 if (i != OK)
7934 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007935#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007936 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007938 semsg(_(e_cant_open_file_str), eap->arg);
7939 }
7940 else
7941 {
7942 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007943 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007944 // Delete the empty line that remains. Historically ex does
7945 // this but vi doesn't.
7946 if (eap->line2 == 0)
7947 lnum = curbuf->b_ml.ml_line_count;
7948 else
7949 lnum = 1;
7950 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007951 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007952 ml_delete(lnum);
7953 if (curwin->w_cursor.lnum > 1
7954 && curwin->w_cursor.lnum >= lnum)
7955 --curwin->w_cursor.lnum;
7956 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007957 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007958 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007959 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 }
7961}
7962
Bram Moolenaarea408852005-06-25 22:49:46 +00007963static char_u *prev_dir = NULL;
7964
7965#if defined(EXITFREE) || defined(PROTO)
7966 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007967free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007968{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007969 VIM_CLEAR(prev_dir);
7970 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007971}
7972#endif
7973
Bram Moolenaarf4258302013-06-02 18:20:17 +02007974/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007975 * Get the previous directory for the given chdir scope.
7976 */
7977 static char_u *
7978get_prevdir(cdscope_T scope)
7979{
7980 if (scope == CDSCOPE_WINDOW)
7981 return curwin->w_prevdir;
7982 else if (scope == CDSCOPE_TABPAGE)
7983 return curtab->tp_prevdir;
7984 return prev_dir;
7985}
7986
7987/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007988 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007989 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7990 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007991 */
7992 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007993post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007994{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007995 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007996 // Clear tab local directory for both :cd and :tcd
7997 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007998 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007999 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008000 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008001 char_u *pdir = get_prevdir(scope);
8002
8003 // If still in the global directory, need to remember current
8004 // directory as the global directory.
8005 if (globaldir == NULL && pdir != NULL)
8006 globaldir = vim_strsave(pdir);
8007
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008008 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02008009 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008010 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008011 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008012 curtab->tp_localdir = vim_strsave(NameBuff);
8013 else
8014 curwin->w_localdir = vim_strsave(NameBuff);
8015 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02008016 }
8017 else
8018 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008019 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01008020 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008021 }
8022
zeertzjq64be6aa2021-11-19 11:59:08 +00008023 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008024 shorten_fnames(TRUE);
8025}
8026
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008027/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008028 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
8029 */
8030 void
8031trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
8032{
8033#ifdef FEAT_EVAL
8034 dict_T *v_event;
8035 save_v_event_T save_v_event;
8036
8037 v_event = get_v_event(&save_v_event);
8038 (void)dict_add_string(v_event, "directory", new_dir);
8039 dict_set_items_ro(v_event);
8040#endif
8041 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
8042#ifdef FEAT_EVAL
8043 restore_v_event(v_event, &save_v_event);
8044#endif
8045}
8046
8047/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008048 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02008049 * chdir() function.
8050 * scope == CDSCOPE_WINDOW: changes the window-local directory
8051 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
8052 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008053 * Returns TRUE if the directory is successfully changed.
8054 */
8055 int
8056changedir_func(
8057 char_u *new_dir,
8058 int forceit,
8059 cdscope_T scope)
8060{
Bram Moolenaar002bc792020-06-05 22:33:42 +02008061 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008062 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008063 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00008064 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008065 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008066
Bram Moolenaar7cc96922020-01-31 22:41:38 +01008067 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008068 return FALSE;
8069
8070 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
8071 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00008072 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008073 return FALSE;
8074 }
8075
8076 // ":cd -": Change to previous directory
8077 if (STRCMP(new_dir, "-") == 0)
8078 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008079 pdir = get_prevdir(scope);
8080 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008081 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008082 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008083 return FALSE;
8084 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02008085 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008086 }
8087
8088 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008089 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02008090 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008091 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02008092 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008093
Bakudankun29f3a452021-12-11 12:28:08 +00008094 // For UNIX ":cd" means: go to home directory.
8095 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008096#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008097 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00008098#else
8099 if (*new_dir == NUL && p_cdh)
8100#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008101 {
8102 // use NameBuff for home directory name
8103# ifdef VMS
8104 char_u *p;
8105
8106 p = mch_getenv((char_u *)"SYS$LOGIN");
8107 if (p == NULL || *p == NUL) // empty is the same as not set
8108 NameBuff[0] = NUL;
8109 else
8110 vim_strncpy(NameBuff, p, MAXPATHL - 1);
8111# else
8112 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8113# endif
8114 new_dir = NameBuff;
8115 }
zeertzjqf38aad82021-12-30 13:45:57 +00008116 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008117 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
8118 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00008119 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008120 if (scope == CDSCOPE_WINDOW)
8121 acmd_fname = (char_u *)"window";
8122 else if (scope == CDSCOPE_TABPAGE)
8123 acmd_fname = (char_u *)"tabpage";
8124 else
8125 acmd_fname = (char_u *)"global";
8126 trigger_DirChangedPre(acmd_fname, new_dir);
8127
8128 if (vim_chdir(new_dir))
8129 {
8130 emsg(_(e_command_failed));
8131 vim_free(pdir);
8132 return FALSE;
8133 }
Richard Doty3d0abad2021-12-29 14:39:08 +00008134 }
zeertzjq73257142022-02-02 13:16:37 +00008135
8136 if (scope == CDSCOPE_WINDOW)
8137 pp = &curwin->w_prevdir;
8138 else if (scope == CDSCOPE_TABPAGE)
8139 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008140 else
zeertzjq73257142022-02-02 13:16:37 +00008141 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008142 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00008143 *pp = pdir;
8144
8145 post_chdir(scope);
8146
8147 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008148 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008149 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00008150 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008151}
Bram Moolenaarea408852005-06-25 22:49:46 +00008152
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008154 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008156 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008157ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01008159 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160
8161 new_dir = eap->arg;
8162#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00008163 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
8164 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008166 ex_pwd(NULL);
8167 return;
8168 }
8169#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008170
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008171 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008172
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008173 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8174 scope = CDSCOPE_WINDOW;
8175 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
8176 scope = CDSCOPE_TABPAGE;
8177
8178 if (changedir_func(new_dir, eap->forceit, scope))
8179 {
8180 // Echo the new current directory if the command was typed.
8181 if (KeyTyped || p_verbose >= 5)
8182 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 }
8184}
8185
8186/*
8187 * ":pwd".
8188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008190ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191{
8192 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8193 {
8194#ifdef BACKSLASH_IN_FILENAME
8195 slash_adjust(NameBuff);
8196#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02008197 if (p_verbose > 0)
8198 {
8199 char *context = "global";
8200
Bram Moolenaar05268152021-11-18 18:53:45 +00008201 if (last_chdir_reason != NULL)
8202 context = last_chdir_reason;
8203 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02008204 context = "window";
8205 else if (curtab->tp_localdir != NULL)
8206 context = "tabpage";
8207 smsg("[%s] %s", context, (char *)NameBuff);
8208 }
8209 else
8210 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 }
8212 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00008213 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214}
8215
8216/*
8217 * ":=".
8218 */
8219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008220ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008222 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008223 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224}
8225
8226 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008227ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008229 int n;
8230 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
8232 if (cursor_valid())
8233 {
8234 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8235 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00008236 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008238
8239 len = eap->line2;
8240 switch (*eap->arg)
8241 {
8242 case 'm': break;
8243 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008244 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008245 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008246
8247 // Hide the cursor if invoked with !
8248 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249}
8250
8251/*
8252 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008253 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 */
8255 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008256do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
Bram Moolenaar52953192019-08-16 10:27:13 +02008258 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008259 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008260# ifdef ELAPSED_FUNC
8261 elapsed_T start_tv;
8262
8263 // Remember at what time we started, so that we know how much longer we
8264 // should wait after waiting for a bit.
8265 ELAPSED_INIT(start_tv);
8266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008268 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008269 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008270 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008271 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008272
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008273 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008274 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008276 wait_now = msec - done > 1000L ? 1000L : msec - done;
8277#ifdef FEAT_TIMERS
8278 {
8279 long due_time = check_due_timer();
8280
8281 if (due_time > 0 && due_time < wait_now)
8282 wait_now = due_time;
8283 }
8284#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008285#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008286 if (has_any_channel() && wait_now > 20L)
8287 wait_now = 20L;
8288#endif
8289#ifdef FEAT_SOUND
8290 if (has_any_sound_callback() && wait_now > 20L)
8291 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008292#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008293 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008294
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008295#ifdef FEAT_JOB_CHANNEL
8296 if (has_any_channel())
8297 ui_breakcheck_force(TRUE);
8298 else
8299#endif
8300 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008301#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008302 // Process the netbeans and clientserver messages that may have been
8303 // received in the call to ui_breakcheck() when the GUI is in use. This
8304 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008305 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008306#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008307
8308# ifdef ELAPSED_FUNC
8309 // actual time passed
8310 done = ELAPSED_FUNC(start_tv);
8311# else
8312 // guestimate time passed (will actually be more)
8313 done += wait_now;
8314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008316
8317 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8318 // input buffer, otherwise a following call to input() fails.
8319 if (got_int)
8320 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008321
8322 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008323 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324}
8325
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326/*
8327 * ":winsize" command (obsolete).
8328 */
8329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008330ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
8332 int w, h;
8333 char_u *arg = eap->arg;
8334 char_u *p;
8335
Keith Thompson184f71c2024-01-04 21:19:04 +01008336 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008337 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008338 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008339 return;
8340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 w = getdigits(&arg);
8342 arg = skipwhite(arg);
8343 p = arg;
8344 h = getdigits(&arg);
8345 if (*p != NUL && *arg == NUL)
8346 set_shellsize(w, h, TRUE);
8347 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008348 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349}
8350
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008352ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353{
8354 int xchar = NUL;
8355 char_u *p;
8356
8357 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8358 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008359 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 if (eap->arg[1] == NUL)
8361 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008362 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 return;
8364 }
8365 xchar = eap->arg[1];
8366 p = eap->arg + 2;
8367 }
8368 else
8369 p = eap->arg + 1;
8370
Bram Moolenaar63b91732021-08-05 20:40:03 +02008371 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008373 if (*p != NUL && *p != (
8374#ifdef FEAT_EVAL
8375 in_vim9script() ? '#' :
8376#endif
8377 '"')
8378 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008379 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008380 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008382 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008383 postponed_split_flags = cmdmod.cmod_split;
8384 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8386 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008387 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 }
8389}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390
Bram Moolenaar843ee412004-06-30 16:16:41 +00008391#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392/*
8393 * ":winpos".
8394 */
8395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008396ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397{
8398 int x, y;
8399 char_u *arg = eap->arg;
8400 char_u *p;
8401
8402 if (*arg == NUL)
8403 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008404# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008405# ifdef VIMDLL
8406 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8407 mch_get_winpos(&x, &y) != FAIL)
8408# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008410# else
8411 if (mch_get_winpos(&x, &y) != FAIL)
8412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 {
8414 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008415 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 }
8417 else
8418# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008419 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 }
8421 else
8422 {
8423 x = getdigits(&arg);
8424 arg = skipwhite(arg);
8425 p = arg;
8426 y = getdigits(&arg);
8427 if (*p == NUL || *arg != NUL)
8428 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008429 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 return;
8431 }
8432# ifdef FEAT_GUI
8433 if (gui.in_use)
8434 gui_mch_set_winpos(x, y);
8435 else if (gui.starting)
8436 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008437 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 gui_win_x = x;
8439 gui_win_y = y;
8440 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008441# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 else
8443# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008444# endif
8445# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008446 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447# endif
8448# ifdef HAVE_TGETENT
8449 if (*T_CWP)
8450 term_set_winpos(x, y);
8451# endif
8452 }
8453}
8454#endif
8455
8456/*
8457 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8458 */
8459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008460ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
8462 oparg_T oa;
8463
8464 clear_oparg(&oa);
8465 oa.regname = eap->regname;
8466 oa.start.lnum = eap->line1;
8467 oa.end.lnum = eap->line2;
8468 oa.line_count = eap->line2 - eap->line1 + 1;
8469 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008471 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 {
8473 setpcmark();
8474 curwin->w_cursor.lnum = eap->line1;
8475 beginline(BL_SOL | BL_FIX);
8476 }
8477
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008478 if (VIsual_active)
8479 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008480
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 switch (eap->cmdidx)
8482 {
8483 case CMD_delete:
8484 oa.op_type = OP_DELETE;
8485 op_delete(&oa);
8486 break;
8487
8488 case CMD_yank:
8489 oa.op_type = OP_YANK;
8490 (void)op_yank(&oa, FALSE, TRUE);
8491 break;
8492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008493 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008494 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008496 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8497#else
8498 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008500 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 oa.op_type = OP_RSHIFT;
8502 else
8503 oa.op_type = OP_LSHIFT;
8504 op_shift(&oa, FALSE, eap->amount);
8505 break;
8506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008508 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509}
8510
8511/*
8512 * ":put".
8513 */
8514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008515ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008517 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 if (eap->line2 == 0)
8519 {
8520 eap->line2 = 1;
8521 eap->forceit = TRUE;
8522 }
8523 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008524 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008525 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008526 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527}
8528
8529/*
64-bitmane08f10a2025-03-18 22:14:34 +01008530 * ":iput".
8531 */
8532 static void
8533ex_iput(exarg_T *eap)
8534{
8535 // ":0iput" works like ":1iput!".
8536 if (eap->line2 == 0)
8537 {
8538 eap->line2 = 1;
8539 eap->forceit = TRUE;
8540 }
8541 curwin->w_cursor.lnum = eap->line2;
8542 check_cursor_col();
8543 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
8544 PUT_LINE|PUT_CURSLINE
8545 |PUT_FIXINDENT);
8546}
8547
8548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 * Handle ":copy" and ":move".
8550 */
8551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008552ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553{
8554 long n;
8555
Bram Moolenaar491799b2020-08-01 19:23:43 +02008556#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008557 if (not_in_vim9(eap) == FAIL)
8558 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008559#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008560 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008561 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 {
8563 eap->nextcmd = NULL;
8564 return;
8565 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008566 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567
8568 /*
8569 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8570 */
8571 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8572 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008573 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 return;
8575 }
8576
8577 if (eap->cmdidx == CMD_move)
8578 {
8579 if (do_move(eap->line1, eap->line2, n) == FAIL)
8580 return;
8581 }
8582 else
8583 ex_copy(eap->line1, eap->line2, n);
8584 u_clearline();
8585 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008586 ex_may_print(eap);
8587}
8588
8589/*
8590 * Print the current line if flags were given to the Ex command.
8591 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008592 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008593ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008594{
8595 if (eap->flags != 0)
8596 {
8597 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8598 (eap->flags & EXFLAG_LIST));
8599 ex_no_reprint = TRUE;
8600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601}
8602
8603/*
8604 * ":smagic" and ":snomagic".
8605 */
8606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008607ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008609 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008611 magic_overruled = eap->cmdidx == CMD_smagic
8612 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008613 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008614 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615}
8616
8617/*
8618 * ":join".
8619 */
8620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008621ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622{
8623 curwin->w_cursor.lnum = eap->line1;
8624 if (eap->line1 == eap->line2)
8625 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008626 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 return;
8628 if (eap->line2 == curbuf->b_ml.ml_line_count)
8629 {
8630 beep_flush();
8631 return;
8632 }
8633 ++eap->line2;
8634 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008635 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008637 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638}
8639
8640/*
8641 * ":[addr]@r" or ":[addr]*r": execute register
8642 */
8643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008644ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008647 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648
8649 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008650 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651
8652#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008653 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654#endif
8655
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008656 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 c = *eap->arg;
8658 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8659 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008660 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008661 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8662 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008663 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008665 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008668 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008670 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008672 /*
8673 * Execute from the typeahead buffer.
8674 * Continue until the stuff buffer is empty and all added characters
8675 * have been consumed.
8676 */
8677 while (!stuff_empty() || typebuf.tb_len > prev_len)
8678 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8679
8680 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681}
8682
8683/*
8684 * ":!".
8685 */
8686 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008687ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688{
8689 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8690}
8691
8692/*
8693 * ":undo".
8694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008696ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008698 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008699 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008700 else
8701 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702}
8703
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008704#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008706ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008707{
8708 char_u hash[UNDO_HASH_SIZE];
8709
8710 u_compute_hash(hash);
8711 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8712}
8713
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008715ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008716{
8717 char_u hash[UNDO_HASH_SIZE];
8718
8719 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008720 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008721}
8722#endif
8723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724/*
8725 * ":redo".
8726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008728ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729{
8730 u_redo(1);
8731}
8732
8733/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008734 * ":earlier" and ":later".
8735 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008737ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008738{
8739 long count = 0;
8740 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008741 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008742 char_u *p = eap->arg;
8743
8744 if (*p == NUL)
8745 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008746 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008747 {
8748 count = getdigits(&p);
8749 switch (*p)
8750 {
8751 case 's': ++p; sec = TRUE; break;
8752 case 'm': ++p; sec = TRUE; count *= 60; break;
8753 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008754 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8755 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008756 }
8757 }
8758
8759 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008760 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008761 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008762 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8763 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008764}
8765
8766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 * ":redir": start/stop redirection.
8768 */
8769 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008770ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771{
8772 char *mode;
8773 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008774 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775
Bram Moolenaarba768492016-07-08 15:32:54 +02008776#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008777 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008778 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008779 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008780 return;
8781 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008782#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008783
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 if (STRICMP(eap->arg, "END") == 0)
8785 close_redir();
8786 else
8787 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008788 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008790 ++arg;
8791 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008793 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 mode = "a";
8795 }
8796 else
8797 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008798 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799
8800 close_redir();
8801
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008802 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008803 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 if (fname == NULL)
8805 return;
8806#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008807 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 {
8809 char_u *browseFile;
8810
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008811 browseFile = do_browse(BROWSE_SAVE,
8812 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008813 fname, NULL, NULL,
8814 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008816 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 vim_free(fname);
8818 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008819 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 }
8821#endif
8822
8823 redir_fd = open_exfile(fname, eap->forceit, mode);
8824 vim_free(fname);
8825 }
8826#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008827 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008829 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008831 ++arg;
8832 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008834 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008835 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008837 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008839 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008840 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008841 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008842 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008844 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008845 if (*arg == '>')
8846 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008847 // Make register empty when not using @A-@Z and the
8848 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008849 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008850 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008852 }
8853 if (*arg != NUL)
8854 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008855 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008856 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008857 }
8858 }
8859 else if (*arg == '=' && arg[1] == '>')
8860 {
8861 int append;
8862
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008863 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008864 close_redir();
8865 arg += 2;
8866
8867 if (*arg == '>')
8868 {
8869 ++arg;
8870 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 }
8872 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008873 append = FALSE;
8874
8875 if (var_redir_start(skipwhite(arg), append) == OK)
8876 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 }
8878#endif
8879
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008880 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008883 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008885
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008886 // Make sure redirection is not off. Can happen for cmdline completion
8887 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008888 if (redir_fd != NULL
8889#ifdef FEAT_EVAL
8890 || redir_reg || redir_vname
8891#endif
8892 )
8893 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894}
8895
8896/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008897 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008899 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008900ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008902 redraw_cmd(eap->forceit);
8903}
8904
8905/*
8906 * ":redraw": force redraw, with clear if "clear" is TRUE.
8907 */
8908 void
8909redraw_cmd(int clear)
8910{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008911 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008913
8914 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008916
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008917 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008919 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 if (need_maketitle)
8921 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008922#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8923# ifdef VIMDLL
8924 if (!gui.in_use)
8925# endif
8926 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008927#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008928 RedrawingDisabled = save_RedrawingDisabled;
8929 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930
Bram Moolenaar5017c662022-04-07 18:06:08 +01008931 // After drawing the statusline screen_attr may still be set.
8932 screen_stop_highlight();
8933
Bram Moolenaara2a89732022-08-31 14:46:18 +01008934 // Reset msg_didout, so that a message that's there is overwritten.
8935 msg_didout = FALSE;
8936 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008938 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008939 need_wait_return = FALSE;
8940
Bram Moolenaara653e532022-04-19 11:38:24 +01008941 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008942 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008943 redrawcmdline();
8944
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 out_flush();
8946}
8947
8948/*
8949 * ":redrawstatus": force redraw of status line(s)
8950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008952ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 if (eap->forceit)
8955 status_redraw_all();
8956 else
8957 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008958 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008959 return; // redraw later
8960
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008961 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008962 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008963
8964 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008965 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008966
zeertzjq320d9102022-09-20 17:12:13 +01008967 if (State & MODE_CMDLINE)
8968 redraw_statuslines();
8969 else
8970 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008971 RedrawingDisabled = save_RedrawingDisabled;
8972 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974}
8975
Bram Moolenaare12bab32019-01-08 22:02:56 +01008976/*
8977 * ":redrawtabline": force redraw of the tabline
8978 */
8979 static void
8980ex_redrawtabline(exarg_T *eap UNUSED)
8981{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008982 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008983 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008984
8985 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008986 p_lz = FALSE;
8987
8988 draw_tabline();
8989
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008990 RedrawingDisabled = save_RedrawingDisabled;
8991 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008992 out_flush();
8993}
8994
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008996close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
8998 if (redir_fd != NULL)
8999 {
9000 fclose(redir_fd);
9001 redir_fd = NULL;
9002 }
9003#ifdef FEAT_EVAL
9004 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009005 if (redir_vname)
9006 {
9007 var_redir_stop();
9008 redir_vname = 0;
9009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010#endif
9011}
9012
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02009013#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009014 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02009015vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009016{
9017 if (vim_mkdir(name, prot) != 0)
9018 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00009019 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009020 return FAIL;
9021 }
9022 return OK;
9023}
9024#endif
9025
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026/*
9027 * Open a file for writing for an Ex command, with some checks.
9028 * Return file descriptor, or NULL on failure.
9029 */
9030 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009031open_exfile(
9032 char_u *fname,
9033 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009034 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035{
9036 FILE *fd;
9037
9038#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009039 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 if (mch_isdir(fname))
9041 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01009042 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 return NULL;
9044 }
9045#endif
9046 if (!forceit && *mode != 'a' && vim_fexists(fname))
9047 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00009048 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 return NULL;
9050 }
9051
9052 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00009053 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054
9055 return fd;
9056}
9057
9058/*
9059 * ":mark" and ":k".
9060 */
9061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009062ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
9064 pos_T pos;
9065
Bram Moolenaar10b94212021-02-19 21:42:57 +01009066#ifdef FEAT_EVAL
9067 if (not_in_vim9(eap) == FAIL)
9068 return;
9069#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009070 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009072 emsg(_(e_argument_required));
9073 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009075
9076 if (eap->arg[1] != NUL) // more than one character?
9077 {
9078 semsg(_(e_trailing_characters_str), eap->arg);
9079 return;
9080 }
9081
9082 pos = curwin->w_cursor; // save curwin->w_cursor
9083 curwin->w_cursor.lnum = eap->line2;
9084 beginline(BL_WHITE | BL_FIX);
9085 if (setmark(*eap->arg) == FAIL) // set mark
9086 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
9087 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088}
9089
9090/*
9091 * Update w_topline, w_leftcol and the cursor position.
9092 */
9093 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009094update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009096 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 update_topline();
9098 if (!curwin->w_p_wrap)
9099 validate_cursor();
9100 update_curswant();
9101}
9102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009104 * Save the current State and go to Normal mode.
9105 * Return TRUE if the typeahead could be saved.
9106 */
9107 int
9108save_current_state(save_state_T *sst)
9109{
9110 sst->save_msg_scroll = msg_scroll;
9111 sst->save_restart_edit = restart_edit;
9112 sst->save_msg_didout = msg_didout;
9113 sst->save_State = State;
9114 sst->save_insertmode = p_im;
9115 sst->save_finish_op = finish_op;
9116 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009117 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009118 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009119
Bram Moolenaar4324d872020-12-01 20:12:24 +01009120 msg_scroll = FALSE; // no msg scrolling in Normal mode
9121 restart_edit = 0; // don't go to Insert mode
9122 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01009123
Bram Moolenaara452b802020-12-01 21:47:59 +01009124 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01009125 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009126
9127 /*
9128 * Save the current typeahead. This is required to allow using ":normal"
9129 * from an event handler and makes sure we don't hang when the argument
9130 * ends with half a command.
9131 */
9132 save_typeahead(&sst->tabuf);
9133 return sst->tabuf.typebuf_valid;
9134}
9135
9136 void
9137restore_current_state(save_state_T *sst)
9138{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009139 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02009140 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009141
9142 msg_scroll = sst->save_msg_scroll;
9143 restart_edit = sst->save_restart_edit;
9144 p_im = sst->save_insertmode;
9145 finish_op = sst->save_finish_op;
9146 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009147 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009148 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009149 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01009150 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009151
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009152 // Restore the state (needed when called from a function executed for
9153 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009154 State = sst->save_State;
9155#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009156 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009157#endif
9158}
9159
9160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 * ":normal[!] {commands}": Execute normal mode commands.
9162 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009163 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009164ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009166 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 char_u *arg = NULL;
9168 int l;
9169 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009171 if (ex_normal_lock > 0)
9172 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00009173 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009174 return;
9175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 if (ex_normal_busy >= p_mmd)
9177 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009178 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 return;
9180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 /*
9183 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9184 * this for the K_SPECIAL leading byte, otherwise special keys will not
9185 * work.
9186 */
9187 if (has_mbyte)
9188 {
9189 int len = 0;
9190
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009191 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 for (p = eap->arg; *p != NUL; ++p)
9193 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009194#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009195 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01009197#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009198 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009199 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009200#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 )
9204 len += 2;
9205 }
9206 if (len > 0)
9207 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009208 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 if (arg != NULL)
9210 {
9211 len = 0;
9212 for (p = eap->arg; *p != NUL; ++p)
9213 {
9214 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01009215#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 if (*p == CSI)
9217 {
9218 arg[len++] = KS_EXTRA;
9219 arg[len++] = (int)KE_CSI;
9220 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009221#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009222 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 {
9224 arg[len++] = *++p;
9225 if (*p == K_SPECIAL)
9226 {
9227 arg[len++] = KS_SPECIAL;
9228 arg[len++] = KE_FILLER;
9229 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009230#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 else if (*p == CSI)
9232 {
9233 arg[len++] = KS_EXTRA;
9234 arg[len++] = (int)KE_CSI;
9235 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 }
9238 arg[len] = NUL;
9239 }
9240 }
9241 }
9242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009244 ++ex_normal_busy;
9245 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 {
9247 /*
9248 * Repeat the :normal command for each line in the range. When no
9249 * range given, execute it just once, without positioning the cursor
9250 * first.
9251 */
9252 do
9253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 if (eap->addr_count != 0)
9255 {
9256 curwin->w_cursor.lnum = eap->line1++;
9257 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02009258 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 }
9260
Bram Moolenaar13505972019-01-24 15:04:48 +01009261 exec_normal_cmd(arg != NULL
9262 ? arg
9263 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 }
9265 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9266 }
9267
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009268 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 update_topline_cursor();
9270
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009271 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009273 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009274#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009275 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009276#endif
9277
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279}
9280
9281/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009282 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 */
9284 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009285ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009287 if (eap->forceit)
9288 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009289 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009290 if (!curwin->w_cursor.lnum)
9291 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009292 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009293 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009294#ifdef FEAT_TERMINAL
9295 // Ignore this when running in an active terminal.
9296 if (term_job_running(curbuf->b_term))
9297 return;
9298#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009299
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009300 // Ignore the command when already in Insert mode. Inserting an
9301 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009302 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009303 return;
9304
Bram Moolenaar64969662005-12-14 21:59:55 +00009305 if (eap->cmdidx == CMD_startinsert)
9306 restart_edit = 'a';
9307 else if (eap->cmdidx == CMD_startreplace)
9308 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009310 restart_edit = 'V';
9311
9312 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009314 if (eap->cmdidx == CMD_startinsert)
9315 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009316 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009318
9319 if (VIsual_active)
9320 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321}
9322
9323/*
9324 * ":stopinsert"
9325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009327ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328{
9329 restart_edit = 0;
9330 stop_insert_mode = TRUE;
Christian Brabandtcf665cc2025-04-12 18:09:28 +02009331#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
9332 // when called from remote_expr in insert mode, make sure insert mode is
9333 // ended by adding K_NOP to the typeahead buffer
9334 if (vgetc_busy)
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02009335 ins_char_typebuf(K_NOP, 0);
Christian Brabandtcf665cc2025-04-12 18:09:28 +02009336#endif
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009337 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009340/*
9341 * Execute normal mode command "cmd".
9342 * "remap" can be REMAP_NONE or REMAP_YES.
9343 */
9344 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009345exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009346{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009347 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009348 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009349 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009350}
Bram Moolenaar25281632016-01-21 23:32:32 +01009351
Bram Moolenaar25281632016-01-21 23:32:32 +01009352/*
9353 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009354 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009355 */
9356 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009357exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009358{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009359 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009360 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009361
Bram Moolenaar905dd902019-04-07 14:21:47 +02009362 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9363 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009364 clear_oparg(&oa);
9365 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009366 while ((!stuff_empty()
9367 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009368 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009369 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009370 {
9371 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009372#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009373 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009374 && oa.op_type == OP_NOP && oa.regname == NUL
9375 && !VIsual_active)
9376 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009377 // If terminal_loop() returns OK we got a key that is handled
9378 // in Normal model. With FAIL we first need to position the
9379 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009380 if (terminal_loop(TRUE) == OK)
9381 normal_cmd(&oa, TRUE);
9382 }
9383 else
9384#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009385 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009386 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009387 }
9388}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009389
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390#ifdef FEAT_FIND_ID
9391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009392ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
9394 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9395 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009396 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397}
9398
Bram Moolenaar4033c552017-09-16 20:54:51 +02009399#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400/*
9401 * ":psearch"
9402 */
9403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009404ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405{
9406 g_do_tagpreview = p_pvh;
9407 ex_findpat(eap);
9408 g_do_tagpreview = 0;
9409}
9410#endif
9411
9412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009413ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414{
9415 int whole = TRUE;
9416 long n;
9417 char_u *p;
9418 int action;
9419
9420 switch (cmdnames[eap->cmdidx].cmd_name[2])
9421 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009422 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9424 action = ACTION_GOTO;
9425 else
9426 action = ACTION_SHOW;
9427 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009428 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 action = ACTION_SHOW_ALL;
9430 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009431 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 action = ACTION_GOTO;
9433 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009434 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 action = ACTION_SPLIT;
9436 break;
9437 }
9438
9439 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009440 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 {
9442 n = getdigits(&eap->arg);
9443 eap->arg = skipwhite(eap->arg);
9444 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009445 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
9447 whole = FALSE;
9448 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009449 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 if (*p)
9451 {
9452 *p++ = NUL;
9453 p = skipwhite(p);
9454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009455 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009456 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009457 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009459 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 }
9461 }
9462 if (!eap->skip)
9463 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9464 whole, !eap->forceit,
9465 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009466 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467}
9468#endif
9469
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470
Bram Moolenaar4033c552017-09-16 20:54:51 +02009471#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472/*
9473 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9474 */
9475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009476ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009478 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9480}
9481
9482/*
9483 * ":pedit"
9484 */
9485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009486ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487{
9488 win_T *curwin_save = curwin;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009489 prepare_preview_window();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009491 // Edit the file.
9492 do_exedit(eap, NULL);
9493
9494 back_to_current_window(curwin_save);
9495}
9496
9497/*
9498 * ":pbuffer"
9499 */
9500 static void
9501ex_pbuffer(exarg_T *eap)
9502{
9503 win_T *curwin_save = curwin;
9504 prepare_preview_window();
9505
9506 // Go to the buffer.
9507 do_exbuffer(eap);
9508
9509 back_to_current_window(curwin_save);
9510}
9511
9512 static void
9513prepare_preview_window(void)
9514{
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009515 if (ERROR_IF_ANY_POPUP_WINDOW)
9516 return;
9517
Bram Moolenaar026587b2019-08-17 15:08:00 +02009518 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009520 prepare_tagpreview(TRUE, TRUE, FALSE);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009521}
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009522
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009523 static void
9524back_to_current_window(win_T *curwin_save)
9525{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 if (curwin != curwin_save && win_valid(curwin_save))
9527 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009528 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009530 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 win_enter(curwin_save, TRUE);
9532 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009533# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009534 else if (WIN_IS_POPUP(curwin))
9535 {
9536 // can't keep focus in popup window
9537 win_enter(firstwin, TRUE);
9538 }
9539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 g_do_tagpreview = 0;
9541}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543
9544/*
9545 * ":stag", ":stselect" and ":stjump".
9546 */
9547 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009548ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009551 postponed_split_flags = cmdmod.cmod_split;
9552 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9554 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009555 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557
9558/*
9559 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9560 */
9561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009562ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9565}
9566
9567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009568ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569{
9570 int cmd;
9571
9572 switch (name[1])
9573 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009574 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009576 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009578 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009580 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009582 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009584 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009586 case 'f': // ":tfirst"
9587 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009589 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009591 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009593 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009595 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 return;
9597 }
9598#endif
9599 cmd = DT_TAG;
9600 break;
9601 }
9602
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009603 if (name[0] == 'l')
9604 {
9605#ifndef FEAT_QUICKFIX
9606 ex_ni(eap);
9607 return;
9608#else
9609 cmd = DT_LTAG;
9610#endif
9611 }
9612
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9614 eap->forceit, TRUE);
9615}
9616
John Marriotted908f72024-04-18 22:46:56 +02009617enum {
9618 SPEC_PERC = 0,
9619 SPEC_HASH,
9620 SPEC_CWORD, // cursor word
9621 SPEC_CCWORD, // cursor WORD
9622 SPEC_CEXPR, // expr under cursor
9623 SPEC_CFILE, // cursor path name
9624 SPEC_SFILE, // ":so" file name
9625 SPEC_SLNUM, // ":so" file line number
9626 SPEC_STACK, // call stack
9627 SPEC_SCRIPT, // script file name
9628 SPEC_AFILE, // autocommand file name
9629 SPEC_ABUF, // autocommand buffer number
9630 SPEC_AMATCH, // autocommand match name
9631 SPEC_SFLNUM, // script file line number
9632 SPEC_SID // script ID: <SNR>123_
9633#ifdef FEAT_CLIENTSERVER
9634 , SPEC_CLIENT
9635#endif
9636};
9637
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009639 * Check "str" for starting with a special cmdline variable.
9640 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9641 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9642 */
9643 int
Mike Williams51024bb2024-05-30 07:46:30 +02009644find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009645{
John Marriotted908f72024-04-18 22:46:56 +02009646 // must be sorted by the 'value' field because it is used by bsearch()!
9647 static keyvalue_T spec_str_tab[] = {
9648 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9649 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9650 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9651 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9652 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9653 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9654 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009655#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009656 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009657#endif
John Marriotted908f72024-04-18 22:46:56 +02009658 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9659 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9660 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9661 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9662 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9663 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009664 };
John Marriotted908f72024-04-18 22:46:56 +02009665 keyvalue_T target;
9666 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009667
John Marriotted908f72024-04-18 22:46:56 +02009668 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009669 {
John Marriotted908f72024-04-18 22:46:56 +02009670 case '%':
9671 *usedlen = 1;
9672 return SPEC_PERC;
9673
9674 case '#':
9675 *usedlen = 1;
9676 return SPEC_HASH;
9677
9678 case '<':
9679 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +01009680 target.value.string = src + 1; // skip over '<'
9681 target.value.length = 0; // not used, see cmp_keyvalue_value_n()
John Marriotted908f72024-04-18 22:46:56 +02009682
John Marriott8d4477e2024-11-02 15:59:01 +01009683 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab,
9684 ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]),
9685 cmp_keyvalue_value_n);
John Marriotted908f72024-04-18 22:46:56 +02009686 if (entry == NULL)
9687 return -1;
9688
John Marriott8d4477e2024-11-02 15:59:01 +01009689 *usedlen = entry->value.length + 1;
John Marriotted908f72024-04-18 22:46:56 +02009690 return entry->key;
9691
9692 default:
9693 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009694 }
John Marriotted908f72024-04-18 22:46:56 +02009695
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009696 return -1;
9697}
9698
9699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 * Evaluate cmdline variables.
9701 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009702 * change "%" to curbuf->b_ffname
9703 * "#" to curwin->w_alt_fnum
9704 * "%%" to curwin->w_alt_fnum in Vim9 script
9705 * "<cword>" to word under the cursor
9706 * "<cWORD>" to WORD under the cursor
9707 * "<cexpr>" to C-expression under the cursor
9708 * "<cfile>" to path name under the cursor
9709 * "<sfile>" to sourced file name
9710 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009711 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009712 * "<slnum>" to sourced file line number
9713 * "<afile>" to file name for autocommand
9714 * "<abuf>" to buffer number for autocommand
9715 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 *
9717 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9718 * "" for error without a message) and NULL is returned.
9719 * Returns an allocated string if a valid match was found.
9720 * Returns NULL if no match was found. "usedlen" then still contains the
9721 * number of characters to skip.
9722 */
9723 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009724eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009725 char_u *src, // pointer into commandline
9726 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009727 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009728 linenr_T *lnump, // line number for :e command, or NULL
9729 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009730 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009731 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009732 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733{
9734 int i;
9735 char_u *s;
9736 char_u *result;
9737 char_u *resultbuf = NULL;
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +01009738 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009740 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009742 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745
9746 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009747 if (escaped != NULL)
9748 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749
9750 /*
9751 * Check if there is something to do.
9752 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009753 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009754 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 {
9756 *usedlen = 1;
9757 return NULL;
9758 }
9759
9760 /*
9761 * Skip when preceded with a backslash "\%" and "\#".
9762 * Note: In "\\%" the % is also not recognized!
9763 */
9764 if (src > srcstart && src[-1] == '\\')
9765 {
9766 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009767 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 return NULL;
9769 }
9770
9771 /*
9772 * word or WORD under cursor
9773 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009774 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9775 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009777 resultlen = find_ident_under_cursor(&result,
9778 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9779 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9780 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 if (resultlen == 0)
9782 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009783 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 return NULL;
9785 }
9786 }
9787
9788 /*
9789 * '#': Alternate file name
9790 * '%': Current file name
9791 * File name under the cursor
9792 * File name for autocommand
9793 * and following modifiers
9794 */
9795 else
9796 {
Mike Williams51024bb2024-05-30 07:46:30 +02009797 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009798
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 switch (spec_idx)
9800 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009801 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009802#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009803 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009806 // '%': current file
9807 if (curbuf->b_fname == NULL)
9808 {
9809 result = (char_u *)"";
9810 valid = 0; // Must have ":p:h" to be valid
9811 }
9812 else
9813 {
9814 result = curbuf->b_fname;
9815 tilde_file = STRCMP(result, "~") == 0;
9816 }
9817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009819#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009820 // "%%" alternate file
9821 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009822#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009823 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009824 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009825 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009827 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 result = arg_all();
9829 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009830 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009831 if (escaped != NULL)
9832 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 break;
9835 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009836 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009837 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009838 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009840 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009841 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009842 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009843 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009845 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009847 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009849 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009850 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009851 return NULL;
9852 }
9853#ifdef FEAT_EVAL
9854 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9855 (long)i);
9856 if (result == NULL)
9857 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009858 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009859 return NULL;
9860 }
9861#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009862 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 }
9866 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009867 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009868 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9869 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009870 buf = buflist_findnr(i);
9871 if (buf == NULL)
9872 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009873 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009874 return NULL;
9875 }
9876 if (lnump != NULL)
9877 *lnump = ECMD_LAST;
9878 if (buf->b_fname == NULL)
9879 {
9880 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009881 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009882 }
9883 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009884 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009885 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009886 tilde_file = STRCMP(result, "~") == 0;
9887 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 break;
9890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009891 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009892 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 if (result == NULL)
9894 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009895 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 return NULL;
9897 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009898 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009901 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009903 if (result != NULL && !autocmd_fname_full)
9904 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009905 // Still need to turn the fname into a full path. It is
9906 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009907 autocmd_fname_full = TRUE;
9908 result = FullName_save(autocmd_fname, FALSE);
9909 vim_free(autocmd_fname);
9910 autocmd_fname = result;
9911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 if (result == NULL)
9913 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009914 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 return NULL;
9916 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009917 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 break;
9919
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009920 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 if (autocmd_bufnr <= 0)
9922 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009923 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 return NULL;
9925 }
9926 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9927 result = strbuf;
9928 break;
9929
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009930 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 result = autocmd_match;
9932 if (result == NULL)
9933 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009934 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 return NULL;
9936 }
9937 break;
9938
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009939 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009940 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 if (result == NULL)
9942 {
LemonBoy6013d002022-04-09 21:42:10 +01009943 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9944 return NULL;
9945 }
9946 resultbuf = result; // remember allocated string
9947 break;
9948 case SPEC_STACK: // call stack
9949 result = estack_sfile(ESTACK_STACK);
9950 if (result == NULL)
9951 {
9952 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9953 return NULL;
9954 }
9955 resultbuf = result; // remember allocated string
9956 break;
9957 case SPEC_SCRIPT: // script file name
9958 result = estack_sfile(ESTACK_SCRIPT);
9959 if (result == NULL)
9960 {
9961 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 return NULL;
9963 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009964 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009966
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009967 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009968 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009969 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009970 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009971 return NULL;
9972 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009973 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009974 result = strbuf;
9975 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009976
9977#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009978 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009979 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009980 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009981 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009982 return NULL;
9983 }
9984 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009985 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009986 result = strbuf;
9987 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009988
Bram Moolenaar90944302020-08-01 20:45:11 +02009989 case SPEC_SID:
9990 if (current_sctx.sc_sid <= 0)
9991 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009992 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009993 return NULL;
9994 }
9995 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9996 result = strbuf;
9997 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009998#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009999
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010000#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010001 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010002 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10003 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 result = strbuf;
10005 break;
10006#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010007
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010008 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010009 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010010 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 }
10012
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +010010013 resultlen = STRLEN(result); // length of new string
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010014 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 {
10016 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +010010018 resultlen = s - result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 else if (!skip_mod)
10021 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010022 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 &resultlen);
10024 if (result == NULL)
10025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010026 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 return NULL;
10028 }
10029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 }
10031
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010032 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010034 if (empty_is_error)
10035 {
10036 if (valid != VALID_HEAD + VALID_PATH)
10037 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
10038 else
10039 *errormsg = _(e_evaluates_to_an_empty_string);
10040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 result = NULL;
10042 }
10043 else
10044 result = vim_strnsave(result, resultlen);
10045 vim_free(resultbuf);
10046 return result;
10047}
10048
10049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 * Expand the <sfile> string in "arg".
10051 *
10052 * Returns an allocated string, or NULL for any error.
10053 */
10054 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010055expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010057 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +020010059 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +020010061 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +020010063 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +020010064 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 char_u *p;
10066
John Marriotted908f72024-04-18 22:46:56 +020010067 resultlen = STRLEN(arg);
10068 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if (result == NULL)
10070 return NULL;
10071
10072 for (p = result; *p; )
10073 {
10074 if (STRNCMP(p, "<sfile>", 7) != 0)
10075 ++p;
10076 else
10077 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010078 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +010010079 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 if (errormsg != NULL)
10081 {
10082 if (*errormsg)
10083 emsg(errormsg);
10084 vim_free(result);
10085 return NULL;
10086 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010087 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 {
10089 p += srclen;
10090 continue;
10091 }
John Marriotted908f72024-04-18 22:46:56 +020010092 repllen = STRLEN(repl);
10093 resultlen += repllen - srclen;
10094 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 if (newres == NULL)
10096 {
10097 vim_free(repl);
10098 vim_free(result);
10099 return NULL;
10100 }
John Marriotted908f72024-04-18 22:46:56 +020010101 len = p - result;
10102 mch_memmove(newres, result, len);
10103 STRCPY(newres + len, repl);
10104 len += repllen;
10105 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 vim_free(repl);
10107 vim_free(result);
10108 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010109 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 }
10111 }
10112
10113 return result;
10114}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010117/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010118 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010119 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010122dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 if (fname == NULL)
10125 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010126 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127}
10128#endif
10129
10130/*
10131 * ":behave {mswin,xterm}"
10132 */
10133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010134ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135{
10136 if (STRCMP(eap->arg, "mswin") == 0)
10137 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010138 set_option_value_give_err((char_u *)"selection",
10139 0L, (char_u *)"exclusive", 0);
10140 set_option_value_give_err((char_u *)"selectmode",
10141 0L, (char_u *)"mouse,key", 0);
10142 set_option_value_give_err((char_u *)"mousemodel",
10143 0L, (char_u *)"popup", 0);
10144 set_option_value_give_err((char_u *)"keymodel",
10145 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 }
10147 else if (STRCMP(eap->arg, "xterm") == 0)
10148 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010149 set_option_value_give_err((char_u *)"selection",
10150 0L, (char_u *)"inclusive", 0);
10151 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
10152 set_option_value_give_err((char_u *)"mousemodel",
10153 0L, (char_u *)"extend", 0);
10154 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 }
10156 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010157 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158}
10159
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160static int filetype_detect = FALSE;
10161static int filetype_plugin = FALSE;
10162static int filetype_indent = FALSE;
10163
10164/*
10165 * ":filetype [plugin] [indent] {on,off,detect}"
10166 * on: Load the filetype.vim file to install autocommands for file types.
10167 * off: Load the ftoff.vim file to remove all autocommands for file types.
10168 * plugin on: load filetype.vim and ftplugin.vim
10169 * plugin off: load ftplugof.vim
10170 * indent on: load filetype.vim and indent.vim
10171 * indent off: load indoff.vim
10172 */
10173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010174ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175{
10176 char_u *arg = eap->arg;
10177 int plugin = FALSE;
10178 int indent = FALSE;
10179
10180 if (*eap->arg == NUL)
10181 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010182 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010183 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 filetype_detect ? "ON" : "OFF",
10185 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10186 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10187 return;
10188 }
10189
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010190 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 for (;;)
10192 {
10193 if (STRNCMP(arg, "plugin", 6) == 0)
10194 {
10195 plugin = TRUE;
10196 arg = skipwhite(arg + 6);
10197 continue;
10198 }
10199 if (STRNCMP(arg, "indent", 6) == 0)
10200 {
10201 indent = TRUE;
10202 arg = skipwhite(arg + 6);
10203 continue;
10204 }
10205 break;
10206 }
10207 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10208 {
10209 if (*arg == 'o' || !filetype_detect)
10210 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010211 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 filetype_detect = TRUE;
10213 if (plugin)
10214 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010215 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 filetype_plugin = TRUE;
10217 }
10218 if (indent)
10219 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010220 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 filetype_indent = TRUE;
10222 }
10223 }
10224 if (*arg == 'd')
10225 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010226 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010227 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 }
10229 }
10230 else if (STRCMP(arg, "off") == 0)
10231 {
10232 if (plugin || indent)
10233 {
10234 if (plugin)
10235 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010236 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 filetype_plugin = FALSE;
10238 }
10239 if (indent)
10240 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010241 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 filetype_indent = FALSE;
10243 }
10244 }
10245 else
10246 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010247 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 filetype_detect = FALSE;
10249 }
10250 }
10251 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010252 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253}
10254
10255/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010256 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 */
10258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010259ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260{
zeertzjq5bf6c212024-03-31 18:41:27 +020010261 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010262 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010263
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010264 char_u *arg = eap->arg;
10265 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10266 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010267
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010268 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10269 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +020010270 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010274ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275{
10276#ifdef FEAT_DIGRAPHS
10277 if (*eap->arg != NUL)
10278 putdigraph(eap->arg);
10279 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010280 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281#else
mityu61065042021-07-19 20:07:21 +020010282 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283#endif
10284}
10285
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010286#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10287 void
10288set_no_hlsearch(int flag)
10289{
10290 no_hlsearch = flag;
10291# ifdef FEAT_EVAL
10292 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10293# endif
10294}
10295
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296/*
10297 * ":nohlsearch"
10298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010300ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010302 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +010010303 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305#endif
10306
10307#ifdef FEAT_CRYPT
10308/*
10309 * ":X": Get crypt key
10310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010312ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010314 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010315 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316}
10317#endif
10318
10319#ifdef FEAT_FOLDING
10320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010321ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322{
10323 if (foldManualAllowed(TRUE))
10324 foldCreate(eap->line1, eap->line2);
10325}
10326
10327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010328ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329{
10330 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10331 eap->forceit, FALSE);
10332}
10333
10334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010335ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336{
10337 linenr_T lnum;
10338
Bram Moolenaar4facea32019-10-12 20:17:40 +020010339# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010340 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010341# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010342
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010343 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10345 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10346 ml_setmarked(lnum);
10347
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010348 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010350 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010351# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010352 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354}
10355#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010357#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010358/*
10359 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10360 * instead of a quickfix command.
10361 */
10362 int
10363is_loclist_cmd(int cmdidx)
10364{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010365 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010366 return FALSE;
10367 return cmdnames[cmdidx].cmd_name[0] == 'l';
10368}
10369#endif
10370
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010371 int
10372get_pressedreturn(void)
10373{
10374 return ex_pressedreturn;
10375}
10376
10377 void
10378set_pressedreturn(int val)
10379{
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +020010380 ex_pressedreturn = val;
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010381}