blob: e6200ed706201c3e6bdd3936eb9a90efd9875c0b [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
John Marriotted908f72024-04-18 22:46:56 +020022#if defined(FEAT_EVAL) || defined(PROTO)
23static int cmp_cmdmod_info(const void *a, const void *b);
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020027static 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 +000028#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020029static 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 +010030static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifndef FEAT_MENU
35# define ex_emenu ex_ni
36# define ex_menu ex_ni
37# define ex_menutranslate ex_ni
38#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static void ex_autocmd(exarg_T *eap);
40static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010041static void ex_bunload(exarg_T *eap);
42static void ex_buffer(exarg_T *eap);
43static void ex_bmodified(exarg_T *eap);
44static void ex_bnext(exarg_T *eap);
45static void ex_bprevious(exarg_T *eap);
46static void ex_brewind(exarg_T *eap);
47static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010048static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010049static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000050#ifndef FEAT_QUICKFIX
51# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000052# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define ex_cc ex_ni
54# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020055# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_cfile ex_ni
57# define qf_list ex_ni
58# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020059# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000061# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000062# define ex_cclose ex_ni
63# define ex_copen ex_ni
64# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020065# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000067#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
68# define ex_cexpr ex_ni
69#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020071static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020072static 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 +020073static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020075#if !defined(FEAT_PERL) \
76 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
77 || !defined(FEAT_TCL) \
78 || !defined(FEAT_RUBY) \
79 || !defined(FEAT_LUA) \
80 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000081# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010082static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000083#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010084static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000088#endif
John Marriotted908f72024-04-18 22:46:56 +020089static 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 +010090static void ex_highlight(exarg_T *eap);
91static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_cquit(exarg_T *eap);
93static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010094static void ex_close(exarg_T *eap);
95static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
96static void ex_only(exarg_T *eap);
97static void ex_resize(exarg_T *eap);
98static void ex_stag(exarg_T *eap);
99static void ex_tabclose(exarg_T *eap);
100static void ex_tabonly(exarg_T *eap);
101static void ex_tabnext(exarg_T *eap);
102static void ex_tabmove(exarg_T *eap);
103static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200104#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100105static void ex_pclose(exarg_T *eap);
106static void ex_ptag(exarg_T *eap);
107static void ex_pedit(exarg_T *eap);
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
112#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100113static void ex_hide(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static void ex_exit(exarg_T *eap);
115static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100117static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#else
119# define ex_goto ex_ni
120#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100121static void ex_shell(exarg_T *eap);
122static void ex_preserve(exarg_T *eap);
123static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100124static void ex_mode(exarg_T *eap);
125static void ex_wrongmodifier(exarg_T *eap);
126static void ex_find(exarg_T *eap);
127static void ex_open(exarg_T *eap);
128static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifndef FEAT_GUI
130# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100131static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100133#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100134static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#else
136# define ex_tearoff ex_ni
137#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100138#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
139 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100140static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#else
142# define ex_popup ex_ni
143#endif
144#ifndef FEAT_GUI_MSWIN
145# define ex_simalt ex_ni
146#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000147#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148# define gui_mch_find_dialog ex_ni
149# define gui_mch_replace_dialog ex_ni
150#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000151#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152# define ex_helpfind ex_ni
153#endif
154#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100155# define ex_cscope ex_ni
156# define ex_scscope ex_ni
157# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158#endif
159#ifndef FEAT_SYN_HL
160# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200161# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000162#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200163#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200164# define ex_syntime ex_ni
165#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000166#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000167# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000168# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000169# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000170# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000171# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000172#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200173#ifndef FEAT_PERSISTENT_UNDO
174# define ex_rundo ex_ni
175# define ex_wundo ex_ni
176#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200177#ifndef FEAT_LUA
178# define ex_lua ex_script_ni
179# define ex_luado ex_ni
180# define ex_luafile ex_ni
181#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000182#ifndef FEAT_MZSCHEME
183# define ex_mzscheme ex_script_ni
184# define ex_mzfile ex_ni
185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186#ifndef FEAT_PERL
187# define ex_perl ex_script_ni
188# define ex_perldo ex_ni
189#endif
190#ifndef FEAT_PYTHON
191# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200192# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193# define ex_pyfile ex_ni
194#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200195#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200196# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200197# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200198# define ex_py3file ex_ni
199#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100200#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
201# define ex_pyx ex_script_ni
202# define ex_pyxdo ex_ni
203# define ex_pyxfile ex_ni
204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#ifndef FEAT_TCL
206# define ex_tcl ex_script_ni
207# define ex_tcldo ex_ni
208# define ex_tclfile ex_ni
209#endif
210#ifndef FEAT_RUBY
211# define ex_ruby ex_script_ni
212# define ex_rubydo ex_ni
213# define ex_rubyfile ex_ni
214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215#ifndef FEAT_KEYMAP
216# define ex_loadkeymap ex_ni
217#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100218static void ex_swapname(exarg_T *eap);
219static void ex_syncbind(exarg_T *eap);
220static void ex_read(exarg_T *eap);
221static void ex_pwd(exarg_T *eap);
222static void ex_equal(exarg_T *eap);
223static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000226#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228#else
229# define ex_winpos ex_ni
230#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static void ex_operators(exarg_T *eap);
232static void ex_put(exarg_T *eap);
233static void ex_copymove(exarg_T *eap);
234static void ex_submagic(exarg_T *eap);
235static void ex_join(exarg_T *eap);
236static void ex_at(exarg_T *eap);
237static void ex_bang(exarg_T *eap);
238static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200239#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static void ex_wundo(exarg_T *eap);
241static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200242#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static void ex_redo(exarg_T *eap);
244static void ex_later(exarg_T *eap);
245static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100247static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_startinsert(exarg_T *eap);
251static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void ex_checkpath(exarg_T *eap);
254static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#else
256# define ex_findpat ex_ni
257# define ex_checkpath ex_ni
258#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200259#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#else
262# define ex_psearch ex_ni
263#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static void ex_tag(exarg_T *eap);
265static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200267# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200268# define ex_break ex_ni
269# define ex_breakadd ex_ni
270# define ex_breakdel ex_ni
271# define ex_breaklist ex_ni
272# define ex_call ex_ni
273# define ex_catch ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000274# define ex_class ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200275# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200276# define ex_continue ex_ni
277# define ex_debug ex_ni
278# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200279# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200280# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_echo ex_ni
283# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200285# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200286# define ex_endfunction ex_ni
287# define ex_endif ex_ni
288# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289# define ex_endwhile ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000290# define ex_enum ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200291# define ex_eval ex_ni
292# define ex_execute ex_ni
293# define ex_finally ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000294# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200295# define ex_finish ex_ni
296# define ex_function ex_ni
297# define ex_if ex_ni
298# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200299# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200300# define ex_lockvar ex_ni
301# define ex_oldfiles ex_ni
302# define ex_options ex_ni
303# define ex_packadd ex_ni
304# define ex_packloadall ex_ni
305# define ex_return ex_ni
306# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307# define ex_throw ex_ni
308# define ex_try ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000309# define ex_type ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200311# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312# define ex_import ex_ni
313# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200315#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316# define ex_loadview ex_ni
317#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200318#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319# define ex_viminfo ex_ni
320#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100322static void ex_filetype(exarg_T *eap);
323static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000325# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326# define ex_diffpatch ex_ni
327# define ex_diffgetput ex_ni
328# define ex_diffsplit ex_ni
329# define ex_diffthis ex_ni
330# define ex_diffupdate ex_ni
331#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100334static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#else
336# define ex_nohlsearch ex_ni
337# define ex_match ex_ni
338#endif
339#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100340static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_X ex_ni
343#endif
344#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100345static void ex_fold(exarg_T *eap);
346static void ex_foldopen(exarg_T *eap);
347static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#else
349# define ex_fold ex_ni
350# define ex_foldopen ex_ni
351# define ex_folddo ex_ni
352#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100353#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354# define ex_language ex_ni
355#endif
356#ifndef FEAT_SIGNS
357# define ex_sign ex_ni
358#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200360# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000361# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200362# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364
Bram Moolenaar05159a02005-02-26 23:04:13 +0000365#ifndef FEAT_PROFILE
366# define ex_profile ex_ni
367#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200368#ifndef FEAT_TERMINAL
369# define ex_terminal ex_ni
370#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200371#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
372# define ex_xrestore ex_ni
373#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100374#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200375# define ex_popupclear ex_ni
376#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000377
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378/*
379 * Declare cmdnames[].
380 */
381#define DO_DECLARE_EXCMD
382#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200383#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385static char_u dollar_command[2] = {'$', 0};
386
387
388#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100389// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390typedef struct
391{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100392 char_u *line; // command line
393 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394} wcmd_T;
395
396/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000397 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000399 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100403 garray_T *lines_gap; // growarray with line info
404 int current_line; // last read line from growarray
405 int repeating; // TRUE when looping a second time
406 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100407 char_u *(*lc_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 void *cookie;
409};
410
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200411static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100412static int store_loop_line(garray_T *gap, char_u *line);
413static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000414
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100415// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000416struct dbg_stuff
417{
418 int trylevel;
419 int force_abort;
420 except_T *caught_stack;
421 char_u *vv_exception;
422 char_u *vv_throwpoint;
423 int did_emsg;
424 int got_int;
425 int did_throw;
426 int need_rethrow;
427 int check_cstack;
428 except_T *current_exception;
429};
430
Bram Moolenaared203462004-06-16 11:19:22 +0000431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100432save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000433{
434 dsp->trylevel = trylevel; trylevel = 0;
435 dsp->force_abort = force_abort; force_abort = FALSE;
436 dsp->caught_stack = caught_stack; caught_stack = NULL;
437 dsp->vv_exception = v_exception(NULL);
438 dsp->vv_throwpoint = v_throwpoint(NULL);
439
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100440 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000441 dsp->did_emsg = did_emsg; did_emsg = FALSE;
442 dsp->got_int = got_int; got_int = FALSE;
443 dsp->did_throw = did_throw; did_throw = FALSE;
444 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
445 dsp->check_cstack = check_cstack; check_cstack = FALSE;
446 dsp->current_exception = current_exception; current_exception = NULL;
447}
448
449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100450restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000451{
452 suppress_errthrow = FALSE;
453 trylevel = dsp->trylevel;
454 force_abort = dsp->force_abort;
455 caught_stack = dsp->caught_stack;
456 (void)v_exception(dsp->vv_exception);
457 (void)v_throwpoint(dsp->vv_throwpoint);
458 did_emsg = dsp->did_emsg;
459 got_int = dsp->got_int;
460 did_throw = dsp->did_throw;
461 need_rethrow = dsp->need_rethrow;
462 check_cstack = dsp->check_cstack;
463 current_exception = dsp->current_exception;
464}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465#endif
466
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467/*
Colin Kennedy65e580b2024-03-26 18:29:30 +0100468 * Check if ffname differs from fnum.
469 * fnum is a buffer number. 0 == current buffer, 1-or-more must be a valid buffer ID.
470 * ffname is a full path to where a buffer lives on-disk or would live on-disk.
471 *
472 */
473 static int
474is_other_file(int fnum, char_u *ffname)
475{
476 if (fnum != 0)
477 {
478 if (fnum == curbuf->b_fnum)
479 return FALSE;
480
481 return TRUE;
482 }
483
484 if (ffname == NULL)
485 return TRUE;
486
487 if (*ffname == NUL)
488 return FALSE;
489
490 // TODO: Need a reliable way to know whether a buffer is meant to live on-disk
491 // !curbuf->b_dev_valid is not always available (example: missing on Windows)
492 if (curbuf->b_sfname != NULL
493 && *curbuf->b_sfname != NUL)
494 // This occurs with unsaved buffers. In which case `ffname`
495 // actually corresponds to curbuf->b_sfname
496 return fnamecmp(ffname, curbuf->b_sfname) != 0;
497
498 return otherfile(ffname);
499}
500
501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
503 * command is given.
504 */
505 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100506do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100507 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508{
509 int save_msg_scroll;
510 int prev_msg_row;
511 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100512 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000513
514 if (improved)
515 exmode_active = EXMODE_VIM;
516 else
517 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100518 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100519 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000520
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100521 // When using ":global /pat/ visual" and then "Q" we return to continue
522 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if (global_busy)
524 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525
526 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100527 ++RedrawingDisabled; // don't redisplay the window
528 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100530 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 ++hold_gui_events;
532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533
Bram Moolenaar32526b32019-01-19 17:43:09 +0100534 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 while (exmode_active)
536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100537 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000538 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
539 {
540 exmode_active = FALSE;
541 break;
542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 msg_scroll = TRUE;
544 need_wait_return = FALSE;
545 ex_pressedreturn = FALSE;
546 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100547 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 prev_msg_row = msg_row;
549 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 if (improved)
551 {
552 cmdline_row = msg_row;
553 do_cmdline(NULL, getexline, NULL, 0);
554 }
555 else
556 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
557 lines_left = Rows - 1;
558
Bram Moolenaardf177f62005-02-22 08:39:57 +0000559 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100560 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000562 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000563 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000566 if (ex_pressedreturn)
567 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100568 // go up one line, to overwrite the ":<CR>" line, so the
569 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000570 msg_row = prev_msg_row;
571 if (prev_msg_row == Rows - 1)
572 msg_row--;
573 }
574 msg_col = 0;
575 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
576 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100579 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000580 {
581 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000582 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000583 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000584 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587
588#ifdef FEAT_GUI
589 --hold_gui_events;
590#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100591 if (RedrawingDisabled > 0)
592 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100594 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 need_wait_return = FALSE;
596 msg_scroll = save_msg_scroll;
597}
598
599/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200600 * Print the executed command for when 'verbose' is set.
601 * When "lnum" is 0 only print the command.
602 */
603 static void
604msg_verbose_cmd(linenr_T lnum, char_u *cmd)
605{
606 ++no_wait_return;
607 verbose_enter_scroll();
608
609 if (lnum == 0)
610 smsg(_("Executing: %s"), cmd);
611 else
612 smsg(_("line %ld: %s"), (long)lnum, cmd);
613 if (msg_silent == 0)
614 msg_puts("\n"); // don't overwrite this
615
616 verbose_leave_scroll();
617 --no_wait_return;
618}
619
620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 * Execute a simple command line. Used for translated commands like "*".
622 */
623 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100624do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 return do_cmdline(cmd, NULL, NULL,
627 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
628}
629
630/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100631 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
632 * This allows for using a range without ":" in Vim9 script.
633 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200634 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100635do_cmd_argument(char_u *cmd)
636{
637 return do_cmdline(cmd, NULL, NULL,
638 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
639}
640
641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 * do_cmdline(): execute one Ex command line
643 *
644 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100645 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 * 2. Split up in parts separated with '|'.
647 *
648 * This function can be called recursively!
649 *
650 * flags:
651 * DOCMD_VERBOSE - The command will be included in the error message.
652 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100653 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 * DOCMD_KEYTYPED - Don't reset KeyTyped.
655 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
656 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
657 *
658 * return FAIL if cmdline could not be executed, OK otherwise
659 */
660 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100661do_cmdline(
662 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200663 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100664 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100665 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100667 char_u *next_cmdline; // next cmd to execute
668 char_u *cmdline_copy = NULL; // copy of cmd line
669 int used_getline = FALSE; // used "fgetline" to obtain command
670 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100672 int count = 0; // line number count
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100673 int did_inc_RedrawingDisabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 int retval = OK;
675#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100676 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100677 garray_T lines_ga; // keep lines for ":while"/":for"
678 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200679 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100680 char_u *fname = NULL; // function or script name
681 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
682 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
683 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200685 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200686 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100688 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200689 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000691 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000693 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100695# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696# define cmd_cookie cookie
697#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100698 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200699#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
701 // location for storing error messages to be converted to an exception.
702 // This ensures that the do_errthrow() call in do_one_cmd() does not
703 // combine the messages stored by an earlier invocation of do_one_cmd()
704 // with the command name of the later one. This would happen when
705 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 saved_msg_list = msg_list;
707 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708#endif
709
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100710 // It's possible to create an endless loop with ":execute", catch that
711 // here. The value of 200 allows nested function calls, ":source", etc.
712 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100713 if (call_depth >= 200
714#ifdef FEAT_EVAL
715 && call_depth >= p_mfd
716#endif
717 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000719 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100721 // When converting to an exception, we do not include the command name
722 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100723 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 msg_list = saved_msg_list;
725#endif
726 return FAIL;
727 }
728 ++call_depth;
729
730#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100731 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000733 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100735 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100737 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100738 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000739 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 ++ex_nesting_level;
741
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100742 // Get the function or script name and the address where the next breakpoint
743 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000744 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
746 fname = func_name(real_cookie);
747 breakpoint = func_breakpoint(real_cookie);
748 dbg_tick = func_dbg_tick(real_cookie);
749 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100750 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100752 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 breakpoint = source_breakpoint(real_cookie);
754 dbg_tick = source_dbg_tick(real_cookie);
755 }
756
757 /*
758 * Initialize "force_abort" and "suppress_errthrow" at the top level.
759 */
760 if (!recursive)
761 {
762 force_abort = FALSE;
763 suppress_errthrow = FALSE;
764 }
765
766 /*
767 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000768 * exception handling (used when debugging). Otherwise clear it to avoid
769 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000771 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000772 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000773 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200774 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775
776 initial_trylevel = trylevel;
777
778 /*
779 * "did_throw" will be set to TRUE when an exception is being thrown.
780 */
781 did_throw = FALSE;
782#endif
783 /*
784 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000785 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 * If force_abort is set, we cancel everything.
787 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100788#ifdef FEAT_EVAL
789 did_emsg_cumul += did_emsg;
790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 did_emsg = FALSE;
792
793 /*
794 * KeyTyped is only set when calling vgetc(). Reset it here when not
795 * calling vgetc() (sourced command lines).
796 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100797 if (!(flags & DOCMD_KEYTYPED)
798 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 KeyTyped = FALSE;
800
801 /*
802 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000803 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 * - for multiple commands on one line, separated with '|'
805 * - when repeating until there are no more lines (for ":source")
806 */
807 next_cmdline = cmdline;
808 do
809 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100811 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#endif
813
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100814 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 if (next_cmdline == NULL
816#ifdef FEAT_EVAL
817 && !force_abort
818 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820#endif
821 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100822 {
823#ifdef FEAT_EVAL
824 did_emsg_cumul += did_emsg;
825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
829 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000830 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100831 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 * 3. If a line is given: Make a copy, so we can mess with it.
833 */
834
835#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100836 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000837 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100839 // Each '|' separated command is stored separately in lines_ga, to
840 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100841 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100843 // Check if a function has returned or, unless it has an unclosed
844 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000848 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849 func_line_end(real_cookie);
850# endif
851 if (func_has_ended(real_cookie))
852 {
853 retval = FAIL;
854 break;
855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000857#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000858 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100859 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860 script_line_end();
861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100863 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100864 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 {
866 retval = FAIL;
867 break;
868 }
869
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100870 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 if (breakpoint != NULL && dbg_tick != NULL
872 && *dbg_tick != debug_tick)
873 {
874 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100875 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100876 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 *dbg_tick = debug_tick;
878 }
879
880 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100881 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100883 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100885 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100887 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100888 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100890 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100891 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 *dbg_tick = debug_tick;
893 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000895 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896 {
897 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100898 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100899 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900 script_line_start();
901 }
902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#endif
905
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100906 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 if (next_cmdline == NULL)
908 {
909 /*
910 * Need to set msg_didout for the first line after an ":if",
911 * otherwise the ":if" will be overwritten.
912 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100913 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100915 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916#ifdef FEAT_EVAL
917 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
918#else
919 0
920#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100921 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
922 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100924 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100925 // returned for the end of a sourced file or executed function
926 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 if (KeyTyped && !(flags & DOCMD_REPEAT))
928 need_wait_return = FALSE;
929 retval = FAIL;
930 break;
931 }
932 used_getline = TRUE;
933
934 /*
935 * Keep the first typed line. Clear it when more lines are typed.
936 */
937 if (flags & DOCMD_KEEPLINE)
938 {
939 vim_free(repeat_cmdline);
940 if (count == 0)
941 repeat_cmdline = vim_strsave(next_cmdline);
942 else
943 repeat_cmdline = NULL;
944 }
945 }
946
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100947 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 else if (cmdline_copy == NULL)
949 {
950 next_cmdline = vim_strsave(next_cmdline);
951 if (next_cmdline == NULL)
952 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200953 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 retval = FAIL;
955 break;
956 }
957 }
958 cmdline_copy = next_cmdline;
959
960#ifdef FEAT_EVAL
961 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200962 * Inside a while/for loop, and when the command looks like a ":while"
963 * or ":for", the line is stored, because we may need it later when
964 * looping.
965 *
966 * When there is a '|' and another command, it is stored separately,
967 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000968 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200969 *
970 * Pass a different "fgetline" function to do_one_cmd() below,
971 * that it stores lines in or reads them from "lines_ga". Makes it
972 * possible to define a function inside a while/for loop and handles
973 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200975 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200977 cmd_getline = get_loop_line;
978 cmd_cookie = (void *)&cmd_loop_cookie;
979 cmd_loop_cookie.lines_gap = &lines_ga;
980 cmd_loop_cookie.current_line = current_line;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100981 cmd_loop_cookie.lc_getline = fgetline;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200982 cmd_loop_cookie.cookie = cookie;
983 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
984
985 // Save the current line when encountering it the first time.
986 if (current_line == lines_ga.ga_len
987 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
989 retval = FAIL;
990 break;
991 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200992 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200994 else
995 {
996 cmd_getline = fgetline;
997 cmd_cookie = cookie;
998 }
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 did_endif = FALSE;
1001#endif
1002
1003 if (count++ == 0)
1004 {
1005 /*
1006 * All output from the commands is put below each other, without
1007 * waiting for a return. Don't do this when executing commands
1008 * from a script or when being called recursive (e.g. for ":e
1009 * +command file").
1010 */
1011 if (!(flags & DOCMD_NOWAIT) && !recursive)
1012 {
1013 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001014 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001016 msg_scroll = TRUE; // put messages below each other
1017 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001019 did_inc_RedrawingDisabled = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 }
1021 }
1022
Bram Moolenaar823654b2020-05-29 23:03:09 +02001023 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001024 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
1026 /*
1027 * 2. Execute one '|' separated command.
1028 * do_one_cmd() will return NULL if there is no trailing '|'.
1029 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1030 */
1031 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001032 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033#ifdef FEAT_EVAL
1034 &cstack,
1035#endif
1036 cmd_getline, cmd_cookie);
1037 --recursive;
1038
1039#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001040 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001041 // Use "current_line" from "cmd_loop_cookie", it may have been
1042 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044#endif
1045
1046 if (next_cmdline == NULL)
1047 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001048 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 /*
1051 * If the command was typed, remember it for the ':' register.
1052 * Do this AFTER executing the command to make :@: work.
1053 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001054 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 && new_last_cmdline != NULL)
1056 {
1057 vim_free(last_cmdline);
1058 last_cmdline = new_last_cmdline;
1059 new_last_cmdline = NULL;
1060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 }
1062 else
1063 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001064 // need to copy the command after the '|' to cmdline_copy, for the
1065 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001066 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 next_cmdline = cmdline_copy;
1068 }
1069
1070
1071#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001072 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001074 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001076 {
1077 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
1083 ++current_line;
1084
1085 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001086 * An ":endwhile", ":endfor" and ":continue" is handled here.
1087 * If we were executing commands, jump back to the ":while" or
1088 * ":for".
1089 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001091 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001095 // Jump back to the matching ":while" or ":for". Be careful
1096 // not to use a cs_line[] from an entry that isn't a ":while"
1097 // or ":for": It would make "current_line" invalid and can
1098 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (!did_emsg && !got_int && !did_throw
1100 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 && (cstack.cs_flags[cstack.cs_idx]
1102 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 && cstack.cs_line[cstack.cs_idx] >= 0
1104 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1105 {
1106 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001107 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001108 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001109 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001111 // Check for the next breakpoint at or after the ":while"
1112 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001113 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
1115 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001116 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 fname,
1118 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1119 *dbg_tick = debug_tick;
1120 }
1121 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001124 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001126 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1127 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 }
1129 }
1130
1131 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001136 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001137 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139 }
1140
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001142 if (breakpoint != NULL && has_watchexpr())
1143 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001144 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001145 *dbg_tick = debug_tick;
1146 }
1147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 /*
1149 * When not inside any ":while" loop, clear remembered lines.
1150 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001151 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 {
1153 if (lines_ga.ga_len > 0)
1154 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001155 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1157 free_cmdlines(&lines_ga);
1158 }
1159 current_line = 0;
1160 }
1161
1162 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1164 * being restored at the ":endtry". Reset them here and set the
1165 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1166 * This includes the case where a missing ":endif", ":endwhile" or
1167 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 cstack.cs_lflags &= ~CSL_HAD_FINA;
1172 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1173 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 did_throw ? (void *)current_exception : NULL);
1175 did_emsg = got_int = did_throw = FALSE;
1176 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1177 }
1178
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001179 // Update global "trylevel" for recursive calls to do_cmdline() from
1180 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 trylevel = initial_trylevel + cstack.cs_trylevel;
1182
1183 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001184 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 * files) is aborted because of an error, an interrupt, or an uncaught
1186 * exception, cancel everything. If it is left normally, reset
1187 * force_abort to get the non-EH compatible abortion behavior for
1188 * the rest of the script.
1189 */
1190 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1191 force_abort = FALSE;
1192
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001193 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001195#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196
1197 }
1198 /*
1199 * Continue executing command lines when:
1200 * - no CTRL-C typed, no aborting error, no exception thrown or try
1201 * conditionals need to be checked for executing finally clauses or
1202 * catching an interrupt exception
1203 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001204 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 * looping for ":source" command or function call.
1206 */
1207 while (!((got_int
1208#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001209 || (did_emsg && (force_abort || in_vim9script()))
1210 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211#endif
1212 )
1213#ifdef FEAT_EVAL
1214 && cstack.cs_trylevel == 0
1215#endif
1216 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001217 && !(did_emsg
1218#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001219 // Keep going when inside try/catch, so that the error can be
1220 // deal with, except when it is a syntax error, it may cause
1221 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001222 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1223#endif
1224 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001225 && (getline_equal(fgetline, cookie, getexmodeline)
1226 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 && (next_cmdline != NULL
1228#ifdef FEAT_EVAL
1229 || cstack.cs_idx >= 0
1230#endif
1231 || (flags & DOCMD_REPEAT)));
1232
1233 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001234 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235#ifdef FEAT_EVAL
1236 free_cmdlines(&lines_ga);
1237 ga_clear(&lines_ga);
1238
1239 if (cstack.cs_idx >= 0)
1240 {
1241 /*
1242 * If a sourced file or executed function ran to its end, report the
1243 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001244 * In Vim9 script do not give a second error, executing aborts after
1245 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001247 if (!got_int && !did_throw && !aborting()
1248 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001249 && ((getline_equal(fgetline, cookie, getsourceline)
1250 && !source_finished(fgetline, cookie))
1251 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 && !func_has_ended(real_cookie))))
1253 {
1254 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001255 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001257 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001258 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001259 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001261 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263
1264 /*
1265 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1266 * ":endtry" in a sourced file or executed function. If the try
1267 * conditional is in its finally clause, ignore anything pending.
1268 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001269 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 */
1271 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001272 {
1273 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1274
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001275 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001276 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001277 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1278 &cstack.cs_looplevel);
1279 }
1280 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 trylevel = initial_trylevel;
1282 }
1283
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001284 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1285 // lack was reported above and the error message is to be converted to an
1286 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001287 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 ? (char_u *)"endfunction" : (char_u *)NULL);
1289
1290 if (trylevel == 0)
1291 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001292 // Just in case did_throw got set but current_exception wasn't.
1293 if (current_exception == NULL)
1294 did_throw = FALSE;
1295
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 /*
1297 * When an exception is being thrown out of the outermost try
1298 * conditional, discard the uncaught exception, disable the conversion
1299 * of interrupts or errors to exceptions, and ensure that no more
1300 * commands are executed.
1301 */
1302 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001303 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
1305 /*
1306 * On an interrupt or an aborting error not converted to an exception,
1307 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001308 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 * when force_abort is set and did_emsg unset in case of an interrupt
1310 * from a finally clause after an error.
1311 */
1312 else if (got_int || (did_emsg && force_abort))
1313 suppress_errthrow = TRUE;
1314 }
1315
1316 /*
1317 * The current cstack will be freed when do_cmdline() returns. An uncaught
1318 * exception will have to be rethrown in the previous cstack. If a function
1319 * has just returned or a script file was just finished and the previous
1320 * cstack belongs to the same function or, respectively, script file, it
1321 * will have to be checked for finally clauses to be executed due to the
1322 * ":return" or ":finish". This is done in do_one_cmd().
1323 */
1324 if (did_throw)
1325 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001326 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001328 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 && ex_nesting_level > func_level(real_cookie) + 1))
1330 {
1331 if (!did_throw)
1332 check_cstack = TRUE;
1333 }
1334 else
1335 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001336 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 --ex_nesting_level;
1339 /*
1340 * Go to debug mode when returning from a function in which we are
1341 * single-stepping.
1342 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001343 if ((getline_equal(fgetline, cookie, getsourceline)
1344 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001346 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 ? (char_u *)_("End of sourced file")
1348 : (char_u *)_("End of function"));
1349 }
1350
1351 /*
1352 * Restore the exception environment (done after returning from the
1353 * debugger).
1354 */
1355 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001356 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
1358 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001359
1360 // Cleanup if "cs_emsg_silent_list" remains.
1361 if (cstack.cs_emsg_silent_list != NULL)
1362 {
1363 eslist_T *elem, *temp;
1364
1365 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1366 {
1367 temp = elem->next;
1368 vim_free(elem);
1369 }
1370 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001371#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
1373 /*
1374 * If there was too much output to fit on the command line, ask the user to
1375 * hit return before redrawing the screen. With the ":global" command we do
1376 * this only once after the command is finished.
1377 */
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001378 if (did_inc_RedrawingDisabled)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001380 if (RedrawingDisabled > 0)
1381 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 --no_wait_return;
1383 msg_scroll = FALSE;
1384
1385 /*
1386 * When just finished an ":if"-":else" which was typed, no need to
1387 * wait for hit-return. Also for an error situation.
1388 */
1389 if (retval == FAIL
1390#ifdef FEAT_EVAL
1391 || (did_endif && KeyTyped && !did_emsg)
1392#endif
1393 )
1394 {
1395 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001396 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398 else if (need_wait_return)
1399 {
1400 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001401 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 * here should not overwrite the command that may be shown before
1403 * doing that.
1404 */
1405 msg_didout |= msg_didout_before_start;
1406 wait_return(FALSE);
1407 }
1408 }
1409
Bram Moolenaar2a942252012-11-28 23:03:07 +01001410#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001411 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001412#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 /*
1414 * Reset if_level, in case a sourced script file contains more ":if" than
1415 * ":endif" (could be ":if x | foo | endif").
1416 */
1417 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001418#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001419
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 --call_depth;
1421 return retval;
1422}
1423
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001424#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001425/*
1426 * Handle when "did_throw" is set after executing commands.
1427 */
1428 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001429handle_did_throw(void)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001430{
1431 char *p = NULL;
1432 msglist_T *messages = NULL;
ichizok7e5fe382023-04-15 13:17:50 +01001433 ESTACK_CHECK_DECLARATION;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001434
1435 /*
1436 * If the uncaught exception is a user exception, report it as an
1437 * error. If it is an error exception, display the saved error
1438 * message now. For an interrupt exception, do nothing; the
1439 * interrupt message is given elsewhere.
1440 */
1441 switch (current_exception->type)
1442 {
1443 case ET_USER:
1444 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001445 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001446 current_exception->value);
1447 p = (char *)vim_strsave(IObuff);
1448 break;
1449 case ET_ERROR:
1450 messages = current_exception->messages;
1451 current_exception->messages = NULL;
1452 break;
1453 case ET_INTERRUPT:
1454 break;
1455 }
1456
1457 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1458 current_exception->throw_lnum);
ichizok7e5fe382023-04-15 13:17:50 +01001459 ESTACK_CHECK_SETUP;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001460 current_exception->throw_name = NULL;
1461
1462 discard_current_exception(); // uses IObuff if 'verbose'
LemonBoy749ba0f2024-07-04 19:23:16 +02001463
1464 // If "silent!" is active the uncaught exception is not fatal.
1465 if (emsg_silent == 0)
1466 {
1467 suppress_errthrow = TRUE;
1468 force_abort = TRUE;
1469 }
Bram Moolenaar620c9592021-07-31 21:32:31 +02001470
1471 if (messages != NULL)
1472 {
1473 do
1474 {
1475 msglist_T *next = messages->next;
1476 int save_compiling = estack_compiling;
1477
1478 estack_compiling = messages->msg_compiling;
1479 emsg(messages->msg);
1480 vim_free(messages->msg);
1481 vim_free(messages->sfile);
1482 vim_free(messages);
1483 messages = next;
1484 estack_compiling = save_compiling;
1485 }
1486 while (messages != NULL);
1487 }
1488 else if (p != NULL)
1489 {
1490 emsg(p);
1491 vim_free(p);
1492 }
1493 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01001494 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001495 estack_pop();
1496}
1497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 */
1501 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001502get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001504 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 wcmd_T *wp;
1506 char_u *line;
1507
1508 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1509 {
1510 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001511 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001513 // First time inside the ":while"/":for": get line normally.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001514 if (cp->lc_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001515 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001517 line = cp->lc_getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001518 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 ++cp->current_line;
1520
1521 return line;
1522 }
1523
1524 KeyTyped = FALSE;
1525 ++cp->current_line;
1526 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001527 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 return vim_strsave(wp->line);
1529}
1530
1531/*
1532 * Store a line in "gap" so that a ":while" loop can execute it again.
1533 */
1534 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001535store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
1537 if (ga_grow(gap, 1) == FAIL)
1538 return FAIL;
1539 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001540 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 return OK;
1543}
1544
1545/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 */
1548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001549free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550{
1551 while (gap->ga_len > 0)
1552 {
1553 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1554 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 }
1556}
1557#endif
1558
1559/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001560 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1561 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001564getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001565 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001566 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001567 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
1569#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001570 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001571 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001573 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1574 // function that's originally used to obtain the lines. This may be
1575 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001576 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001577 cp = (struct loop_cookie *)cookie;
1578 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001580 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 cp = cp->cookie;
1582 }
1583 return gp == func;
1584#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001585 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586#endif
1587}
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001590 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 * getline function. Otherwise return "cookie".
1592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001594getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001595 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001596 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
Bram Moolenaar13505972019-01-24 15:04:48 +01001598#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001599 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001600 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001602 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1603 // cookie that's originally used to obtain the lines. This may be nested
1604 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001605 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001606 cp = (struct loop_cookie *)cookie;
1607 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001609 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 cp = cp->cookie;
1611 }
1612 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001613#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001616}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001618#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001619/*
1620 * Get the next line source line without advancing.
1621 */
1622 char_u *
1623getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001624 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001625 void *cookie) // argument for fgetline()
1626{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001627 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001628 struct loop_cookie *cp;
1629 wcmd_T *wp;
1630
1631 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1632 // cookie that's originally used to obtain the lines. This may be nested
1633 // several levels.
1634 gp = fgetline;
1635 cp = (struct loop_cookie *)cookie;
1636 while (gp == get_loop_line)
1637 {
1638 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1639 {
1640 // executing lines a second time, use the stored copy
1641 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1642 return wp->line;
1643 }
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001644 gp = cp->lc_getline;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001645 cp = cp->cookie;
1646 }
1647 if (gp == getsourceline)
1648 return source_nextline(cp);
1649 return NULL;
1650}
1651#endif
1652
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001653
1654/*
1655 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1656 * ":bwipeout", etc.
1657 * Returns the buffer number.
1658 */
1659 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001660compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001661{
1662 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001663 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001664 int count = offset;
1665
1666 buf = firstbuf;
1667 while (buf->b_next != NULL && buf->b_fnum < lnum)
1668 buf = buf->b_next;
1669 while (count != 0)
1670 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001671 count += (offset < 0) ? 1 : -1;
1672 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1673 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001674 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001675 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001676 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001677 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001678 while (buf->b_ml.ml_mfp == NULL)
1679 {
1680 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1681 if (nextbuf == NULL)
1682 break;
1683 buf = nextbuf;
1684 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001685 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001686 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001687 if (addr_type == ADDR_LOADED_BUFFERS)
1688 while (buf->b_ml.ml_mfp == NULL)
1689 {
1690 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1691 if (nextbuf == NULL)
1692 break;
1693 buf = nextbuf;
1694 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001695 return buf->b_fnum;
1696}
1697
Bram Moolenaarb7316892019-05-01 18:08:42 +02001698/*
1699 * Return the window number of "win".
1700 * When "win" is NULL return the number of windows.
1701 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001702 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001703current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001704{
1705 win_T *wp;
1706 int nr = 0;
1707
Bram Moolenaar29323592016-07-24 22:04:11 +02001708 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001709 {
1710 ++nr;
1711 if (wp == win)
1712 break;
1713 }
1714 return nr;
1715}
1716
1717 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001718current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001719{
1720 tabpage_T *tp;
1721 int nr = 0;
1722
Bram Moolenaar29323592016-07-24 22:04:11 +02001723 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001724 {
1725 ++nr;
1726 if (tp == tab)
1727 break;
1728 }
1729 return nr;
1730}
1731
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001732 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001733comment_start(char_u *p, int starts_with_colon UNUSED)
1734{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001735 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001736 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001737 return *p == '"';
1738}
1739
Bram Moolenaarf240e182014-11-27 18:33:02 +01001740# define CURRENT_WIN_NR current_win_nr(curwin)
1741# define LAST_WIN_NR current_win_nr(NULL)
1742# define CURRENT_TAB_NR current_tab_nr(curtab)
1743# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745/*
1746 * Execute one Ex command.
1747 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001748 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1749 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 *
1751 * 1. skip comment lines and leading space
1752 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001753 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001754 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001755 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001756 * 6. parse arguments
1757 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001759 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 *
1761 * This function may be called recursively!
1762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001764do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001765 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001766 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001768 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001770 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001771 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001773 char_u *p;
1774 linenr_T lnum;
1775 long n;
1776 char *errormsg = NULL; // error message
1777 char_u *after_modifier = NULL;
1778 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001779 cmdmod_T save_cmdmod;
1780 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001781 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001782 int ni; // set when Not Implemented
1783 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001784 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001785 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001786#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001787 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001788#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001789 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001790 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
Bram Moolenaara80faa82020-04-12 19:37:17 +02001792 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 ea.line1 = 1;
1794 ea.line2 = 1;
1795#ifdef FEAT_EVAL
1796 ++ex_nesting_level;
1797#endif
1798
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001799 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (quitmore
1801#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001802 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001803 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001804#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001805 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001806 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 --quitmore;
1808
1809 /*
1810 * Reset browse, confirm, etc.. They are restored when returning, for
1811 * recursive calls.
1812 */
1813 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001815 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001816 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1817 goto doend;
1818
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001819/*
1820 * 1. Skip comment lines and leading white space and colons.
1821 * 2. Handle command modifiers.
1822 */
1823 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001825 ea.cmdlinep = cmdlinep;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001826 ea.ea_getline = fgetline;
Bram Moolenaareffed932018-08-14 13:38:17 +02001827 ea.cookie = cookie;
1828#ifdef FEAT_EVAL
1829 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001830 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001832 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001833 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001834 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001835 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
1837#ifdef FEAT_EVAL
1838 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1839 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1840#else
1841 ea.skip = (if_level > 0);
1842#endif
1843
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001845 * 3. Skip over the range to find the command. Let "p" point to after it.
1846 *
1847 * We need the command to know what kind of range it uses.
1848 */
1849 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001850
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001851 // In Vim9 script a colon is required before the range. This may also be
1852 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001853 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001854 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001855 {
1856 may_have_range = FALSE;
1857 for (p = ea.cmd; p >= *cmdlinep; --p)
1858 {
1859 if (*p == ':')
1860 may_have_range = TRUE;
1861 if (p < ea.cmd && !VIM_ISWHITE(*p))
1862 break;
1863 }
1864 }
1865 else
1866 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001867 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001868 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001870#ifdef FEAT_EVAL
1871 // Handle ":export" - it functions almost like a command modifier.
1872 // ":export var Name: type"
1873 // ":export def Name(..."
1874 // etc.
1875 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1876 is_export = TRUE;
1877#endif
1878
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001879 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001880 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001881 if (ea.cmd == cmd + 1 && *cmd == '$')
1882 // should be "$VAR = val"
1883 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001884#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001885 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001886#else
1887 p = find_ex_command(&ea, NULL, NULL, NULL);
1888#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001889 if (ea.cmdidx == CMD_SIZE)
1890 {
1891 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1892
1893 // If a ':' before the range is missing, give a clearer error
1894 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001895 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001896 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001897 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001898 goto doend;
1899 }
1900 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001904
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001905#ifdef FEAT_EVAL
1906# ifdef FEAT_PROFILE
1907 // Count this line for profiling if skip is TRUE.
1908 if (do_profiling == PROF_YES
1909 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1910 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1911 {
1912 int skip = did_emsg || got_int || did_throw;
1913
1914 if (ea.cmdidx == CMD_catch)
1915 skip = !skip && !(cstack->cs_idx >= 0
1916 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1917 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1918 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1919 skip = skip || !(cstack->cs_idx >= 0
1920 && !(cstack->cs_flags[cstack->cs_idx]
1921 & (CSF_ACTIVE | CSF_TRUE)));
1922 else if (ea.cmdidx == CMD_finally)
1923 skip = FALSE;
1924 else if (ea.cmdidx != CMD_endif
1925 && ea.cmdidx != CMD_endfor
1926 && ea.cmdidx != CMD_endtry
1927 && ea.cmdidx != CMD_endwhile)
1928 skip = ea.skip;
1929
1930 if (!skip)
1931 {
1932 if (getline_equal(fgetline, cookie, get_func_line))
1933 func_line_exec(getline_cookie(fgetline, cookie));
1934 else if (getline_equal(fgetline, cookie, getsourceline))
1935 script_line_exec();
1936 }
1937 }
1938# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001939#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001940
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001941 ea.cmd = cmd;
1942
1943#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001944 // May go to debug mode. If this happens and the ">quit" debug command is
1945 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001946 dbg_check_breakpoint(&ea);
1947 if (!ea.skip && got_int)
1948 {
1949 ea.skip = TRUE;
1950 (void)do_intthrow(cstack);
1951 }
1952#endif
1953
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001954/*
1955 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 *
1957 * where 'addr' is:
1958 *
1959 * % (entire file)
1960 * $ [+-NUM]
1961 * 'x [+-NUM] (where x denotes a currently defined mark)
1962 * . [+-NUM]
1963 * [+-NUM]..
1964 * NUM
1965 *
1966 * The ea.cmd pointer is updated to point to the first character following the
1967 * range spec. If an initial address is found, but no second, the upper bound
1968 * is equal to the lower.
1969 */
1970
Bram Moolenaar25190db2019-05-04 15:05:28 +02001971 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001972 if (!IS_USER_CMDIDX(ea.cmdidx))
1973 {
1974 if (ea.cmdidx != CMD_SIZE)
1975 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1976 else
1977 ea.addr_type = ADDR_LINES;
1978
Bram Moolenaar25190db2019-05-04 15:05:28 +02001979 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001980 if (ea.cmdidx == CMD_wincmd && p != NULL)
1981 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001982#ifdef FEAT_QUICKFIX
1983 // :.cc in quickfix window uses line number
1984 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1985 ea.addr_type = ADDR_OTHER;
1986#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001987 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001988
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001989 if (!may_have_range)
1990 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001991 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1992 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001995 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 */
1997
1998 /*
1999 * Skip ':' and any white space
2000 */
2001 ea.cmd = skipwhite(ea.cmd);
2002 while (*ea.cmd == ':')
2003 ea.cmd = skipwhite(ea.cmd + 1);
2004
2005 /*
2006 * If we got a line, but no command, then go to the line.
2007 * If we find a '|' or '\n' we set ea.nextcmd.
2008 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002009 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
2010 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
2012 /*
2013 * strange vi behaviour:
2014 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01002015 * ":3|..." prints line 3 (not in Vim9 script)
2016 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002018 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002020 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 goto doend;
2022 }
2023
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002024 // If this looks like an undefined user command and there are CmdUndefined
2025 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002026 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2027 && ASCII_ISUPPER(*ea.cmd)
2028 && has_cmdundefined())
2029 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002030 int ret;
2031
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002032 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002033 while (ASCII_ISALNUM(*p))
2034 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002035 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002036 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2037 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002038 // If the autocommands did something and didn't cause an error, try
2039 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002040 p = (ret
2041#ifdef FEAT_EVAL
2042 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002043#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002045 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002046
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 if (p == NULL)
2048 {
2049 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002050 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 goto doend;
2052 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002053 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002054 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2055 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
2057 errormsg = uc_fun_cmd();
2058 goto doend;
2059 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002060
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (ea.cmdidx == CMD_SIZE)
2062 {
2063 if (!ea.skip)
2064 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002065 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002067 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002068 // If the modifier was parsed OK the error must be in the
2069 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002070 if (after_modifier != NULL)
2071 append_command(after_modifier);
2072 else
2073 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002074 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002075 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002076 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002077 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
2079 goto doend;
2080 }
2081
Bram Moolenaar958636c2014-10-21 20:01:58 +02002082 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2083 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002084#ifdef HAVE_EX_SCRIPT_NI
2085 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2086#endif
2087 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
2089#ifndef FEAT_EVAL
2090 /*
2091 * When the expression evaluation is disabled, recognize the ":if" and
2092 * ":endif" commands and ignore everything in between it.
2093 */
2094 if (ea.cmdidx == CMD_if)
2095 ++if_level;
2096 if (if_level)
2097 {
2098 if (ea.cmdidx == CMD_endif)
2099 --if_level;
2100 goto doend;
2101 }
2102
2103#endif
2104
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002105 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002106 if (*p == '!' && ea.cmdidx != CMD_substitute
2107 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
2109 ++p;
2110 ea.forceit = TRUE;
2111 }
2112 else
2113 ea.forceit = FALSE;
2114
2115/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002116 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002118 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002119 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
2121 if (!ea.skip)
2122 {
2123#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002124 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002126 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002127 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 goto doend;
2129 }
2130#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002131 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002132 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002133 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002134 goto doend;
2135 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002136 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002138 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002139 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 goto doend;
2141 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002142
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002143 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002145 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2146 {
2147 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002148 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002149 goto doend;
2150 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002151 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2152 {
2153 // Command not allowed when text is locked
2154 errormsg = _(get_text_locked_msg());
2155 goto doend;
2156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002158
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002159 // Disallow editing another buffer when "curbuf_lock" is set.
2160 // Do allow ":checktime" (it is postponed).
2161 // Do allow ":edit" (check for an argument later).
2162 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002163 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002164 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002165 && ea.cmdidx != CMD_edit
2166 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002167 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002168 && curbuf_locked())
2169 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002171 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002173 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 goto doend;
2175 }
2176 }
2177
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002178 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002180 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 goto doend;
2182 }
2183
2184 /*
2185 * Don't complain about the range if it is not used
2186 * (could happen if line_count is accidentally set to 0).
2187 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002188 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
2190 /*
2191 * If the range is backwards, ask for confirmation and, if given, swap
2192 * ea.line1 & ea.line2 so it's forwards again.
2193 * When global command is busy, don't ask, will fail below.
2194 */
2195 if (!global_busy && ea.line1 > ea.line2)
2196 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002197 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002199 if (sourcing || exmode_active)
2200 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002201 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002202 goto doend;
2203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 if (ask_yesno((char_u *)
2205 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002206 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 }
2208 lnum = ea.line1;
2209 ea.line1 = ea.line2;
2210 ea.line2 = lnum;
2211 }
2212 if ((errormsg = invalid_range(&ea)) != NULL)
2213 goto doend;
2214 }
2215
Bram Moolenaarb7316892019-05-01 18:08:42 +02002216 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2217 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 ea.line2 = 1;
2219
2220 correct_range(&ea);
2221
2222#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002223 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002224 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002226 // Put the first line at the start of a closed fold, put the last line
2227 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 (void)hasFolding(ea.line1, &ea.line1, NULL);
2229 (void)hasFolding(ea.line2, NULL, &ea.line2);
2230 }
2231#endif
2232
2233#ifdef FEAT_QUICKFIX
2234 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002235 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 * option here, so things like % get expanded.
2237 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002238 p = replace_makeprg(&ea, p, cmdlinep);
2239 if (p == NULL)
2240 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241#endif
2242
2243 /*
2244 * Skip to start of argument.
2245 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2246 */
2247 if (ea.cmdidx == CMD_bang)
2248 ea.arg = p;
2249 else
2250 ea.arg = skipwhite(p);
2251
Bram Moolenaar379fb762018-08-30 15:58:28 +02002252 // ":file" cannot be run with an argument when "curbuf_lock" is set
2253 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2254 goto doend;
2255
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 /*
2257 * Check for "++opt=val" argument.
2258 * Must be first, allow ":w ++enc=utf8 !cmd"
2259 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002260 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2262 if (getargopt(&ea) == FAIL && !ni)
2263 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002264 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 goto doend;
2266 }
2267
2268 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2269 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002270 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002272 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002274 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 goto doend;
2276 }
2277 ea.arg = skipwhite(ea.arg + 1);
2278 ea.append = TRUE;
2279 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002280 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
2282 ++ea.arg;
2283 ea.usefilter = TRUE;
2284 }
2285 }
2286
2287 if (ea.cmdidx == CMD_read)
2288 {
2289 if (ea.forceit)
2290 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002291 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 ea.forceit = FALSE;
2293 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002294 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 ++ea.arg;
2297 ea.usefilter = TRUE;
2298 }
2299 }
2300
2301 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2302 {
2303 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002304 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 ++ea.arg;
2307 ++ea.amount;
2308 }
2309 ea.arg = skipwhite(ea.arg);
2310 }
2311
2312 /*
2313 * Check for "+command" argument, before checking for next command.
2314 * Don't do this for ":read !cmd" and ":write !cmd".
2315 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002316 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2318
2319 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002320 * For commands that do not use '|' inside their argument: Check for '|' to
2321 * separate commands and '"' or '#' to start comments.
2322 *
2323 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002324 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002325 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002326 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002328 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2329 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002330 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002331 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002332 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002333 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002334 || ea.cmdidx == CMD_global
2335 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002336 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002337#ifdef FEAT_EVAL
2338 || inside_block(&ea)
2339#endif
2340 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
2342 for (p = ea.arg; *p; ++p)
2343 {
Mohamed Akramf3daa452024-07-06 17:12:09 +02002344 // Remove one backslash before a newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002346 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002347 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 ea.nextcmd = p + 1;
2350 *p = NUL;
2351 break;
2352 }
2353 }
2354 }
2355
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002356 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002357 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002359 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002362 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002363 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002364 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002366#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002367 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002368 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002370 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002371 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002374 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2375 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002376 {
2377 ea.regname = *ea.arg++;
2378#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002379 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002380 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2381 {
kuuote08d7b1c2021-10-04 22:17:36 +01002382 if (!ea.skip)
2383 {
2384 set_expr_line(vim_strsave(ea.arg), &ea);
2385 did_set_expr_line = TRUE;
2386 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002387 ea.arg += STRLEN(ea.arg);
2388 }
2389#endif
2390 ea.arg = skipwhite(ea.arg);
2391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 }
2393
2394 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002395 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 * count, it's a buffer name.
2397 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002398 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002399 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002400 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002402 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002404 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002406 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 goto doend;
2408 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002409 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
2411 ea.line2 = n;
2412 if (ea.addr_count == 0)
2413 ea.addr_count = 1;
2414 }
2415 else
2416 {
2417 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002418 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002419 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002420 else
2421 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 ++ea.addr_count;
2423 /*
2424 * Be vi compatible: no error message for out of range.
2425 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002426 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 ea.line2 = curbuf->b_ml.ml_line_count;
2428 }
2429 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002430
2431 /*
2432 * Check for flags: 'l', 'p' and '#'.
2433 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002434 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002435 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002436 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2437 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002439 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002440 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 goto doend;
2442 }
2443
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002444 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002446 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 goto doend;
2448 }
2449
2450#ifdef FEAT_EVAL
2451 /*
2452 * Skip the command when it's not going to be executed.
2453 * The commands like :if, :endif, etc. always need to be executed.
2454 * Also make an exception for commands that handle a trailing command
2455 * themselves.
2456 */
2457 if (ea.skip)
2458 {
2459 switch (ea.cmdidx)
2460 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002461 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_while:
2463 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002464 case CMD_for:
2465 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case CMD_if:
2467 case CMD_elseif:
2468 case CMD_else:
2469 case CMD_endif:
2470 case CMD_try:
2471 case CMD_catch:
2472 case CMD_finally:
2473 case CMD_endtry:
2474 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002475 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 break;
2477
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002478 // Commands that handle '|' themselves. Check: A command should
2479 // either have the EX_TRLBAR flag, appear in this list or appear in
2480 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 case CMD_aboveleft:
2482 case CMD_and:
2483 case CMD_belowright:
2484 case CMD_botright:
2485 case CMD_browse:
2486 case CMD_call:
2487 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002488 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 case CMD_delfunction:
2490 case CMD_djump:
2491 case CMD_dlist:
2492 case CMD_dsearch:
2493 case CMD_dsplit:
2494 case CMD_echo:
2495 case CMD_echoerr:
2496 case CMD_echomsg:
2497 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002498 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002500 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002501 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 case CMD_help:
2503 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002504 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 case CMD_ijump:
2506 case CMD_ilist:
2507 case CMD_isearch:
2508 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002509 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 case CMD_keepjumps:
2511 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002512 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 case CMD_leftabove:
2514 case CMD_let:
2515 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002516 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002517 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002519 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002520 case CMD_noautocmd:
2521 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 case CMD_perl:
2523 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002524 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002525 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002526 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 case CMD_return:
2528 case CMD_rightbelow:
2529 case CMD_ruby:
2530 case CMD_silent:
2531 case CMD_smagic:
2532 case CMD_snomagic:
2533 case CMD_substitute:
2534 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002535 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 case CMD_tcl:
2537 case CMD_throw:
2538 case CMD_tilde:
2539 case CMD_topleft:
2540 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002541 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002542 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 case CMD_verbose:
2544 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002545 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 break;
2547
2548 default: goto doend;
2549 }
2550 }
2551#endif
2552
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002553 if ((ea.argt & EX_XFILE)
2554 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2555 goto doend;
2556
2557#ifdef FEAT_EVAL
2558 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002560 emsg(_(e_invalid_command_after_export));
2561 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 /*
2566 * Accept buffer name. Cannot be used at the same time with a buffer
2567 * number. Don't do this for a user command.
2568 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002569 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002570 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 {
2572 /*
2573 * :bdelete, :bwipeout and :bunload take several arguments, separated
2574 * by spaces: find next space (skipping over escaped characters).
2575 * The others take one argument: ignore trailing spaces.
2576 */
2577 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2578 || ea.cmdidx == CMD_bunload)
2579 p = skiptowhite_esc(ea.arg);
2580 else
2581 {
2582 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002583 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 --p;
2585 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002586 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002587 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002588 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 goto doend;
2590 ea.addr_count = 1;
2591 ea.arg = skipwhite(p);
2592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002594 // The :try command saves the emsg_silent flag, reset it here when
2595 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002596 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002597 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002598 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002599 if (emsg_silent < 0)
2600 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002601 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002602 }
2603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002605 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607
Bram Moolenaar958636c2014-10-21 20:01:58 +02002608 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
2610 /*
2611 * Execute a user-defined command.
2612 */
2613 do_ucmd(&ea);
2614 }
2615 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
2617 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002618 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2621 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002622 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
2624
2625#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002626 // A command will reset "is_export" when exporting an item. If it is still
LemonBoy90c27b22023-08-27 19:28:15 +02002627 // set something went wrong or the command was never executed.
2628 if (!ea.skip && is_export)
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002629 {
2630 if (errormsg == NULL)
2631 errormsg = _(e_export_with_invalid_argument);
2632 is_export = FALSE;
2633 }
2634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002636 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002637 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002638 && current_sctx.sc_sid > 0
2639 && ea.cmdidx != CMD_endif
2640 && (cstack->cs_idx < 0
2641 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002642 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 /*
2645 * If the command just executed called do_cmdline(), any throw or ":return"
2646 * or ":finish" encountered there must also check the cstack of the still
2647 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2648 * exception, or reanimate a returned function or finished script file and
2649 * return or finish it again.
2650 */
2651 if (need_rethrow)
2652 do_throw(cstack);
2653 else if (check_cstack)
2654 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002655 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002657 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 && current_func_returned())
2659 do_return(&ea, TRUE, FALSE, NULL);
2660 }
2661 need_rethrow = check_cstack = FALSE;
2662#endif
2663
2664doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002665 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002666 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002668 curwin->w_cursor.col = 0;
2669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670
2671 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2672 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002673 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002675 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
2677 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002678 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002680 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682 emsg(errormsg);
2683 }
2684#ifdef FEAT_EVAL
2685 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002686 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2687 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002688
2689 if (did_set_expr_line)
2690 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002691 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692#endif
2693
Bram Moolenaare1004402020-10-24 20:49:43 +02002694 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002696 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002697 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002699 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 ea.nextcmd = NULL;
2701
2702#ifdef FEAT_EVAL
2703 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002704 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705#endif
2706
2707 return ea.nextcmd;
2708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002710static char ex_error_buf[MSG_BUF_LEN];
2711
2712/*
2713 * Return an error message with argument included.
2714 * Uses a static buffer, only the last error will be kept.
2715 * "msg" will be translated, caller should use N_().
2716 */
2717 char *
2718ex_errmsg(char *msg, char_u *arg)
2719{
2720 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2721 return ex_error_buf;
2722}
2723
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724/*
zeertzjq7d664bf2024-07-14 10:22:54 +02002725 * The "+" string used in place of an empty command in Ex mode.
2726 * This string is used in pointer comparison.
2727 */
2728static char exmode_plus[] = "+";
2729
2730/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002731 * Handle a range without a command.
2732 * Returns an error message on failure.
2733 */
2734 char *
2735ex_range_without_command(exarg_T *eap)
2736{
2737 char *errormsg = NULL;
2738
zeertzjq7d664bf2024-07-14 10:22:54 +02002739 if ((*eap->cmd == '|' ||
2740 (exmode_active && eap->cmd != (char_u *)exmode_plus + 1))
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002741#ifdef FEAT_EVAL
2742 && !in_vim9script()
2743#endif
2744 )
2745 {
2746 eap->cmdidx = CMD_print;
2747 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2748 if ((errormsg = invalid_range(eap)) == NULL)
2749 {
2750 correct_range(eap);
2751 ex_print(eap);
2752 }
2753 }
2754 else if (eap->addr_count != 0)
2755 {
2756 if (eap->line2 > curbuf->b_ml.ml_line_count)
2757 {
2758 // With '-' in 'cpoptions' a line number past the file is an
2759 // error, otherwise put it at the end of the file.
2760 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2761 eap->line2 = -1;
2762 else
2763 eap->line2 = curbuf->b_ml.ml_line_count;
2764 }
2765
2766 if (eap->line2 < 0)
2767 errormsg = _(e_invalid_range);
2768 else
2769 {
2770 if (eap->line2 == 0)
2771 curwin->w_cursor.lnum = 1;
2772 else
2773 curwin->w_cursor.lnum = eap->line2;
2774 beginline(BL_SOL | BL_FIX);
2775 }
2776 }
2777 return errormsg;
2778}
2779
2780/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002781 * Check for an Ex command with optional tail.
2782 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002783 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002784 */
2785 static int
2786checkforcmd_opt(
2787 char_u **pp, // start of command
2788 char *cmd, // name of command
2789 int len, // required length
2790 int noparen)
2791{
2792 int i;
2793
2794 for (i = 0; cmd[i] != NUL; ++i)
2795 if (((char_u *)cmd)[i] != (*pp)[i])
2796 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002797 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002798 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002799 {
2800 *pp = skipwhite(*pp + i);
2801 return TRUE;
2802 }
2803 return FALSE;
2804}
2805
2806/*
2807 * Check for an Ex command with optional tail.
2808 * If there is a match advance "pp" to the argument and return TRUE.
2809 */
2810 int
2811checkforcmd(
2812 char_u **pp, // start of command
2813 char *cmd, // name of command
2814 int len) // required length
2815{
2816 return checkforcmd_opt(pp, cmd, len, FALSE);
2817}
2818
2819/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002820 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002821 * If there is a match advance "pp" to the argument and return TRUE.
2822 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002823 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002824checkforcmd_noparen(
2825 char_u **pp, // start of command
2826 char *cmd, // name of command
2827 int len) // required length
2828{
2829 return checkforcmd_opt(pp, cmd, len, TRUE);
2830}
2831
2832/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002833 * Parse and skip over command modifiers:
2834 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002835 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002837 * When "skip_only" is TRUE the global variables are not changed, except for
2838 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002839 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2840 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002841 * Call apply_cmdmod() to get the side effects of the modifiers:
2842 * - Increment "sandbox" for ":sandbox"
2843 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002844 * - set msg_silent for ":silent"
2845 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 * Return FAIL when the command is not to be executed.
2847 * May set "errormsg" to an error message.
2848 */
2849 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002850parse_command_modifiers(
2851 exarg_T *eap,
2852 char **errormsg,
2853 cmdmod_T *cmod,
2854 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002855{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002856 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002857 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002858 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002859 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002860 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002861 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002862
Bram Moolenaare1004402020-10-24 20:49:43 +02002863 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002864 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002865
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002866 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2867 {
2868 // The automatically inserted Visual area range is skipped, so that
2869 // typing ":cmdmod cmd" in Visual mode works without having to move the
zeertzjqd9be94c2024-07-14 10:20:20 +02002870 // range to after the modifiers. The command will be "'<,'>cmdmod cmd",
2871 // parse "cmdmod cmd" and then put back "'<,'>" before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002872 eap->cmd += 5;
2873 cmd_start = eap->cmd;
2874 has_visual_range = TRUE;
2875 }
2876
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002877 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002878 for (;;)
2879 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002880 char_u *p;
2881
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002883 {
2884 if (*eap->cmd == ':')
2885 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002887 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002888
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002889 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002890 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002891 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2892 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002893 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2894 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002895 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002896 if (!skip_only)
2897 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002898 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002899 }
2900
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002901 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002902 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002903 {
2904 // a comment ends at a NL
2905 if (eap->nextcmd == NULL)
2906 {
2907 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2908 if (eap->nextcmd != NULL)
2909 ++eap->nextcmd;
2910 }
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 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002923 if (*eap->cmd == NUL)
2924 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002925 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002926 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002927 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002928 if (vim9script && has_cmdmod(cmod, FALSE))
2929 *errormsg = _(e_command_modifier_without_command);
2930 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002931 return FAIL;
2932 }
2933
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002934 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002935
2936 // In Vim9 script a variable can shadow a command modifier:
2937 // verbose = 123
2938 // verbose += 123
2939 // silent! verbose = func()
2940 // verbose.member = 2
2941 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002942 // But not:
2943 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002944 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002945 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002946 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002947
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002948 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002949 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002950 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002951 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2952 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002953 break;
2954 }
2955
Bram Moolenaareffed932018-08-14 13:38:17 +02002956 switch (*p)
2957 {
John Marriotted908f72024-04-18 22:46:56 +02002958 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002959 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002960 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002961 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002962 continue;
2963
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002964 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002965 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002966 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002967 continue;
2968 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002969 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002970 {
2971#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002972 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002973#endif
2974 continue;
2975 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002976 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002977 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002978 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002979 continue;
2980
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002981 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002982 break;
2983#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002984 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002985#endif
2986 continue;
2987
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002988 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002990 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002991 continue;
2992 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002993 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002995 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002996 continue;
2997 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002998 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003000 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003001 continue;
3002 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003003 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003005 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003006 continue;
3007
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003008 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003010 char_u *reg_pat;
3011 char_u *nulp = NULL;
3012 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003013
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003014 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003015 || *p == NUL
3016 || (ends_excmd(*p)
3017#ifdef FEAT_EVAL
3018 // in ":filter #pat# cmd" # does not
3019 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003020 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003021#endif
3022 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003023 break;
3024 if (*p == '!')
3025 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003026 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003027 p = skipwhite(p + 1);
3028 if (*p == NUL || ends_excmd(*p))
3029 break;
3030 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003031#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003032 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003033 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003034 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003035#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003036 if (skip_only)
3037 p = skip_vimgrep_pat(p, NULL, NULL);
3038 else
3039 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003040 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3041 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003042 if (p == NULL || *p == NUL)
3043 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003044 if (!skip_only)
3045 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003046 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003047 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003048 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003049 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003050 // restore the character overwritten by NUL
3051 if (nulp != NULL)
3052 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003053 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003054 eap->cmd = p;
3055 continue;
3056 }
3057
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003058 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3059 {
3060 cmod->cmod_split |= WSP_HOR;
3061 continue;
3062 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003063 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003064 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003065 || *p == NUL || ends_excmd(*p))
3066 break;
3067 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003068 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003069 continue;
3070
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003071 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003072 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003073 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003074 continue;
3075 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003076 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3077 {
3078 if (ends_excmd2(p, eap->cmd))
3079 {
3080 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003081 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003082 return FAIL;
3083 }
3084 cmod->cmod_flags |= CMOD_LEGACY;
3085 continue;
3086 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003087
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003088 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003089 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003090 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003091 continue;
3092
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003093 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003094 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003095 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003096 continue;
3097 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003098 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003099 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003100 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003101 continue;
3102
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003103 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003104 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003105 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003106 continue;
3107
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003108 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003109 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003110 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003111 continue;
3112 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003113 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003114 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003115 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003116 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3117 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003118 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003119 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003120 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003121 }
3122 continue;
3123
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003124 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003125 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003126 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003127 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003128 long tabnr = get_address(eap, &eap->cmd,
3129 ADDR_TABS, eap->skip,
3130 skip_only, FALSE, 1);
3131 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003132 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003133 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003134 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003135 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3136 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003137 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003138 return FAIL;
3139 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003140 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003141 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003142 }
3143 eap->cmd = p;
3144 continue;
3145 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003146 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003147 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003148 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003149 continue;
3150
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003151 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003152 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003153 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003154 continue;
3155
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003156 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003157 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003158 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003159 continue;
3160 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003161 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003162 {
3163 if (ends_excmd2(p, eap->cmd))
3164 {
3165 *errormsg =
3166 _(e_vim9cmd_must_be_followed_by_command);
3167 return FAIL;
3168 }
3169 cmod->cmod_flags |= CMOD_VIM9CMD;
3170 continue;
3171 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003172 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003173 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003174 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003175 {
zeertzjqcd749632022-06-14 13:30:35 +01003176 // zero means not set, one is verbose == 0, etc.
3177 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003178 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003179 else
zeertzjqcd749632022-06-14 13:30:35 +01003180 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003181 eap->cmd = p;
3182 continue;
3183 }
3184 break;
3185 }
3186
Bram Moolenaar1501b632022-03-27 16:56:21 +01003187 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003188 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003189 if (eap->cmd > cmd_start)
3190 {
3191 // Move the '<,'> range to after the modifiers and insert a colon.
3192 // Since the modifiers have been parsed put the colon on top of the
3193 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3194 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003195 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003196 {
3197 size_t len = STRLEN(cmd_start);
3198
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003199 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003200 // "'<,'>mods" -> "mods *+
3201 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003202 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003203 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003204 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003205 }
3206 else
3207 {
3208 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3209 eap->cmd -= 5;
3210 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3211 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003212 }
3213 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003214 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003215 // Special case: change empty command to "+".
3216 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003217 eap->cmd = (char_u *)"'<,'>+";
3218 else
3219 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003220 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003221 else if (use_plus_cmd)
zeertzjq7d664bf2024-07-14 10:22:54 +02003222 eap->cmd = (char_u *)exmode_plus;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003223
Bram Moolenaareffed932018-08-14 13:38:17 +02003224 return OK;
3225}
3226
3227/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003228 * Return TRUE if "cmod" has anything set.
3229 */
3230 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003231has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003232{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003233 return (cmod->cmod_flags != 0 && (!ignore_silent
3234 || (cmod->cmod_flags
3235 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003236 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003237 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003238 || cmod->cmod_tab != 0
3239 || cmod->cmod_filter_regmatch.regprog != NULL;
3240}
3241
Bram Moolenaar8991be22022-02-14 21:51:46 +00003242#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003243/*
3244 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3245 */
3246 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003247cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003248{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003249 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003250 {
3251 emsg(_(e_misplaced_command_modifier));
3252 return TRUE;
3253 }
3254 return FALSE;
3255}
Dominique Pelle748b3082022-01-08 12:41:16 +00003256#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003257
3258/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003259 * Apply the command modifiers. Saves current state in "cmdmod", call
3260 * undo_cmdmod() later.
3261 */
3262 void
3263apply_cmdmod(cmdmod_T *cmod)
3264{
3265#ifdef HAVE_SANDBOX
3266 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3267 {
3268 ++sandbox;
3269 cmod->cmod_did_sandbox = TRUE;
3270 }
3271#endif
zeertzjqcd749632022-06-14 13:30:35 +01003272 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003273 {
3274 if (cmod->cmod_verbose_save == 0)
3275 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003276 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003277 }
3278
3279 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3280 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003281 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003282 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003283 cmod->cmod_save_msg_scroll = msg_scroll;
3284 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003285 if (cmod->cmod_flags & CMOD_SILENT)
3286 ++msg_silent;
3287 if (cmod->cmod_flags & CMOD_UNSILENT)
3288 msg_silent = 0;
3289
3290 if (cmod->cmod_flags & CMOD_ERRSILENT)
3291 {
3292 ++emsg_silent;
3293 ++cmod->cmod_did_esilent;
3294 }
3295
Bram Moolenaare1004402020-10-24 20:49:43 +02003296 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003297 {
3298 // Set 'eventignore' to "all".
3299 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003300 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003301 set_string_option_direct((char_u *)"ei", -1,
3302 (char_u *)"all", OPT_FREE, SID_NONE);
3303 }
3304}
3305
3306/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003307 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003308 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003309 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003310undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003311{
Bram Moolenaare1004402020-10-24 20:49:43 +02003312 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003313 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003314 p_verbose = cmod->cmod_verbose_save - 1;
3315 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003316 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003317
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003318#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003319 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003320 {
3321 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003322 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003323 }
3324#endif
3325
Bram Moolenaare1004402020-10-24 20:49:43 +02003326 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003327 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003328 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003329 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3330 OPT_FREE, SID_NONE);
3331 free_string_option(cmod->cmod_save_ei);
3332 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003333 }
3334
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003335 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003336
Bram Moolenaare1004402020-10-24 20:49:43 +02003337 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003338 {
3339 // messages could be enabled for a serious error, need to check if the
3340 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003341 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3342 msg_silent = cmod->cmod_save_msg_silent - 1;
3343 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003344 if (emsg_silent < 0)
3345 emsg_silent = 0;
3346 // Restore msg_scroll, it's set by file I/O commands, even when no
3347 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003348 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003349
3350 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3351 // somewhere in the line. Put it back in the first column.
3352 if (redirecting())
3353 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003354
Bram Moolenaare1004402020-10-24 20:49:43 +02003355 cmod->cmod_save_msg_silent = 0;
3356 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003357 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003358}
3359
3360/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003361 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003362 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003363 * Return FAIL and set "errormsg" or return OK.
3364 */
3365 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003366parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003367{
3368 int address_count = 1;
3369 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003370 int need_check_cursor = FALSE;
3371 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003372
3373 // Repeat for all ',' or ';' separated addresses.
3374 for (;;)
3375 {
3376 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003377 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003378 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003379 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003380 eap->addr_count == 0, address_count++);
3381 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003382 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003383 if (lnum == MAXLNUM)
3384 {
3385 if (*eap->cmd == '%') // '%' - all lines
3386 {
3387 ++eap->cmd;
3388 switch (eap->addr_type)
3389 {
3390 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003391 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003392 eap->line1 = 1;
3393 eap->line2 = curbuf->b_ml.ml_line_count;
3394 break;
3395 case ADDR_LOADED_BUFFERS:
3396 {
3397 buf_T *buf = firstbuf;
3398
3399 while (buf->b_next != NULL
3400 && buf->b_ml.ml_mfp == NULL)
3401 buf = buf->b_next;
3402 eap->line1 = buf->b_fnum;
3403 buf = lastbuf;
3404 while (buf->b_prev != NULL
3405 && buf->b_ml.ml_mfp == NULL)
3406 buf = buf->b_prev;
3407 eap->line2 = buf->b_fnum;
3408 break;
3409 }
3410 case ADDR_BUFFERS:
3411 eap->line1 = firstbuf->b_fnum;
3412 eap->line2 = lastbuf->b_fnum;
3413 break;
3414 case ADDR_WINDOWS:
3415 case ADDR_TABS:
3416 if (IS_USER_CMDIDX(eap->cmdidx))
3417 {
3418 eap->line1 = 1;
3419 eap->line2 = eap->addr_type == ADDR_WINDOWS
3420 ? LAST_WIN_NR : LAST_TAB_NR;
3421 }
3422 else
3423 {
3424 // there is no Vim command which uses '%' and
3425 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003426 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003427 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003428 }
3429 break;
3430 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003431 case ADDR_UNSIGNED:
3432 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003433 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003434 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003435 case ADDR_ARGUMENTS:
3436 if (ARGCOUNT == 0)
3437 eap->line1 = eap->line2 = 0;
3438 else
3439 {
3440 eap->line1 = 1;
3441 eap->line2 = ARGCOUNT;
3442 }
3443 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003444 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003445#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003446 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003447 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003448 if (eap->line2 == 0)
3449 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003450#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003451 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003452 case ADDR_NONE:
3453 // Will give an error later if a range is found.
3454 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003455 }
3456 ++eap->addr_count;
3457 }
3458 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3459 {
3460 pos_T *fp;
3461
3462 // '*' - visual area
3463 if (eap->addr_type != ADDR_LINES)
3464 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003465 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003466 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003467 }
3468
3469 ++eap->cmd;
3470 if (!eap->skip)
3471 {
3472 fp = getmark('<', FALSE);
3473 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003474 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003475 eap->line1 = fp->lnum;
3476 fp = getmark('>', FALSE);
3477 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003478 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003479 eap->line2 = fp->lnum;
3480 ++eap->addr_count;
3481 }
3482 }
3483 }
3484 else
3485 eap->line2 = lnum;
3486 eap->addr_count++;
3487
3488 if (*eap->cmd == ';')
3489 {
3490 if (!eap->skip)
3491 {
3492 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003493
Bram Moolenaar0e717042020-04-27 19:29:01 +02003494 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003495 // accept zero as address, so 0;/PATTERN/ works correctly
3496 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003497 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003498 if (eap->line2 > 0)
3499 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003500 else
3501 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003502 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003503 }
3504 }
3505 else if (*eap->cmd != ',')
3506 break;
3507 ++eap->cmd;
3508 }
3509
3510 // One address given: set start and end lines.
3511 if (eap->addr_count == 1)
3512 {
3513 eap->line1 = eap->line2;
3514 // ... but only implicit: really no address given
3515 if (lnum == MAXLNUM)
3516 eap->addr_count = 0;
3517 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003518 ret = OK;
3519
3520theend:
3521 if (need_check_cursor)
3522 check_cursor();
3523 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003524}
3525
3526/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003527 * Append "cmd" to the error message in IObuff.
3528 * Takes care of limiting the length and handling 0xa0, which would be
3529 * invisible otherwise.
3530 */
3531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003532append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003533{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003534 size_t len = STRLEN(IObuff);
3535 char_u *s = cmd;
3536 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003537
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003538 if (len > IOSIZE - 100)
3539 {
3540 // Not enough space, truncate and put in "...".
3541 d = IObuff + IOSIZE - 100;
3542 d -= mb_head_off(IObuff, d);
3543 STRCPY(d, "...");
3544 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003545 STRCAT(IObuff, ": ");
3546 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003547 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003548 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003549 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003550 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003551 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003552 STRCPY(d, "<a0>");
3553 d += 4;
3554 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003555 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3556 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003557 else
3558 MB_COPY_CHAR(s, d);
3559 }
3560 *d = NUL;
3561}
3562
Dominique Pelle748b3082022-01-08 12:41:16 +00003563#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003564/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003565 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3566 * the name. Otherwise just return "start".
3567 */
3568 char_u *
3569skip_option_env_lead(char_u *start)
3570{
3571 char_u *name = start;
3572
3573 if (*start == '&')
3574 {
3575 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3576 name += 3;
3577 else
3578 name += 1;
3579 }
3580 else if (*start == '$')
3581 name += 1;
3582 return name;
3583}
Dominique Pelle748b3082022-01-08 12:41:16 +00003584#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003585
3586/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003587 * Return TRUE and set "*idx" if "p" points to a one letter command.
3588 * If not in Vim9 script:
3589 * - The 'k' command can directly be followed by any character.
3590 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3591 * but :sre[wind] is another command, as are :scr[iptnames],
3592 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3593 */
3594 static int
3595one_letter_cmd(char_u *p, cmdidx_T *idx)
3596{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003597 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003598 return FALSE;
3599 if (*p == 'k')
3600 {
3601 *idx = CMD_k;
3602 return TRUE;
3603 }
3604 if (p[0] == 's'
3605 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3606 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3607 || p[1] == 'g'
3608 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3609 || p[1] == 'I'
3610 || (p[1] == 'r' && p[2] != 'e')))
3611 {
3612 *idx = CMD_substitute;
3613 return TRUE;
3614 }
3615 return FALSE;
3616}
3617
Dominique Pellee764d1b2023-03-12 21:20:59 +00003618#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003619/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003620 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3621 * call.
3622 */
3623 int
3624number_method(char_u *cmd)
3625{
3626 char_u *p = skipdigits(cmd);
3627
3628 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3629}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003630#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003631
3632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003634 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003635 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003636 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 *
3638 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3639 * assignment without "let". Sets eap->cmdidx to the command while returning
3640 * "eap->cmd".
3641 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 * Returns NULL for an ambiguous user command.
3643 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 char_u *
3645find_ex_command(
3646 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003647 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003648 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003649 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
3651 int len;
3652 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003653 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003654#ifndef FEAT_EVAL
3655 int vim9 = FALSE;
3656#else
3657 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 /*
3660 * Recognize a Vim9 script function/method call and assignment:
3661 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3662 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003663 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003664 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003666 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003668 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003669 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3670 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003672 int oplen;
3673 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003674 char_u *swp;
3675
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003676 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003677 || (eap->cmd[0] == '$'
3678 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003679 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003680 && (valid_yank_reg(eap->cmd[1], FALSE)
3681 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003682 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003683 if (*eap->cmd == '&')
3684 {
3685 p = eap->cmd + 1;
3686 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3687 p += 2;
3688 p = to_name_end(p, FALSE);
3689 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003690 else if (*eap->cmd == '$')
3691 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003692 else
3693 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003694 if (ends_excmd(*skipwhite(p)))
3695 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003696 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3697 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003698 eap->cmdidx = CMD_eval;
3699 return eap->cmd;
3700 }
3701 // "&option" can be followed by "->" or "=", check below
3702 }
3703
3704 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003706 if (
3707 // "(..." is an expression.
3708 // "funcname(" is always a function call.
3709 *p == '('
3710 || (p == eap->cmd
3711 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003712 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003713 *eap->cmd == '{'
3714 // "'string'->func()" is an expression.
3715 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003716 // '"string"->func()' is an expression.
3717 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003718 // '$"string"->func()' is an expression.
3719 // "$'string'->func()" is an expression.
3720 || (eap->cmd[0] == '$'
3721 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3722 // '0z1234->func()' is an expression.
3723 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003724 // "g:varname" is an expression.
3725 || eap->cmd[1] == ':'
3726 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003727 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003728 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003729 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003730 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3731 {
3732 // "{" by itself is the start of a block.
3733 eap->cmdidx = CMD_block;
3734 return eap->cmd + 1;
3735 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003736 eap->cmdidx = CMD_eval;
3737 return eap->cmd;
3738 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003739
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003740 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003741 // "varname[]" is an expression.
3742 *p == '['
3743 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003744 || (*p == '.'
3745 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3746 // g:[key] is an expression
3747 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003748 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003749 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003750
3751 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003752 // Skip over the whole thing, it can be:
3753 // name.member = val
3754 // name[a : b] = val
3755 // name[idx] = val
3756 // name[idx].member = val
3757 // etc.
3758 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003759 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003760 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003761 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003762 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003763 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003764 || (after[0] == '.' && after[1] == '.'
3765 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003766 eap->cmdidx = CMD_var;
3767 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003768 --emsg_silent;
3769 return eap->cmd;
3770 }
3771
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003772 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3773 // an assignment.
3774 // If there is no line break inside the "[...]" then "p" is
3775 // advanced to after the "]" by to_name_const_end(): check if a "="
3776 // follows.
3777 // If "[...]" has a line break "p" still points at the "[" and it
3778 // can't be an assignment.
3779 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003780 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003781 char_u *eq;
3782
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003783 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003784 if (p == eap->cmd && *p == '[')
3785 {
3786 int count = 0;
3787 int semicolon = FALSE;
3788
3789 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3790 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003791 eq = p;
3792 if (eq != NULL)
3793 {
3794 eq = skipwhite(eq);
3795 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3796 ++eq;
3797 }
3798 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003799 {
3800 eap->cmdidx = CMD_eval;
3801 return eap->cmd;
3802 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003803 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003804 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003805 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003806 return eap->cmd;
3807 }
3808 }
3809
3810 // Recognize an assignment if we recognize the variable name:
3811 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003812 // "@r = expr"
3813 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003814 // "var = expr" where "var" is a variable name or we are skipping
3815 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003816 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003817 oplen = assignment_len(skipwhite(p), &heredoc);
3818 if (oplen > 0)
3819 {
3820 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3821 || *eap->cmd == '&'
3822 || *eap->cmd == '$'
3823 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003824 || (eap->skip && IS_WHITE_OR_NUL(
3825 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003826 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003827 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003828 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003829 return eap->cmd;
3830 }
3831 }
3832
Bram Moolenaar9510d222022-09-11 15:14:05 +01003833 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003834 // "w:varname: number = 123".
3835 if (eap->cmd[1] == ':' && *p == ':')
3836 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003837 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003838 return eap->cmd;
3839 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003840 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003841
Bram Moolenaar31d99482022-05-26 22:24:43 +01003842 // 1234->func() is a method call
3843 if (number_method(eap->cmd))
3844 {
3845 eap->cmdidx = CMD_eval;
3846 return eap->cmd;
3847 }
3848
Bram Moolenaar7b829262021-10-13 15:04:34 +01003849 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3850 // an expression. A global/substitute/list command needs to use a
3851 // longer name.
3852 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3853 {
3854 eap->cmdidx = CMD_eval;
3855 return eap->cmd;
3856 }
3857
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003858 // If it is an ID it might be a variable with an operator on the next
3859 // line, if the variable exists it can't be an Ex command.
3860 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003861 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003862 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3863 {
3864 eap->cmdidx = CMD_eval;
3865 return eap->cmd;
3866 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003867
3868 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003869 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3870 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003871 {
3872 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3873 return eap->cmd + 2;
3874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 }
3876#endif
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 /*
3879 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 */
3881 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003882 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 ++p;
3885 }
3886 else
3887 {
3888 while (ASCII_ISALPHA(*p))
3889 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003890 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003891 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003893 while (ASCII_ISALNUM(*p))
3894 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 }
3896 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3897 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003898 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 ++p;
3900 while (ASCII_ISALPHA(*p))
3901 ++p;
3902 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003903
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003904 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003905 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 ++p;
3907 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003908 // The "d" command can directly be followed by 'l' or 'p' flag, when
3909 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003910 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003911 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003912 // Check for ":dl", ":dell", etc. to ":deletel": that's
3913 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003914 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003915 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003916 break;
3917 if (i == len - 1)
3918 {
3919 --len;
3920 if (p[-1] == 'l')
3921 eap->flags |= EXFLAG_LIST;
3922 else
3923 eap->flags |= EXFLAG_PRINT;
3924 }
3925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003927 if (ASCII_ISLOWER(eap->cmd[0]))
3928 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003929 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003930 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003931
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003932 if (command_count != (int)CMD_SIZE)
3933 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003934 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003935 getout(1);
3936 }
3937
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003938 // Use a precomputed index for fast look-up in cmdnames[]
3939 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003940 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3941 if (ASCII_ISLOWER(c2))
3942 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3943 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003944 else if (ASCII_ISUPPER(eap->cmd[0]))
3945 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003947 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948
3949 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3950 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3951 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3952 (size_t)len) == 0)
3953 {
3954#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003955 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 *full = TRUE;
3957#endif
3958 break;
3959 }
3960
Bram Moolenaar204852a2022-03-05 12:56:44 +00003961 // :Print and :mode are not supported in Vim9 script.
3962 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003963 if (vim9 && eap->cmdidx != CMD_SIZE)
3964 {
3965 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3966 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003967 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003968 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003969 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003970 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003971 eap->cmdidx = CMD_SIZE;
3972 }
3973 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003974
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003975 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003976 // 'cpoptions'.
3977 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3978 p = eap->cmd;
3979
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003980 // Look for a user defined command as a last resort. Let ":Print" be
3981 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003982 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3983 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003985 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 while (ASCII_ISALNUM(*p))
3987 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003988 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003990 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 eap->cmdidx = CMD_SIZE;
3992 }
3993
Bram Moolenaar204852a2022-03-05 12:56:44 +00003994 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3995 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003996 eap->cmdidx = CMD_finally;
3997
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003998#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003999 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004000 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02004001 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004002 && (eap->cmdidx < 0 ||
4003 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004004 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004005 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4006
4007 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004008 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004009 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004010 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004011#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004012
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return p;
4014}
4015
4016#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004017typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004018{
4019 char *name;
4020 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004021 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004022} cmdmod_info_T;
4023
4024static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004025 {"aboveleft", 3, FALSE},
4026 {"belowright", 3, FALSE},
4027 {"botright", 2, FALSE},
4028 {"browse", 3, FALSE},
4029 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004030 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004031 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004032 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004033 {"keepalt", 5, FALSE},
4034 {"keepjumps", 5, FALSE},
4035 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004036 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004037 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004038 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004039 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004040 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004041 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004042 {"rightbelow", 6, FALSE},
4043 {"sandbox", 3, FALSE},
4044 {"silent", 3, FALSE},
4045 {"tab", 3, TRUE},
4046 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004047 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004048 {"verbose", 4, TRUE},
4049 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004050 {"vim9cmd", 4, FALSE}
4051}; // cmdmod_info_tab
4052
4053// compare two cmdmod_info_T structs by case sensitive name with length
4054 static int
4055cmp_cmdmod_info(const void *a, const void *b)
4056{
4057 cmdmod_info_T *cm1 = (cmdmod_info_T *)a;
4058 cmdmod_info_T *cm2 = (cmdmod_info_T *)b;
4059
4060 return STRNCMP(cm1->name, cm2->name, cm2->minlen);
4061}
Bram Moolenaared53fb92007-11-24 20:50:24 +00004062
4063/*
4064 * Return length of a command modifier (including optional count).
4065 * Return zero when it's not a modifier.
4066 */
4067 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004068modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004069{
Bram Moolenaared53fb92007-11-24 20:50:24 +00004070 char_u *p = cmd;
John Marriotted908f72024-04-18 22:46:56 +02004071 cmdmod_info_T target;
4072 cmdmod_info_T *entry;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004073
4074 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004075 p = skipwhite(skipdigits(cmd + 1));
John Marriotted908f72024-04-18 22:46:56 +02004076
4077 // only lowercase characters can match
4078 if (!ASCII_ISLOWER(*p))
4079 return 0;
4080
4081 target.name = (char *)p;
4082 target.minlen = 0; // not used, see cmp_cmdmod_info()
4083 target.has_count = FALSE;
4084
4085 entry = (cmdmod_info_T *)bsearch(&target, &cmdmod_info_tab, ARRAY_LENGTH(cmdmod_info_tab), sizeof(cmdmod_info_tab[0]), cmp_cmdmod_info);
4086 if (entry != NULL)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004087 {
John Marriotted908f72024-04-18 22:46:56 +02004088 int i;
4089
4090 for (i = entry->minlen; p[i] != NUL; ++i)
4091 {
4092 if (p[i] != entry->name[i])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004093 break;
John Marriotted908f72024-04-18 22:46:56 +02004094 }
4095
4096 if (!ASCII_ISALPHA(p[i]) && i >= entry->minlen && (p == cmd || entry->has_count))
4097 return i + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004098 }
John Marriotted908f72024-04-18 22:46:56 +02004099
Bram Moolenaared53fb92007-11-24 20:50:24 +00004100 return 0;
4101}
4102
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103/*
4104 * Return > 0 if an Ex command "name" exists.
4105 * Return 2 if there is an exact match.
4106 * Return 3 if there is an ambiguous match.
4107 */
4108 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004109cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110{
4111 exarg_T ea;
4112 int full = FALSE;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004113 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
John Marriotted908f72024-04-18 22:46:56 +02004115 // only lowercase characters can match
4116 if (ASCII_ISLOWER(*name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 {
John Marriotted908f72024-04-18 22:46:56 +02004118 cmdmod_info_T target;
4119 cmdmod_info_T *entry;
4120
4121 target.name = (char *)name;
4122 target.minlen = 0; // not used, see cmp_cmdmod_info()
4123 target.has_count = FALSE;
4124
4125 // Check command modifiers.
4126 entry = (cmdmod_info_T *)bsearch(&target, &cmdmod_info_tab, ARRAY_LENGTH(cmdmod_info_tab), sizeof(cmdmod_info_tab[0]), cmp_cmdmod_info);
4127 if (entry != NULL)
4128 {
4129 int i;
4130
4131 for (i = entry->minlen; name[i] != NUL; ++i)
4132 {
4133 if (name[i] != entry->name[i])
4134 break;
4135 }
4136
4137 if (name[i] == NUL && i >= entry->minlen)
4138 return (entry->name[i] == NUL ? 2 : 1);
4139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004142 // Check built-in commands and user defined commands.
4143 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004144 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004146 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004148 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004150 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4151 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004152 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004153 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4155}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004156
4157/*
4158 * "fullcommand" function
4159 */
4160 void
4161f_fullcommand(typval_T *argvars, typval_T *rettv)
4162{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004163 exarg_T ea;
4164 char_u *name;
4165 char_u *p;
4166 int vim9script = in_vim9script();
4167 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004168
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004169 rettv->v_type = VAR_STRING;
4170 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004171
Bram Moolenaaraa534142022-09-15 21:46:02 +01004172 if (in_vim9script()
4173 && (check_for_string_arg(argvars, 0) == FAIL
4174 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004175 return;
4176
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004177 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004178 if (name == NULL)
4179 return;
4180
Bram Moolenaaraa534142022-09-15 21:46:02 +01004181 if (argvars[1].v_type != VAR_UNKNOWN)
4182 {
4183 vim9script = tv_get_bool(&argvars[1]);
4184 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4185 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4186 }
4187
zeertzjqc024ed92022-01-04 16:22:52 +00004188 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004189 name++;
4190 name = skip_range(name, TRUE, NULL);
4191
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004192 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4193 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004194 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004195 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004196 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004197 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004198 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004199 goto theend;
4200
4201 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004202 {
4203 int res;
4204
4205 ++emsg_silent;
4206 res = not_in_vim9(&ea);
4207 --emsg_silent;
4208
4209 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004210 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004211 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004212
4213 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004214 ? get_user_command_name(ea.useridx, ea.cmdidx)
4215 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004216theend:
4217 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004218}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219#endif
4220
Bram Moolenaard0190392019-08-23 21:17:35 +02004221 cmdidx_T
4222excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223{
Bram Moolenaard0190392019-08-23 21:17:35 +02004224 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004226 if (!one_letter_cmd(cmd, &idx))
4227 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4228 idx = (cmdidx_T)((int)idx + 1))
4229 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4230 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231
Bram Moolenaard0190392019-08-23 21:17:35 +02004232 return idx;
4233}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
Bram Moolenaard0190392019-08-23 21:17:35 +02004235 long
4236excmd_get_argt(cmdidx_T idx)
4237{
4238 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239}
4240
4241/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004242 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 *
4244 * Backslashed delimiters after / or ? will be skipped, and commands will
4245 * not be expanded between /'s and ?'s or after "'".
4246 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004247 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 * Returns the "cmd" pointer advanced to beyond the range.
4249 */
4250 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004251skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004252 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004253 int skip_star, // skip "*" used for Visual range
4254 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004256 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004257 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004259 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004261 if (*cmd == '\\')
4262 {
4263 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4264 ++cmd;
4265 else
4266 break;
4267 }
4268 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004270 char_u *p = cmd;
4271
4272 // a quote is only valid at the start or after a separator
4273 while (p > cmd_start)
4274 {
4275 --p;
4276 if (!VIM_ISWHITE(*p))
4277 break;
4278 }
4279 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4280 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 if (*++cmd == NUL && ctx != NULL)
4282 *ctx = EXPAND_NOTHING;
4283 }
4284 else if (*cmd == '/' || *cmd == '?')
4285 {
4286 delim = *cmd++;
4287 while (*cmd != NUL && *cmd != delim)
4288 if (*cmd++ == '\\' && *cmd != NUL)
4289 ++cmd;
4290 if (*cmd == NUL && ctx != NULL)
4291 *ctx = EXPAND_NOTHING;
4292 }
4293 if (*cmd != NUL)
4294 ++cmd;
4295 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004296
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004297 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004298 while (*cmd == ':')
4299 cmd = skipwhite(cmd + 1);
4300
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004301 // Skip "*" used for Visual range.
4302 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4303 cmd = skipwhite(cmd + 1);
4304
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 return cmd;
4306}
4307
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004308 static void
4309addr_error(cmd_addr_T addr_type)
4310{
4311 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004312 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004313 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004314 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004315}
4316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004318 * Return the default address for an address type.
4319 */
4320 static linenr_T
4321default_address(exarg_T *eap)
4322{
4323 linenr_T lnum = 0;
4324
4325 switch (eap->addr_type)
4326 {
4327 case ADDR_LINES:
4328 case ADDR_OTHER:
4329 // Default is the cursor line number. Avoid using an invalid
4330 // line number though.
4331 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4332 lnum = curbuf->b_ml.ml_line_count;
4333 else
4334 lnum = curwin->w_cursor.lnum;
4335 break;
4336 case ADDR_WINDOWS:
4337 lnum = CURRENT_WIN_NR;
4338 break;
4339 case ADDR_ARGUMENTS:
4340 lnum = curwin->w_arg_idx + 1;
4341 if (lnum > ARGCOUNT)
4342 lnum = ARGCOUNT;
4343 break;
4344 case ADDR_LOADED_BUFFERS:
4345 case ADDR_BUFFERS:
4346 lnum = curbuf->b_fnum;
4347 break;
4348 case ADDR_TABS:
4349 lnum = CURRENT_TAB_NR;
4350 break;
4351 case ADDR_TABS_RELATIVE:
4352 case ADDR_UNSIGNED:
4353 lnum = 1;
4354 break;
4355 case ADDR_QUICKFIX:
4356#ifdef FEAT_QUICKFIX
4357 lnum = qf_get_cur_idx(eap);
4358#endif
4359 break;
4360 case ADDR_QUICKFIX_VALID:
4361#ifdef FEAT_QUICKFIX
4362 lnum = qf_get_cur_valid_idx(eap);
4363#endif
4364 break;
4365 case ADDR_NONE:
4366 // Will give an error later if a range is found.
4367 break;
4368 }
4369 return lnum;
4370}
4371
4372/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004373 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 *
4375 * Set ptr to the next character after the part that was interpreted.
4376 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004377 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 *
4379 * Return MAXLNUM when no Ex address was found.
4380 */
4381 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004382get_address(
4383 exarg_T *eap UNUSED,
4384 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004385 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004386 int skip, // only skip the address, don't use it
4387 int silent, // no errors or side effects
4388 int to_other_file, // flag: may jump to other file
4389 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
4391 int c;
4392 int i;
4393 long n;
4394 char_u *cmd;
4395 pos_T pos;
4396 pos_T *fp;
4397 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004398 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
4400 cmd = skipwhite(*ptr);
4401 lnum = MAXLNUM;
4402 do
4403 {
4404 switch (*cmd)
4405 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004406 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004407 ++cmd;
4408 switch (addr_type)
4409 {
4410 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004411 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 lnum = curwin->w_cursor.lnum;
4413 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004414 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004415 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004416 break;
4417 case ADDR_ARGUMENTS:
4418 lnum = curwin->w_arg_idx + 1;
4419 break;
4420 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004421 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004422 lnum = curbuf->b_fnum;
4423 break;
4424 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004425 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004426 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004427 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004428 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004429 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004430 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004431 cmd = NULL;
4432 goto error;
4433 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004434 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004435#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004436 lnum = qf_get_cur_idx(eap);
4437#endif
4438 break;
4439 case ADDR_QUICKFIX_VALID:
4440#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004441 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004442#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004443 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004444 }
4445 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004447 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 ++cmd;
4449 switch (addr_type)
4450 {
4451 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004452 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 lnum = curbuf->b_ml.ml_line_count;
4454 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004455 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004456 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 break;
4458 case ADDR_ARGUMENTS:
4459 lnum = ARGCOUNT;
4460 break;
4461 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004462 buf = lastbuf;
4463 while (buf->b_ml.ml_mfp == NULL)
4464 {
4465 if (buf->b_prev == NULL)
4466 break;
4467 buf = buf->b_prev;
4468 }
4469 lnum = buf->b_fnum;
4470 break;
4471 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 lnum = lastbuf->b_fnum;
4473 break;
4474 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004475 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004477 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004478 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004479 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004480 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004481 cmd = NULL;
4482 goto error;
4483 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004484 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004485#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004486 lnum = qf_get_size(eap);
4487 if (lnum == 0)
4488 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004489#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004490 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004491 case ADDR_QUICKFIX_VALID:
4492#ifdef FEAT_QUICKFIX
4493 lnum = qf_get_valid_size(eap);
4494 if (lnum == 0)
4495 lnum = 1;
4496#endif
4497 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004498 }
4499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004501 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004502 if (*++cmd == NUL)
4503 {
4504 cmd = NULL;
4505 goto error;
4506 }
4507 if (addr_type != ADDR_LINES)
4508 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004509 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004510 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004511 goto error;
4512 }
4513 if (skip)
4514 ++cmd;
4515 else
4516 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004517 // Only accept a mark in another file when it is
4518 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004519 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4520 ++cmd;
4521 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004522 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004523 lnum = curwin->w_cursor.lnum;
4524 else
4525 {
4526 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 {
4528 cmd = NULL;
4529 goto error;
4530 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004531 lnum = fp->lnum;
4532 }
4533 }
4534 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
4536 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004537 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004538 c = *cmd++;
4539 if (addr_type != ADDR_LINES)
4540 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004541 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004542 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004543 goto error;
4544 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004545 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004546 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004547 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004548 if (*cmd == c)
4549 ++cmd;
4550 }
4551 else
4552 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004553 int flags;
4554
4555 pos = curwin->w_cursor; // save curwin->w_cursor
4556
4557 // When '/' or '?' follows another address, start from
4558 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004559 if (lnum > 0 && lnum != MAXLNUM)
4560 curwin->w_cursor.lnum =
4561 lnum > curbuf->b_ml.ml_line_count
4562 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004563
4564 // Start a forward search at the end of the line (unless
4565 // before the first line).
4566 // Start a backward search at the start of the line.
4567 // This makes sure we never match in the current
4568 // line, and can match anywhere in the
4569 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004570 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004571 curwin->w_cursor.col = MAXCOL;
4572 else
4573 curwin->w_cursor.col = 0;
4574 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004575 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004576 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004577 {
4578 curwin->w_cursor = pos;
4579 cmd = NULL;
4580 goto error;
4581 }
4582 lnum = curwin->w_cursor.lnum;
4583 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004584 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004585 cmd += searchcmdlen;
4586 }
4587 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004589 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004590 ++cmd;
4591 if (addr_type != ADDR_LINES)
4592 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004593 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004594 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004595 goto error;
4596 }
4597 if (*cmd == '&')
4598 i = RE_SUBST;
4599 else if (*cmd == '?' || *cmd == '/')
4600 i = RE_SEARCH;
4601 else
4602 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004603 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004604 cmd = NULL;
4605 goto error;
4606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004608 if (!skip)
4609 {
4610 /*
4611 * When search follows another address, start from
4612 * there.
4613 */
4614 if (lnum != MAXLNUM)
4615 pos.lnum = lnum;
4616 else
4617 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004619 /*
4620 * Start the search just like for the above
4621 * do_search().
4622 */
4623 if (*cmd != '?')
4624 pos.col = MAXCOL;
4625 else
4626 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004627 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004628 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004629 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004630 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004631 lnum = pos.lnum;
4632 else
4633 {
4634 cmd = NULL;
4635 goto error;
4636 }
4637 }
4638 ++cmd;
4639 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
4641 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004642 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004643 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645
4646 for (;;)
4647 {
4648 cmd = skipwhite(cmd);
4649 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4650 break;
4651
4652 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004653 {
4654 switch (addr_type)
4655 {
4656 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004657 case ADDR_OTHER:
4658 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004659 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004660 break;
4661 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004662 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004663 break;
4664 case ADDR_ARGUMENTS:
4665 lnum = curwin->w_arg_idx + 1;
4666 break;
4667 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004668 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004669 lnum = curbuf->b_fnum;
4670 break;
4671 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004672 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004673 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004674 case ADDR_TABS_RELATIVE:
4675 lnum = 1;
4676 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004677 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004678#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004679 lnum = qf_get_cur_idx(eap);
4680#endif
4681 break;
4682 case ADDR_QUICKFIX_VALID:
4683#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004684 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004685#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004686 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004687 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004688 case ADDR_UNSIGNED:
4689 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004690 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004691 }
4692 }
4693
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004695 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 else
4697 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004698 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 n = 1;
4700 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004701 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004702 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004704 if (n == MAXLNUM)
4705 {
4706 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004707 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004708 goto error;
4709 }
4710 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004711
4712 if (addr_type == ADDR_TABS_RELATIVE)
4713 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004714 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004715 cmd = NULL;
4716 goto error;
4717 }
4718 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004719 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004720 lnum = compute_buffer_local_count(
4721 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004723 {
4724#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004725 // Relative line addressing: need to adjust for lines in a
4726 // closed fold after the first address.
4727 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4728 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004729 (void)hasFolding(lnum, NULL, &lnum);
4730#endif
4731 if (i == '-')
4732 lnum -= n;
4733 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004734 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004735 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004736 {
4737 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004738 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004739 goto error;
4740 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004741 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004742 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745 } while (*cmd == '/' || *cmd == '?');
4746
4747error:
4748 *ptr = cmd;
4749 return lnum;
4750}
4751
4752/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004753 * Set eap->line1 and eap->line2 to the whole range.
4754 * Used for commands with the EX_DFLALL flag and no range given.
4755 */
4756 static void
4757address_default_all(exarg_T *eap)
4758{
4759 eap->line1 = 1;
4760 switch (eap->addr_type)
4761 {
4762 case ADDR_LINES:
4763 case ADDR_OTHER:
4764 eap->line2 = curbuf->b_ml.ml_line_count;
4765 break;
4766 case ADDR_LOADED_BUFFERS:
4767 {
4768 buf_T *buf = firstbuf;
4769
4770 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4771 buf = buf->b_next;
4772 eap->line1 = buf->b_fnum;
4773 buf = lastbuf;
4774 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4775 buf = buf->b_prev;
4776 eap->line2 = buf->b_fnum;
4777 }
4778 break;
4779 case ADDR_BUFFERS:
4780 eap->line1 = firstbuf->b_fnum;
4781 eap->line2 = lastbuf->b_fnum;
4782 break;
4783 case ADDR_WINDOWS:
4784 eap->line2 = LAST_WIN_NR;
4785 break;
4786 case ADDR_TABS:
4787 eap->line2 = LAST_TAB_NR;
4788 break;
4789 case ADDR_TABS_RELATIVE:
4790 eap->line2 = 1;
4791 break;
4792 case ADDR_ARGUMENTS:
4793 if (ARGCOUNT == 0)
4794 eap->line1 = eap->line2 = 0;
4795 else
4796 eap->line2 = ARGCOUNT;
4797 break;
4798 case ADDR_QUICKFIX_VALID:
4799#ifdef FEAT_QUICKFIX
4800 eap->line2 = qf_get_valid_size(eap);
4801 if (eap->line2 == 0)
4802 eap->line2 = 1;
4803#endif
4804 break;
4805 case ADDR_NONE:
4806 case ADDR_UNSIGNED:
4807 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004808 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004809 break;
4810 }
4811}
4812
4813
4814/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004815 * Get flags from an Ex command argument.
4816 */
4817 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004818get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004819{
4820 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4821 {
4822 if (*eap->arg == 'l')
4823 eap->flags |= EXFLAG_LIST;
4824 else if (*eap->arg == 'p')
4825 eap->flags |= EXFLAG_PRINT;
4826 else
4827 eap->flags |= EXFLAG_NR;
4828 eap->arg = skipwhite(eap->arg + 1);
4829 }
4830}
4831
4832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 * Function called for command which is Not Implemented. NI!
4834 */
4835 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004836ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
4838 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004839 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004840 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841}
4842
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004843#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844/*
4845 * Function called for script command which is Not Implemented. NI!
4846 * Skips over ":perl <<EOF" constructs.
4847 */
4848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004849ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850{
4851 if (!eap->skip)
4852 ex_ni(eap);
4853 else
4854 vim_free(script_get(eap, eap->arg));
4855}
4856#endif
4857
4858/*
4859 * Check range in Ex command for validity.
4860 * Return NULL when valid, error message when invalid.
4861 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004862 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004863invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004865 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004866
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 if ( eap->line1 < 0
4868 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004869 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004870 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004871
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004872 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004873 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004874 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004875 {
4876 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004877 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004878#ifdef FEAT_DIFF
4879 + (eap->cmdidx == CMD_diffget)
4880#endif
4881 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004882 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004883 break;
4884 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004885 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004886 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004887 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004888 break;
4889 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004890 // Only a boundary check, not whether the buffers actually
4891 // exist.
4892 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004893 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004894 break;
4895 case ADDR_LOADED_BUFFERS:
4896 buf = firstbuf;
4897 while (buf->b_ml.ml_mfp == NULL)
4898 {
4899 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004900 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004901 buf = buf->b_next;
4902 }
4903 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004904 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004905 buf = lastbuf;
4906 while (buf->b_ml.ml_mfp == NULL)
4907 {
4908 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004909 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004910 buf = buf->b_prev;
4911 }
4912 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004913 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004914 break;
4915 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004916 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004917 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004918 break;
4919 case ADDR_TABS:
4920 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004921 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004922 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004923 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004924 case ADDR_OTHER:
4925 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004926 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004927 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004928#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004929 // No error for value that is too big, will use the last entry.
4930 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004931 {
4932 if (eap->addr_count == 0)
4933 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004934 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004935 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004936#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004937 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004938 case ADDR_QUICKFIX_VALID:
4939#ifdef FEAT_QUICKFIX
4940 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4941 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004942 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004943#endif
4944 break;
4945 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004946 case ADDR_NONE:
4947 // Will give an error elsewhere.
4948 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004949 }
4950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 return NULL;
4952}
4953
4954/*
4955 * Correct the range for zero line number, if required.
4956 */
4957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004958correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004960 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
4962 if (eap->line1 == 0)
4963 eap->line1 = 1;
4964 if (eap->line2 == 0)
4965 eap->line2 = 1;
4966 }
4967}
4968
Bram Moolenaar748bf032005-02-02 23:04:36 +00004969#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004970/*
4971 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4972 * pattern. Otherwise return eap->arg.
4973 */
4974 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004975skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004976{
4977 char_u *p = eap->arg;
4978
Bram Moolenaara37420f2006-02-04 22:37:47 +00004979 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4980 || eap->cmdidx == CMD_vimgrepadd
4981 || eap->cmdidx == CMD_lvimgrepadd
4982 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004983 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004984 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004985 if (p == NULL)
4986 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004987 }
4988 return p;
4989}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004990
4991/*
4992 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4993 * in the command line, so that things like % get expanded.
4994 */
4995 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004996replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004997{
4998 char_u *new_cmdline;
4999 char_u *program;
5000 char_u *pos;
5001 char_u *ptr;
5002 int len;
5003 int i;
5004
5005 /*
5006 * Don't do it when ":vimgrep" is used for ":grep".
5007 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00005008 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
5009 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
5010 || eap->cmdidx == CMD_grepadd
5011 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005012 && !grep_internal(eap->cmdidx))
5013 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00005014 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
5015 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005016 {
5017 if (*curbuf->b_p_gp == NUL)
5018 program = p_gp;
5019 else
5020 program = curbuf->b_p_gp;
5021 }
5022 else
5023 {
5024 if (*curbuf->b_p_mp == NUL)
5025 program = p_mp;
5026 else
5027 program = curbuf->b_p_mp;
5028 }
5029
5030 p = skipwhite(p);
5031
5032 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5033 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005034 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005035 i = 1;
5036 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
5037 ++i;
5038 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005039 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005040 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005041 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005042 ptr = new_cmdline;
5043 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5044 {
5045 i = (int)(pos - program);
5046 STRNCPY(ptr, program, i);
5047 STRCPY(ptr += i, p);
5048 ptr += len;
5049 program = pos + 2;
5050 }
5051 STRCPY(ptr, program);
5052 }
5053 else
5054 {
John Marriotted908f72024-04-18 22:46:56 +02005055 size_t program_len = STRLEN(program);
5056
5057 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005058 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005059 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005060 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005061 STRCPY(new_cmdline + program_len, " ");
5062 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005063 }
5064 msg_make(p);
5065
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005066 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005067 vim_free(*cmdlinep);
5068 *cmdlinep = new_cmdline;
5069 p = new_cmdline;
5070 }
5071 return p;
5072}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005073#endif
5074
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075/*
5076 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005077 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 * Return FAIL for failure, OK otherwise.
5079 */
5080 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005081expand_filename(
5082 exarg_T *eap,
5083 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005084 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005086 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005088 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 char_u *p;
5090 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005091 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092
Bram Moolenaar748bf032005-02-02 23:04:36 +00005093#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005094 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005095 p = skip_grep_pat(eap);
5096#else
5097 p = eap->arg;
5098#endif
5099
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 /*
5101 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5102 * the file name contains a wildcard it should not cause expanding.
5103 * (it will be expanded anyway if there is a wildcard before replacing).
5104 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005105 has_wildcards = mch_has_wildcard(p);
5106 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005108#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005109 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005110 if (p[0] == '`' && p[1] == '=')
5111 {
5112 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005113 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005114 if (*p == '`')
5115 ++p;
5116 continue;
5117 }
5118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 /*
5120 * Quick check if this cannot be the start of a special string.
5121 * Also removes backslash before '%', '#' and '<'.
5122 */
5123 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5124 {
5125 ++p;
5126 continue;
5127 }
5128
5129 /*
5130 * Try to find a match at this position.
5131 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005132 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005133 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005134 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005136 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 p += srclen;
5139 continue;
5140 }
5141
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005142 // Wildcards won't be expanded below, the replacement is taken
5143 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005144 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5145 {
5146 char_u *l = repl;
5147
5148 repl = expand_env_save(repl);
5149 vim_free(l);
5150 }
5151
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005152 // Need to escape white space et al. with a backslash.
5153 // Don't do this for:
5154 // - replacement that already has been escaped: "##"
5155 // - shell commands (may have to use quotes instead).
5156 // - non-unix systems when there is a single argument (spaces don't
5157 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005159 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 && eap->cmdidx != CMD_grep
5162 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005163 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005164 && eap->cmdidx != CMD_lgrep
5165 && eap->cmdidx != CMD_lgrepadd
5166 && eap->cmdidx != CMD_lmake
5167 && eap->cmdidx != CMD_make
5168 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005170 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171#endif
5172 )
5173 {
5174 char_u *l;
5175#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005176 // Don't escape a backslash here, because rem_backslash() doesn't
5177 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 static char_u *nobslash = (char_u *)" \t\"|";
5179# define ESCAPE_CHARS nobslash
5180#else
5181# define ESCAPE_CHARS escape_chars
5182#endif
5183
5184 for (l = repl; *l; ++l)
5185 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5186 {
5187 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5188 if (l != NULL)
5189 {
5190 vim_free(repl);
5191 repl = l;
5192 }
5193 break;
5194 }
5195 }
5196
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005197 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005198 if ((eap->usefilter || eap->cmdidx == CMD_bang
5199 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005200 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
5202 char_u *l;
5203
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005204 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 if (l != NULL)
5206 {
5207 vim_free(repl);
5208 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210 }
5211
Mike Williams51024bb2024-05-30 07:46:30 +02005212 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 vim_free(repl);
5214 if (p == NULL)
5215 return FAIL;
5216 }
5217
5218 /*
5219 * One file argument: Expand wildcards.
5220 * Don't do this with ":r !command" or ":w !command".
5221 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005222 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
5224 /*
5225 * May do this twice:
5226 * 1. Replace environment variables.
5227 * 2. Replace any other wildcards, remove backslashes.
5228 */
5229 for (n = 1; n <= 2; ++n)
5230 {
5231 if (n == 2)
5232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 /*
5234 * Halve the number of backslashes (this is Vi compatible).
5235 * For Unix and OS/2, when wildcards are expanded, this is
5236 * done by ExpandOne() below.
5237 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005238#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 if (!has_wildcards)
5240#endif
5241 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
5243
5244 if (has_wildcards)
5245 {
5246 if (n == 1)
5247 {
5248 /*
5249 * First loop: May expand environment variables. This
5250 * can be done much faster with expand_env() than with
5251 * something else (e.g., calling a shell).
5252 * After expanding environment variables, check again
5253 * if there are still wildcards present.
5254 */
5255 if (vim_strchr(eap->arg, '$') != NULL
5256 || vim_strchr(eap->arg, '~') != NULL)
5257 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005258 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005259 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 has_wildcards = mch_has_wildcard(NameBuff);
5261 p = NameBuff;
5262 }
5263 else
5264 p = NULL;
5265 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005266 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 {
5268 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005269 int options = WILD_LIST_NOTFOUND
5270 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
5272 ExpandInit(&xpc);
5273 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005274 if (p_wic)
5275 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005277 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (p == NULL)
5279 return FAIL;
5280 }
5281 if (p != NULL)
5282 {
John Marriotted908f72024-04-18 22:46:56 +02005283 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005285 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 vim_free(p);
5287 }
5288 }
5289 }
5290 }
5291 return OK;
5292}
5293
5294/*
5295 * Replace part of the command line, keeping eap->cmd, eap->arg and
5296 * eap->nextcmd correct.
5297 * "src" points to the part that is to be replaced, of length "srclen".
5298 * "repl" is the replacement string.
5299 * Returns a pointer to the character after the replaced string.
5300 * Returns NULL for failure.
5301 */
5302 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005303repl_cmdline(
5304 exarg_T *eap,
5305 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005306 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005307 char_u *repl,
5308 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
John Marriotted908f72024-04-18 22:46:56 +02005310 size_t repllen;
5311 size_t taillen;
5312 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005314 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
5316 /*
5317 * The new command line is build in new_cmdline[].
5318 * First allocate it.
5319 * Careful: a "+cmd" argument may have been NUL terminated.
5320 */
John Marriotted908f72024-04-18 22:46:56 +02005321 repllen = STRLEN(repl);
5322 taillen = STRLEN(src + srclen);
5323 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005324 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005325 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005326 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005327 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329 /*
5330 * Copy the stuff before the expanded part.
5331 * Copy the expanded stuff.
5332 * Copy what came after the expanded part.
5333 * Copy the next commands, if there are any.
5334 */
John Marriotted908f72024-04-18 22:46:56 +02005335 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5336 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005337
John Marriotted908f72024-04-18 22:46:56 +02005338 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5339 new_cmdlinelen += repllen; // remember the end of the string
5340 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5341 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005343 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 {
John Marriotted908f72024-04-18 22:46:56 +02005345 new_cmdlinelen += taillen + 1;
5346 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5347 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
5349 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5350 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5351 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5352 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5353 vim_free(*cmdlinep);
5354 *cmdlinep = new_cmdline;
5355
5356 return src;
5357}
5358
5359/*
5360 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005361 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 */
5363 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005364separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
5366 char_u *p;
5367
Bram Moolenaar86b68352004-12-27 21:59:20 +00005368#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005369 p = skip_grep_pat(eap);
5370#else
5371 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005372#endif
5373
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005374 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {
5376 if (*p == Ctrl_V)
5377 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005378 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005379 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005381 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005382 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005383 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 break;
5385 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005386
5387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005388 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005389 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005390 {
5391 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005392 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005393 if (*p == NUL) // stop at NUL after CTRL-V
5394 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005395 }
5396#endif
5397
Bram Moolenaareda29c92022-10-02 12:59:00 +01005398 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005399 // :@" and :*" do not start a comment!
5400 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005401 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005402 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005403 && !(eap->argt & EX_NOTRLCOM)
5404 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5405 || p != eap->arg)
5406 && (eap->cmdidx != CMD_redir
5407 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005408 || (*p == '#'
5409 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005410 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005411 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005412 || (*p == '|'
5413 && eap->cmdidx != CMD_append
5414 && eap->cmdidx != CMD_change
5415 && eap->cmdidx != CMD_insert)
5416 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
5418 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005419 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 * AND 'b' is present in 'cpoptions'.
5421 */
5422 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005423 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005425 if (!keep_backslash)
5426 {
5427 STRMOVE(p - 1, p); // remove the '\'
5428 --p;
5429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431 else
5432 {
5433 eap->nextcmd = check_nextcmd(p);
5434 *p = NUL;
5435 break;
5436 }
5437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005440 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 del_trailing_spaces(eap->arg);
5442}
5443
5444/*
5445 * get + command from ex argument
5446 */
5447 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005448getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449{
5450 char_u *arg = *argp;
5451 char_u *command = NULL;
5452
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005453 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {
5455 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005456 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 command = dollar_command;
5458 else
5459 {
5460 command = arg;
5461 arg = skip_cmd_arg(command, TRUE);
5462 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005463 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
5465
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005466 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 *argp = arg;
5468 }
5469 return command;
5470}
5471
5472/*
5473 * Find end of "+command" argument. Skip over "\ " and "\\".
5474 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005475 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005476skip_cmd_arg(
5477 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005478 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
5480 while (*p && !vim_isspace(*p))
5481 {
5482 if (*p == '\\' && p[1] != NUL)
5483 {
5484 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005485 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 else
5487 ++p;
5488 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005489 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
5491 return p;
5492}
5493
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005494 int
5495get_bad_opt(char_u *p, exarg_T *eap)
5496{
5497 if (STRICMP(p, "keep") == 0)
5498 eap->bad_char = BAD_KEEP;
5499 else if (STRICMP(p, "drop") == 0)
5500 eap->bad_char = BAD_DROP;
5501 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5502 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005503 else
5504 return FAIL;
5505 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005506}
5507
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005509 * Function given to ExpandGeneric() to obtain the list of bad= names.
5510 */
5511 static char_u *
5512get_bad_name(expand_T *xp UNUSED, int idx)
5513{
5514 // Note: Keep this in sync with getargopt.
5515 static char *(p_bad_values[]) =
5516 {
5517 "?",
5518 "keep",
5519 "drop",
5520 };
5521
John Marriotted908f72024-04-18 22:46:56 +02005522 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5523 return NULL;
5524
5525 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005526}
5527
5528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 * Get "++opt=arg" argument.
5530 * Return FAIL or OK.
5531 */
5532 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005533getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 char_u *arg = eap->arg + 2;
5536 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005537 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005540 // Note: Keep this in sync with get_argopt_name.
5541
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005542 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5544 {
5545 if (*arg == 'n')
5546 {
5547 arg += 2;
5548 eap->force_bin = FORCE_NOBIN;
5549 }
5550 else
5551 eap->force_bin = FORCE_BIN;
5552 if (!checkforcmd(&arg, "binary", 3))
5553 return FAIL;
5554 eap->arg = skipwhite(arg);
5555 return OK;
5556 }
5557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005558 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005559 if (STRNCMP(arg, "edit", 4) == 0)
5560 {
5561 eap->read_edit = TRUE;
5562 eap->arg = skipwhite(arg + 4);
5563 return OK;
5564 }
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 if (STRNCMP(arg, "ff", 2) == 0)
5567 {
5568 arg += 2;
5569 pp = &eap->force_ff;
5570 }
5571 else if (STRNCMP(arg, "fileformat", 10) == 0)
5572 {
5573 arg += 10;
5574 pp = &eap->force_ff;
5575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 else if (STRNCMP(arg, "enc", 3) == 0)
5577 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005578 if (STRNCMP(arg, "encoding", 8) == 0)
5579 arg += 8;
5580 else
5581 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 pp = &eap->force_enc;
5583 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005584 else if (STRNCMP(arg, "bad", 3) == 0)
5585 {
5586 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005587 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
5590 if (pp == NULL || *arg != '=')
5591 return FAIL;
5592
5593 ++arg;
5594 *pp = (int)(arg - eap->cmd);
5595 arg = skip_cmd_arg(arg, FALSE);
5596 eap->arg = skipwhite(arg);
5597 *arg = NUL;
5598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 if (pp == &eap->force_ff)
5600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5602 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005603 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005605 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005607 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5609 *p = TOLOWER_ASC(*p);
5610 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005611 else
5612 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005613 // Check ++bad= argument. Must be a single-byte character, "keep" or
5614 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005615 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005616 return FAIL;
5617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
5619 return OK;
5620}
5621
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005622/*
5623 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5624 */
5625 static char_u *
5626get_argopt_name(expand_T *xp UNUSED, int idx)
5627{
5628 // Note: Keep this in sync with getargopt.
5629 static char *(p_opt_values[]) =
5630 {
5631 "fileformat=",
5632 "encoding=",
5633 "binary",
5634 "nobinary",
5635 "bad=",
5636 "edit",
5637 };
5638
John Marriotted908f72024-04-18 22:46:56 +02005639 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5640 return NULL;
5641
5642 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005643}
5644
5645/*
5646 * Command-line expansion for ++opt=name.
5647 */
5648 int
5649expand_argopt(
5650 char_u *pat,
5651 expand_T *xp,
5652 regmatch_T *rmp,
5653 char_u ***matches,
5654 int *numMatches)
5655{
5656 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5657 {
5658 char_u *(*cb)(expand_T *, int) = NULL;
5659
5660 char_u *name_end = xp->xp_pattern - 1;
5661 if (name_end - xp->xp_line >= 2
5662 && STRNCMP(name_end - 2, "ff", 2) == 0)
5663 cb = get_fileformat_name;
5664 else if (name_end - xp->xp_line >= 10
5665 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5666 cb = get_fileformat_name;
5667 else if (name_end - xp->xp_line >= 3
5668 && STRNCMP(name_end - 3, "enc", 3) == 0)
5669 cb = get_encoding_name;
5670 else if (name_end - xp->xp_line >= 8
5671 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5672 cb = get_encoding_name;
5673 else if (name_end - xp->xp_line >= 3
5674 && STRNCMP(name_end - 3, "bad", 3) == 0)
5675 cb = get_bad_name;
5676
5677 if (cb != NULL)
5678 {
5679 return ExpandGeneric(
5680 pat,
5681 xp,
5682 rmp,
5683 matches,
5684 numMatches,
5685 cb,
5686 FALSE);
5687 }
5688 return FAIL;
5689 }
5690
5691 // Special handling of "ff" which acts as a short form of
5692 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005693 if (xp->xp_pattern_len == 2
5694 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005695 {
5696 *matches = ALLOC_MULT(char_u *, 1);
5697 if (*matches == NULL)
5698 return FAIL;
5699 *numMatches = 1;
5700 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5701 return OK;
5702 }
5703
5704 return ExpandGeneric(
5705 pat,
5706 xp,
5707 rmp,
5708 matches,
5709 numMatches,
5710 get_argopt_name,
5711 FALSE);
5712}
5713
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005715ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
5717 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005718 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 * directory for security reasons.
5720 */
5721 if (secure)
5722 {
5723 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005724 eap->errmsg =
5725 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005728 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 else
5730 do_augroup(eap->arg, eap->forceit);
5731}
5732
5733/*
5734 * ":doautocmd": Apply the automatic commands to the current buffer.
5735 */
5736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005737ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005739 char_u *arg = eap->arg;
5740 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005741 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005742
Bram Moolenaar1610d052016-06-09 22:53:01 +02005743 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005744 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005745 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005746 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749/*
5750 * :[N]bunload[!] [N] [bufname] unload buffer
5751 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5752 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5753 */
5754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005755ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005757 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005758 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 eap->errmsg = do_bufdel(
5760 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5761 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5762 : DOBUF_UNLOAD, eap->arg,
5763 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5764}
5765
5766/*
5767 * :[N]buffer [N] to buffer N
5768 * :[N]sbuffer [N] to buffer N
5769 */
5770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005771ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005773 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005774 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005776 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 else
5778 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005779 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5781 else
5782 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005783 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005784 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 }
5786}
5787
5788/*
5789 * :[N]bmodified [N] to next mod. buffer
5790 * :[N]sbmodified [N] to next mod. buffer
5791 */
5792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005793ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005796 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005797 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798}
5799
5800/*
5801 * :[N]bnext [N] to next buffer
5802 * :[N]sbnext [N] split and to next buffer
5803 */
5804 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005805ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005807 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005808 return;
5809
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005811 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005812 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813}
5814
5815/*
5816 * :[N]bNext [N] to previous buffer
5817 * :[N]bprevious [N] to previous buffer
5818 * :[N]sbNext [N] split and to previous buffer
5819 * :[N]sbprevious [N] split and to previous buffer
5820 */
5821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005822ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005824 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005825 return;
5826
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005828 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005829 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830}
5831
5832/*
5833 * :brewind to first buffer
5834 * :bfirst to first buffer
5835 * :sbrewind split and to first buffer
5836 * :sbfirst split and to first buffer
5837 */
5838 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005839ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005841 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005842 return;
5843
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005845 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005846 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847}
5848
5849/*
5850 * :blast to last buffer
5851 * :sblast split and to last buffer
5852 */
5853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005854ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005856 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005857 return;
5858
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005860 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005861 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
Bram Moolenaar7a092242020-04-16 22:10:49 +02005864/*
5865 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005866 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005869ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005871 int comment_char = '"';
5872
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005873 if (in_vim9script())
5874 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005875 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005876}
5877
5878/*
5879 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5880 * to "cmd_start" or has a white space character before it.
5881 */
5882 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005883ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005884{
5885 int c = *cmd;
5886
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005887 if (c == NUL || c == '|' || c == '\n')
5888 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005889 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005890 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5891 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005892 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005893 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894}
5895
5896#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5897 || defined(PROTO)
5898/*
5899 * Return the next command, after the first '|' or '\n'.
5900 * Return NULL if not found.
5901 */
5902 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005903find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
5905 while (*p != '|' && *p != '\n')
5906 {
5907 if (*p == NUL)
5908 return NULL;
5909 ++p;
5910 }
5911 return (p + 1);
5912}
5913#endif
5914
5915/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005916 * Check if *p is a separator between Ex commands, skipping over white space.
5917 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 */
5919 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005920check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005922 char_u *s = skipwhite(p);
5923
5924 if (*s == '|' || *s == '\n')
5925 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 else
5927 return NULL;
5928}
5929
5930/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005931 * If "eap->nextcmd" is not set, check for a next command at "p".
5932 */
5933 void
5934set_nextcmd(exarg_T *eap, char_u *arg)
5935{
5936 char_u *p = check_nextcmd(arg);
5937
5938 if (eap->nextcmd == NULL)
5939 eap->nextcmd = p;
5940 else if (p != NULL)
5941 // cannot use "| command" inside a {} block
5942 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5943}
5944
5945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 * - if there are more files to edit
5947 * - and this is the last window
5948 * - and forceit not used
5949 * - and not repeated twice on a row
5950 * return FAIL and give error message if 'message' TRUE
5951 * return OK otherwise
5952 */
5953 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005954check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005955 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005956 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957{
5958 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5959
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005960 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005961 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 {
5963 if (message)
5964 {
5965#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005966 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5967 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005969 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005971 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5972 NGETTEXT("%d more file to edit. Quit anyway?",
5973 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5975 return OK;
5976 return FAIL;
5977 }
5978#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005979 semsg(NGETTEXT(e_nr_more_file_to_edit,
5980 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005981 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 }
5983 return FAIL;
5984 }
5985 return OK;
5986}
5987
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988/*
5989 * Function given to ExpandGeneric() to obtain the list of command names.
5990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005992get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
5994 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005995 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 return cmdnames[idx].cmd_name;
5997}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006000ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001{
Bram Moolenaare6850792010-05-14 15:28:44 +02006002 if (*eap->arg == NUL)
6003 {
6004#ifdef FEAT_EVAL
6005 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6006 char_u *p = NULL;
6007
6008 if (expr != NULL)
6009 {
6010 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006011 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02006012 --emsg_off;
6013 vim_free(expr);
6014 }
6015 if (p != NULL)
6016 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006017 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02006018 vim_free(p);
6019 }
6020 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006021 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02006022#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006023 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02006024#endif
6025 }
6026 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00006027 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006028
6029#ifdef FEAT_VTP
6030 else if (has_vtp_working())
6031 {
6032 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006033 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006034 redrawcmd();
6035 }
6036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037}
6038
6039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006040ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006043 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 do_highlight(eap->arg, eap->forceit, FALSE);
6045}
6046
6047
6048/*
6049 * Call this function if we thought we were going to exit, but we won't
6050 * (because of an error). May need to restore the terminal mode.
6051 */
6052 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006053not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 exiting = FALSE;
6056 settmode(TMODE_RAW);
6057}
6058
Bram Moolenaar3552e742021-05-29 12:21:58 +02006059 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006060before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6061{
6062 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6063
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006064 // Bail out when autocommands closed the window.
6065 // Refuse to quit when the buffer in the last window is being closed (can
6066 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006067 if (!win_valid(wp)
6068 || curbuf_locked()
6069 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6070 return TRUE;
6071
6072 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6073 {
6074 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006075 // Refuse to quit when locked or when the window was closed or the
6076 // buffer in the last window is being closed (can only happen in
6077 // autocommands).
6078 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006079 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6080 return TRUE;
6081 }
6082
6083 return FALSE;
6084}
6085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006087 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006088 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006089 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006091 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006092ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006094 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 if (cmdwin_type != 0)
6097 {
6098 cmdwin_result = Ctrl_C;
6099 return;
6100 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006101 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006102 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006103 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006104 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006105 return;
6106 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006107 if (eap->addr_count > 0)
6108 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006109 int wnr = eap->line2;
6110
6111 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6112 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006113 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006114 }
6115 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006116 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006117
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006118 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006119 if (curbuf_locked())
6120 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006121
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006122 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006123 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006124 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
6126#ifdef FEAT_NETBEANS_INTG
6127 netbeansForcedQuit = eap->forceit;
6128#endif
6129
6130 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006131 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 */
6133 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6134 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006135 if ((!buf_hide(wp->w_buffer)
6136 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006137 | (eap->forceit ? CCGD_FORCEIT : 0)
6138 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006140 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 {
6142 not_exiting();
6143 }
6144 else
6145 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006146 // quit last window
6147 // Note: only_one_window() returns true, even so a help window is
6148 // still open. In that case only quit, if no address has been
6149 // specified. Example:
6150 // :h|wincmd w|1q - don't quit
6151 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006152 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006154 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006155#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006157#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006158 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006159 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
6161}
6162
6163/*
6164 * ":cquit".
6165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006167ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006169 // this does not always pass on the exit code to the Manx compiler. why?
6170 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171}
6172
6173/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006174 * Do preparations for "qall" and "wqall".
6175 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006177 int
6178before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 if (cmdwin_type != 0)
6181 {
6182 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006183 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 else
6185 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006186 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006188
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006189 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006190 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006191 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006192 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006193 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006194 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006195
6196 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006197 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006198
Bram Moolenaar411da642023-05-10 16:53:27 +01006199 return OK;
6200}
6201
6202/*
6203 * ":qall": try to quit all windows
6204 */
6205 static void
6206ex_quit_all(exarg_T *eap)
6207{
6208 if (before_quit_all(eap) == FAIL)
6209 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006211 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 getout(0);
6213 not_exiting();
6214}
6215
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216/*
6217 * ":close": close current window, unless it is the last one
6218 */
6219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006220ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006222 win_T *win;
6223 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006225 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006227 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006228 {
6229 if (eap->addr_count == 0)
6230 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006231 else
6232 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006233 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006234 {
6235 winnr++;
6236 if (winnr == eap->line2)
6237 break;
6238 }
6239 if (win == NULL)
6240 win = lastwin;
6241 ex_win_close(eap->forceit, win, NULL);
6242 }
6243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244}
6245
Bram Moolenaar4033c552017-09-16 20:54:51 +02006246#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006247/*
6248 * ":pclose": Close any preview window.
6249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006251ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006252{
6253 win_T *win;
6254
Bram Moolenaar79648732019-07-18 21:43:07 +02006255 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006256 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006257 if (win->w_p_pvw)
6258 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006259 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006260 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006261 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006262# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006263 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006264 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006265# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006266}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006267#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006268
Bram Moolenaarf740b292006-02-16 22:11:02 +00006269/*
6270 * Close window "win" and take care of handling closing the last window for a
6271 * modified buffer.
6272 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006274ex_win_close(
6275 int forceit,
6276 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006277 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
6279 int need_hide;
6280 buf_T *buf = win->w_buffer;
6281
Bram Moolenaarcf844172020-06-26 19:44:06 +02006282 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006283 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006284 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006285 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006286 return;
6287 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006288 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006289 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006290
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006292 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006294#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006295 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006297# ifdef FEAT_TERMINAL
6298 if (term_job_running(buf->b_term))
6299 {
6300 if (term_confirm_stop(buf) == FAIL)
6301 return;
6302 // Manually kill the terminal here because this command will
6303 // hide it otherwise.
6304 free_terminal(buf);
6305 need_hide = FALSE;
6306 }
6307 else
6308# endif
6309 {
6310 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006311
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006312 set_bufref(&bufref, buf);
6313 dialog_changed(buf, FALSE);
6314 if (bufref_valid(&bufref) && bufIsChanged(buf))
6315 return;
6316 need_hide = FALSE;
6317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 }
6319 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006322 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 return;
6324 }
6325 }
6326
Bram Moolenaar4033c552017-09-16 20:54:51 +02006327#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006329#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006330
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006331 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006332 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006333 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006334 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006335 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336}
6337
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006339 * Handle the argument for a tabpage related ex command.
6340 * Returns a tabpage number.
6341 * When an error is encountered then eap->errmsg is set.
6342 */
6343 static int
6344get_tabpage_arg(exarg_T *eap)
6345{
6346 int tab_number;
6347 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6348
6349 if (eap->arg && *eap->arg != NUL)
6350 {
6351 char_u *p = eap->arg;
6352 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006353 int relative = 0; // argument +N/-N means: go to N places to the
6354 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006355
6356 if (*p == '-')
6357 {
6358 relative = -1;
6359 p++;
6360 }
6361 else if (*p == '+')
6362 {
6363 relative = 1;
6364 p++;
6365 }
6366
6367 p_save = p;
6368 tab_number = getdigits(&p);
6369
6370 if (relative == 0)
6371 {
6372 if (STRCMP(p, "$") == 0)
6373 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006374 else if (STRCMP(p, "#") == 0)
6375 if (valid_tabpage(lastused_tabpage))
6376 tab_number = tabpage_index(lastused_tabpage);
6377 else
6378 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006379 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6380 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006381 tab_number = 0;
6382 goto theend;
6383 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006384 else if (p == p_save || *p_save == '-' || *p != NUL
6385 || tab_number > LAST_TAB_NR)
6386 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006387 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006388 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006389 goto theend;
6390 }
6391 }
6392 else
6393 {
6394 if (*p_save == NUL)
6395 tab_number = 1;
6396 else if (p == p_save || *p_save == '-' || *p != NUL
6397 || tab_number == 0)
6398 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006399 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006400 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006401 goto theend;
6402 }
6403 tab_number = tab_number * relative + tabpage_index(curtab);
6404 if (!unaccept_arg0 && relative == -1)
6405 --tab_number;
6406 }
6407 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006408 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006409 }
6410 else if (eap->addr_count > 0)
6411 {
6412 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006413 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006414 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006415 tab_number = 0;
6416 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006417 else
6418 {
6419 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006420 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006421 {
zeertzjq076faac2024-03-25 16:41:06 +01006422 char_u *cmdp = eap->cmd;
6423
6424 while (--cmdp > *eap->cmdlinep
6425 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6426 ;
6427 if (*cmdp == '-')
6428 {
6429 --tab_number;
6430 if (tab_number < unaccept_arg0)
6431 eap->errmsg = _(e_invalid_range);
6432 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006433 }
6434 }
6435 }
6436 else
6437 {
6438 switch (eap->cmdidx)
6439 {
6440 case CMD_tabnext:
6441 tab_number = tabpage_index(curtab) + 1;
6442 if (tab_number > LAST_TAB_NR)
6443 tab_number = 1;
6444 break;
6445 case CMD_tabmove:
6446 tab_number = LAST_TAB_NR;
6447 break;
6448 default:
6449 tab_number = tabpage_index(curtab);
6450 }
6451 }
6452
6453theend:
6454 return tab_number;
6455}
6456
6457/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006458 * ":tabclose": close current tab page, unless it is the last one.
6459 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 */
6461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006462ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006464 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006465 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006466
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006467 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006468 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006469 cmdwin_result = K_IGNORE;
6470 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006471 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006472
6473 if (first_tabpage->tp_next == NULL)
6474 {
6475 emsg(_(e_cannot_close_last_tab_page));
6476 return;
6477 }
6478
6479 if (window_layout_locked(CMD_tabclose))
6480 return;
6481
6482 tab_number = get_tabpage_arg(eap);
6483 if (eap->errmsg != NULL)
6484 return;
6485
6486 tp = find_tabpage(tab_number);
6487 if (tp == NULL)
6488 {
6489 beep_flush();
6490 return;
6491 }
6492 if (tp != curtab)
6493 {
6494 tabpage_close_other(tp, eap->forceit);
6495 return;
6496 }
6497 else if (!text_locked() && !curbuf_locked())
6498 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006499}
6500
6501/*
6502 * ":tabonly": close all tab pages except the current one
6503 */
6504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006505ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006506{
6507 tabpage_T *tp;
6508 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006509 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006510
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006511 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006512 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006513 cmdwin_result = K_IGNORE;
6514 return;
6515 }
6516
6517 if (first_tabpage->tp_next == NULL)
6518 {
6519 msg(_("Already only one tab page"));
6520 return;
6521 }
6522
6523 if (window_layout_locked(CMD_tabonly))
6524 return;
6525
6526 tab_number = get_tabpage_arg(eap);
6527 if (eap->errmsg != NULL)
6528 return;
6529
6530 goto_tabpage(tab_number);
6531 // Repeat this up to a 1000 times, because autocommands may
6532 // mess up the lists.
6533 for (done = 0; done < 1000; ++done)
6534 {
6535 FOR_ALL_TABPAGES(tp)
6536 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006537 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006538 tabpage_close_other(tp, eap->forceit);
6539 // if we failed to close it quit
6540 if (valid_tabpage(tp))
6541 done = 1000;
6542 // start over, "tp" is now invalid
6543 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006544 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006545 if (first_tabpage->tp_next == NULL)
6546 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
6550/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006551 * Close the current tab page.
6552 */
6553 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006554tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006555{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006556 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006557 return;
6558
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006559 // First close all the windows but the current one. If that worked then
6560 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006561 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006562 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006563 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006564 ex_win_close(forceit, curwin, NULL);
6565# ifdef FEAT_GUI
6566 need_mouse_correct = TRUE;
6567# endif
6568}
6569
6570/*
6571 * Close tab page "tp", which is not the current tab page.
6572 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006573 * Also takes care of the tab pages line disappearing when closing the
6574 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006575 */
6576 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006577tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006578{
6579 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006580 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006581
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006582 // Limit to 1000 windows, autocommands may add a window while we close
6583 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006584 while (++done < 1000)
6585 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006586 wp = tp->tp_firstwin;
6587 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006588
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006589 // Autocommands may delete the tab page under our fingers and we may
6590 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006591 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006592 break;
6593 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006594
Bram Moolenaar29323592016-07-24 22:04:11 +02006595 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006596}
6597
6598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 * ":only".
6600 */
6601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006602ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006604 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006605 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606# ifdef FEAT_GUI
6607 need_mouse_correct = TRUE;
6608# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006609 if (eap->addr_count > 0)
6610 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006611 win_T *wp;
6612 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006613 for (wp = firstwin; --wnr > 0; )
6614 {
6615 if (wp->w_next == NULL)
6616 break;
6617 else
6618 wp = wp->w_next;
6619 }
6620 win_goto(wp);
6621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 close_others(TRUE, eap->forceit);
6623}
6624
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006626ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006628 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006629 if (eap->skip)
6630 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006631
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006632 if (window_layout_locked(CMD_hide))
6633 return;
6634#ifdef FEAT_GUI
6635 need_mouse_correct = TRUE;
6636#endif
6637 if (eap->addr_count == 0)
6638 win_close(curwin, FALSE); // don't free buffer
6639 else
6640 {
6641 int winnr = 0;
6642 win_T *win;
6643
6644 FOR_ALL_WINDOWS(win)
6645 {
6646 winnr++;
6647 if (winnr == eap->line2)
6648 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006650 if (win == NULL)
6651 win = lastwin;
6652 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 }
6654}
6655
6656/*
6657 * ":stop" and ":suspend": Suspend Vim.
6658 */
dbivolaruab16ad32021-12-29 19:41:47 +00006659 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006660ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
6662 /*
6663 * Disallow suspending for "rvim".
6664 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006665 if (check_restricted())
6666 return;
6667
6668 if (!eap->forceit)
6669 autowrite_all();
6670 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6671 windgoto((int)Rows - 1, 0);
6672 out_char('\n');
6673 out_flush();
6674 stoptermcap();
6675 out_flush(); // needed for SUN to restore xterm buffer
6676 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6677 ui_suspend(); // call machine specific function
6678 maketitle();
6679 resettitle(); // force updating the title
6680 starttermcap();
6681 scroll_start(); // scroll screen before redrawing
6682 redraw_later_clear();
6683 shell_resized(); // may have resized window
6684 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685}
6686
6687/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006688 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 */
6690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006691ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006693#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006694 if (not_in_vim9(eap) == FAIL)
6695 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 if (cmdwin_type != 0)
6698 {
6699 cmdwin_result = Ctrl_C;
6700 return;
6701 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006702 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006703 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006704 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006705 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006706 return;
6707 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006708
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006710 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 */
6712 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6713 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006714
6715 // Write the buffer for ":wq" or when it was changed.
6716 // Trigger QuitPre and ExitPre.
6717 // Check if we can exit now, after autocommands have changed things.
6718 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6719 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006721 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 {
6723 not_exiting();
6724 }
6725 else
6726 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006727 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006729 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730# ifdef FEAT_GUI
6731 need_mouse_correct = TRUE;
6732# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006733 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006734 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 }
6736}
6737
6738/*
6739 * ":print", ":list", ":number".
6740 */
6741 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006742ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006744 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006745 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006746 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006748 for ( ;!got_int; ui_breakcheck())
6749 {
6750 print_line(eap->line1,
6751 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6752 || (eap->flags & EXFLAG_NR)),
6753 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6754 if (++eap->line1 > eap->line2)
6755 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006756 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006757 }
6758 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006759 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006760 curwin->w_cursor.lnum = eap->line2;
6761 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 }
6763
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765}
6766
6767#ifdef FEAT_BYTEOFF
6768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006769ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770{
6771 goto_byte(eap->line2);
6772}
6773#endif
6774
6775/*
6776 * ":shell".
6777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006779ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781 do_shell(NULL, 0);
6782}
6783
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006784#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006786static int drop_busy = FALSE;
6787static int drop_filec;
6788static char_u **drop_filev = NULL;
6789static int drop_split;
6790static void (*drop_callback)(void *);
6791static void *drop_cookie;
6792
6793 static void
6794handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795{
6796 exarg_T ea;
6797 int save_msg_scroll = msg_scroll;
6798
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006799 // Setting the argument list may cause screen updates and being called
6800 // recursively. Avoid that by setting drop_busy.
6801 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006803 // Check whether the current buffer is changed. If so, we will need
6804 // to split the current window or data could be lost.
6805 // We don't need to check if the 'hidden' option is set, as in this
6806 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006807 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {
6809 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006810 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 --emsg_off;
6812 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006813 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (win_split(0, 0) == FAIL)
6816 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006817 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006819 // When splitting the window, create a new alist. Otherwise the
6820 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 alist_unlink(curwin->w_alist);
6822 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 }
6824
6825 /*
6826 * Set up the new argument list.
6827 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006828 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829
6830 /*
6831 * Move to the first file.
6832 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006833 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006834 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 ea.cmd = (char_u *)"next";
6836 do_argfile(&ea, 0);
6837
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006838 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6839 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 need_start_insertmode = FALSE;
6841
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006842 // Restore msg_scroll, otherwise a following command may cause scrolling
6843 // unexpectedly. The screen will be redrawn by the caller, thus
6844 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006846
6847 if (drop_callback != NULL)
6848 drop_callback(drop_cookie);
6849
6850 drop_filev = NULL;
6851 drop_busy = FALSE;
6852}
6853
6854/*
6855 * Handle a file drop. The code is here because a drop is *nearly* like an
6856 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006857 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006858 * code is very similar to :args and hence needs access to a lot of the static
6859 * functions in this file.
6860 *
6861 * The "filev" list must have been allocated using alloc(), as should each item
6862 * in the list. This function takes over responsibility for freeing the "filev"
6863 * list.
6864 */
6865 void
6866handle_drop(
6867 int filec, // the number of files dropped
6868 char_u **filev, // the list of files dropped
6869 int split, // force splitting the window
6870 void (*callback)(void *), // to be called after setting the argument
6871 // list
6872 void *cookie) // argument for "callback" (allocated)
6873{
6874 // Cannot handle recursive drops, finish the pending one.
6875 if (drop_busy)
6876 {
6877 FreeWild(filec, filev);
6878 vim_free(cookie);
6879 return;
6880 }
6881
6882 // When calling handle_drop() more than once in a row we only use the last
6883 // one.
6884 if (drop_filev != NULL)
6885 {
6886 FreeWild(drop_filec, drop_filev);
6887 vim_free(drop_cookie);
6888 }
6889
6890 drop_filec = filec;
6891 drop_filev = filev;
6892 drop_split = split;
6893 drop_callback = callback;
6894 drop_cookie = cookie;
6895
6896 // Postpone this when:
6897 // - editing the command line
6898 // - not possible to change the current buffer
6899 // - updating the screen
6900 // As it may change buffers and window structures that are in use and cause
6901 // freed memory to be used.
6902 if (text_locked() || curbuf_locked() || updating_screen)
6903 return;
6904
6905 handle_drop_internal();
6906}
6907
6908/*
6909 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6910 * reset: Handle a postponed drop.
6911 */
6912 void
6913handle_any_postponed_drop(void)
6914{
6915 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006916 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006917 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918}
6919#endif
6920
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 * ":preserve".
6923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006925ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006927 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 ml_preserve(curbuf, TRUE);
6929}
6930
6931/*
6932 * ":recover".
6933 */
6934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006935ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006937 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006939 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6940 | CCGD_MULTWIN
6941 | (eap->forceit ? CCGD_FORCEIT : 0)
6942 | CCGD_EXCMD)
6943
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 && (*eap->arg == NUL
6945 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006946 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 recoverymode = FALSE;
6948}
6949
6950/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006951 * Command modifier used in a wrong way. Also for other commands that can't
6952 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 */
6954 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006955ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006957 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958}
6959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960/*
6961 * :sview [+command] file split window with new file, read-only
6962 * :split [[+command] file] split window with current or new file
6963 * :vsplit [[+command] file] split window vertically with current or new file
6964 * :new [[+command] file] split window with no or new file
6965 * :vnew [[+command] file] split vertically window with no or new file
6966 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006967 *
6968 * :tabedit open new Tab page with empty window
6969 * :tabedit [+command] file open new Tab page and edit "file"
6970 * :tabnew [[+command] file] just like :tabedit
6971 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 */
6973 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006974ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006976 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006978#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006979 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006980 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006981#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006982 int use_tab = eap->cmdidx == CMD_tabedit
6983 || eap->cmdidx == CMD_tabfind
6984 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006986 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006987 return;
6988
Bram Moolenaar4033c552017-09-16 20:54:51 +02006989#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992
Bram Moolenaar4033c552017-09-16 20:54:51 +02006993#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006994 // A ":split" in the quickfix window works like ":new". Don't want two
6995 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006996 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {
6998 if (eap->cmdidx == CMD_split)
6999 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 if (eap->cmdidx == CMD_vsplit)
7001 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007005 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007007 char_u *file_to_find = NULL;
7008 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007010 FNAME_MESS, TRUE, curbuf->b_ffname,
7011 &file_to_find, &search_ctx);
7012 vim_free(file_to_find);
7013 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 if (fname == NULL)
7015 goto theend;
7016 eap->arg = fname;
7017 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007018# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007019 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 && eap->cmdidx != CMD_new)
7022 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007023 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007024# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007025 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007026# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007027 au_has_group((char_u *)"FileExplorer"))
7028 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007029 // No browsing supported but we do have the file explorer:
7030 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007031 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007032 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007033 }
7034 else
7035 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02007036 fname = do_browse(0, (char_u *)(use_tab
7037 ? _("Edit File in new tab page")
7038 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007040 if (fname == NULL)
7041 goto theend;
7042 eap->arg = fname;
7043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007045 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007048 /*
7049 * Either open new tab page or split the window.
7050 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007051 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007052 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007053 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007054 : eap->addr_count == 0 ? 0
7055 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007056 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007057 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007058
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007059 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007060 if (curwin != old_curwin
7061 && win_valid(old_curwin)
7062 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007063 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007064 old_curwin->w_alt_fnum = curbuf->b_fnum;
7065 }
7066 }
7067 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007070 // Reset 'scrollbind' when editing another file, but keep it when
7071 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007072 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007073 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 else
7075 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 do_exedit(eap, old_curwin);
7077 }
7078
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007079# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007080 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007081# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083theend:
7084 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007086
7087/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007088 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007089 */
7090 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007091tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007092{
7093 exarg_T ea;
7094
Bram Moolenaara80faa82020-04-12 19:37:17 +02007095 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007096 ea.cmdidx = CMD_tabnew;
7097 ea.cmd = (char_u *)"tabn";
7098 ea.arg = (char_u *)"";
7099 ex_splitview(&ea);
7100}
7101
7102/*
7103 * :tabnext command
7104 */
7105 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007106ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007107{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007108 int tab_number;
7109
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007110 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007111 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007112 switch (eap->cmdidx)
7113 {
7114 case CMD_tabfirst:
7115 case CMD_tabrewind:
7116 goto_tabpage(1);
7117 break;
7118 case CMD_tablast:
7119 goto_tabpage(9999);
7120 break;
7121 case CMD_tabprevious:
7122 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007123 if (eap->arg && *eap->arg != NUL)
7124 {
7125 char_u *p = eap->arg;
7126 char_u *p_save = p;
7127
7128 tab_number = getdigits(&p);
7129 if (p == p_save || *p_save == '-' || *p != NUL
7130 || tab_number == 0)
7131 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007132 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007133 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007134 return;
7135 }
7136 }
7137 else
7138 {
7139 if (eap->addr_count == 0)
7140 tab_number = 1;
7141 else
7142 {
7143 tab_number = eap->line2;
7144 if (tab_number < 1)
7145 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007146 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007147 return;
7148 }
7149 }
7150 }
7151 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007152 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007153 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007154 tab_number = get_tabpage_arg(eap);
7155 if (eap->errmsg == NULL)
7156 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007157 break;
7158 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007159}
7160
7161/*
7162 * :tabmove command
7163 */
7164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007165ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007166{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007167 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007168
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007169 tab_number = get_tabpage_arg(eap);
7170 if (eap->errmsg == NULL)
7171 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007172}
7173
7174/*
7175 * :tabs command: List tabs and their contents.
7176 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007177 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007178ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007179{
7180 tabpage_T *tp;
7181 win_T *wp;
7182 int tabcount = 1;
7183
7184 msg_start();
7185 msg_scroll = TRUE;
7186 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7187 {
7188 msg_putchar('\n');
7189 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007190 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007191 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007192 ui_breakcheck();
7193
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007194 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007195 wp = firstwin;
7196 else
7197 wp = tp->tp_firstwin;
7198 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7199 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007200 msg_putchar('\n');
7201 msg_putchar(wp == curwin ? '>' : ' ');
7202 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007203 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7204 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007205 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007206 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007207 else
7208 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7209 IObuff, IOSIZE, TRUE);
7210 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007211 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007212 ui_breakcheck();
7213 }
7214 }
7215}
7216
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217/*
7218 * ":mode": Set screen mode.
7219 * If no argument given, just get the screen size and redraw.
7220 */
7221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007222ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
7224 if (*eap->arg == NUL)
7225 shell_resized();
7226 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007227 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228}
7229
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230/*
7231 * ":resize".
7232 * set, increment or decrement current window height
7233 */
7234 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007235ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236{
7237 int n;
7238 win_T *wp = curwin;
7239
7240 if (eap->addr_count > 0)
7241 {
7242 n = eap->line2;
7243 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7244 ;
7245 }
7246
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007247# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007251 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {
7253 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007254 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007255 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007257 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
7259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 {
7261 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007262 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007263 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007265 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 }
7267}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
7269/*
7270 * ":find [+command] <file>" command.
7271 */
7272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007273ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
Colin Kennedy21570352024-03-03 16:16:47 +01007275 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007276 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007277
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 char_u *fname;
7279 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007280 char_u *file_to_find = NULL;
7281 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
7283 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007284 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 if (eap->addr_count > 0)
7286 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007287 // Repeat finding the file "count" times. This matters when it appears
7288 // several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 count = eap->line2;
7290 while (fname != NULL && --count > 0)
7291 {
7292 vim_free(fname);
7293 fname = find_file_in_path(NULL, 0, FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007294 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 }
7296 }
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007297 VIM_CLEAR(file_to_find);
7298 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007300 if (fname == NULL)
7301 return;
7302
7303 eap->arg = fname;
7304 do_exedit(eap, NULL);
7305 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306}
7307
7308/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007309 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007310 */
7311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007312ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007313{
7314 regmatch_T regmatch;
7315 char_u *p;
7316
Bram Moolenaar65088802021-03-13 21:07:21 +01007317#ifdef FEAT_EVAL
7318 if (not_in_vim9(eap) == FAIL)
7319 return;
7320#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007321 curwin->w_cursor.lnum = eap->line2;
7322 beginline(BL_SOL | BL_FIX);
7323 if (*eap->arg == '/')
7324 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007325 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007326 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007327 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007328 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007329 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007330 if (regmatch.regprog != NULL)
7331 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007332 // make a copy of the line, when searching for a mark it might be
7333 // flushed
7334 char_u *line = vim_strsave(ml_get_curline());
7335
Bram Moolenaardf177f62005-02-22 08:39:57 +00007336 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007337 if (vim_regexec(&regmatch, line, (colnr_T)0))
7338 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007339 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007340 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007341 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007342 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007343 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007344 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007345 eap->arg += STRLEN(eap->arg);
7346 }
7347 check_cursor();
7348
7349 eap->cmdidx = CMD_visual;
7350 do_exedit(eap, NULL);
7351}
7352
7353/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007354 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 */
7356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007357ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007359 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7360
Colin Kennedy21570352024-03-03 16:16:47 +01007361 // Exclude commands which keep the window's current buffer
7362 if (
7363 eap->cmdidx != CMD_badd
7364 && eap->cmdidx != CMD_balt
7365 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007366 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7367 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007368 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007369
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 do_exedit(eap, NULL);
7371}
7372
7373/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007374 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007377do_exedit(
7378 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007379 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380{
7381 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007383 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007385 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7386 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007387 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 /*
7389 * ":vi" command ends Ex mode.
7390 */
7391 if (exmode_active && (eap->cmdidx == CMD_visual
7392 || eap->cmdidx == CMD_view))
7393 {
7394 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007395 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007397 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007398 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007399 if (global_busy)
7400 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007401 if (eap->nextcmd != NULL)
7402 {
7403 stuffReadbuff(eap->nextcmd);
7404 eap->nextcmd = NULL;
7405 }
7406
7407 if (exmode_was != EXMODE_VIM)
7408 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007409 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007410 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007411 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007412 no_wait_return = 0;
7413 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007414 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007415 msg_scroll = 0;
7416#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007417 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007418 hold_gui_events = 0;
7419#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007420 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007421 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007422
7423 main_loop(FALSE, TRUE);
7424
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007425 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007426 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007427 no_wait_return = save_nwr;
7428 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007429#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007430 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007431#endif
7432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 }
7436
7437 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007438 || eap->cmdidx == CMD_tabnew
7439 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007440 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007442 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 setpcmark();
7444 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007445 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7446 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007448 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 || *eap->arg != NUL
7450#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007451 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452#endif
7453 )
7454 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007455 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7456 // Only ":edit" or ":script" can bring us here, others are stopped
7457 // earlier.
7458 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007459 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007460
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 n = readonlymode;
7462 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7463 readonlymode = TRUE;
7464 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007465 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7466 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007467 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7468 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7470 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007471 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7473 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7474 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007475 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007477 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007478 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007479 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7480 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007481 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007483 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 if (old_curwin != NULL)
7485 {
7486 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007487 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007489#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007490 cleanup_T cs;
7491
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007492 // Reset the error/interrupt/exception state here so that
7493 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007494 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007495#endif
7496#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007498#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007499 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007500
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007501#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007502 // Restore the error/interrupt/exception state if not
7503 // discarded by a new aborting error, interrupt, or
7504 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007505 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 }
7508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
7510 else if (readonlymode && curbuf->b_nwindows == 1)
7511 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007512 // When editing an already visited buffer, 'readonly' won't be set
7513 // but the previous value is kept. With ":view" and ":sview" we
7514 // want the file to be readonly, except when another window is
7515 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 curbuf->b_p_ro = TRUE;
7517 }
7518 readonlymode = n;
7519 }
7520 else
7521 {
7522 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007523 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 if (n != curwin->w_arg_idx_invalid)
7527 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 }
7529
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 /*
7531 * if ":split file" worked, set alternate file name in old window to new
7532 * file
7533 */
7534 if (old_curwin != NULL
7535 && *eap->arg != NUL
7536 && curwin != old_curwin
7537 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007538 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007539 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541
7542 ex_no_reprint = TRUE;
7543}
7544
7545#ifndef FEAT_GUI
7546/*
7547 * ":gui" and ":gvim" when there is no GUI.
7548 */
7549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007550ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007552 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553}
7554#endif
7555
Bram Moolenaar4f974752019-02-17 17:44:42 +01007556#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007558ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559{
7560 gui_make_tearoff(eap->arg);
7561}
7562#endif
7563
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007564#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7565 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007567ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007569# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7570 if (gui.in_use)
7571 gui_make_popup(eap->arg, eap->forceit);
7572# ifdef FEAT_TERM_POPUP_MENU
7573 else
7574# endif
7575# endif
7576# ifdef FEAT_TERM_POPUP_MENU
7577 pum_make_popup(eap->arg, eap->forceit);
7578# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579}
7580#endif
7581
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007583ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584{
7585 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007586 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007588 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589}
7590
7591/*
7592 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7593 * offset.
7594 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007597ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007600 win_T *save_curwin = curwin;
7601 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 long topline;
7603 long y;
7604 linenr_T old_linenr = curwin->w_cursor.lnum;
7605
7606 setpcmark();
7607
7608 /*
7609 * determine max topline
7610 */
7611 if (curwin->w_p_scb)
7612 {
7613 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007614 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 {
7616 if (wp->w_p_scb && wp->w_buffer)
7617 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007618 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 if (topline > y)
7620 topline = y;
7621 }
7622 }
7623 if (topline < 1)
7624 topline = 1;
7625 }
7626 else
7627 {
7628 topline = 1;
7629 }
7630
7631
7632 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007633 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007635 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 {
7637 if (curwin->w_p_scb)
7638 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007639 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 y = topline - curwin->w_topline;
7641 if (y > 0)
7642 scrollup(y, TRUE);
7643 else
7644 scrolldown(-y, TRUE);
7645 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007646 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 }
7650 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007651 curwin = save_curwin;
7652 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 if (curwin->w_p_scb)
7654 {
7655 did_syncbind = TRUE;
7656 checkpcmark();
7657 if (old_linenr != curwin->w_cursor.lnum)
7658 {
7659 char_u ctrl_o[2];
7660
7661 ctrl_o[0] = Ctrl_O;
7662 ctrl_o[1] = 0;
7663 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7664 }
7665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666}
7667
7668
7669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007670ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007672 int i;
7673 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7674 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007676 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007678 do_bang(1, eap, FALSE, FALSE, TRUE);
7679 return;
7680 }
7681
7682 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7683 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684
7685#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007686 if (cmdmod.cmod_flags & CMOD_BROWSE)
7687 {
7688 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007690 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7691 NULL, NULL, NULL, curbuf);
7692 if (browseFile != NULL)
7693 {
7694 i = readfile(browseFile, NULL,
7695 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7696 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 }
7698 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007699 i = OK;
7700 }
7701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007703 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007705 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 return;
7707 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007708 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
7710 else
7711 {
7712 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7713 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7714 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007715 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716
7717 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007718 if (i != OK)
7719 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007720#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007721 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007723 semsg(_(e_cant_open_file_str), eap->arg);
7724 }
7725 else
7726 {
7727 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007728 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007729 // Delete the empty line that remains. Historically ex does
7730 // this but vi doesn't.
7731 if (eap->line2 == 0)
7732 lnum = curbuf->b_ml.ml_line_count;
7733 else
7734 lnum = 1;
7735 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007736 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007737 ml_delete(lnum);
7738 if (curwin->w_cursor.lnum > 1
7739 && curwin->w_cursor.lnum >= lnum)
7740 --curwin->w_cursor.lnum;
7741 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007742 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007743 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007744 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 }
7746}
7747
Bram Moolenaarea408852005-06-25 22:49:46 +00007748static char_u *prev_dir = NULL;
7749
7750#if defined(EXITFREE) || defined(PROTO)
7751 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007752free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007753{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007754 VIM_CLEAR(prev_dir);
7755 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007756}
7757#endif
7758
Bram Moolenaarf4258302013-06-02 18:20:17 +02007759/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007760 * Get the previous directory for the given chdir scope.
7761 */
7762 static char_u *
7763get_prevdir(cdscope_T scope)
7764{
7765 if (scope == CDSCOPE_WINDOW)
7766 return curwin->w_prevdir;
7767 else if (scope == CDSCOPE_TABPAGE)
7768 return curtab->tp_prevdir;
7769 return prev_dir;
7770}
7771
7772/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007773 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007774 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7775 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007776 */
7777 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007778post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007779{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007780 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007781 // Clear tab local directory for both :cd and :tcd
7782 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007783 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007784 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007785 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007786 char_u *pdir = get_prevdir(scope);
7787
7788 // If still in the global directory, need to remember current
7789 // directory as the global directory.
7790 if (globaldir == NULL && pdir != NULL)
7791 globaldir = vim_strsave(pdir);
7792
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007793 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007794 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007795 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007796 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007797 curtab->tp_localdir = vim_strsave(NameBuff);
7798 else
7799 curwin->w_localdir = vim_strsave(NameBuff);
7800 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007801 }
7802 else
7803 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007804 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007805 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007806 }
7807
zeertzjq64be6aa2021-11-19 11:59:08 +00007808 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007809 shorten_fnames(TRUE);
7810}
7811
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007812/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007813 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7814 */
7815 void
7816trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7817{
7818#ifdef FEAT_EVAL
7819 dict_T *v_event;
7820 save_v_event_T save_v_event;
7821
7822 v_event = get_v_event(&save_v_event);
7823 (void)dict_add_string(v_event, "directory", new_dir);
7824 dict_set_items_ro(v_event);
7825#endif
7826 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7827#ifdef FEAT_EVAL
7828 restore_v_event(v_event, &save_v_event);
7829#endif
7830}
7831
7832/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007833 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007834 * chdir() function.
7835 * scope == CDSCOPE_WINDOW: changes the window-local directory
7836 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7837 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007838 * Returns TRUE if the directory is successfully changed.
7839 */
7840 int
7841changedir_func(
7842 char_u *new_dir,
7843 int forceit,
7844 cdscope_T scope)
7845{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007846 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007847 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007848 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007849 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007850 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007851
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007852 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007853 return FALSE;
7854
7855 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7856 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007857 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007858 return FALSE;
7859 }
7860
7861 // ":cd -": Change to previous directory
7862 if (STRCMP(new_dir, "-") == 0)
7863 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007864 pdir = get_prevdir(scope);
7865 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007866 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007867 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007868 return FALSE;
7869 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007870 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007871 }
7872
7873 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007874 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007875 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007876 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007877 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007878
Bakudankun29f3a452021-12-11 12:28:08 +00007879 // For UNIX ":cd" means: go to home directory.
7880 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007881#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007882 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007883#else
7884 if (*new_dir == NUL && p_cdh)
7885#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007886 {
7887 // use NameBuff for home directory name
7888# ifdef VMS
7889 char_u *p;
7890
7891 p = mch_getenv((char_u *)"SYS$LOGIN");
7892 if (p == NULL || *p == NUL) // empty is the same as not set
7893 NameBuff[0] = NUL;
7894 else
7895 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7896# else
7897 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7898# endif
7899 new_dir = NameBuff;
7900 }
zeertzjqf38aad82021-12-30 13:45:57 +00007901 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007902 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7903 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007904 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007905 if (scope == CDSCOPE_WINDOW)
7906 acmd_fname = (char_u *)"window";
7907 else if (scope == CDSCOPE_TABPAGE)
7908 acmd_fname = (char_u *)"tabpage";
7909 else
7910 acmd_fname = (char_u *)"global";
7911 trigger_DirChangedPre(acmd_fname, new_dir);
7912
7913 if (vim_chdir(new_dir))
7914 {
7915 emsg(_(e_command_failed));
7916 vim_free(pdir);
7917 return FALSE;
7918 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007919 }
zeertzjq73257142022-02-02 13:16:37 +00007920
7921 if (scope == CDSCOPE_WINDOW)
7922 pp = &curwin->w_prevdir;
7923 else if (scope == CDSCOPE_TABPAGE)
7924 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007925 else
zeertzjq73257142022-02-02 13:16:37 +00007926 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007927 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007928 *pp = pdir;
7929
7930 post_chdir(scope);
7931
7932 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007933 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007934 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007935 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007936}
Bram Moolenaarea408852005-06-25 22:49:46 +00007937
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007939 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007941 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007942ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007944 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945
7946 new_dir = eap->arg;
7947#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007948 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7949 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007951 ex_pwd(NULL);
7952 return;
7953 }
7954#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007955
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007956 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007957
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007958 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7959 scope = CDSCOPE_WINDOW;
7960 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7961 scope = CDSCOPE_TABPAGE;
7962
7963 if (changedir_func(new_dir, eap->forceit, scope))
7964 {
7965 // Echo the new current directory if the command was typed.
7966 if (KeyTyped || p_verbose >= 5)
7967 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 }
7969}
7970
7971/*
7972 * ":pwd".
7973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007975ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7978 {
7979#ifdef BACKSLASH_IN_FILENAME
7980 slash_adjust(NameBuff);
7981#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007982 if (p_verbose > 0)
7983 {
7984 char *context = "global";
7985
Bram Moolenaar05268152021-11-18 18:53:45 +00007986 if (last_chdir_reason != NULL)
7987 context = last_chdir_reason;
7988 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007989 context = "window";
7990 else if (curtab->tp_localdir != NULL)
7991 context = "tabpage";
7992 smsg("[%s] %s", context, (char *)NameBuff);
7993 }
7994 else
7995 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 }
7997 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007998 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999}
8000
8001/*
8002 * ":=".
8003 */
8004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008005ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008007 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008008 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009}
8010
8011 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008012ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008014 int n;
8015 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
8017 if (cursor_valid())
8018 {
8019 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8020 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00008021 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008023
8024 len = eap->line2;
8025 switch (*eap->arg)
8026 {
8027 case 'm': break;
8028 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008029 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008030 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008031
8032 // Hide the cursor if invoked with !
8033 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034}
8035
8036/*
8037 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008038 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 */
8040 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008041do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042{
Bram Moolenaar52953192019-08-16 10:27:13 +02008043 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008044 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008045# ifdef ELAPSED_FUNC
8046 elapsed_T start_tv;
8047
8048 // Remember at what time we started, so that we know how much longer we
8049 // should wait after waiting for a bit.
8050 ELAPSED_INIT(start_tv);
8051# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008053 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008054 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008055 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008056 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008057
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008058 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008059 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008061 wait_now = msec - done > 1000L ? 1000L : msec - done;
8062#ifdef FEAT_TIMERS
8063 {
8064 long due_time = check_due_timer();
8065
8066 if (due_time > 0 && due_time < wait_now)
8067 wait_now = due_time;
8068 }
8069#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008070#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008071 if (has_any_channel() && wait_now > 20L)
8072 wait_now = 20L;
8073#endif
8074#ifdef FEAT_SOUND
8075 if (has_any_sound_callback() && wait_now > 20L)
8076 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008077#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008078 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008079
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008080#ifdef FEAT_JOB_CHANNEL
8081 if (has_any_channel())
8082 ui_breakcheck_force(TRUE);
8083 else
8084#endif
8085 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008086#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008087 // Process the netbeans and clientserver messages that may have been
8088 // received in the call to ui_breakcheck() when the GUI is in use. This
8089 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008090 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008091#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008092
8093# ifdef ELAPSED_FUNC
8094 // actual time passed
8095 done = ELAPSED_FUNC(start_tv);
8096# else
8097 // guestimate time passed (will actually be more)
8098 done += wait_now;
8099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008101
8102 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8103 // input buffer, otherwise a following call to input() fails.
8104 if (got_int)
8105 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008106
8107 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008108 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109}
8110
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111/*
8112 * ":winsize" command (obsolete).
8113 */
8114 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008115ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116{
8117 int w, h;
8118 char_u *arg = eap->arg;
8119 char_u *p;
8120
Keith Thompson184f71c2024-01-04 21:19:04 +01008121 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008122 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008123 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008124 return;
8125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 w = getdigits(&arg);
8127 arg = skipwhite(arg);
8128 p = arg;
8129 h = getdigits(&arg);
8130 if (*p != NUL && *arg == NUL)
8131 set_shellsize(w, h, TRUE);
8132 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008133 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134}
8135
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008137ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138{
8139 int xchar = NUL;
8140 char_u *p;
8141
8142 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8143 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008144 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 if (eap->arg[1] == NUL)
8146 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008147 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 return;
8149 }
8150 xchar = eap->arg[1];
8151 p = eap->arg + 2;
8152 }
8153 else
8154 p = eap->arg + 1;
8155
Bram Moolenaar63b91732021-08-05 20:40:03 +02008156 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008158 if (*p != NUL && *p != (
8159#ifdef FEAT_EVAL
8160 in_vim9script() ? '#' :
8161#endif
8162 '"')
8163 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008164 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008165 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008167 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008168 postponed_split_flags = cmdmod.cmod_split;
8169 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8171 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008172 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 }
8174}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175
Bram Moolenaar843ee412004-06-30 16:16:41 +00008176#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177/*
8178 * ":winpos".
8179 */
8180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008181ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182{
8183 int x, y;
8184 char_u *arg = eap->arg;
8185 char_u *p;
8186
8187 if (*arg == NUL)
8188 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008189# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008190# ifdef VIMDLL
8191 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8192 mch_get_winpos(&x, &y) != FAIL)
8193# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008195# else
8196 if (mch_get_winpos(&x, &y) != FAIL)
8197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {
8199 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008200 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 }
8202 else
8203# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008204 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 }
8206 else
8207 {
8208 x = getdigits(&arg);
8209 arg = skipwhite(arg);
8210 p = arg;
8211 y = getdigits(&arg);
8212 if (*p == NUL || *arg != NUL)
8213 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008214 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 return;
8216 }
8217# ifdef FEAT_GUI
8218 if (gui.in_use)
8219 gui_mch_set_winpos(x, y);
8220 else if (gui.starting)
8221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008222 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 gui_win_x = x;
8224 gui_win_y = y;
8225 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008226# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 else
8228# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008229# endif
8230# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008231 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232# endif
8233# ifdef HAVE_TGETENT
8234 if (*T_CWP)
8235 term_set_winpos(x, y);
8236# endif
8237 }
8238}
8239#endif
8240
8241/*
8242 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8243 */
8244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008245ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246{
8247 oparg_T oa;
8248
8249 clear_oparg(&oa);
8250 oa.regname = eap->regname;
8251 oa.start.lnum = eap->line1;
8252 oa.end.lnum = eap->line2;
8253 oa.line_count = eap->line2 - eap->line1 + 1;
8254 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008256 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {
8258 setpcmark();
8259 curwin->w_cursor.lnum = eap->line1;
8260 beginline(BL_SOL | BL_FIX);
8261 }
8262
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008263 if (VIsual_active)
8264 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008265
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 switch (eap->cmdidx)
8267 {
8268 case CMD_delete:
8269 oa.op_type = OP_DELETE;
8270 op_delete(&oa);
8271 break;
8272
8273 case CMD_yank:
8274 oa.op_type = OP_YANK;
8275 (void)op_yank(&oa, FALSE, TRUE);
8276 break;
8277
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008278 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008279 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008281 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8282#else
8283 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008285 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 oa.op_type = OP_RSHIFT;
8287 else
8288 oa.op_type = OP_LSHIFT;
8289 op_shift(&oa, FALSE, eap->amount);
8290 break;
8291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008293 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294}
8295
8296/*
8297 * ":put".
8298 */
8299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008300ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008302 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 if (eap->line2 == 0)
8304 {
8305 eap->line2 = 1;
8306 eap->forceit = TRUE;
8307 }
8308 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008309 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008310 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008311 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312}
8313
8314/*
8315 * Handle ":copy" and ":move".
8316 */
8317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008318ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319{
8320 long n;
8321
Bram Moolenaar491799b2020-08-01 19:23:43 +02008322#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008323 if (not_in_vim9(eap) == FAIL)
8324 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008325#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008326 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008327 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {
8329 eap->nextcmd = NULL;
8330 return;
8331 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008332 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333
8334 /*
8335 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8336 */
8337 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8338 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008339 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 return;
8341 }
8342
8343 if (eap->cmdidx == CMD_move)
8344 {
8345 if (do_move(eap->line1, eap->line2, n) == FAIL)
8346 return;
8347 }
8348 else
8349 ex_copy(eap->line1, eap->line2, n);
8350 u_clearline();
8351 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008352 ex_may_print(eap);
8353}
8354
8355/*
8356 * Print the current line if flags were given to the Ex command.
8357 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008358 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008359ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008360{
8361 if (eap->flags != 0)
8362 {
8363 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8364 (eap->flags & EXFLAG_LIST));
8365 ex_no_reprint = TRUE;
8366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367}
8368
8369/*
8370 * ":smagic" and ":snomagic".
8371 */
8372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008373ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008375 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008377 magic_overruled = eap->cmdidx == CMD_smagic
8378 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008379 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008380 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381}
8382
8383/*
8384 * ":join".
8385 */
8386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008387ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388{
8389 curwin->w_cursor.lnum = eap->line1;
8390 if (eap->line1 == eap->line2)
8391 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008392 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 return;
8394 if (eap->line2 == curbuf->b_ml.ml_line_count)
8395 {
8396 beep_flush();
8397 return;
8398 }
8399 ++eap->line2;
8400 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008401 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008403 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404}
8405
8406/*
8407 * ":[addr]@r" or ":[addr]*r": execute register
8408 */
8409 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008410ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
8412 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008413 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
8415 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008416 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417
8418#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008419 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420#endif
8421
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008422 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 c = *eap->arg;
8424 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8425 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008426 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008427 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8428 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008429 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008431 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008434 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008436 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008438 /*
8439 * Execute from the typeahead buffer.
8440 * Continue until the stuff buffer is empty and all added characters
8441 * have been consumed.
8442 */
8443 while (!stuff_empty() || typebuf.tb_len > prev_len)
8444 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8445
8446 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447}
8448
8449/*
8450 * ":!".
8451 */
8452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008453ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8456}
8457
8458/*
8459 * ":undo".
8460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008462ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008464 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008465 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008466 else
8467 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468}
8469
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008470#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008472ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008473{
8474 char_u hash[UNDO_HASH_SIZE];
8475
8476 u_compute_hash(hash);
8477 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8478}
8479
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008481ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008482{
8483 char_u hash[UNDO_HASH_SIZE];
8484
8485 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008486 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008487}
8488#endif
8489
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490/*
8491 * ":redo".
8492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008494ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495{
8496 u_redo(1);
8497}
8498
8499/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008500 * ":earlier" and ":later".
8501 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008503ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008504{
8505 long count = 0;
8506 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008507 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008508 char_u *p = eap->arg;
8509
8510 if (*p == NUL)
8511 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008512 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008513 {
8514 count = getdigits(&p);
8515 switch (*p)
8516 {
8517 case 's': ++p; sec = TRUE; break;
8518 case 'm': ++p; sec = TRUE; count *= 60; break;
8519 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008520 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8521 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008522 }
8523 }
8524
8525 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008526 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008527 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008528 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8529 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008530}
8531
8532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 * ":redir": start/stop redirection.
8534 */
8535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008536ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
8538 char *mode;
8539 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008540 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541
Bram Moolenaarba768492016-07-08 15:32:54 +02008542#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008543 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008544 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008545 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008546 return;
8547 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008548#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008549
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 if (STRICMP(eap->arg, "END") == 0)
8551 close_redir();
8552 else
8553 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008554 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008556 ++arg;
8557 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008559 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 mode = "a";
8561 }
8562 else
8563 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008564 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565
8566 close_redir();
8567
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008568 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008569 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 if (fname == NULL)
8571 return;
8572#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008573 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 {
8575 char_u *browseFile;
8576
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008577 browseFile = do_browse(BROWSE_SAVE,
8578 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008579 fname, NULL, NULL,
8580 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008582 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 vim_free(fname);
8584 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008585 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 }
8587#endif
8588
8589 redir_fd = open_exfile(fname, eap->forceit, mode);
8590 vim_free(fname);
8591 }
8592#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008593 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008595 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008597 ++arg;
8598 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008600 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008601 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008603 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008605 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008606 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008607 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008608 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008610 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008611 if (*arg == '>')
8612 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008613 // Make register empty when not using @A-@Z and the
8614 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008615 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008616 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008618 }
8619 if (*arg != NUL)
8620 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008621 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008622 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008623 }
8624 }
8625 else if (*arg == '=' && arg[1] == '>')
8626 {
8627 int append;
8628
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008629 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008630 close_redir();
8631 arg += 2;
8632
8633 if (*arg == '>')
8634 {
8635 ++arg;
8636 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 }
8638 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008639 append = FALSE;
8640
8641 if (var_redir_start(skipwhite(arg), append) == OK)
8642 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 }
8644#endif
8645
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008646 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008649 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008652 // Make sure redirection is not off. Can happen for cmdline completion
8653 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008654 if (redir_fd != NULL
8655#ifdef FEAT_EVAL
8656 || redir_reg || redir_vname
8657#endif
8658 )
8659 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660}
8661
8662/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008663 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008665 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008666ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008668 redraw_cmd(eap->forceit);
8669}
8670
8671/*
8672 * ":redraw": force redraw, with clear if "clear" is TRUE.
8673 */
8674 void
8675redraw_cmd(int clear)
8676{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008677 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008679
8680 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008682
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008683 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008685 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 if (need_maketitle)
8687 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008688#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8689# ifdef VIMDLL
8690 if (!gui.in_use)
8691# endif
8692 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008693#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008694 RedrawingDisabled = save_RedrawingDisabled;
8695 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
Bram Moolenaar5017c662022-04-07 18:06:08 +01008697 // After drawing the statusline screen_attr may still be set.
8698 screen_stop_highlight();
8699
Bram Moolenaara2a89732022-08-31 14:46:18 +01008700 // Reset msg_didout, so that a message that's there is overwritten.
8701 msg_didout = FALSE;
8702 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008704 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008705 need_wait_return = FALSE;
8706
Bram Moolenaara653e532022-04-19 11:38:24 +01008707 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008708 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008709 redrawcmdline();
8710
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 out_flush();
8712}
8713
8714/*
8715 * ":redrawstatus": force redraw of status line(s)
8716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008718ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 if (eap->forceit)
8721 status_redraw_all();
8722 else
8723 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008724 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008725 return; // redraw later
8726
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008727 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008728 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008729
8730 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008731 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008732
zeertzjq320d9102022-09-20 17:12:13 +01008733 if (State & MODE_CMDLINE)
8734 redraw_statuslines();
8735 else
8736 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008737 RedrawingDisabled = save_RedrawingDisabled;
8738 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740}
8741
Bram Moolenaare12bab32019-01-08 22:02:56 +01008742/*
8743 * ":redrawtabline": force redraw of the tabline
8744 */
8745 static void
8746ex_redrawtabline(exarg_T *eap UNUSED)
8747{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008748 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008749 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008750
8751 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008752 p_lz = FALSE;
8753
8754 draw_tabline();
8755
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008756 RedrawingDisabled = save_RedrawingDisabled;
8757 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008758 out_flush();
8759}
8760
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008762close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763{
8764 if (redir_fd != NULL)
8765 {
8766 fclose(redir_fd);
8767 redir_fd = NULL;
8768 }
8769#ifdef FEAT_EVAL
8770 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008771 if (redir_vname)
8772 {
8773 var_redir_stop();
8774 redir_vname = 0;
8775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776#endif
8777}
8778
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008779#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008780 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008781vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008782{
8783 if (vim_mkdir(name, prot) != 0)
8784 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008785 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008786 return FAIL;
8787 }
8788 return OK;
8789}
8790#endif
8791
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792/*
8793 * Open a file for writing for an Ex command, with some checks.
8794 * Return file descriptor, or NULL on failure.
8795 */
8796 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008797open_exfile(
8798 char_u *fname,
8799 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008800 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
8802 FILE *fd;
8803
8804#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008805 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 if (mch_isdir(fname))
8807 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008808 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 return NULL;
8810 }
8811#endif
8812 if (!forceit && *mode != 'a' && vim_fexists(fname))
8813 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008814 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 return NULL;
8816 }
8817
8818 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008819 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820
8821 return fd;
8822}
8823
8824/*
8825 * ":mark" and ":k".
8826 */
8827 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008828ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829{
8830 pos_T pos;
8831
Bram Moolenaar10b94212021-02-19 21:42:57 +01008832#ifdef FEAT_EVAL
8833 if (not_in_vim9(eap) == FAIL)
8834 return;
8835#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008836 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008838 emsg(_(e_argument_required));
8839 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008841
8842 if (eap->arg[1] != NUL) // more than one character?
8843 {
8844 semsg(_(e_trailing_characters_str), eap->arg);
8845 return;
8846 }
8847
8848 pos = curwin->w_cursor; // save curwin->w_cursor
8849 curwin->w_cursor.lnum = eap->line2;
8850 beginline(BL_WHITE | BL_FIX);
8851 if (setmark(*eap->arg) == FAIL) // set mark
8852 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
8853 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854}
8855
8856/*
8857 * Update w_topline, w_leftcol and the cursor position.
8858 */
8859 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008860update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008862 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 update_topline();
8864 if (!curwin->w_p_wrap)
8865 validate_cursor();
8866 update_curswant();
8867}
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008870 * Save the current State and go to Normal mode.
8871 * Return TRUE if the typeahead could be saved.
8872 */
8873 int
8874save_current_state(save_state_T *sst)
8875{
8876 sst->save_msg_scroll = msg_scroll;
8877 sst->save_restart_edit = restart_edit;
8878 sst->save_msg_didout = msg_didout;
8879 sst->save_State = State;
8880 sst->save_insertmode = p_im;
8881 sst->save_finish_op = finish_op;
8882 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008883 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008884 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008885
Bram Moolenaar4324d872020-12-01 20:12:24 +01008886 msg_scroll = FALSE; // no msg scrolling in Normal mode
8887 restart_edit = 0; // don't go to Insert mode
8888 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008889
Bram Moolenaara452b802020-12-01 21:47:59 +01008890 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008891 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008892
8893 /*
8894 * Save the current typeahead. This is required to allow using ":normal"
8895 * from an event handler and makes sure we don't hang when the argument
8896 * ends with half a command.
8897 */
8898 save_typeahead(&sst->tabuf);
8899 return sst->tabuf.typebuf_valid;
8900}
8901
8902 void
8903restore_current_state(save_state_T *sst)
8904{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008905 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008906 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008907
8908 msg_scroll = sst->save_msg_scroll;
8909 restart_edit = sst->save_restart_edit;
8910 p_im = sst->save_insertmode;
8911 finish_op = sst->save_finish_op;
8912 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008913 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008914 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008915 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008916 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008917
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008918 // Restore the state (needed when called from a function executed for
8919 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008920 State = sst->save_State;
8921#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008922 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008923#endif
8924}
8925
8926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 * ":normal[!] {commands}": Execute normal mode commands.
8928 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008929 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008930ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008932 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 char_u *arg = NULL;
8934 int l;
8935 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008937 if (ex_normal_lock > 0)
8938 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008939 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008940 return;
8941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 if (ex_normal_busy >= p_mmd)
8943 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008944 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 return;
8946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 /*
8949 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8950 * this for the K_SPECIAL leading byte, otherwise special keys will not
8951 * work.
8952 */
8953 if (has_mbyte)
8954 {
8955 int len = 0;
8956
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008957 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 for (p = eap->arg; *p != NUL; ++p)
8959 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008960#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008961 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008963#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008964 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008965 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008966#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 )
8970 len += 2;
8971 }
8972 if (len > 0)
8973 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008974 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 if (arg != NULL)
8976 {
8977 len = 0;
8978 for (p = eap->arg; *p != NUL; ++p)
8979 {
8980 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008981#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 if (*p == CSI)
8983 {
8984 arg[len++] = KS_EXTRA;
8985 arg[len++] = (int)KE_CSI;
8986 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008987#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008988 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 {
8990 arg[len++] = *++p;
8991 if (*p == K_SPECIAL)
8992 {
8993 arg[len++] = KS_SPECIAL;
8994 arg[len++] = KE_FILLER;
8995 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008996#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 else if (*p == CSI)
8998 {
8999 arg[len++] = KS_EXTRA;
9000 arg[len++] = (int)KE_CSI;
9001 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 }
9004 arg[len] = NUL;
9005 }
9006 }
9007 }
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009010 ++ex_normal_busy;
9011 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 {
9013 /*
9014 * Repeat the :normal command for each line in the range. When no
9015 * range given, execute it just once, without positioning the cursor
9016 * first.
9017 */
9018 do
9019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 if (eap->addr_count != 0)
9021 {
9022 curwin->w_cursor.lnum = eap->line1++;
9023 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02009024 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 }
9026
Bram Moolenaar13505972019-01-24 15:04:48 +01009027 exec_normal_cmd(arg != NULL
9028 ? arg
9029 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 }
9031 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9032 }
9033
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009034 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 update_topline_cursor();
9036
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009037 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009039 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009040#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009041 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009042#endif
9043
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045}
9046
9047/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009048 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 */
9050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009051ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009053 if (eap->forceit)
9054 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009055 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009056 if (!curwin->w_cursor.lnum)
9057 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009058 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009059 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009060#ifdef FEAT_TERMINAL
9061 // Ignore this when running in an active terminal.
9062 if (term_job_running(curbuf->b_term))
9063 return;
9064#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009065
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009066 // Ignore the command when already in Insert mode. Inserting an
9067 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009068 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009069 return;
9070
Bram Moolenaar64969662005-12-14 21:59:55 +00009071 if (eap->cmdidx == CMD_startinsert)
9072 restart_edit = 'a';
9073 else if (eap->cmdidx == CMD_startreplace)
9074 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009076 restart_edit = 'V';
9077
9078 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009080 if (eap->cmdidx == CMD_startinsert)
9081 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009082 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009084
9085 if (VIsual_active)
9086 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087}
9088
9089/*
9090 * ":stopinsert"
9091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009093ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094{
9095 restart_edit = 0;
9096 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009097 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009100/*
9101 * Execute normal mode command "cmd".
9102 * "remap" can be REMAP_NONE or REMAP_YES.
9103 */
9104 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009105exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009106{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009107 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009108 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009109 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009110}
Bram Moolenaar25281632016-01-21 23:32:32 +01009111
Bram Moolenaar25281632016-01-21 23:32:32 +01009112/*
9113 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009114 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009115 */
9116 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009117exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009118{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009119 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009120 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009121
Bram Moolenaar905dd902019-04-07 14:21:47 +02009122 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9123 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009124 clear_oparg(&oa);
9125 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009126 while ((!stuff_empty()
9127 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009128 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009129 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009130 {
9131 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009132#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009133 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009134 && oa.op_type == OP_NOP && oa.regname == NUL
9135 && !VIsual_active)
9136 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009137 // If terminal_loop() returns OK we got a key that is handled
9138 // in Normal model. With FAIL we first need to position the
9139 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009140 if (terminal_loop(TRUE) == OK)
9141 normal_cmd(&oa, TRUE);
9142 }
9143 else
9144#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009145 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009146 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009147 }
9148}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009149
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150#ifdef FEAT_FIND_ID
9151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009152ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153{
9154 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9155 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009156 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157}
9158
Bram Moolenaar4033c552017-09-16 20:54:51 +02009159#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160/*
9161 * ":psearch"
9162 */
9163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009164ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165{
9166 g_do_tagpreview = p_pvh;
9167 ex_findpat(eap);
9168 g_do_tagpreview = 0;
9169}
9170#endif
9171
9172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009173ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174{
9175 int whole = TRUE;
9176 long n;
9177 char_u *p;
9178 int action;
9179
9180 switch (cmdnames[eap->cmdidx].cmd_name[2])
9181 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009182 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9184 action = ACTION_GOTO;
9185 else
9186 action = ACTION_SHOW;
9187 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009188 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 action = ACTION_SHOW_ALL;
9190 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009191 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 action = ACTION_GOTO;
9193 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009194 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 action = ACTION_SPLIT;
9196 break;
9197 }
9198
9199 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009200 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 {
9202 n = getdigits(&eap->arg);
9203 eap->arg = skipwhite(eap->arg);
9204 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009205 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 {
9207 whole = FALSE;
9208 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009209 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 if (*p)
9211 {
9212 *p++ = NUL;
9213 p = skipwhite(p);
9214
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009215 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009216 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009217 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009219 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 }
9221 }
9222 if (!eap->skip)
9223 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9224 whole, !eap->forceit,
9225 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009226 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227}
9228#endif
9229
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
Bram Moolenaar4033c552017-09-16 20:54:51 +02009231#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232/*
9233 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9234 */
9235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009236ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009238 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9240}
9241
9242/*
9243 * ":pedit"
9244 */
9245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009246ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247{
9248 win_T *curwin_save = curwin;
9249
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009250 if (ERROR_IF_ANY_POPUP_WINDOW)
9251 return;
9252
Bram Moolenaar026587b2019-08-17 15:08:00 +02009253 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009255 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009256
Bram Moolenaar026587b2019-08-17 15:08:00 +02009257 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009259
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 if (curwin != curwin_save && win_valid(curwin_save))
9261 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009262 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009264 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 win_enter(curwin_save, TRUE);
9266 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009267# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009268 else if (WIN_IS_POPUP(curwin))
9269 {
9270 // can't keep focus in popup window
9271 win_enter(firstwin, TRUE);
9272 }
9273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 g_do_tagpreview = 0;
9275}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277
9278/*
9279 * ":stag", ":stselect" and ":stjump".
9280 */
9281 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009282ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283{
9284 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009285 postponed_split_flags = cmdmod.cmod_split;
9286 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9288 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009289 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291
9292/*
9293 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9294 */
9295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009296ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297{
9298 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9299}
9300
9301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009302ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304 int cmd;
9305
9306 switch (name[1])
9307 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009308 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009310 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009312 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009314 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009316 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009318 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009320 case 'f': // ":tfirst"
9321 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009323 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009325 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009327 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009329 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 return;
9331 }
9332#endif
9333 cmd = DT_TAG;
9334 break;
9335 }
9336
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009337 if (name[0] == 'l')
9338 {
9339#ifndef FEAT_QUICKFIX
9340 ex_ni(eap);
9341 return;
9342#else
9343 cmd = DT_LTAG;
9344#endif
9345 }
9346
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9348 eap->forceit, TRUE);
9349}
9350
John Marriotted908f72024-04-18 22:46:56 +02009351enum {
9352 SPEC_PERC = 0,
9353 SPEC_HASH,
9354 SPEC_CWORD, // cursor word
9355 SPEC_CCWORD, // cursor WORD
9356 SPEC_CEXPR, // expr under cursor
9357 SPEC_CFILE, // cursor path name
9358 SPEC_SFILE, // ":so" file name
9359 SPEC_SLNUM, // ":so" file line number
9360 SPEC_STACK, // call stack
9361 SPEC_SCRIPT, // script file name
9362 SPEC_AFILE, // autocommand file name
9363 SPEC_ABUF, // autocommand buffer number
9364 SPEC_AMATCH, // autocommand match name
9365 SPEC_SFLNUM, // script file line number
9366 SPEC_SID // script ID: <SNR>123_
9367#ifdef FEAT_CLIENTSERVER
9368 , SPEC_CLIENT
9369#endif
9370};
9371
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009373 * Check "str" for starting with a special cmdline variable.
9374 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9375 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9376 */
9377 int
Mike Williams51024bb2024-05-30 07:46:30 +02009378find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009379{
John Marriotted908f72024-04-18 22:46:56 +02009380 // must be sorted by the 'value' field because it is used by bsearch()!
9381 static keyvalue_T spec_str_tab[] = {
9382 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9383 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9384 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9385 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9386 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9387 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9388 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009389#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009390 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009391#endif
John Marriotted908f72024-04-18 22:46:56 +02009392 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9393 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9394 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9395 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9396 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9397 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009398 };
John Marriotted908f72024-04-18 22:46:56 +02009399 keyvalue_T target;
9400 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009401
John Marriotted908f72024-04-18 22:46:56 +02009402 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009403 {
John Marriotted908f72024-04-18 22:46:56 +02009404 case '%':
9405 *usedlen = 1;
9406 return SPEC_PERC;
9407
9408 case '#':
9409 *usedlen = 1;
9410 return SPEC_HASH;
9411
9412 case '<':
9413 target.key = 0;
9414 target.value = (char *)src + 1; // skip over '<'
9415 target.length = 0; // not used, see cmp_keyvalue_value_n()
9416
9417 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab, ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]), cmp_keyvalue_value_n);
9418 if (entry == NULL)
9419 return -1;
9420
9421 *usedlen = entry->length + 1;
9422 return entry->key;
9423
9424 default:
9425 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009426 }
John Marriotted908f72024-04-18 22:46:56 +02009427
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009428 return -1;
9429}
9430
9431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 * Evaluate cmdline variables.
9433 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009434 * change "%" to curbuf->b_ffname
9435 * "#" to curwin->w_alt_fnum
9436 * "%%" to curwin->w_alt_fnum in Vim9 script
9437 * "<cword>" to word under the cursor
9438 * "<cWORD>" to WORD under the cursor
9439 * "<cexpr>" to C-expression under the cursor
9440 * "<cfile>" to path name under the cursor
9441 * "<sfile>" to sourced file name
9442 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009443 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009444 * "<slnum>" to sourced file line number
9445 * "<afile>" to file name for autocommand
9446 * "<abuf>" to buffer number for autocommand
9447 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 *
9449 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9450 * "" for error without a message) and NULL is returned.
9451 * Returns an allocated string if a valid match was found.
9452 * Returns NULL if no match was found. "usedlen" then still contains the
9453 * number of characters to skip.
9454 */
9455 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009456eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009457 char_u *src, // pointer into commandline
9458 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009459 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009460 linenr_T *lnump, // line number for :e command, or NULL
9461 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009462 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009463 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009464 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465{
9466 int i;
9467 char_u *s;
9468 char_u *result;
9469 char_u *resultbuf = NULL;
9470 int resultlen;
9471 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009472 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009474 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477
9478 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009479 if (escaped != NULL)
9480 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
9482 /*
9483 * Check if there is something to do.
9484 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009485 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009486 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 {
9488 *usedlen = 1;
9489 return NULL;
9490 }
9491
9492 /*
9493 * Skip when preceded with a backslash "\%" and "\#".
9494 * Note: In "\\%" the % is also not recognized!
9495 */
9496 if (src > srcstart && src[-1] == '\\')
9497 {
9498 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009499 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 return NULL;
9501 }
9502
9503 /*
9504 * word or WORD under cursor
9505 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009506 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9507 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009509 resultlen = find_ident_under_cursor(&result,
9510 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9511 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9512 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 if (resultlen == 0)
9514 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009515 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 return NULL;
9517 }
9518 }
9519
9520 /*
9521 * '#': Alternate file name
9522 * '%': Current file name
9523 * File name under the cursor
9524 * File name for autocommand
9525 * and following modifiers
9526 */
9527 else
9528 {
Mike Williams51024bb2024-05-30 07:46:30 +02009529 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009530
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 switch (spec_idx)
9532 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009533 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009534#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009535 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009538 // '%': current file
9539 if (curbuf->b_fname == NULL)
9540 {
9541 result = (char_u *)"";
9542 valid = 0; // Must have ":p:h" to be valid
9543 }
9544 else
9545 {
9546 result = curbuf->b_fname;
9547 tilde_file = STRCMP(result, "~") == 0;
9548 }
9549 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009551#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009552 // "%%" alternate file
9553 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009554#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009555 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009556 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009557 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009559 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 result = arg_all();
9561 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009562 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009563 if (escaped != NULL)
9564 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 break;
9567 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009568 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009569 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009570 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009572 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009573 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009574 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009575 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009577 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009579 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009580 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009581 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009582 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009583 return NULL;
9584 }
9585#ifdef FEAT_EVAL
9586 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9587 (long)i);
9588 if (result == NULL)
9589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009590 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009591 return NULL;
9592 }
9593#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009594 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 }
9598 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009599 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009600 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9601 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009602 buf = buflist_findnr(i);
9603 if (buf == NULL)
9604 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009605 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009606 return NULL;
9607 }
9608 if (lnump != NULL)
9609 *lnump = ECMD_LAST;
9610 if (buf->b_fname == NULL)
9611 {
9612 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009613 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009614 }
9615 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009616 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009617 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009618 tilde_file = STRCMP(result, "~") == 0;
9619 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 break;
9622
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009623 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009624 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 if (result == NULL)
9626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009627 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 return NULL;
9629 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009630 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009633 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009635 if (result != NULL && !autocmd_fname_full)
9636 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009637 // Still need to turn the fname into a full path. It is
9638 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009639 autocmd_fname_full = TRUE;
9640 result = FullName_save(autocmd_fname, FALSE);
9641 vim_free(autocmd_fname);
9642 autocmd_fname = result;
9643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 if (result == NULL)
9645 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009646 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 return NULL;
9648 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009649 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 break;
9651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009652 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 if (autocmd_bufnr <= 0)
9654 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009655 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 return NULL;
9657 }
9658 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9659 result = strbuf;
9660 break;
9661
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009662 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 result = autocmd_match;
9664 if (result == NULL)
9665 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009666 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 return NULL;
9668 }
9669 break;
9670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009671 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009672 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 if (result == NULL)
9674 {
LemonBoy6013d002022-04-09 21:42:10 +01009675 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9676 return NULL;
9677 }
9678 resultbuf = result; // remember allocated string
9679 break;
9680 case SPEC_STACK: // call stack
9681 result = estack_sfile(ESTACK_STACK);
9682 if (result == NULL)
9683 {
9684 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9685 return NULL;
9686 }
9687 resultbuf = result; // remember allocated string
9688 break;
9689 case SPEC_SCRIPT: // script file name
9690 result = estack_sfile(ESTACK_SCRIPT);
9691 if (result == NULL)
9692 {
9693 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 return NULL;
9695 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009696 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009699 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009700 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009701 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009702 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009703 return NULL;
9704 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009705 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009706 result = strbuf;
9707 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009708
9709#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009710 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009711 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009712 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009713 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009714 return NULL;
9715 }
9716 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009717 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009718 result = strbuf;
9719 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009720
Bram Moolenaar90944302020-08-01 20:45:11 +02009721 case SPEC_SID:
9722 if (current_sctx.sc_sid <= 0)
9723 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009724 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009725 return NULL;
9726 }
9727 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9728 result = strbuf;
9729 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009730#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009731
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009732#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009733 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009734 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9735 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 result = strbuf;
9737 break;
9738#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009739
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009740 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009741 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009742 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 }
9744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009745 resultlen = (int)STRLEN(result); // length of new string
9746 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 {
9748 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 resultlen = (int)(s - result);
9751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 else if (!skip_mod)
9753 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009754 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 &resultlen);
9756 if (result == NULL)
9757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009758 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 return NULL;
9760 }
9761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 }
9763
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009764 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009766 if (empty_is_error)
9767 {
9768 if (valid != VALID_HEAD + VALID_PATH)
9769 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9770 else
9771 *errormsg = _(e_evaluates_to_an_empty_string);
9772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 result = NULL;
9774 }
9775 else
9776 result = vim_strnsave(result, resultlen);
9777 vim_free(resultbuf);
9778 return result;
9779}
9780
9781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 * Expand the <sfile> string in "arg".
9783 *
9784 * Returns an allocated string, or NULL for any error.
9785 */
9786 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009787expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009789 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +02009791 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +02009793 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +02009795 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +02009796 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 char_u *p;
9798
John Marriotted908f72024-04-18 22:46:56 +02009799 resultlen = STRLEN(arg);
9800 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 if (result == NULL)
9802 return NULL;
9803
9804 for (p = result; *p; )
9805 {
9806 if (STRNCMP(p, "<sfile>", 7) != 0)
9807 ++p;
9808 else
9809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009810 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009811 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 if (errormsg != NULL)
9813 {
9814 if (*errormsg)
9815 emsg(errormsg);
9816 vim_free(result);
9817 return NULL;
9818 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009819 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 {
9821 p += srclen;
9822 continue;
9823 }
John Marriotted908f72024-04-18 22:46:56 +02009824 repllen = STRLEN(repl);
9825 resultlen += repllen - srclen;
9826 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 if (newres == NULL)
9828 {
9829 vim_free(repl);
9830 vim_free(result);
9831 return NULL;
9832 }
John Marriotted908f72024-04-18 22:46:56 +02009833 len = p - result;
9834 mch_memmove(newres, result, len);
9835 STRCPY(newres + len, repl);
9836 len += repllen;
9837 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 vim_free(repl);
9839 vim_free(result);
9840 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009841 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 }
9843 }
9844
9845 return result;
9846}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009849/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009850 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009851 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009854dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 if (fname == NULL)
9857 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009858 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859}
9860#endif
9861
9862/*
9863 * ":behave {mswin,xterm}"
9864 */
9865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009866ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867{
9868 if (STRCMP(eap->arg, "mswin") == 0)
9869 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009870 set_option_value_give_err((char_u *)"selection",
9871 0L, (char_u *)"exclusive", 0);
9872 set_option_value_give_err((char_u *)"selectmode",
9873 0L, (char_u *)"mouse,key", 0);
9874 set_option_value_give_err((char_u *)"mousemodel",
9875 0L, (char_u *)"popup", 0);
9876 set_option_value_give_err((char_u *)"keymodel",
9877 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 }
9879 else if (STRCMP(eap->arg, "xterm") == 0)
9880 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009881 set_option_value_give_err((char_u *)"selection",
9882 0L, (char_u *)"inclusive", 0);
9883 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9884 set_option_value_give_err((char_u *)"mousemodel",
9885 0L, (char_u *)"extend", 0);
9886 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 }
9888 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009889 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890}
9891
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892static int filetype_detect = FALSE;
9893static int filetype_plugin = FALSE;
9894static int filetype_indent = FALSE;
9895
9896/*
9897 * ":filetype [plugin] [indent] {on,off,detect}"
9898 * on: Load the filetype.vim file to install autocommands for file types.
9899 * off: Load the ftoff.vim file to remove all autocommands for file types.
9900 * plugin on: load filetype.vim and ftplugin.vim
9901 * plugin off: load ftplugof.vim
9902 * indent on: load filetype.vim and indent.vim
9903 * indent off: load indoff.vim
9904 */
9905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009906ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907{
9908 char_u *arg = eap->arg;
9909 int plugin = FALSE;
9910 int indent = FALSE;
9911
9912 if (*eap->arg == NUL)
9913 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009914 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009915 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 filetype_detect ? "ON" : "OFF",
9917 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9918 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9919 return;
9920 }
9921
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009922 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 for (;;)
9924 {
9925 if (STRNCMP(arg, "plugin", 6) == 0)
9926 {
9927 plugin = TRUE;
9928 arg = skipwhite(arg + 6);
9929 continue;
9930 }
9931 if (STRNCMP(arg, "indent", 6) == 0)
9932 {
9933 indent = TRUE;
9934 arg = skipwhite(arg + 6);
9935 continue;
9936 }
9937 break;
9938 }
9939 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9940 {
9941 if (*arg == 'o' || !filetype_detect)
9942 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009943 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 filetype_detect = TRUE;
9945 if (plugin)
9946 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009947 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 filetype_plugin = TRUE;
9949 }
9950 if (indent)
9951 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009952 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 filetype_indent = TRUE;
9954 }
9955 }
9956 if (*arg == 'd')
9957 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009958 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009959 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 }
9961 }
9962 else if (STRCMP(arg, "off") == 0)
9963 {
9964 if (plugin || indent)
9965 {
9966 if (plugin)
9967 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009968 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 filetype_plugin = FALSE;
9970 }
9971 if (indent)
9972 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009973 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 filetype_indent = FALSE;
9975 }
9976 }
9977 else
9978 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009979 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 filetype_detect = FALSE;
9981 }
9982 }
9983 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009984 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985}
9986
9987/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009988 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 */
9990 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009991ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992{
zeertzjq5bf6c212024-03-31 18:41:27 +02009993 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009994 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009995
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009996 char_u *arg = eap->arg;
9997 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9998 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009999
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010000 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10001 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +020010002 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010006ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007{
10008#ifdef FEAT_DIGRAPHS
10009 if (*eap->arg != NUL)
10010 putdigraph(eap->arg);
10011 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010012 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013#else
mityu61065042021-07-19 20:07:21 +020010014 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015#endif
10016}
10017
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010018#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10019 void
10020set_no_hlsearch(int flag)
10021{
10022 no_hlsearch = flag;
10023# ifdef FEAT_EVAL
10024 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10025# endif
10026}
10027
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028/*
10029 * ":nohlsearch"
10030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010032ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010034 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +010010035 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037#endif
10038
10039#ifdef FEAT_CRYPT
10040/*
10041 * ":X": Get crypt key
10042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010044ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010046 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010047 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048}
10049#endif
10050
10051#ifdef FEAT_FOLDING
10052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010053ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054{
10055 if (foldManualAllowed(TRUE))
10056 foldCreate(eap->line1, eap->line2);
10057}
10058
10059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010060ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061{
10062 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10063 eap->forceit, FALSE);
10064}
10065
10066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010067ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068{
10069 linenr_T lnum;
10070
Bram Moolenaar4facea32019-10-12 20:17:40 +020010071# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010072 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010073# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010074
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010075 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10077 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10078 ml_setmarked(lnum);
10079
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010080 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010082 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010083# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010084 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010085# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086}
10087#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010089#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010090/*
10091 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10092 * instead of a quickfix command.
10093 */
10094 int
10095is_loclist_cmd(int cmdidx)
10096{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010097 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010098 return FALSE;
10099 return cmdnames[cmdidx].cmd_name[0] == 'l';
10100}
10101#endif
10102
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010103 int
10104get_pressedreturn(void)
10105{
10106 return ex_pressedreturn;
10107}
10108
10109 void
10110set_pressedreturn(int val)
10111{
10112 ex_pressedreturn = val;
10113}