blob: 614ba20805a80a8003a8978f2817eb7e9827107d [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/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002725 * Handle a range without a command.
2726 * Returns an error message on failure.
2727 */
2728 char *
2729ex_range_without_command(exarg_T *eap)
2730{
2731 char *errormsg = NULL;
2732
Mohamed Akramc25a7082024-07-12 20:17:55 +02002733 if ((*eap->cmd == '|' || exmode_active)
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002734#ifdef FEAT_EVAL
2735 && !in_vim9script()
2736#endif
2737 )
2738 {
2739 eap->cmdidx = CMD_print;
2740 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2741 if ((errormsg = invalid_range(eap)) == NULL)
2742 {
2743 correct_range(eap);
2744 ex_print(eap);
2745 }
2746 }
2747 else if (eap->addr_count != 0)
2748 {
2749 if (eap->line2 > curbuf->b_ml.ml_line_count)
2750 {
2751 // With '-' in 'cpoptions' a line number past the file is an
2752 // error, otherwise put it at the end of the file.
2753 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2754 eap->line2 = -1;
2755 else
2756 eap->line2 = curbuf->b_ml.ml_line_count;
2757 }
2758
2759 if (eap->line2 < 0)
2760 errormsg = _(e_invalid_range);
2761 else
2762 {
2763 if (eap->line2 == 0)
2764 curwin->w_cursor.lnum = 1;
2765 else
2766 curwin->w_cursor.lnum = eap->line2;
2767 beginline(BL_SOL | BL_FIX);
2768 }
2769 }
2770 return errormsg;
2771}
2772
2773/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002774 * Check for an Ex command with optional tail.
2775 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002776 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002777 */
2778 static int
2779checkforcmd_opt(
2780 char_u **pp, // start of command
2781 char *cmd, // name of command
2782 int len, // required length
2783 int noparen)
2784{
2785 int i;
2786
2787 for (i = 0; cmd[i] != NUL; ++i)
2788 if (((char_u *)cmd)[i] != (*pp)[i])
2789 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002790 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002791 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002792 {
2793 *pp = skipwhite(*pp + i);
2794 return TRUE;
2795 }
2796 return FALSE;
2797}
2798
2799/*
2800 * Check for an Ex command with optional tail.
2801 * If there is a match advance "pp" to the argument and return TRUE.
2802 */
2803 int
2804checkforcmd(
2805 char_u **pp, // start of command
2806 char *cmd, // name of command
2807 int len) // required length
2808{
2809 return checkforcmd_opt(pp, cmd, len, FALSE);
2810}
2811
2812/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002813 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002814 * If there is a match advance "pp" to the argument and return TRUE.
2815 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002816 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002817checkforcmd_noparen(
2818 char_u **pp, // start of command
2819 char *cmd, // name of command
2820 int len) // required length
2821{
2822 return checkforcmd_opt(pp, cmd, len, TRUE);
2823}
2824
2825/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002826 * Parse and skip over command modifiers:
2827 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002828 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002829 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002830 * When "skip_only" is TRUE the global variables are not changed, except for
2831 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002832 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2833 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002834 * Call apply_cmdmod() to get the side effects of the modifiers:
2835 * - Increment "sandbox" for ":sandbox"
2836 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002837 * - set msg_silent for ":silent"
2838 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 * Return FAIL when the command is not to be executed.
2840 * May set "errormsg" to an error message.
2841 */
2842 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002843parse_command_modifiers(
2844 exarg_T *eap,
2845 char **errormsg,
2846 cmdmod_T *cmod,
2847 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002848{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002849 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002850 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002851 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002852 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002853 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002854 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002855
Bram Moolenaare1004402020-10-24 20:49:43 +02002856 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002857 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002858
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002859 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2860 {
2861 // The automatically inserted Visual area range is skipped, so that
2862 // typing ":cmdmod cmd" in Visual mode works without having to move the
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002863 // range to after the modififiers. The command will be
2864 // "'<,'>cmdmod cmd", parse "cmdmod cmd" and then put back "'<,'>"
2865 // before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002866 eap->cmd += 5;
2867 cmd_start = eap->cmd;
2868 has_visual_range = TRUE;
2869 }
2870
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002871 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002872 for (;;)
2873 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002874 char_u *p;
2875
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002877 {
2878 if (*eap->cmd == ':')
2879 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002881 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002882
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002883 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002885 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2886 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002887 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2888 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002889 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002890 if (!skip_only)
2891 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002892 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002893 }
2894
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002895 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002896 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002897 {
2898 // a comment ends at a NL
2899 if (eap->nextcmd == NULL)
2900 {
2901 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2902 if (eap->nextcmd != NULL)
2903 ++eap->nextcmd;
2904 }
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002905 if (vim9script)
2906 {
2907 if (has_cmdmod(cmod, FALSE))
2908 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002909#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002910 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2911 && eap->cmd[2] != '{')
2912 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002913#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002914 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002915 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002916 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002917 if (*eap->cmd == NUL)
2918 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002919 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002920 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002921 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002922 if (vim9script && has_cmdmod(cmod, FALSE))
2923 *errormsg = _(e_command_modifier_without_command);
2924 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002925 return FAIL;
2926 }
2927
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002928 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002929
2930 // In Vim9 script a variable can shadow a command modifier:
2931 // verbose = 123
2932 // verbose += 123
2933 // silent! verbose = func()
2934 // verbose.member = 2
2935 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002936 // But not:
2937 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002938 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002939 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002940 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002941
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002942 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002943 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002944 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002945 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2946 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002947 break;
2948 }
2949
Bram Moolenaareffed932018-08-14 13:38:17 +02002950 switch (*p)
2951 {
John Marriotted908f72024-04-18 22:46:56 +02002952 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002953 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002954 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002955 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002956 continue;
2957
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002958 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002959 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002960 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002961 continue;
2962 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002963 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002964 {
2965#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002966 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002967#endif
2968 continue;
2969 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002970 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002971 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002972 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002973 continue;
2974
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002975 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002976 break;
2977#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002978 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002979#endif
2980 continue;
2981
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002982 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002983 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002984 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002985 continue;
2986 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002987 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002988 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002989 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002990 continue;
2991 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002992 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002993 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002994 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002995 continue;
2996 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002997 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002998 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002999 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003000 continue;
3001
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003002 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003003 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003004 char_u *reg_pat;
3005 char_u *nulp = NULL;
3006 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003007
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003008 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003009 || *p == NUL
3010 || (ends_excmd(*p)
3011#ifdef FEAT_EVAL
3012 // in ":filter #pat# cmd" # does not
3013 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003014 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003015#endif
3016 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003017 break;
3018 if (*p == '!')
3019 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003020 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 p = skipwhite(p + 1);
3022 if (*p == NUL || ends_excmd(*p))
3023 break;
3024 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003025#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003026 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003027 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003028 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003029#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003030 if (skip_only)
3031 p = skip_vimgrep_pat(p, NULL, NULL);
3032 else
3033 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003034 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3035 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003036 if (p == NULL || *p == NUL)
3037 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003038 if (!skip_only)
3039 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003040 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003041 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003042 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003043 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003044 // restore the character overwritten by NUL
3045 if (nulp != NULL)
3046 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003047 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003048 eap->cmd = p;
3049 continue;
3050 }
3051
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003052 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3053 {
3054 cmod->cmod_split |= WSP_HOR;
3055 continue;
3056 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003057 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003058 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003059 || *p == NUL || ends_excmd(*p))
3060 break;
3061 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003062 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003063 continue;
3064
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003065 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003066 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003067 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003068 continue;
3069 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003070 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3071 {
3072 if (ends_excmd2(p, eap->cmd))
3073 {
3074 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003075 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003076 return FAIL;
3077 }
3078 cmod->cmod_flags |= CMOD_LEGACY;
3079 continue;
3080 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003081
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003082 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003083 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003084 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003085 continue;
3086
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003087 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003088 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003089 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003090 continue;
3091 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003092 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003093 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003094 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003095 continue;
3096
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003097 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003098 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003099 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003100 continue;
3101
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003102 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003103 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003104 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003105 continue;
3106 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003107 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003108 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003109 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003110 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3111 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003112 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003113 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003114 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003115 }
3116 continue;
3117
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003118 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003119 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003120 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003121 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003122 long tabnr = get_address(eap, &eap->cmd,
3123 ADDR_TABS, eap->skip,
3124 skip_only, FALSE, 1);
3125 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003126 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003127 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003128 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003129 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3130 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003131 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003132 return FAIL;
3133 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003134 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003135 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003136 }
3137 eap->cmd = p;
3138 continue;
3139 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003140 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003141 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003142 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003143 continue;
3144
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003145 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003146 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003147 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003148 continue;
3149
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003150 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003151 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003152 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003153 continue;
3154 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003155 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003156 {
3157 if (ends_excmd2(p, eap->cmd))
3158 {
3159 *errormsg =
3160 _(e_vim9cmd_must_be_followed_by_command);
3161 return FAIL;
3162 }
3163 cmod->cmod_flags |= CMOD_VIM9CMD;
3164 continue;
3165 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003166 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003167 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003168 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003169 {
zeertzjqcd749632022-06-14 13:30:35 +01003170 // zero means not set, one is verbose == 0, etc.
3171 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003172 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003173 else
zeertzjqcd749632022-06-14 13:30:35 +01003174 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003175 eap->cmd = p;
3176 continue;
3177 }
3178 break;
3179 }
3180
Bram Moolenaar1501b632022-03-27 16:56:21 +01003181 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003182 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003183 if (eap->cmd > cmd_start)
3184 {
3185 // Move the '<,'> range to after the modifiers and insert a colon.
3186 // Since the modifiers have been parsed put the colon on top of the
3187 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3188 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003189 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003190 {
3191 size_t len = STRLEN(cmd_start);
3192
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003193 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003194 // "'<,'>mods" -> "mods *+
3195 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003196 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003197 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003198 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003199 }
3200 else
3201 {
3202 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3203 eap->cmd -= 5;
3204 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3205 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003206 }
3207 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003208 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003209 // Special case: change empty command to "+".
3210 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003211 eap->cmd = (char_u *)"'<,'>+";
3212 else
3213 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003214 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003215 else if (use_plus_cmd)
3216 eap->cmd = (char_u *)"+";
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003217
Bram Moolenaareffed932018-08-14 13:38:17 +02003218 return OK;
3219}
3220
3221/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003222 * Return TRUE if "cmod" has anything set.
3223 */
3224 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003225has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003226{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003227 return (cmod->cmod_flags != 0 && (!ignore_silent
3228 || (cmod->cmod_flags
3229 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003230 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003231 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003232 || cmod->cmod_tab != 0
3233 || cmod->cmod_filter_regmatch.regprog != NULL;
3234}
3235
Bram Moolenaar8991be22022-02-14 21:51:46 +00003236#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003237/*
3238 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3239 */
3240 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003241cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003242{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003243 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003244 {
3245 emsg(_(e_misplaced_command_modifier));
3246 return TRUE;
3247 }
3248 return FALSE;
3249}
Dominique Pelle748b3082022-01-08 12:41:16 +00003250#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003251
3252/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003253 * Apply the command modifiers. Saves current state in "cmdmod", call
3254 * undo_cmdmod() later.
3255 */
3256 void
3257apply_cmdmod(cmdmod_T *cmod)
3258{
3259#ifdef HAVE_SANDBOX
3260 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3261 {
3262 ++sandbox;
3263 cmod->cmod_did_sandbox = TRUE;
3264 }
3265#endif
zeertzjqcd749632022-06-14 13:30:35 +01003266 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003267 {
3268 if (cmod->cmod_verbose_save == 0)
3269 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003270 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003271 }
3272
3273 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3274 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003275 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003276 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003277 cmod->cmod_save_msg_scroll = msg_scroll;
3278 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003279 if (cmod->cmod_flags & CMOD_SILENT)
3280 ++msg_silent;
3281 if (cmod->cmod_flags & CMOD_UNSILENT)
3282 msg_silent = 0;
3283
3284 if (cmod->cmod_flags & CMOD_ERRSILENT)
3285 {
3286 ++emsg_silent;
3287 ++cmod->cmod_did_esilent;
3288 }
3289
Bram Moolenaare1004402020-10-24 20:49:43 +02003290 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003291 {
3292 // Set 'eventignore' to "all".
3293 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003294 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003295 set_string_option_direct((char_u *)"ei", -1,
3296 (char_u *)"all", OPT_FREE, SID_NONE);
3297 }
3298}
3299
3300/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003301 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003302 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003303 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003304undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003305{
Bram Moolenaare1004402020-10-24 20:49:43 +02003306 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003307 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003308 p_verbose = cmod->cmod_verbose_save - 1;
3309 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003310 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003311
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003312#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003313 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003314 {
3315 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003316 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003317 }
3318#endif
3319
Bram Moolenaare1004402020-10-24 20:49:43 +02003320 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003321 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003322 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003323 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3324 OPT_FREE, SID_NONE);
3325 free_string_option(cmod->cmod_save_ei);
3326 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003327 }
3328
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003329 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003330
Bram Moolenaare1004402020-10-24 20:49:43 +02003331 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003332 {
3333 // messages could be enabled for a serious error, need to check if the
3334 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003335 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3336 msg_silent = cmod->cmod_save_msg_silent - 1;
3337 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003338 if (emsg_silent < 0)
3339 emsg_silent = 0;
3340 // Restore msg_scroll, it's set by file I/O commands, even when no
3341 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003342 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003343
3344 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3345 // somewhere in the line. Put it back in the first column.
3346 if (redirecting())
3347 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003348
Bram Moolenaare1004402020-10-24 20:49:43 +02003349 cmod->cmod_save_msg_silent = 0;
3350 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003351 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003352}
3353
3354/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003355 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003356 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003357 * Return FAIL and set "errormsg" or return OK.
3358 */
3359 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003360parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003361{
3362 int address_count = 1;
3363 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003364 int need_check_cursor = FALSE;
3365 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003366
3367 // Repeat for all ',' or ';' separated addresses.
3368 for (;;)
3369 {
3370 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003371 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003372 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003373 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003374 eap->addr_count == 0, address_count++);
3375 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003376 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003377 if (lnum == MAXLNUM)
3378 {
3379 if (*eap->cmd == '%') // '%' - all lines
3380 {
3381 ++eap->cmd;
3382 switch (eap->addr_type)
3383 {
3384 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003385 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003386 eap->line1 = 1;
3387 eap->line2 = curbuf->b_ml.ml_line_count;
3388 break;
3389 case ADDR_LOADED_BUFFERS:
3390 {
3391 buf_T *buf = firstbuf;
3392
3393 while (buf->b_next != NULL
3394 && buf->b_ml.ml_mfp == NULL)
3395 buf = buf->b_next;
3396 eap->line1 = buf->b_fnum;
3397 buf = lastbuf;
3398 while (buf->b_prev != NULL
3399 && buf->b_ml.ml_mfp == NULL)
3400 buf = buf->b_prev;
3401 eap->line2 = buf->b_fnum;
3402 break;
3403 }
3404 case ADDR_BUFFERS:
3405 eap->line1 = firstbuf->b_fnum;
3406 eap->line2 = lastbuf->b_fnum;
3407 break;
3408 case ADDR_WINDOWS:
3409 case ADDR_TABS:
3410 if (IS_USER_CMDIDX(eap->cmdidx))
3411 {
3412 eap->line1 = 1;
3413 eap->line2 = eap->addr_type == ADDR_WINDOWS
3414 ? LAST_WIN_NR : LAST_TAB_NR;
3415 }
3416 else
3417 {
3418 // there is no Vim command which uses '%' and
3419 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003420 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003421 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003422 }
3423 break;
3424 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003425 case ADDR_UNSIGNED:
3426 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003427 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003428 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003429 case ADDR_ARGUMENTS:
3430 if (ARGCOUNT == 0)
3431 eap->line1 = eap->line2 = 0;
3432 else
3433 {
3434 eap->line1 = 1;
3435 eap->line2 = ARGCOUNT;
3436 }
3437 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003438 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003439#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003440 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003441 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003442 if (eap->line2 == 0)
3443 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003444#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003445 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003446 case ADDR_NONE:
3447 // Will give an error later if a range is found.
3448 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003449 }
3450 ++eap->addr_count;
3451 }
3452 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3453 {
3454 pos_T *fp;
3455
3456 // '*' - visual area
3457 if (eap->addr_type != ADDR_LINES)
3458 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003459 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003460 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003461 }
3462
3463 ++eap->cmd;
3464 if (!eap->skip)
3465 {
3466 fp = getmark('<', FALSE);
3467 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003468 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003469 eap->line1 = fp->lnum;
3470 fp = getmark('>', FALSE);
3471 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003472 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003473 eap->line2 = fp->lnum;
3474 ++eap->addr_count;
3475 }
3476 }
3477 }
3478 else
3479 eap->line2 = lnum;
3480 eap->addr_count++;
3481
3482 if (*eap->cmd == ';')
3483 {
3484 if (!eap->skip)
3485 {
3486 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003487
Bram Moolenaar0e717042020-04-27 19:29:01 +02003488 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003489 // accept zero as address, so 0;/PATTERN/ works correctly
3490 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003491 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003492 if (eap->line2 > 0)
3493 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003494 else
3495 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003496 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003497 }
3498 }
3499 else if (*eap->cmd != ',')
3500 break;
3501 ++eap->cmd;
3502 }
3503
3504 // One address given: set start and end lines.
3505 if (eap->addr_count == 1)
3506 {
3507 eap->line1 = eap->line2;
3508 // ... but only implicit: really no address given
3509 if (lnum == MAXLNUM)
3510 eap->addr_count = 0;
3511 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003512 ret = OK;
3513
3514theend:
3515 if (need_check_cursor)
3516 check_cursor();
3517 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003518}
3519
3520/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003521 * Append "cmd" to the error message in IObuff.
3522 * Takes care of limiting the length and handling 0xa0, which would be
3523 * invisible otherwise.
3524 */
3525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003526append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003527{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003528 size_t len = STRLEN(IObuff);
3529 char_u *s = cmd;
3530 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003531
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003532 if (len > IOSIZE - 100)
3533 {
3534 // Not enough space, truncate and put in "...".
3535 d = IObuff + IOSIZE - 100;
3536 d -= mb_head_off(IObuff, d);
3537 STRCPY(d, "...");
3538 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003539 STRCAT(IObuff, ": ");
3540 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003541 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003542 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003543 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003544 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003545 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003546 STRCPY(d, "<a0>");
3547 d += 4;
3548 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003549 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3550 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003551 else
3552 MB_COPY_CHAR(s, d);
3553 }
3554 *d = NUL;
3555}
3556
Dominique Pelle748b3082022-01-08 12:41:16 +00003557#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003558/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003559 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3560 * the name. Otherwise just return "start".
3561 */
3562 char_u *
3563skip_option_env_lead(char_u *start)
3564{
3565 char_u *name = start;
3566
3567 if (*start == '&')
3568 {
3569 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3570 name += 3;
3571 else
3572 name += 1;
3573 }
3574 else if (*start == '$')
3575 name += 1;
3576 return name;
3577}
Dominique Pelle748b3082022-01-08 12:41:16 +00003578#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003579
3580/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003581 * Return TRUE and set "*idx" if "p" points to a one letter command.
3582 * If not in Vim9 script:
3583 * - The 'k' command can directly be followed by any character.
3584 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3585 * but :sre[wind] is another command, as are :scr[iptnames],
3586 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3587 */
3588 static int
3589one_letter_cmd(char_u *p, cmdidx_T *idx)
3590{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003591 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003592 return FALSE;
3593 if (*p == 'k')
3594 {
3595 *idx = CMD_k;
3596 return TRUE;
3597 }
3598 if (p[0] == 's'
3599 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3600 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3601 || p[1] == 'g'
3602 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3603 || p[1] == 'I'
3604 || (p[1] == 'r' && p[2] != 'e')))
3605 {
3606 *idx = CMD_substitute;
3607 return TRUE;
3608 }
3609 return FALSE;
3610}
3611
Dominique Pellee764d1b2023-03-12 21:20:59 +00003612#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003613/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003614 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3615 * call.
3616 */
3617 int
3618number_method(char_u *cmd)
3619{
3620 char_u *p = skipdigits(cmd);
3621
3622 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3623}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003624#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003625
3626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003628 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003629 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003630 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 *
3632 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3633 * assignment without "let". Sets eap->cmdidx to the command while returning
3634 * "eap->cmd".
3635 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 * Returns NULL for an ambiguous user command.
3637 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 char_u *
3639find_ex_command(
3640 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003641 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003642 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003643 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
3645 int len;
3646 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003647 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003648#ifndef FEAT_EVAL
3649 int vim9 = FALSE;
3650#else
3651 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 /*
3654 * Recognize a Vim9 script function/method call and assignment:
3655 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3656 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003657 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003658 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003660 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003662 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003663 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3664 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003666 int oplen;
3667 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003668 char_u *swp;
3669
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003670 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003671 || (eap->cmd[0] == '$'
3672 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003673 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003674 && (valid_yank_reg(eap->cmd[1], FALSE)
3675 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003676 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003677 if (*eap->cmd == '&')
3678 {
3679 p = eap->cmd + 1;
3680 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3681 p += 2;
3682 p = to_name_end(p, FALSE);
3683 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003684 else if (*eap->cmd == '$')
3685 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003686 else
3687 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003688 if (ends_excmd(*skipwhite(p)))
3689 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003690 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3691 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003692 eap->cmdidx = CMD_eval;
3693 return eap->cmd;
3694 }
3695 // "&option" can be followed by "->" or "=", check below
3696 }
3697
3698 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003700 if (
3701 // "(..." is an expression.
3702 // "funcname(" is always a function call.
3703 *p == '('
3704 || (p == eap->cmd
3705 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003706 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003707 *eap->cmd == '{'
3708 // "'string'->func()" is an expression.
3709 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003710 // '"string"->func()' is an expression.
3711 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003712 // '$"string"->func()' is an expression.
3713 // "$'string'->func()" is an expression.
3714 || (eap->cmd[0] == '$'
3715 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3716 // '0z1234->func()' is an expression.
3717 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003718 // "g:varname" is an expression.
3719 || eap->cmd[1] == ':'
3720 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003721 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003722 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003723 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003724 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3725 {
3726 // "{" by itself is the start of a block.
3727 eap->cmdidx = CMD_block;
3728 return eap->cmd + 1;
3729 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003730 eap->cmdidx = CMD_eval;
3731 return eap->cmd;
3732 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003733
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003734 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003735 // "varname[]" is an expression.
3736 *p == '['
3737 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003738 || (*p == '.'
3739 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3740 // g:[key] is an expression
3741 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003742 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003743 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003744
3745 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003746 // Skip over the whole thing, it can be:
3747 // name.member = val
3748 // name[a : b] = val
3749 // name[idx] = val
3750 // name[idx].member = val
3751 // etc.
3752 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003753 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003754 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003755 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003756 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003757 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003758 || (after[0] == '.' && after[1] == '.'
3759 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003760 eap->cmdidx = CMD_var;
3761 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003762 --emsg_silent;
3763 return eap->cmd;
3764 }
3765
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003766 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3767 // an assignment.
3768 // If there is no line break inside the "[...]" then "p" is
3769 // advanced to after the "]" by to_name_const_end(): check if a "="
3770 // follows.
3771 // If "[...]" has a line break "p" still points at the "[" and it
3772 // can't be an assignment.
3773 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003774 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003775 char_u *eq;
3776
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003777 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003778 if (p == eap->cmd && *p == '[')
3779 {
3780 int count = 0;
3781 int semicolon = FALSE;
3782
3783 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3784 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003785 eq = p;
3786 if (eq != NULL)
3787 {
3788 eq = skipwhite(eq);
3789 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3790 ++eq;
3791 }
3792 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003793 {
3794 eap->cmdidx = CMD_eval;
3795 return eap->cmd;
3796 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003797 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003798 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003799 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003800 return eap->cmd;
3801 }
3802 }
3803
3804 // Recognize an assignment if we recognize the variable name:
3805 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003806 // "@r = expr"
3807 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003808 // "var = expr" where "var" is a variable name or we are skipping
3809 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003810 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003811 oplen = assignment_len(skipwhite(p), &heredoc);
3812 if (oplen > 0)
3813 {
3814 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3815 || *eap->cmd == '&'
3816 || *eap->cmd == '$'
3817 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003818 || (eap->skip && IS_WHITE_OR_NUL(
3819 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003820 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003821 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003822 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003823 return eap->cmd;
3824 }
3825 }
3826
Bram Moolenaar9510d222022-09-11 15:14:05 +01003827 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003828 // "w:varname: number = 123".
3829 if (eap->cmd[1] == ':' && *p == ':')
3830 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003831 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003832 return eap->cmd;
3833 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003834 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003835
Bram Moolenaar31d99482022-05-26 22:24:43 +01003836 // 1234->func() is a method call
3837 if (number_method(eap->cmd))
3838 {
3839 eap->cmdidx = CMD_eval;
3840 return eap->cmd;
3841 }
3842
Bram Moolenaar7b829262021-10-13 15:04:34 +01003843 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3844 // an expression. A global/substitute/list command needs to use a
3845 // longer name.
3846 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3847 {
3848 eap->cmdidx = CMD_eval;
3849 return eap->cmd;
3850 }
3851
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003852 // If it is an ID it might be a variable with an operator on the next
3853 // line, if the variable exists it can't be an Ex command.
3854 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003855 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003856 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3857 {
3858 eap->cmdidx = CMD_eval;
3859 return eap->cmd;
3860 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003861
3862 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003863 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3864 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003865 {
3866 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3867 return eap->cmd + 2;
3868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 }
3870#endif
3871
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 /*
3873 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 */
3875 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003876 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 ++p;
3879 }
3880 else
3881 {
3882 while (ASCII_ISALPHA(*p))
3883 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003884 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003885 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003887 while (ASCII_ISALNUM(*p))
3888 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 }
3890 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3891 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003892 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 ++p;
3894 while (ASCII_ISALPHA(*p))
3895 ++p;
3896 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003897
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003898 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003899 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 ++p;
3901 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003902 // The "d" command can directly be followed by 'l' or 'p' flag, when
3903 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003904 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003905 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003906 // Check for ":dl", ":dell", etc. to ":deletel": that's
3907 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003908 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003909 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003910 break;
3911 if (i == len - 1)
3912 {
3913 --len;
3914 if (p[-1] == 'l')
3915 eap->flags |= EXFLAG_LIST;
3916 else
3917 eap->flags |= EXFLAG_PRINT;
3918 }
3919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003921 if (ASCII_ISLOWER(eap->cmd[0]))
3922 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003923 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003924 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003925
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003926 if (command_count != (int)CMD_SIZE)
3927 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003928 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003929 getout(1);
3930 }
3931
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003932 // Use a precomputed index for fast look-up in cmdnames[]
3933 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003934 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3935 if (ASCII_ISLOWER(c2))
3936 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3937 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003938 else if (ASCII_ISUPPER(eap->cmd[0]))
3939 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003941 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
3943 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3944 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3945 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3946 (size_t)len) == 0)
3947 {
3948#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003949 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 *full = TRUE;
3951#endif
3952 break;
3953 }
3954
Bram Moolenaar204852a2022-03-05 12:56:44 +00003955 // :Print and :mode are not supported in Vim9 script.
3956 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003957 if (vim9 && eap->cmdidx != CMD_SIZE)
3958 {
3959 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3960 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003961 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003962 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003963 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003964 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003965 eap->cmdidx = CMD_SIZE;
3966 }
3967 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003968
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003969 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003970 // 'cpoptions'.
3971 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3972 p = eap->cmd;
3973
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003974 // Look for a user defined command as a last resort. Let ":Print" be
3975 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003976 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3977 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003979 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 while (ASCII_ISALNUM(*p))
3981 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003982 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003984 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 eap->cmdidx = CMD_SIZE;
3986 }
3987
Bram Moolenaar204852a2022-03-05 12:56:44 +00003988 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3989 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003990 eap->cmdidx = CMD_finally;
3991
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003992#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003993 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003994 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02003995 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003996 && (eap->cmdidx < 0 ||
3997 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003998 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003999 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4000
4001 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004002 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004003 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004004 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004005#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004006
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 return p;
4008}
4009
4010#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004011typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004012{
4013 char *name;
4014 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004015 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004016} cmdmod_info_T;
4017
4018static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004019 {"aboveleft", 3, FALSE},
4020 {"belowright", 3, FALSE},
4021 {"botright", 2, FALSE},
4022 {"browse", 3, FALSE},
4023 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004024 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004025 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004026 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004027 {"keepalt", 5, FALSE},
4028 {"keepjumps", 5, FALSE},
4029 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004030 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004031 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004032 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004033 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004034 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004035 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004036 {"rightbelow", 6, FALSE},
4037 {"sandbox", 3, FALSE},
4038 {"silent", 3, FALSE},
4039 {"tab", 3, TRUE},
4040 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004041 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004042 {"verbose", 4, TRUE},
4043 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004044 {"vim9cmd", 4, FALSE}
4045}; // cmdmod_info_tab
4046
4047// compare two cmdmod_info_T structs by case sensitive name with length
4048 static int
4049cmp_cmdmod_info(const void *a, const void *b)
4050{
4051 cmdmod_info_T *cm1 = (cmdmod_info_T *)a;
4052 cmdmod_info_T *cm2 = (cmdmod_info_T *)b;
4053
4054 return STRNCMP(cm1->name, cm2->name, cm2->minlen);
4055}
Bram Moolenaared53fb92007-11-24 20:50:24 +00004056
4057/*
4058 * Return length of a command modifier (including optional count).
4059 * Return zero when it's not a modifier.
4060 */
4061 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004062modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004063{
Bram Moolenaared53fb92007-11-24 20:50:24 +00004064 char_u *p = cmd;
John Marriotted908f72024-04-18 22:46:56 +02004065 cmdmod_info_T target;
4066 cmdmod_info_T *entry;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004067
4068 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004069 p = skipwhite(skipdigits(cmd + 1));
John Marriotted908f72024-04-18 22:46:56 +02004070
4071 // only lowercase characters can match
4072 if (!ASCII_ISLOWER(*p))
4073 return 0;
4074
4075 target.name = (char *)p;
4076 target.minlen = 0; // not used, see cmp_cmdmod_info()
4077 target.has_count = FALSE;
4078
4079 entry = (cmdmod_info_T *)bsearch(&target, &cmdmod_info_tab, ARRAY_LENGTH(cmdmod_info_tab), sizeof(cmdmod_info_tab[0]), cmp_cmdmod_info);
4080 if (entry != NULL)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004081 {
John Marriotted908f72024-04-18 22:46:56 +02004082 int i;
4083
4084 for (i = entry->minlen; p[i] != NUL; ++i)
4085 {
4086 if (p[i] != entry->name[i])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004087 break;
John Marriotted908f72024-04-18 22:46:56 +02004088 }
4089
4090 if (!ASCII_ISALPHA(p[i]) && i >= entry->minlen && (p == cmd || entry->has_count))
4091 return i + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004092 }
John Marriotted908f72024-04-18 22:46:56 +02004093
Bram Moolenaared53fb92007-11-24 20:50:24 +00004094 return 0;
4095}
4096
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097/*
4098 * Return > 0 if an Ex command "name" exists.
4099 * Return 2 if there is an exact match.
4100 * Return 3 if there is an ambiguous match.
4101 */
4102 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004103cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
4105 exarg_T ea;
4106 int full = FALSE;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004107 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
John Marriotted908f72024-04-18 22:46:56 +02004109 // only lowercase characters can match
4110 if (ASCII_ISLOWER(*name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 {
John Marriotted908f72024-04-18 22:46:56 +02004112 cmdmod_info_T target;
4113 cmdmod_info_T *entry;
4114
4115 target.name = (char *)name;
4116 target.minlen = 0; // not used, see cmp_cmdmod_info()
4117 target.has_count = FALSE;
4118
4119 // Check command modifiers.
4120 entry = (cmdmod_info_T *)bsearch(&target, &cmdmod_info_tab, ARRAY_LENGTH(cmdmod_info_tab), sizeof(cmdmod_info_tab[0]), cmp_cmdmod_info);
4121 if (entry != NULL)
4122 {
4123 int i;
4124
4125 for (i = entry->minlen; name[i] != NUL; ++i)
4126 {
4127 if (name[i] != entry->name[i])
4128 break;
4129 }
4130
4131 if (name[i] == NUL && i >= entry->minlen)
4132 return (entry->name[i] == NUL ? 2 : 1);
4133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004136 // Check built-in commands and user defined commands.
4137 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004138 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004140 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004142 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004144 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4145 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004146 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004147 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4149}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004150
4151/*
4152 * "fullcommand" function
4153 */
4154 void
4155f_fullcommand(typval_T *argvars, typval_T *rettv)
4156{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004157 exarg_T ea;
4158 char_u *name;
4159 char_u *p;
4160 int vim9script = in_vim9script();
4161 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004162
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004163 rettv->v_type = VAR_STRING;
4164 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004165
Bram Moolenaaraa534142022-09-15 21:46:02 +01004166 if (in_vim9script()
4167 && (check_for_string_arg(argvars, 0) == FAIL
4168 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004169 return;
4170
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004171 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004172 if (name == NULL)
4173 return;
4174
Bram Moolenaaraa534142022-09-15 21:46:02 +01004175 if (argvars[1].v_type != VAR_UNKNOWN)
4176 {
4177 vim9script = tv_get_bool(&argvars[1]);
4178 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4179 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4180 }
4181
zeertzjqc024ed92022-01-04 16:22:52 +00004182 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004183 name++;
4184 name = skip_range(name, TRUE, NULL);
4185
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004186 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4187 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004188 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004189 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004190 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004191 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004192 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004193 goto theend;
4194
4195 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004196 {
4197 int res;
4198
4199 ++emsg_silent;
4200 res = not_in_vim9(&ea);
4201 --emsg_silent;
4202
4203 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004204 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004205 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004206
4207 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004208 ? get_user_command_name(ea.useridx, ea.cmdidx)
4209 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004210theend:
4211 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004212}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213#endif
4214
Bram Moolenaard0190392019-08-23 21:17:35 +02004215 cmdidx_T
4216excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217{
Bram Moolenaard0190392019-08-23 21:17:35 +02004218 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004220 if (!one_letter_cmd(cmd, &idx))
4221 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4222 idx = (cmdidx_T)((int)idx + 1))
4223 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4224 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
Bram Moolenaard0190392019-08-23 21:17:35 +02004226 return idx;
4227}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
Bram Moolenaard0190392019-08-23 21:17:35 +02004229 long
4230excmd_get_argt(cmdidx_T idx)
4231{
4232 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233}
4234
4235/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004236 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 *
4238 * Backslashed delimiters after / or ? will be skipped, and commands will
4239 * not be expanded between /'s and ?'s or after "'".
4240 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004241 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 * Returns the "cmd" pointer advanced to beyond the range.
4243 */
4244 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004245skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004246 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004247 int skip_star, // skip "*" used for Visual range
4248 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004250 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004251 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004253 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004255 if (*cmd == '\\')
4256 {
4257 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4258 ++cmd;
4259 else
4260 break;
4261 }
4262 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004264 char_u *p = cmd;
4265
4266 // a quote is only valid at the start or after a separator
4267 while (p > cmd_start)
4268 {
4269 --p;
4270 if (!VIM_ISWHITE(*p))
4271 break;
4272 }
4273 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 if (*++cmd == NUL && ctx != NULL)
4276 *ctx = EXPAND_NOTHING;
4277 }
4278 else if (*cmd == '/' || *cmd == '?')
4279 {
4280 delim = *cmd++;
4281 while (*cmd != NUL && *cmd != delim)
4282 if (*cmd++ == '\\' && *cmd != NUL)
4283 ++cmd;
4284 if (*cmd == NUL && ctx != NULL)
4285 *ctx = EXPAND_NOTHING;
4286 }
4287 if (*cmd != NUL)
4288 ++cmd;
4289 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004290
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004291 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004292 while (*cmd == ':')
4293 cmd = skipwhite(cmd + 1);
4294
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004295 // Skip "*" used for Visual range.
4296 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4297 cmd = skipwhite(cmd + 1);
4298
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 return cmd;
4300}
4301
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004302 static void
4303addr_error(cmd_addr_T addr_type)
4304{
4305 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004306 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004307 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004308 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004309}
4310
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004312 * Return the default address for an address type.
4313 */
4314 static linenr_T
4315default_address(exarg_T *eap)
4316{
4317 linenr_T lnum = 0;
4318
4319 switch (eap->addr_type)
4320 {
4321 case ADDR_LINES:
4322 case ADDR_OTHER:
4323 // Default is the cursor line number. Avoid using an invalid
4324 // line number though.
4325 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4326 lnum = curbuf->b_ml.ml_line_count;
4327 else
4328 lnum = curwin->w_cursor.lnum;
4329 break;
4330 case ADDR_WINDOWS:
4331 lnum = CURRENT_WIN_NR;
4332 break;
4333 case ADDR_ARGUMENTS:
4334 lnum = curwin->w_arg_idx + 1;
4335 if (lnum > ARGCOUNT)
4336 lnum = ARGCOUNT;
4337 break;
4338 case ADDR_LOADED_BUFFERS:
4339 case ADDR_BUFFERS:
4340 lnum = curbuf->b_fnum;
4341 break;
4342 case ADDR_TABS:
4343 lnum = CURRENT_TAB_NR;
4344 break;
4345 case ADDR_TABS_RELATIVE:
4346 case ADDR_UNSIGNED:
4347 lnum = 1;
4348 break;
4349 case ADDR_QUICKFIX:
4350#ifdef FEAT_QUICKFIX
4351 lnum = qf_get_cur_idx(eap);
4352#endif
4353 break;
4354 case ADDR_QUICKFIX_VALID:
4355#ifdef FEAT_QUICKFIX
4356 lnum = qf_get_cur_valid_idx(eap);
4357#endif
4358 break;
4359 case ADDR_NONE:
4360 // Will give an error later if a range is found.
4361 break;
4362 }
4363 return lnum;
4364}
4365
4366/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004367 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 *
4369 * Set ptr to the next character after the part that was interpreted.
4370 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004371 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 *
4373 * Return MAXLNUM when no Ex address was found.
4374 */
4375 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004376get_address(
4377 exarg_T *eap UNUSED,
4378 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004379 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004380 int skip, // only skip the address, don't use it
4381 int silent, // no errors or side effects
4382 int to_other_file, // flag: may jump to other file
4383 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
4385 int c;
4386 int i;
4387 long n;
4388 char_u *cmd;
4389 pos_T pos;
4390 pos_T *fp;
4391 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004392 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394 cmd = skipwhite(*ptr);
4395 lnum = MAXLNUM;
4396 do
4397 {
4398 switch (*cmd)
4399 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004400 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004401 ++cmd;
4402 switch (addr_type)
4403 {
4404 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004405 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 lnum = curwin->w_cursor.lnum;
4407 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004408 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004409 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004410 break;
4411 case ADDR_ARGUMENTS:
4412 lnum = curwin->w_arg_idx + 1;
4413 break;
4414 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004415 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004416 lnum = curbuf->b_fnum;
4417 break;
4418 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004419 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004421 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004422 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004423 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004424 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004425 cmd = NULL;
4426 goto error;
4427 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004428 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004429#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004430 lnum = qf_get_cur_idx(eap);
4431#endif
4432 break;
4433 case ADDR_QUICKFIX_VALID:
4434#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004435 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004436#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004437 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004438 }
4439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004441 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 ++cmd;
4443 switch (addr_type)
4444 {
4445 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004446 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 lnum = curbuf->b_ml.ml_line_count;
4448 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004450 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004451 break;
4452 case ADDR_ARGUMENTS:
4453 lnum = ARGCOUNT;
4454 break;
4455 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004456 buf = lastbuf;
4457 while (buf->b_ml.ml_mfp == NULL)
4458 {
4459 if (buf->b_prev == NULL)
4460 break;
4461 buf = buf->b_prev;
4462 }
4463 lnum = buf->b_fnum;
4464 break;
4465 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 lnum = lastbuf->b_fnum;
4467 break;
4468 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004469 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004470 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004471 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004472 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004473 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004474 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004475 cmd = NULL;
4476 goto error;
4477 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004478 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004479#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004480 lnum = qf_get_size(eap);
4481 if (lnum == 0)
4482 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004483#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004484 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004485 case ADDR_QUICKFIX_VALID:
4486#ifdef FEAT_QUICKFIX
4487 lnum = qf_get_valid_size(eap);
4488 if (lnum == 0)
4489 lnum = 1;
4490#endif
4491 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 }
4493 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004495 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004496 if (*++cmd == NUL)
4497 {
4498 cmd = NULL;
4499 goto error;
4500 }
4501 if (addr_type != ADDR_LINES)
4502 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004503 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004504 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 goto error;
4506 }
4507 if (skip)
4508 ++cmd;
4509 else
4510 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004511 // Only accept a mark in another file when it is
4512 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004513 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4514 ++cmd;
4515 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004516 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004517 lnum = curwin->w_cursor.lnum;
4518 else
4519 {
4520 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 {
4522 cmd = NULL;
4523 goto error;
4524 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004525 lnum = fp->lnum;
4526 }
4527 }
4528 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
4530 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004531 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 c = *cmd++;
4533 if (addr_type != ADDR_LINES)
4534 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004535 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004536 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004537 goto error;
4538 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004539 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004540 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004541 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004542 if (*cmd == c)
4543 ++cmd;
4544 }
4545 else
4546 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004547 int flags;
4548
4549 pos = curwin->w_cursor; // save curwin->w_cursor
4550
4551 // When '/' or '?' follows another address, start from
4552 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004553 if (lnum > 0 && lnum != MAXLNUM)
4554 curwin->w_cursor.lnum =
4555 lnum > curbuf->b_ml.ml_line_count
4556 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004557
4558 // Start a forward search at the end of the line (unless
4559 // before the first line).
4560 // Start a backward search at the start of the line.
4561 // This makes sure we never match in the current
4562 // line, and can match anywhere in the
4563 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004564 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004565 curwin->w_cursor.col = MAXCOL;
4566 else
4567 curwin->w_cursor.col = 0;
4568 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004569 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004570 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004571 {
4572 curwin->w_cursor = pos;
4573 cmd = NULL;
4574 goto error;
4575 }
4576 lnum = curwin->w_cursor.lnum;
4577 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004578 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 cmd += searchcmdlen;
4580 }
4581 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004583 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004584 ++cmd;
4585 if (addr_type != ADDR_LINES)
4586 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004587 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004588 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004589 goto error;
4590 }
4591 if (*cmd == '&')
4592 i = RE_SUBST;
4593 else if (*cmd == '?' || *cmd == '/')
4594 i = RE_SEARCH;
4595 else
4596 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004597 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004598 cmd = NULL;
4599 goto error;
4600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004602 if (!skip)
4603 {
4604 /*
4605 * When search follows another address, start from
4606 * there.
4607 */
4608 if (lnum != MAXLNUM)
4609 pos.lnum = lnum;
4610 else
4611 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004613 /*
4614 * Start the search just like for the above
4615 * do_search().
4616 */
4617 if (*cmd != '?')
4618 pos.col = MAXCOL;
4619 else
4620 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004621 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004622 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004623 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004624 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004625 lnum = pos.lnum;
4626 else
4627 {
4628 cmd = NULL;
4629 goto error;
4630 }
4631 }
4632 ++cmd;
4633 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
4635 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004636 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004637 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
4639
4640 for (;;)
4641 {
4642 cmd = skipwhite(cmd);
4643 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4644 break;
4645
4646 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004647 {
4648 switch (addr_type)
4649 {
4650 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004651 case ADDR_OTHER:
4652 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004653 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004654 break;
4655 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004656 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004657 break;
4658 case ADDR_ARGUMENTS:
4659 lnum = curwin->w_arg_idx + 1;
4660 break;
4661 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004662 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004663 lnum = curbuf->b_fnum;
4664 break;
4665 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004666 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004667 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004668 case ADDR_TABS_RELATIVE:
4669 lnum = 1;
4670 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004671 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004672#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004673 lnum = qf_get_cur_idx(eap);
4674#endif
4675 break;
4676 case ADDR_QUICKFIX_VALID:
4677#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004678 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004679#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004680 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004681 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004682 case ADDR_UNSIGNED:
4683 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004684 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004685 }
4686 }
4687
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004689 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 else
4691 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004692 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 n = 1;
4694 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004695 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004696 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004698 if (n == MAXLNUM)
4699 {
4700 emsg(_(e_line_number_out_of_range));
4701 goto error;
4702 }
4703 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004704
4705 if (addr_type == ADDR_TABS_RELATIVE)
4706 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004707 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004708 cmd = NULL;
4709 goto error;
4710 }
4711 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004712 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004713 lnum = compute_buffer_local_count(
4714 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004716 {
4717#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004718 // Relative line addressing: need to adjust for lines in a
4719 // closed fold after the first address.
4720 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4721 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004722 (void)hasFolding(lnum, NULL, &lnum);
4723#endif
4724 if (i == '-')
4725 lnum -= n;
4726 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004727 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004728 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004729 {
4730 emsg(_(e_line_number_out_of_range));
4731 goto error;
4732 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004733 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004734 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
4737 } while (*cmd == '/' || *cmd == '?');
4738
4739error:
4740 *ptr = cmd;
4741 return lnum;
4742}
4743
4744/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004745 * Set eap->line1 and eap->line2 to the whole range.
4746 * Used for commands with the EX_DFLALL flag and no range given.
4747 */
4748 static void
4749address_default_all(exarg_T *eap)
4750{
4751 eap->line1 = 1;
4752 switch (eap->addr_type)
4753 {
4754 case ADDR_LINES:
4755 case ADDR_OTHER:
4756 eap->line2 = curbuf->b_ml.ml_line_count;
4757 break;
4758 case ADDR_LOADED_BUFFERS:
4759 {
4760 buf_T *buf = firstbuf;
4761
4762 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4763 buf = buf->b_next;
4764 eap->line1 = buf->b_fnum;
4765 buf = lastbuf;
4766 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4767 buf = buf->b_prev;
4768 eap->line2 = buf->b_fnum;
4769 }
4770 break;
4771 case ADDR_BUFFERS:
4772 eap->line1 = firstbuf->b_fnum;
4773 eap->line2 = lastbuf->b_fnum;
4774 break;
4775 case ADDR_WINDOWS:
4776 eap->line2 = LAST_WIN_NR;
4777 break;
4778 case ADDR_TABS:
4779 eap->line2 = LAST_TAB_NR;
4780 break;
4781 case ADDR_TABS_RELATIVE:
4782 eap->line2 = 1;
4783 break;
4784 case ADDR_ARGUMENTS:
4785 if (ARGCOUNT == 0)
4786 eap->line1 = eap->line2 = 0;
4787 else
4788 eap->line2 = ARGCOUNT;
4789 break;
4790 case ADDR_QUICKFIX_VALID:
4791#ifdef FEAT_QUICKFIX
4792 eap->line2 = qf_get_valid_size(eap);
4793 if (eap->line2 == 0)
4794 eap->line2 = 1;
4795#endif
4796 break;
4797 case ADDR_NONE:
4798 case ADDR_UNSIGNED:
4799 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004800 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004801 break;
4802 }
4803}
4804
4805
4806/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004807 * Get flags from an Ex command argument.
4808 */
4809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004810get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004811{
4812 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4813 {
4814 if (*eap->arg == 'l')
4815 eap->flags |= EXFLAG_LIST;
4816 else if (*eap->arg == 'p')
4817 eap->flags |= EXFLAG_PRINT;
4818 else
4819 eap->flags |= EXFLAG_NR;
4820 eap->arg = skipwhite(eap->arg + 1);
4821 }
4822}
4823
4824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 * Function called for command which is Not Implemented. NI!
4826 */
4827 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004828ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829{
4830 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004831 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004832 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833}
4834
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004835#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836/*
4837 * Function called for script command which is Not Implemented. NI!
4838 * Skips over ":perl <<EOF" constructs.
4839 */
4840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004841ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
4843 if (!eap->skip)
4844 ex_ni(eap);
4845 else
4846 vim_free(script_get(eap, eap->arg));
4847}
4848#endif
4849
4850/*
4851 * Check range in Ex command for validity.
4852 * Return NULL when valid, error message when invalid.
4853 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004854 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004855invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004857 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004858
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 if ( eap->line1 < 0
4860 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004861 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004862 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004863
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004864 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004865 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004866 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004867 {
4868 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004869 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004870#ifdef FEAT_DIFF
4871 + (eap->cmdidx == CMD_diffget)
4872#endif
4873 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004874 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004875 break;
4876 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004877 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004878 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004879 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004880 break;
4881 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004882 // Only a boundary check, not whether the buffers actually
4883 // exist.
4884 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004885 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004886 break;
4887 case ADDR_LOADED_BUFFERS:
4888 buf = firstbuf;
4889 while (buf->b_ml.ml_mfp == NULL)
4890 {
4891 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004892 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004893 buf = buf->b_next;
4894 }
4895 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004896 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004897 buf = lastbuf;
4898 while (buf->b_ml.ml_mfp == NULL)
4899 {
4900 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004901 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004902 buf = buf->b_prev;
4903 }
4904 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004905 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004906 break;
4907 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004908 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004909 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004910 break;
4911 case ADDR_TABS:
4912 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004913 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004914 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004915 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004916 case ADDR_OTHER:
4917 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004918 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004919 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004920#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004921 // No error for value that is too big, will use the last entry.
4922 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004923 {
4924 if (eap->addr_count == 0)
4925 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004926 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004927 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004928#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004929 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004930 case ADDR_QUICKFIX_VALID:
4931#ifdef FEAT_QUICKFIX
4932 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4933 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004934 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004935#endif
4936 break;
4937 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004938 case ADDR_NONE:
4939 // Will give an error elsewhere.
4940 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004941 }
4942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 return NULL;
4944}
4945
4946/*
4947 * Correct the range for zero line number, if required.
4948 */
4949 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004950correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004952 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
4954 if (eap->line1 == 0)
4955 eap->line1 = 1;
4956 if (eap->line2 == 0)
4957 eap->line2 = 1;
4958 }
4959}
4960
Bram Moolenaar748bf032005-02-02 23:04:36 +00004961#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004962/*
4963 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4964 * pattern. Otherwise return eap->arg.
4965 */
4966 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004967skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004968{
4969 char_u *p = eap->arg;
4970
Bram Moolenaara37420f2006-02-04 22:37:47 +00004971 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4972 || eap->cmdidx == CMD_vimgrepadd
4973 || eap->cmdidx == CMD_lvimgrepadd
4974 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004975 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004976 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004977 if (p == NULL)
4978 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004979 }
4980 return p;
4981}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004982
4983/*
4984 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4985 * in the command line, so that things like % get expanded.
4986 */
4987 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004988replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004989{
4990 char_u *new_cmdline;
4991 char_u *program;
4992 char_u *pos;
4993 char_u *ptr;
4994 int len;
4995 int i;
4996
4997 /*
4998 * Don't do it when ":vimgrep" is used for ":grep".
4999 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00005000 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
5001 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
5002 || eap->cmdidx == CMD_grepadd
5003 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005004 && !grep_internal(eap->cmdidx))
5005 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00005006 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
5007 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005008 {
5009 if (*curbuf->b_p_gp == NUL)
5010 program = p_gp;
5011 else
5012 program = curbuf->b_p_gp;
5013 }
5014 else
5015 {
5016 if (*curbuf->b_p_mp == NUL)
5017 program = p_mp;
5018 else
5019 program = curbuf->b_p_mp;
5020 }
5021
5022 p = skipwhite(p);
5023
5024 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5025 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005026 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005027 i = 1;
5028 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
5029 ++i;
5030 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005031 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005032 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005033 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005034 ptr = new_cmdline;
5035 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5036 {
5037 i = (int)(pos - program);
5038 STRNCPY(ptr, program, i);
5039 STRCPY(ptr += i, p);
5040 ptr += len;
5041 program = pos + 2;
5042 }
5043 STRCPY(ptr, program);
5044 }
5045 else
5046 {
John Marriotted908f72024-04-18 22:46:56 +02005047 size_t program_len = STRLEN(program);
5048
5049 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005050 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005051 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005052 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005053 STRCPY(new_cmdline + program_len, " ");
5054 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005055 }
5056 msg_make(p);
5057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005058 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005059 vim_free(*cmdlinep);
5060 *cmdlinep = new_cmdline;
5061 p = new_cmdline;
5062 }
5063 return p;
5064}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005065#endif
5066
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067/*
5068 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005069 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 * Return FAIL for failure, OK otherwise.
5071 */
5072 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005073expand_filename(
5074 exarg_T *eap,
5075 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005076 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005078 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005080 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 char_u *p;
5082 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005083 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
Bram Moolenaar748bf032005-02-02 23:04:36 +00005085#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005086 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005087 p = skip_grep_pat(eap);
5088#else
5089 p = eap->arg;
5090#endif
5091
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 /*
5093 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5094 * the file name contains a wildcard it should not cause expanding.
5095 * (it will be expanded anyway if there is a wildcard before replacing).
5096 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005097 has_wildcards = mch_has_wildcard(p);
5098 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005100#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005101 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005102 if (p[0] == '`' && p[1] == '=')
5103 {
5104 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005105 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005106 if (*p == '`')
5107 ++p;
5108 continue;
5109 }
5110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 /*
5112 * Quick check if this cannot be the start of a special string.
5113 * Also removes backslash before '%', '#' and '<'.
5114 */
5115 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5116 {
5117 ++p;
5118 continue;
5119 }
5120
5121 /*
5122 * Try to find a match at this position.
5123 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005124 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005125 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005126 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005128 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
5130 p += srclen;
5131 continue;
5132 }
5133
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005134 // Wildcards won't be expanded below, the replacement is taken
5135 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005136 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5137 {
5138 char_u *l = repl;
5139
5140 repl = expand_env_save(repl);
5141 vim_free(l);
5142 }
5143
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005144 // Need to escape white space et al. with a backslash.
5145 // Don't do this for:
5146 // - replacement that already has been escaped: "##"
5147 // - shell commands (may have to use quotes instead).
5148 // - non-unix systems when there is a single argument (spaces don't
5149 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005151 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 && eap->cmdidx != CMD_grep
5154 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005155 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005156 && eap->cmdidx != CMD_lgrep
5157 && eap->cmdidx != CMD_lgrepadd
5158 && eap->cmdidx != CMD_lmake
5159 && eap->cmdidx != CMD_make
5160 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005162 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163#endif
5164 )
5165 {
5166 char_u *l;
5167#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005168 // Don't escape a backslash here, because rem_backslash() doesn't
5169 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 static char_u *nobslash = (char_u *)" \t\"|";
5171# define ESCAPE_CHARS nobslash
5172#else
5173# define ESCAPE_CHARS escape_chars
5174#endif
5175
5176 for (l = repl; *l; ++l)
5177 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5178 {
5179 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5180 if (l != NULL)
5181 {
5182 vim_free(repl);
5183 repl = l;
5184 }
5185 break;
5186 }
5187 }
5188
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005189 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005190 if ((eap->usefilter || eap->cmdidx == CMD_bang
5191 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005192 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
5194 char_u *l;
5195
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005196 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (l != NULL)
5198 {
5199 vim_free(repl);
5200 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202 }
5203
Mike Williams51024bb2024-05-30 07:46:30 +02005204 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 vim_free(repl);
5206 if (p == NULL)
5207 return FAIL;
5208 }
5209
5210 /*
5211 * One file argument: Expand wildcards.
5212 * Don't do this with ":r !command" or ":w !command".
5213 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005214 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
5216 /*
5217 * May do this twice:
5218 * 1. Replace environment variables.
5219 * 2. Replace any other wildcards, remove backslashes.
5220 */
5221 for (n = 1; n <= 2; ++n)
5222 {
5223 if (n == 2)
5224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 /*
5226 * Halve the number of backslashes (this is Vi compatible).
5227 * For Unix and OS/2, when wildcards are expanded, this is
5228 * done by ExpandOne() below.
5229 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005230#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 if (!has_wildcards)
5232#endif
5233 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
5235
5236 if (has_wildcards)
5237 {
5238 if (n == 1)
5239 {
5240 /*
5241 * First loop: May expand environment variables. This
5242 * can be done much faster with expand_env() than with
5243 * something else (e.g., calling a shell).
5244 * After expanding environment variables, check again
5245 * if there are still wildcards present.
5246 */
5247 if (vim_strchr(eap->arg, '$') != NULL
5248 || vim_strchr(eap->arg, '~') != NULL)
5249 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005250 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005251 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 has_wildcards = mch_has_wildcard(NameBuff);
5253 p = NameBuff;
5254 }
5255 else
5256 p = NULL;
5257 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005258 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
5260 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005261 int options = WILD_LIST_NOTFOUND
5262 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
5264 ExpandInit(&xpc);
5265 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005266 if (p_wic)
5267 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005269 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 if (p == NULL)
5271 return FAIL;
5272 }
5273 if (p != NULL)
5274 {
John Marriotted908f72024-04-18 22:46:56 +02005275 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005277 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 vim_free(p);
5279 }
5280 }
5281 }
5282 }
5283 return OK;
5284}
5285
5286/*
5287 * Replace part of the command line, keeping eap->cmd, eap->arg and
5288 * eap->nextcmd correct.
5289 * "src" points to the part that is to be replaced, of length "srclen".
5290 * "repl" is the replacement string.
5291 * Returns a pointer to the character after the replaced string.
5292 * Returns NULL for failure.
5293 */
5294 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005295repl_cmdline(
5296 exarg_T *eap,
5297 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005298 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005299 char_u *repl,
5300 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301{
John Marriotted908f72024-04-18 22:46:56 +02005302 size_t repllen;
5303 size_t taillen;
5304 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005306 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307
5308 /*
5309 * The new command line is build in new_cmdline[].
5310 * First allocate it.
5311 * Careful: a "+cmd" argument may have been NUL terminated.
5312 */
John Marriotted908f72024-04-18 22:46:56 +02005313 repllen = STRLEN(repl);
5314 taillen = STRLEN(src + srclen);
5315 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005316 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005317 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005318 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005319 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320
5321 /*
5322 * Copy the stuff before the expanded part.
5323 * Copy the expanded stuff.
5324 * Copy what came after the expanded part.
5325 * Copy the next commands, if there are any.
5326 */
John Marriotted908f72024-04-18 22:46:56 +02005327 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5328 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005329
John Marriotted908f72024-04-18 22:46:56 +02005330 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5331 new_cmdlinelen += repllen; // remember the end of the string
5332 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5333 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005335 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
John Marriotted908f72024-04-18 22:46:56 +02005337 new_cmdlinelen += taillen + 1;
5338 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5339 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
5341 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5342 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5343 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5344 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5345 vim_free(*cmdlinep);
5346 *cmdlinep = new_cmdline;
5347
5348 return src;
5349}
5350
5351/*
5352 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005353 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 */
5355 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005356separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357{
5358 char_u *p;
5359
Bram Moolenaar86b68352004-12-27 21:59:20 +00005360#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005361 p = skip_grep_pat(eap);
5362#else
5363 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005364#endif
5365
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005366 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
5368 if (*p == Ctrl_V)
5369 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005370 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005371 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005373 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005374 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005375 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 break;
5377 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005378
5379#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005380 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005381 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005382 {
5383 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005384 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005385 if (*p == NUL) // stop at NUL after CTRL-V
5386 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005387 }
5388#endif
5389
Bram Moolenaareda29c92022-10-02 12:59:00 +01005390 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005391 // :@" and :*" do not start a comment!
5392 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005393 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005394 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005395 && !(eap->argt & EX_NOTRLCOM)
5396 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5397 || p != eap->arg)
5398 && (eap->cmdidx != CMD_redir
5399 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005400 || (*p == '#'
5401 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005402 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005403 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005404 || (*p == '|'
5405 && eap->cmdidx != CMD_append
5406 && eap->cmdidx != CMD_change
5407 && eap->cmdidx != CMD_insert)
5408 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {
5410 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005411 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 * AND 'b' is present in 'cpoptions'.
5413 */
5414 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005415 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005417 if (!keep_backslash)
5418 {
5419 STRMOVE(p - 1, p); // remove the '\'
5420 --p;
5421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
5423 else
5424 {
5425 eap->nextcmd = check_nextcmd(p);
5426 *p = NUL;
5427 break;
5428 }
5429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005431
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005432 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 del_trailing_spaces(eap->arg);
5434}
5435
5436/*
5437 * get + command from ex argument
5438 */
5439 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005440getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 char_u *arg = *argp;
5443 char_u *command = NULL;
5444
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005445 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 {
5447 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005448 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 command = dollar_command;
5450 else
5451 {
5452 command = arg;
5453 arg = skip_cmd_arg(command, TRUE);
5454 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005455 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 }
5457
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005458 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 *argp = arg;
5460 }
5461 return command;
5462}
5463
5464/*
5465 * Find end of "+command" argument. Skip over "\ " and "\\".
5466 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005467 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005468skip_cmd_arg(
5469 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005470 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471{
5472 while (*p && !vim_isspace(*p))
5473 {
5474 if (*p == '\\' && p[1] != NUL)
5475 {
5476 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005477 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 else
5479 ++p;
5480 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005481 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
5483 return p;
5484}
5485
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005486 int
5487get_bad_opt(char_u *p, exarg_T *eap)
5488{
5489 if (STRICMP(p, "keep") == 0)
5490 eap->bad_char = BAD_KEEP;
5491 else if (STRICMP(p, "drop") == 0)
5492 eap->bad_char = BAD_DROP;
5493 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5494 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005495 else
5496 return FAIL;
5497 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005498}
5499
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005501 * Function given to ExpandGeneric() to obtain the list of bad= names.
5502 */
5503 static char_u *
5504get_bad_name(expand_T *xp UNUSED, int idx)
5505{
5506 // Note: Keep this in sync with getargopt.
5507 static char *(p_bad_values[]) =
5508 {
5509 "?",
5510 "keep",
5511 "drop",
5512 };
5513
John Marriotted908f72024-04-18 22:46:56 +02005514 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5515 return NULL;
5516
5517 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005518}
5519
5520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 * Get "++opt=arg" argument.
5522 * Return FAIL or OK.
5523 */
5524 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005525getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526{
5527 char_u *arg = eap->arg + 2;
5528 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005529 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005532 // Note: Keep this in sync with get_argopt_name.
5533
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005534 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5536 {
5537 if (*arg == 'n')
5538 {
5539 arg += 2;
5540 eap->force_bin = FORCE_NOBIN;
5541 }
5542 else
5543 eap->force_bin = FORCE_BIN;
5544 if (!checkforcmd(&arg, "binary", 3))
5545 return FAIL;
5546 eap->arg = skipwhite(arg);
5547 return OK;
5548 }
5549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005550 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005551 if (STRNCMP(arg, "edit", 4) == 0)
5552 {
5553 eap->read_edit = TRUE;
5554 eap->arg = skipwhite(arg + 4);
5555 return OK;
5556 }
5557
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 if (STRNCMP(arg, "ff", 2) == 0)
5559 {
5560 arg += 2;
5561 pp = &eap->force_ff;
5562 }
5563 else if (STRNCMP(arg, "fileformat", 10) == 0)
5564 {
5565 arg += 10;
5566 pp = &eap->force_ff;
5567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 else if (STRNCMP(arg, "enc", 3) == 0)
5569 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005570 if (STRNCMP(arg, "encoding", 8) == 0)
5571 arg += 8;
5572 else
5573 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 pp = &eap->force_enc;
5575 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005576 else if (STRNCMP(arg, "bad", 3) == 0)
5577 {
5578 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005579 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581
5582 if (pp == NULL || *arg != '=')
5583 return FAIL;
5584
5585 ++arg;
5586 *pp = (int)(arg - eap->cmd);
5587 arg = skip_cmd_arg(arg, FALSE);
5588 eap->arg = skipwhite(arg);
5589 *arg = NUL;
5590
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 if (pp == &eap->force_ff)
5592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5594 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005595 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005597 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005599 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5601 *p = TOLOWER_ASC(*p);
5602 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005603 else
5604 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005605 // Check ++bad= argument. Must be a single-byte character, "keep" or
5606 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005607 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005608 return FAIL;
5609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610
5611 return OK;
5612}
5613
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005614/*
5615 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5616 */
5617 static char_u *
5618get_argopt_name(expand_T *xp UNUSED, int idx)
5619{
5620 // Note: Keep this in sync with getargopt.
5621 static char *(p_opt_values[]) =
5622 {
5623 "fileformat=",
5624 "encoding=",
5625 "binary",
5626 "nobinary",
5627 "bad=",
5628 "edit",
5629 };
5630
John Marriotted908f72024-04-18 22:46:56 +02005631 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5632 return NULL;
5633
5634 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005635}
5636
5637/*
5638 * Command-line expansion for ++opt=name.
5639 */
5640 int
5641expand_argopt(
5642 char_u *pat,
5643 expand_T *xp,
5644 regmatch_T *rmp,
5645 char_u ***matches,
5646 int *numMatches)
5647{
5648 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5649 {
5650 char_u *(*cb)(expand_T *, int) = NULL;
5651
5652 char_u *name_end = xp->xp_pattern - 1;
5653 if (name_end - xp->xp_line >= 2
5654 && STRNCMP(name_end - 2, "ff", 2) == 0)
5655 cb = get_fileformat_name;
5656 else if (name_end - xp->xp_line >= 10
5657 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5658 cb = get_fileformat_name;
5659 else if (name_end - xp->xp_line >= 3
5660 && STRNCMP(name_end - 3, "enc", 3) == 0)
5661 cb = get_encoding_name;
5662 else if (name_end - xp->xp_line >= 8
5663 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5664 cb = get_encoding_name;
5665 else if (name_end - xp->xp_line >= 3
5666 && STRNCMP(name_end - 3, "bad", 3) == 0)
5667 cb = get_bad_name;
5668
5669 if (cb != NULL)
5670 {
5671 return ExpandGeneric(
5672 pat,
5673 xp,
5674 rmp,
5675 matches,
5676 numMatches,
5677 cb,
5678 FALSE);
5679 }
5680 return FAIL;
5681 }
5682
5683 // Special handling of "ff" which acts as a short form of
5684 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005685 if (xp->xp_pattern_len == 2
5686 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005687 {
5688 *matches = ALLOC_MULT(char_u *, 1);
5689 if (*matches == NULL)
5690 return FAIL;
5691 *numMatches = 1;
5692 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5693 return OK;
5694 }
5695
5696 return ExpandGeneric(
5697 pat,
5698 xp,
5699 rmp,
5700 matches,
5701 numMatches,
5702 get_argopt_name,
5703 FALSE);
5704}
5705
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005707ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005710 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 * directory for security reasons.
5712 */
5713 if (secure)
5714 {
5715 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005716 eap->errmsg =
5717 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
5719 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005720 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 else
5722 do_augroup(eap->arg, eap->forceit);
5723}
5724
5725/*
5726 * ":doautocmd": Apply the automatic commands to the current buffer.
5727 */
5728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005729ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005731 char_u *arg = eap->arg;
5732 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005733 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005734
Bram Moolenaar1610d052016-06-09 22:53:01 +02005735 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005736 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005737 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005738 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741/*
5742 * :[N]bunload[!] [N] [bufname] unload buffer
5743 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5744 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5745 */
5746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005747ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005749 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005750 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 eap->errmsg = do_bufdel(
5752 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5753 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5754 : DOBUF_UNLOAD, eap->arg,
5755 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5756}
5757
5758/*
5759 * :[N]buffer [N] to buffer N
5760 * :[N]sbuffer [N] to buffer N
5761 */
5762 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005763ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005765 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005766 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005768 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 else
5770 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005771 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5773 else
5774 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005775 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005776 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
5778}
5779
5780/*
5781 * :[N]bmodified [N] to next mod. buffer
5782 * :[N]sbmodified [N] to next mod. buffer
5783 */
5784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005785ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005788 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005789 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790}
5791
5792/*
5793 * :[N]bnext [N] to next buffer
5794 * :[N]sbnext [N] split and to next buffer
5795 */
5796 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005797ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005799 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005800 return;
5801
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005803 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005804 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805}
5806
5807/*
5808 * :[N]bNext [N] to previous buffer
5809 * :[N]bprevious [N] to previous buffer
5810 * :[N]sbNext [N] split and to previous buffer
5811 * :[N]sbprevious [N] split and to previous buffer
5812 */
5813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005814ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005816 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005817 return;
5818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005820 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005821 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822}
5823
5824/*
5825 * :brewind to first buffer
5826 * :bfirst to first buffer
5827 * :sbrewind split and to first buffer
5828 * :sbfirst split and to first buffer
5829 */
5830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005831ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005833 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005834 return;
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005837 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005838 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839}
5840
5841/*
5842 * :blast to last buffer
5843 * :sblast split and to last buffer
5844 */
5845 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005846ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005848 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005849 return;
5850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005852 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005853 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855
Bram Moolenaar7a092242020-04-16 22:10:49 +02005856/*
5857 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005858 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005861ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005863 int comment_char = '"';
5864
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005865 if (in_vim9script())
5866 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005867 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005868}
5869
5870/*
5871 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5872 * to "cmd_start" or has a white space character before it.
5873 */
5874 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005875ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005876{
5877 int c = *cmd;
5878
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005879 if (c == NUL || c == '|' || c == '\n')
5880 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005881 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005882 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5883 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005884 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005885 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886}
5887
5888#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5889 || defined(PROTO)
5890/*
5891 * Return the next command, after the first '|' or '\n'.
5892 * Return NULL if not found.
5893 */
5894 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005895find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
5897 while (*p != '|' && *p != '\n')
5898 {
5899 if (*p == NUL)
5900 return NULL;
5901 ++p;
5902 }
5903 return (p + 1);
5904}
5905#endif
5906
5907/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005908 * Check if *p is a separator between Ex commands, skipping over white space.
5909 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 */
5911 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005912check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005914 char_u *s = skipwhite(p);
5915
5916 if (*s == '|' || *s == '\n')
5917 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 else
5919 return NULL;
5920}
5921
5922/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005923 * If "eap->nextcmd" is not set, check for a next command at "p".
5924 */
5925 void
5926set_nextcmd(exarg_T *eap, char_u *arg)
5927{
5928 char_u *p = check_nextcmd(arg);
5929
5930 if (eap->nextcmd == NULL)
5931 eap->nextcmd = p;
5932 else if (p != NULL)
5933 // cannot use "| command" inside a {} block
5934 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5935}
5936
5937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 * - if there are more files to edit
5939 * - and this is the last window
5940 * - and forceit not used
5941 * - and not repeated twice on a row
5942 * return FAIL and give error message if 'message' TRUE
5943 * return OK otherwise
5944 */
5945 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005946check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005947 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005948 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949{
5950 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5951
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005952 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005953 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 {
5955 if (message)
5956 {
5957#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005958 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5959 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005961 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005963 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5964 NGETTEXT("%d more file to edit. Quit anyway?",
5965 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5967 return OK;
5968 return FAIL;
5969 }
5970#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005971 semsg(NGETTEXT(e_nr_more_file_to_edit,
5972 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005973 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 }
5975 return FAIL;
5976 }
5977 return OK;
5978}
5979
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980/*
5981 * Function given to ExpandGeneric() to obtain the list of command names.
5982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005984get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985{
5986 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005987 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 return cmdnames[idx].cmd_name;
5989}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005992ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
Bram Moolenaare6850792010-05-14 15:28:44 +02005994 if (*eap->arg == NUL)
5995 {
5996#ifdef FEAT_EVAL
5997 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5998 char_u *p = NULL;
5999
6000 if (expr != NULL)
6001 {
6002 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006003 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02006004 --emsg_off;
6005 vim_free(expr);
6006 }
6007 if (p != NULL)
6008 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006009 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02006010 vim_free(p);
6011 }
6012 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006013 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02006014#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006015 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02006016#endif
6017 }
6018 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00006019 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006020
6021#ifdef FEAT_VTP
6022 else if (has_vtp_working())
6023 {
6024 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006025 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006026 redrawcmd();
6027 }
6028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029}
6030
6031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006032ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
6034 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006035 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 do_highlight(eap->arg, eap->forceit, FALSE);
6037}
6038
6039
6040/*
6041 * Call this function if we thought we were going to exit, but we won't
6042 * (because of an error). May need to restore the terminal mode.
6043 */
6044 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006045not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
6047 exiting = FALSE;
6048 settmode(TMODE_RAW);
6049}
6050
Bram Moolenaar3552e742021-05-29 12:21:58 +02006051 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006052before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6053{
6054 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6055
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006056 // Bail out when autocommands closed the window.
6057 // Refuse to quit when the buffer in the last window is being closed (can
6058 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006059 if (!win_valid(wp)
6060 || curbuf_locked()
6061 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6062 return TRUE;
6063
6064 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6065 {
6066 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006067 // Refuse to quit when locked or when the window was closed or the
6068 // buffer in the last window is being closed (can only happen in
6069 // autocommands).
6070 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006071 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6072 return TRUE;
6073 }
6074
6075 return FALSE;
6076}
6077
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006079 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006080 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006081 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006084ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006086 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 if (cmdwin_type != 0)
6089 {
6090 cmdwin_result = Ctrl_C;
6091 return;
6092 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006093 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006094 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006095 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006096 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006097 return;
6098 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006099 if (eap->addr_count > 0)
6100 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006101 int wnr = eap->line2;
6102
6103 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6104 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006105 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006106 }
6107 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006108 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006110 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006111 if (curbuf_locked())
6112 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006114 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006115 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006116 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117
6118#ifdef FEAT_NETBEANS_INTG
6119 netbeansForcedQuit = eap->forceit;
6120#endif
6121
6122 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006123 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 */
6125 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6126 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006127 if ((!buf_hide(wp->w_buffer)
6128 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006129 | (eap->forceit ? CCGD_FORCEIT : 0)
6130 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006132 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 {
6134 not_exiting();
6135 }
6136 else
6137 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006138 // quit last window
6139 // Note: only_one_window() returns true, even so a help window is
6140 // still open. In that case only quit, if no address has been
6141 // specified. Example:
6142 // :h|wincmd w|1q - don't quit
6143 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006144 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006146 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006147#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006149#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006150 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006151 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 }
6153}
6154
6155/*
6156 * ":cquit".
6157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006159ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006161 // this does not always pass on the exit code to the Manx compiler. why?
6162 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163}
6164
6165/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006166 * Do preparations for "qall" and "wqall".
6167 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006169 int
6170before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (cmdwin_type != 0)
6173 {
6174 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006175 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 else
6177 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006178 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006180
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006181 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006182 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006183 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006184 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006185 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006186 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006187
6188 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006189 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006190
Bram Moolenaar411da642023-05-10 16:53:27 +01006191 return OK;
6192}
6193
6194/*
6195 * ":qall": try to quit all windows
6196 */
6197 static void
6198ex_quit_all(exarg_T *eap)
6199{
6200 if (before_quit_all(eap) == FAIL)
6201 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006203 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 getout(0);
6205 not_exiting();
6206}
6207
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208/*
6209 * ":close": close current window, unless it is the last one
6210 */
6211 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006212ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006214 win_T *win;
6215 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006217 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006219 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006220 {
6221 if (eap->addr_count == 0)
6222 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006223 else
6224 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006225 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006226 {
6227 winnr++;
6228 if (winnr == eap->line2)
6229 break;
6230 }
6231 if (win == NULL)
6232 win = lastwin;
6233 ex_win_close(eap->forceit, win, NULL);
6234 }
6235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236}
6237
Bram Moolenaar4033c552017-09-16 20:54:51 +02006238#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006239/*
6240 * ":pclose": Close any preview window.
6241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006243ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006244{
6245 win_T *win;
6246
Bram Moolenaar79648732019-07-18 21:43:07 +02006247 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006248 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006249 if (win->w_p_pvw)
6250 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006251 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006252 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006253 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006254# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006255 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006256 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006257# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006258}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006259#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006260
Bram Moolenaarf740b292006-02-16 22:11:02 +00006261/*
6262 * Close window "win" and take care of handling closing the last window for a
6263 * modified buffer.
6264 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006265 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006266ex_win_close(
6267 int forceit,
6268 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006269 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
6271 int need_hide;
6272 buf_T *buf = win->w_buffer;
6273
Bram Moolenaarcf844172020-06-26 19:44:06 +02006274 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006275 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006276 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006277 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006278 return;
6279 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006280 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006281 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006282
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006284 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006286#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006287 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006289# ifdef FEAT_TERMINAL
6290 if (term_job_running(buf->b_term))
6291 {
6292 if (term_confirm_stop(buf) == FAIL)
6293 return;
6294 // Manually kill the terminal here because this command will
6295 // hide it otherwise.
6296 free_terminal(buf);
6297 need_hide = FALSE;
6298 }
6299 else
6300# endif
6301 {
6302 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006303
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006304 set_bufref(&bufref, buf);
6305 dialog_changed(buf, FALSE);
6306 if (bufref_valid(&bufref) && bufIsChanged(buf))
6307 return;
6308 need_hide = FALSE;
6309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 }
6311 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006314 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 return;
6316 }
6317 }
6318
Bram Moolenaar4033c552017-09-16 20:54:51 +02006319#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006321#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006322
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006323 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006324 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006325 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006326 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006327 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328}
6329
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006331 * Handle the argument for a tabpage related ex command.
6332 * Returns a tabpage number.
6333 * When an error is encountered then eap->errmsg is set.
6334 */
6335 static int
6336get_tabpage_arg(exarg_T *eap)
6337{
6338 int tab_number;
6339 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6340
6341 if (eap->arg && *eap->arg != NUL)
6342 {
6343 char_u *p = eap->arg;
6344 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006345 int relative = 0; // argument +N/-N means: go to N places to the
6346 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006347
6348 if (*p == '-')
6349 {
6350 relative = -1;
6351 p++;
6352 }
6353 else if (*p == '+')
6354 {
6355 relative = 1;
6356 p++;
6357 }
6358
6359 p_save = p;
6360 tab_number = getdigits(&p);
6361
6362 if (relative == 0)
6363 {
6364 if (STRCMP(p, "$") == 0)
6365 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006366 else if (STRCMP(p, "#") == 0)
6367 if (valid_tabpage(lastused_tabpage))
6368 tab_number = tabpage_index(lastused_tabpage);
6369 else
6370 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006371 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6372 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006373 tab_number = 0;
6374 goto theend;
6375 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006376 else if (p == p_save || *p_save == '-' || *p != NUL
6377 || tab_number > LAST_TAB_NR)
6378 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006379 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006380 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006381 goto theend;
6382 }
6383 }
6384 else
6385 {
6386 if (*p_save == NUL)
6387 tab_number = 1;
6388 else if (p == p_save || *p_save == '-' || *p != NUL
6389 || tab_number == 0)
6390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006391 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006392 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006393 goto theend;
6394 }
6395 tab_number = tab_number * relative + tabpage_index(curtab);
6396 if (!unaccept_arg0 && relative == -1)
6397 --tab_number;
6398 }
6399 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
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 }
6402 else if (eap->addr_count > 0)
6403 {
6404 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006405 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006406 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006407 tab_number = 0;
6408 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006409 else
6410 {
6411 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006412 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006413 {
zeertzjq076faac2024-03-25 16:41:06 +01006414 char_u *cmdp = eap->cmd;
6415
6416 while (--cmdp > *eap->cmdlinep
6417 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6418 ;
6419 if (*cmdp == '-')
6420 {
6421 --tab_number;
6422 if (tab_number < unaccept_arg0)
6423 eap->errmsg = _(e_invalid_range);
6424 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006425 }
6426 }
6427 }
6428 else
6429 {
6430 switch (eap->cmdidx)
6431 {
6432 case CMD_tabnext:
6433 tab_number = tabpage_index(curtab) + 1;
6434 if (tab_number > LAST_TAB_NR)
6435 tab_number = 1;
6436 break;
6437 case CMD_tabmove:
6438 tab_number = LAST_TAB_NR;
6439 break;
6440 default:
6441 tab_number = tabpage_index(curtab);
6442 }
6443 }
6444
6445theend:
6446 return tab_number;
6447}
6448
6449/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006450 * ":tabclose": close current tab page, unless it is the last one.
6451 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 */
6453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006454ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006456 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006457 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006458
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006459 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006460 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006461 cmdwin_result = K_IGNORE;
6462 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006463 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006464
6465 if (first_tabpage->tp_next == NULL)
6466 {
6467 emsg(_(e_cannot_close_last_tab_page));
6468 return;
6469 }
6470
6471 if (window_layout_locked(CMD_tabclose))
6472 return;
6473
6474 tab_number = get_tabpage_arg(eap);
6475 if (eap->errmsg != NULL)
6476 return;
6477
6478 tp = find_tabpage(tab_number);
6479 if (tp == NULL)
6480 {
6481 beep_flush();
6482 return;
6483 }
6484 if (tp != curtab)
6485 {
6486 tabpage_close_other(tp, eap->forceit);
6487 return;
6488 }
6489 else if (!text_locked() && !curbuf_locked())
6490 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006491}
6492
6493/*
6494 * ":tabonly": close all tab pages except the current one
6495 */
6496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006497ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006498{
6499 tabpage_T *tp;
6500 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006501 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006502
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006503 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006504 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006505 cmdwin_result = K_IGNORE;
6506 return;
6507 }
6508
6509 if (first_tabpage->tp_next == NULL)
6510 {
6511 msg(_("Already only one tab page"));
6512 return;
6513 }
6514
6515 if (window_layout_locked(CMD_tabonly))
6516 return;
6517
6518 tab_number = get_tabpage_arg(eap);
6519 if (eap->errmsg != NULL)
6520 return;
6521
6522 goto_tabpage(tab_number);
6523 // Repeat this up to a 1000 times, because autocommands may
6524 // mess up the lists.
6525 for (done = 0; done < 1000; ++done)
6526 {
6527 FOR_ALL_TABPAGES(tp)
6528 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006529 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006530 tabpage_close_other(tp, eap->forceit);
6531 // if we failed to close it quit
6532 if (valid_tabpage(tp))
6533 done = 1000;
6534 // start over, "tp" is now invalid
6535 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006536 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006537 if (first_tabpage->tp_next == NULL)
6538 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006543 * Close the current tab page.
6544 */
6545 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006546tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006547{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006548 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006549 return;
6550
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006551 // First close all the windows but the current one. If that worked then
6552 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006553 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006554 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006555 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006556 ex_win_close(forceit, curwin, NULL);
6557# ifdef FEAT_GUI
6558 need_mouse_correct = TRUE;
6559# endif
6560}
6561
6562/*
6563 * Close tab page "tp", which is not the current tab page.
6564 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006565 * Also takes care of the tab pages line disappearing when closing the
6566 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006567 */
6568 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006569tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006570{
6571 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006572 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006573
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006574 // Limit to 1000 windows, autocommands may add a window while we close
6575 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006576 while (++done < 1000)
6577 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006578 wp = tp->tp_firstwin;
6579 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006580
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006581 // Autocommands may delete the tab page under our fingers and we may
6582 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006583 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006584 break;
6585 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006586
Bram Moolenaar29323592016-07-24 22:04:11 +02006587 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006588}
6589
6590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 * ":only".
6592 */
6593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006594ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006596 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006597 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598# ifdef FEAT_GUI
6599 need_mouse_correct = TRUE;
6600# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006601 if (eap->addr_count > 0)
6602 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006603 win_T *wp;
6604 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006605 for (wp = firstwin; --wnr > 0; )
6606 {
6607 if (wp->w_next == NULL)
6608 break;
6609 else
6610 wp = wp->w_next;
6611 }
6612 win_goto(wp);
6613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 close_others(TRUE, eap->forceit);
6615}
6616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006618ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006620 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006621 if (eap->skip)
6622 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006623
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006624 if (window_layout_locked(CMD_hide))
6625 return;
6626#ifdef FEAT_GUI
6627 need_mouse_correct = TRUE;
6628#endif
6629 if (eap->addr_count == 0)
6630 win_close(curwin, FALSE); // don't free buffer
6631 else
6632 {
6633 int winnr = 0;
6634 win_T *win;
6635
6636 FOR_ALL_WINDOWS(win)
6637 {
6638 winnr++;
6639 if (winnr == eap->line2)
6640 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006642 if (win == NULL)
6643 win = lastwin;
6644 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
6646}
6647
6648/*
6649 * ":stop" and ":suspend": Suspend Vim.
6650 */
dbivolaruab16ad32021-12-29 19:41:47 +00006651 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006652ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653{
6654 /*
6655 * Disallow suspending for "rvim".
6656 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006657 if (check_restricted())
6658 return;
6659
6660 if (!eap->forceit)
6661 autowrite_all();
6662 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6663 windgoto((int)Rows - 1, 0);
6664 out_char('\n');
6665 out_flush();
6666 stoptermcap();
6667 out_flush(); // needed for SUN to restore xterm buffer
6668 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6669 ui_suspend(); // call machine specific function
6670 maketitle();
6671 resettitle(); // force updating the title
6672 starttermcap();
6673 scroll_start(); // scroll screen before redrawing
6674 redraw_later_clear();
6675 shell_resized(); // may have resized window
6676 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677}
6678
6679/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006680 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 */
6682 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006683ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006685#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006686 if (not_in_vim9(eap) == FAIL)
6687 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 if (cmdwin_type != 0)
6690 {
6691 cmdwin_result = Ctrl_C;
6692 return;
6693 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006694 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006695 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006696 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006697 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006698 return;
6699 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006700
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006702 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 */
6704 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6705 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006706
6707 // Write the buffer for ":wq" or when it was changed.
6708 // Trigger QuitPre and ExitPre.
6709 // Check if we can exit now, after autocommands have changed things.
6710 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6711 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006713 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {
6715 not_exiting();
6716 }
6717 else
6718 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006719 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006721 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722# ifdef FEAT_GUI
6723 need_mouse_correct = TRUE;
6724# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006725 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006726 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 }
6728}
6729
6730/*
6731 * ":print", ":list", ":number".
6732 */
6733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006734ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006736 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006737 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006740 for ( ;!got_int; ui_breakcheck())
6741 {
6742 print_line(eap->line1,
6743 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6744 || (eap->flags & EXFLAG_NR)),
6745 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6746 if (++eap->line1 > eap->line2)
6747 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006748 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006749 }
6750 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006751 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006752 curwin->w_cursor.lnum = eap->line2;
6753 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 }
6755
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757}
6758
6759#ifdef FEAT_BYTEOFF
6760 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006761ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 goto_byte(eap->line2);
6764}
6765#endif
6766
6767/*
6768 * ":shell".
6769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006771ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 do_shell(NULL, 0);
6774}
6775
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006776#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006778static int drop_busy = FALSE;
6779static int drop_filec;
6780static char_u **drop_filev = NULL;
6781static int drop_split;
6782static void (*drop_callback)(void *);
6783static void *drop_cookie;
6784
6785 static void
6786handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787{
6788 exarg_T ea;
6789 int save_msg_scroll = msg_scroll;
6790
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006791 // Setting the argument list may cause screen updates and being called
6792 // recursively. Avoid that by setting drop_busy.
6793 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006795 // Check whether the current buffer is changed. If so, we will need
6796 // to split the current window or data could be lost.
6797 // We don't need to check if the 'hidden' option is set, as in this
6798 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006799 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {
6801 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006802 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 --emsg_off;
6804 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006805 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 if (win_split(0, 0) == FAIL)
6808 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006809 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006811 // When splitting the window, create a new alist. Otherwise the
6812 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 alist_unlink(curwin->w_alist);
6814 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816
6817 /*
6818 * Set up the new argument list.
6819 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006820 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821
6822 /*
6823 * Move to the first file.
6824 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006825 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006826 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 ea.cmd = (char_u *)"next";
6828 do_argfile(&ea, 0);
6829
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006830 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6831 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 need_start_insertmode = FALSE;
6833
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006834 // Restore msg_scroll, otherwise a following command may cause scrolling
6835 // unexpectedly. The screen will be redrawn by the caller, thus
6836 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006838
6839 if (drop_callback != NULL)
6840 drop_callback(drop_cookie);
6841
6842 drop_filev = NULL;
6843 drop_busy = FALSE;
6844}
6845
6846/*
6847 * Handle a file drop. The code is here because a drop is *nearly* like an
6848 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006849 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006850 * code is very similar to :args and hence needs access to a lot of the static
6851 * functions in this file.
6852 *
6853 * The "filev" list must have been allocated using alloc(), as should each item
6854 * in the list. This function takes over responsibility for freeing the "filev"
6855 * list.
6856 */
6857 void
6858handle_drop(
6859 int filec, // the number of files dropped
6860 char_u **filev, // the list of files dropped
6861 int split, // force splitting the window
6862 void (*callback)(void *), // to be called after setting the argument
6863 // list
6864 void *cookie) // argument for "callback" (allocated)
6865{
6866 // Cannot handle recursive drops, finish the pending one.
6867 if (drop_busy)
6868 {
6869 FreeWild(filec, filev);
6870 vim_free(cookie);
6871 return;
6872 }
6873
6874 // When calling handle_drop() more than once in a row we only use the last
6875 // one.
6876 if (drop_filev != NULL)
6877 {
6878 FreeWild(drop_filec, drop_filev);
6879 vim_free(drop_cookie);
6880 }
6881
6882 drop_filec = filec;
6883 drop_filev = filev;
6884 drop_split = split;
6885 drop_callback = callback;
6886 drop_cookie = cookie;
6887
6888 // Postpone this when:
6889 // - editing the command line
6890 // - not possible to change the current buffer
6891 // - updating the screen
6892 // As it may change buffers and window structures that are in use and cause
6893 // freed memory to be used.
6894 if (text_locked() || curbuf_locked() || updating_screen)
6895 return;
6896
6897 handle_drop_internal();
6898}
6899
6900/*
6901 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6902 * reset: Handle a postponed drop.
6903 */
6904 void
6905handle_any_postponed_drop(void)
6906{
6907 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006908 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006909 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910}
6911#endif
6912
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 * ":preserve".
6915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006917ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006919 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 ml_preserve(curbuf, TRUE);
6921}
6922
6923/*
6924 * ":recover".
6925 */
6926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006927ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006929 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006931 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6932 | CCGD_MULTWIN
6933 | (eap->forceit ? CCGD_FORCEIT : 0)
6934 | CCGD_EXCMD)
6935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 && (*eap->arg == NUL
6937 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006938 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 recoverymode = FALSE;
6940}
6941
6942/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006943 * Command modifier used in a wrong way. Also for other commands that can't
6944 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 */
6946 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006947ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006949 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950}
6951
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952/*
6953 * :sview [+command] file split window with new file, read-only
6954 * :split [[+command] file] split window with current or new file
6955 * :vsplit [[+command] file] split window vertically with current or new file
6956 * :new [[+command] file] split window with no or new file
6957 * :vnew [[+command] file] split vertically window with no or new file
6958 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006959 *
6960 * :tabedit open new Tab page with empty window
6961 * :tabedit [+command] file open new Tab page and edit "file"
6962 * :tabnew [[+command] file] just like :tabedit
6963 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 */
6965 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006966ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006968 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006970#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006971 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006972 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006973#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006974 int use_tab = eap->cmdidx == CMD_tabedit
6975 || eap->cmdidx == CMD_tabfind
6976 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006978 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006979 return;
6980
Bram Moolenaar4033c552017-09-16 20:54:51 +02006981#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
Bram Moolenaar4033c552017-09-16 20:54:51 +02006985#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006986 // A ":split" in the quickfix window works like ":new". Don't want two
6987 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006988 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {
6990 if (eap->cmdidx == CMD_split)
6991 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (eap->cmdidx == CMD_vsplit)
6993 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006997 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006999 char_u *file_to_find = NULL;
7000 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007002 FNAME_MESS, TRUE, curbuf->b_ffname,
7003 &file_to_find, &search_ctx);
7004 vim_free(file_to_find);
7005 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 if (fname == NULL)
7007 goto theend;
7008 eap->arg = fname;
7009 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007010# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007011 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 && eap->cmdidx != CMD_new)
7014 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007015 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007016# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007017 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007018# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007019 au_has_group((char_u *)"FileExplorer"))
7020 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007021 // No browsing supported but we do have the file explorer:
7022 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007023 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007024 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007025 }
7026 else
7027 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02007028 fname = do_browse(0, (char_u *)(use_tab
7029 ? _("Edit File in new tab page")
7030 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007032 if (fname == NULL)
7033 goto theend;
7034 eap->arg = fname;
7035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007037 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007040 /*
7041 * Either open new tab page or split the window.
7042 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007043 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007044 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007045 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007046 : eap->addr_count == 0 ? 0
7047 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007048 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007049 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007051 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007052 if (curwin != old_curwin
7053 && win_valid(old_curwin)
7054 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007055 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007056 old_curwin->w_alt_fnum = curbuf->b_fnum;
7057 }
7058 }
7059 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7061 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007062 // Reset 'scrollbind' when editing another file, but keep it when
7063 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007064 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007065 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 else
7067 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 do_exedit(eap, old_curwin);
7069 }
7070
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007071# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007072 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075theend:
7076 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007078
7079/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007080 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007081 */
7082 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007083tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007084{
7085 exarg_T ea;
7086
Bram Moolenaara80faa82020-04-12 19:37:17 +02007087 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007088 ea.cmdidx = CMD_tabnew;
7089 ea.cmd = (char_u *)"tabn";
7090 ea.arg = (char_u *)"";
7091 ex_splitview(&ea);
7092}
7093
7094/*
7095 * :tabnext command
7096 */
7097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007098ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007099{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007100 int tab_number;
7101
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007102 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007103 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007104 switch (eap->cmdidx)
7105 {
7106 case CMD_tabfirst:
7107 case CMD_tabrewind:
7108 goto_tabpage(1);
7109 break;
7110 case CMD_tablast:
7111 goto_tabpage(9999);
7112 break;
7113 case CMD_tabprevious:
7114 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007115 if (eap->arg && *eap->arg != NUL)
7116 {
7117 char_u *p = eap->arg;
7118 char_u *p_save = p;
7119
7120 tab_number = getdigits(&p);
7121 if (p == p_save || *p_save == '-' || *p != NUL
7122 || tab_number == 0)
7123 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007124 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007125 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007126 return;
7127 }
7128 }
7129 else
7130 {
7131 if (eap->addr_count == 0)
7132 tab_number = 1;
7133 else
7134 {
7135 tab_number = eap->line2;
7136 if (tab_number < 1)
7137 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007138 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007139 return;
7140 }
7141 }
7142 }
7143 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007144 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007145 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007146 tab_number = get_tabpage_arg(eap);
7147 if (eap->errmsg == NULL)
7148 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007149 break;
7150 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007151}
7152
7153/*
7154 * :tabmove command
7155 */
7156 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007157ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007158{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007159 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007160
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007161 tab_number = get_tabpage_arg(eap);
7162 if (eap->errmsg == NULL)
7163 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007164}
7165
7166/*
7167 * :tabs command: List tabs and their contents.
7168 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007170ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007171{
7172 tabpage_T *tp;
7173 win_T *wp;
7174 int tabcount = 1;
7175
7176 msg_start();
7177 msg_scroll = TRUE;
7178 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7179 {
7180 msg_putchar('\n');
7181 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007182 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007183 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007184 ui_breakcheck();
7185
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007186 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007187 wp = firstwin;
7188 else
7189 wp = tp->tp_firstwin;
7190 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7191 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007192 msg_putchar('\n');
7193 msg_putchar(wp == curwin ? '>' : ' ');
7194 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007195 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7196 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007197 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007198 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007199 else
7200 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7201 IObuff, IOSIZE, TRUE);
7202 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007203 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007204 ui_breakcheck();
7205 }
7206 }
7207}
7208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209/*
7210 * ":mode": Set screen mode.
7211 * If no argument given, just get the screen size and redraw.
7212 */
7213 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007214ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
7216 if (*eap->arg == NUL)
7217 shell_resized();
7218 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007219 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220}
7221
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222/*
7223 * ":resize".
7224 * set, increment or decrement current window height
7225 */
7226 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007227ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228{
7229 int n;
7230 win_T *wp = curwin;
7231
7232 if (eap->addr_count > 0)
7233 {
7234 n = eap->line2;
7235 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7236 ;
7237 }
7238
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007239# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007241# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007243 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 {
7245 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007246 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007247 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007249 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 }
7251 else
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_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007255 else if (n == 0 && eap->arg[0] == NUL) // default is very high
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_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
7259}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
7261/*
7262 * ":find [+command] <file>" command.
7263 */
7264 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007265ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266{
Colin Kennedy21570352024-03-03 16:16:47 +01007267 if (!check_can_set_curbuf_forceit(eap->forceit))
7268 return;
7269
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 char_u *fname;
7271 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007272 char_u *file_to_find = NULL;
7273 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
7275 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007276 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 if (eap->addr_count > 0)
7278 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007279 // Repeat finding the file "count" times. This matters when it appears
7280 // several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 count = eap->line2;
7282 while (fname != NULL && --count > 0)
7283 {
7284 vim_free(fname);
7285 fname = find_file_in_path(NULL, 0, FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007286 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 }
7288 }
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007289 VIM_CLEAR(file_to_find);
7290 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007292 if (fname == NULL)
7293 return;
7294
7295 eap->arg = fname;
7296 do_exedit(eap, NULL);
7297 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298}
7299
7300/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007301 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007302 */
7303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007304ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007305{
7306 regmatch_T regmatch;
7307 char_u *p;
7308
Bram Moolenaar65088802021-03-13 21:07:21 +01007309#ifdef FEAT_EVAL
7310 if (not_in_vim9(eap) == FAIL)
7311 return;
7312#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007313 curwin->w_cursor.lnum = eap->line2;
7314 beginline(BL_SOL | BL_FIX);
7315 if (*eap->arg == '/')
7316 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007317 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007318 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007319 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007320 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007321 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007322 if (regmatch.regprog != NULL)
7323 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007324 // make a copy of the line, when searching for a mark it might be
7325 // flushed
7326 char_u *line = vim_strsave(ml_get_curline());
7327
Bram Moolenaardf177f62005-02-22 08:39:57 +00007328 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007329 if (vim_regexec(&regmatch, line, (colnr_T)0))
7330 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007331 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007332 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007333 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007334 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007335 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007336 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007337 eap->arg += STRLEN(eap->arg);
7338 }
7339 check_cursor();
7340
7341 eap->cmdidx = CMD_visual;
7342 do_exedit(eap, NULL);
7343}
7344
7345/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007346 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 */
7348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007349ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007351 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7352
Colin Kennedy21570352024-03-03 16:16:47 +01007353 // Exclude commands which keep the window's current buffer
7354 if (
7355 eap->cmdidx != CMD_badd
7356 && eap->cmdidx != CMD_balt
7357 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007358 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7359 )
Colin Kennedy21570352024-03-03 16:16:47 +01007360 return;
7361
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 do_exedit(eap, NULL);
7363}
7364
7365/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007366 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007369do_exedit(
7370 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007371 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372{
7373 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007375 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007377 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7378 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007379 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 /*
7381 * ":vi" command ends Ex mode.
7382 */
7383 if (exmode_active && (eap->cmdidx == CMD_visual
7384 || eap->cmdidx == CMD_view))
7385 {
7386 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007387 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007389 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007390 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007391 if (global_busy)
7392 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007393 if (eap->nextcmd != NULL)
7394 {
7395 stuffReadbuff(eap->nextcmd);
7396 eap->nextcmd = NULL;
7397 }
7398
7399 if (exmode_was != EXMODE_VIM)
7400 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007401 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007402 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007403 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007404 no_wait_return = 0;
7405 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007406 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007407 msg_scroll = 0;
7408#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007409 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007410 hold_gui_events = 0;
7411#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007412 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007413 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007414
7415 main_loop(FALSE, TRUE);
7416
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007417 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007418 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007419 no_wait_return = save_nwr;
7420 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007421#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007422 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007423#endif
7424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428
7429 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007430 || eap->cmdidx == CMD_tabnew
7431 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007432 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007434 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 setpcmark();
7436 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007437 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7438 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007440 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 || *eap->arg != NUL
7442#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007443 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444#endif
7445 )
7446 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007447 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7448 // Only ":edit" or ":script" can bring us here, others are stopped
7449 // earlier.
7450 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007451 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007452
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 n = readonlymode;
7454 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7455 readonlymode = TRUE;
7456 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007457 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7458 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007459 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7460 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7462 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007463 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7465 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7466 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007467 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007469 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007470 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007471 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7472 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007473 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007475 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 if (old_curwin != NULL)
7477 {
7478 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007479 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007481#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007482 cleanup_T cs;
7483
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007484 // Reset the error/interrupt/exception state here so that
7485 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007486 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007487#endif
7488#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007490#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007491 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007492
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007493#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007494 // Restore the error/interrupt/exception state if not
7495 // discarded by a new aborting error, interrupt, or
7496 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007497 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 }
7500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 }
7502 else if (readonlymode && curbuf->b_nwindows == 1)
7503 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007504 // When editing an already visited buffer, 'readonly' won't be set
7505 // but the previous value is kept. With ":view" and ":sview" we
7506 // want the file to be readonly, except when another window is
7507 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 curbuf->b_p_ro = TRUE;
7509 }
7510 readonlymode = n;
7511 }
7512 else
7513 {
7514 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007515 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 if (n != curwin->w_arg_idx_invalid)
7519 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 }
7521
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 /*
7523 * if ":split file" worked, set alternate file name in old window to new
7524 * file
7525 */
7526 if (old_curwin != NULL
7527 && *eap->arg != NUL
7528 && curwin != old_curwin
7529 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007530 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007531 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533
7534 ex_no_reprint = TRUE;
7535}
7536
7537#ifndef FEAT_GUI
7538/*
7539 * ":gui" and ":gvim" when there is no GUI.
7540 */
7541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007542ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007544 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545}
7546#endif
7547
Bram Moolenaar4f974752019-02-17 17:44:42 +01007548#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007550ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551{
7552 gui_make_tearoff(eap->arg);
7553}
7554#endif
7555
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007556#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7557 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007559ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007561# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7562 if (gui.in_use)
7563 gui_make_popup(eap->arg, eap->forceit);
7564# ifdef FEAT_TERM_POPUP_MENU
7565 else
7566# endif
7567# endif
7568# ifdef FEAT_TERM_POPUP_MENU
7569 pum_make_popup(eap->arg, eap->forceit);
7570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571}
7572#endif
7573
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007575ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576{
7577 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007578 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007580 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581}
7582
7583/*
7584 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7585 * offset.
7586 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007589ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007592 win_T *save_curwin = curwin;
7593 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 long topline;
7595 long y;
7596 linenr_T old_linenr = curwin->w_cursor.lnum;
7597
7598 setpcmark();
7599
7600 /*
7601 * determine max topline
7602 */
7603 if (curwin->w_p_scb)
7604 {
7605 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007606 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {
7608 if (wp->w_p_scb && wp->w_buffer)
7609 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007610 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 if (topline > y)
7612 topline = y;
7613 }
7614 }
7615 if (topline < 1)
7616 topline = 1;
7617 }
7618 else
7619 {
7620 topline = 1;
7621 }
7622
7623
7624 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007625 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007627 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 {
7629 if (curwin->w_p_scb)
7630 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007631 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 y = topline - curwin->w_topline;
7633 if (y > 0)
7634 scrollup(y, TRUE);
7635 else
7636 scrolldown(-y, TRUE);
7637 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007638 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 }
7642 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007643 curwin = save_curwin;
7644 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 if (curwin->w_p_scb)
7646 {
7647 did_syncbind = TRUE;
7648 checkpcmark();
7649 if (old_linenr != curwin->w_cursor.lnum)
7650 {
7651 char_u ctrl_o[2];
7652
7653 ctrl_o[0] = Ctrl_O;
7654 ctrl_o[1] = 0;
7655 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7656 }
7657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658}
7659
7660
7661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007662ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007664 int i;
7665 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7666 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007668 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007670 do_bang(1, eap, FALSE, FALSE, TRUE);
7671 return;
7672 }
7673
7674 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7675 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676
7677#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007678 if (cmdmod.cmod_flags & CMOD_BROWSE)
7679 {
7680 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007682 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7683 NULL, NULL, NULL, curbuf);
7684 if (browseFile != NULL)
7685 {
7686 i = readfile(browseFile, NULL,
7687 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7688 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 }
7690 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007691 i = OK;
7692 }
7693 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007695 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007697 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 return;
7699 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007700 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 }
7702 else
7703 {
7704 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7705 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7706 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007707 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708
7709 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007710 if (i != OK)
7711 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007712#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007713 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007715 semsg(_(e_cant_open_file_str), eap->arg);
7716 }
7717 else
7718 {
7719 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007720 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007721 // Delete the empty line that remains. Historically ex does
7722 // this but vi doesn't.
7723 if (eap->line2 == 0)
7724 lnum = curbuf->b_ml.ml_line_count;
7725 else
7726 lnum = 1;
7727 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007728 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007729 ml_delete(lnum);
7730 if (curwin->w_cursor.lnum > 1
7731 && curwin->w_cursor.lnum >= lnum)
7732 --curwin->w_cursor.lnum;
7733 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007734 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007735 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007736 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 }
7738}
7739
Bram Moolenaarea408852005-06-25 22:49:46 +00007740static char_u *prev_dir = NULL;
7741
7742#if defined(EXITFREE) || defined(PROTO)
7743 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007744free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007745{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007746 VIM_CLEAR(prev_dir);
7747 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007748}
7749#endif
7750
Bram Moolenaarf4258302013-06-02 18:20:17 +02007751/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007752 * Get the previous directory for the given chdir scope.
7753 */
7754 static char_u *
7755get_prevdir(cdscope_T scope)
7756{
7757 if (scope == CDSCOPE_WINDOW)
7758 return curwin->w_prevdir;
7759 else if (scope == CDSCOPE_TABPAGE)
7760 return curtab->tp_prevdir;
7761 return prev_dir;
7762}
7763
7764/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007765 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007766 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7767 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007768 */
7769 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007770post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007771{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007772 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007773 // Clear tab local directory for both :cd and :tcd
7774 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007775 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007776 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007777 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007778 char_u *pdir = get_prevdir(scope);
7779
7780 // If still in the global directory, need to remember current
7781 // directory as the global directory.
7782 if (globaldir == NULL && pdir != NULL)
7783 globaldir = vim_strsave(pdir);
7784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007785 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007786 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007787 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007788 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007789 curtab->tp_localdir = vim_strsave(NameBuff);
7790 else
7791 curwin->w_localdir = vim_strsave(NameBuff);
7792 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007793 }
7794 else
7795 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007796 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007797 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007798 }
7799
zeertzjq64be6aa2021-11-19 11:59:08 +00007800 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007801 shorten_fnames(TRUE);
7802}
7803
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007804/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007805 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7806 */
7807 void
7808trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7809{
7810#ifdef FEAT_EVAL
7811 dict_T *v_event;
7812 save_v_event_T save_v_event;
7813
7814 v_event = get_v_event(&save_v_event);
7815 (void)dict_add_string(v_event, "directory", new_dir);
7816 dict_set_items_ro(v_event);
7817#endif
7818 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7819#ifdef FEAT_EVAL
7820 restore_v_event(v_event, &save_v_event);
7821#endif
7822}
7823
7824/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007825 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007826 * chdir() function.
7827 * scope == CDSCOPE_WINDOW: changes the window-local directory
7828 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7829 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007830 * Returns TRUE if the directory is successfully changed.
7831 */
7832 int
7833changedir_func(
7834 char_u *new_dir,
7835 int forceit,
7836 cdscope_T scope)
7837{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007838 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007839 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007840 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007841 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007842 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007843
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007844 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007845 return FALSE;
7846
7847 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7848 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007849 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007850 return FALSE;
7851 }
7852
7853 // ":cd -": Change to previous directory
7854 if (STRCMP(new_dir, "-") == 0)
7855 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007856 pdir = get_prevdir(scope);
7857 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007858 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007859 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007860 return FALSE;
7861 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007862 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007863 }
7864
7865 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007866 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007867 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007868 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007869 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007870
Bakudankun29f3a452021-12-11 12:28:08 +00007871 // For UNIX ":cd" means: go to home directory.
7872 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007873#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007874 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007875#else
7876 if (*new_dir == NUL && p_cdh)
7877#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007878 {
7879 // use NameBuff for home directory name
7880# ifdef VMS
7881 char_u *p;
7882
7883 p = mch_getenv((char_u *)"SYS$LOGIN");
7884 if (p == NULL || *p == NUL) // empty is the same as not set
7885 NameBuff[0] = NUL;
7886 else
7887 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7888# else
7889 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7890# endif
7891 new_dir = NameBuff;
7892 }
zeertzjqf38aad82021-12-30 13:45:57 +00007893 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007894 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7895 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007896 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007897 if (scope == CDSCOPE_WINDOW)
7898 acmd_fname = (char_u *)"window";
7899 else if (scope == CDSCOPE_TABPAGE)
7900 acmd_fname = (char_u *)"tabpage";
7901 else
7902 acmd_fname = (char_u *)"global";
7903 trigger_DirChangedPre(acmd_fname, new_dir);
7904
7905 if (vim_chdir(new_dir))
7906 {
7907 emsg(_(e_command_failed));
7908 vim_free(pdir);
7909 return FALSE;
7910 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007911 }
zeertzjq73257142022-02-02 13:16:37 +00007912
7913 if (scope == CDSCOPE_WINDOW)
7914 pp = &curwin->w_prevdir;
7915 else if (scope == CDSCOPE_TABPAGE)
7916 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007917 else
zeertzjq73257142022-02-02 13:16:37 +00007918 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007919 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007920 *pp = pdir;
7921
7922 post_chdir(scope);
7923
7924 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007925 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007926 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007927 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007928}
Bram Moolenaarea408852005-06-25 22:49:46 +00007929
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007931 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007933 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007934ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007936 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937
7938 new_dir = eap->arg;
7939#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007940 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7941 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007943 ex_pwd(NULL);
7944 return;
7945 }
7946#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007947
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007948 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007949
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007950 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7951 scope = CDSCOPE_WINDOW;
7952 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7953 scope = CDSCOPE_TABPAGE;
7954
7955 if (changedir_func(new_dir, eap->forceit, scope))
7956 {
7957 // Echo the new current directory if the command was typed.
7958 if (KeyTyped || p_verbose >= 5)
7959 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 }
7961}
7962
7963/*
7964 * ":pwd".
7965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007967ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968{
7969 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7970 {
7971#ifdef BACKSLASH_IN_FILENAME
7972 slash_adjust(NameBuff);
7973#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007974 if (p_verbose > 0)
7975 {
7976 char *context = "global";
7977
Bram Moolenaar05268152021-11-18 18:53:45 +00007978 if (last_chdir_reason != NULL)
7979 context = last_chdir_reason;
7980 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007981 context = "window";
7982 else if (curtab->tp_localdir != NULL)
7983 context = "tabpage";
7984 smsg("[%s] %s", context, (char *)NameBuff);
7985 }
7986 else
7987 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 }
7989 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007990 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991}
7992
7993/*
7994 * ":=".
7995 */
7996 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007997ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007999 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008000 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001}
8002
8003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008004ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008006 int n;
8007 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008
8009 if (cursor_valid())
8010 {
8011 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8012 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00008013 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008015
8016 len = eap->line2;
8017 switch (*eap->arg)
8018 {
8019 case 'm': break;
8020 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008021 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008022 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008023
8024 // Hide the cursor if invoked with !
8025 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026}
8027
8028/*
8029 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008030 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 */
8032 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008033do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034{
Bram Moolenaar52953192019-08-16 10:27:13 +02008035 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008036 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008037# ifdef ELAPSED_FUNC
8038 elapsed_T start_tv;
8039
8040 // Remember at what time we started, so that we know how much longer we
8041 // should wait after waiting for a bit.
8042 ELAPSED_INIT(start_tv);
8043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008045 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008046 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008047 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008048 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008049
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008050 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008051 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008053 wait_now = msec - done > 1000L ? 1000L : msec - done;
8054#ifdef FEAT_TIMERS
8055 {
8056 long due_time = check_due_timer();
8057
8058 if (due_time > 0 && due_time < wait_now)
8059 wait_now = due_time;
8060 }
8061#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008062#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008063 if (has_any_channel() && wait_now > 20L)
8064 wait_now = 20L;
8065#endif
8066#ifdef FEAT_SOUND
8067 if (has_any_sound_callback() && wait_now > 20L)
8068 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008069#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008070 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008071
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008072#ifdef FEAT_JOB_CHANNEL
8073 if (has_any_channel())
8074 ui_breakcheck_force(TRUE);
8075 else
8076#endif
8077 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008078#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008079 // Process the netbeans and clientserver messages that may have been
8080 // received in the call to ui_breakcheck() when the GUI is in use. This
8081 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008082 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008083#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008084
8085# ifdef ELAPSED_FUNC
8086 // actual time passed
8087 done = ELAPSED_FUNC(start_tv);
8088# else
8089 // guestimate time passed (will actually be more)
8090 done += wait_now;
8091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008093
8094 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8095 // input buffer, otherwise a following call to input() fails.
8096 if (got_int)
8097 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008098
8099 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008100 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101}
8102
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103/*
8104 * ":winsize" command (obsolete).
8105 */
8106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008107ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
8109 int w, h;
8110 char_u *arg = eap->arg;
8111 char_u *p;
8112
Keith Thompson184f71c2024-01-04 21:19:04 +01008113 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008114 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008115 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008116 return;
8117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 w = getdigits(&arg);
8119 arg = skipwhite(arg);
8120 p = arg;
8121 h = getdigits(&arg);
8122 if (*p != NUL && *arg == NUL)
8123 set_shellsize(w, h, TRUE);
8124 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008125 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126}
8127
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008129ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130{
8131 int xchar = NUL;
8132 char_u *p;
8133
8134 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8135 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008136 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 if (eap->arg[1] == NUL)
8138 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008139 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 return;
8141 }
8142 xchar = eap->arg[1];
8143 p = eap->arg + 2;
8144 }
8145 else
8146 p = eap->arg + 1;
8147
Bram Moolenaar63b91732021-08-05 20:40:03 +02008148 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008150 if (*p != NUL && *p != (
8151#ifdef FEAT_EVAL
8152 in_vim9script() ? '#' :
8153#endif
8154 '"')
8155 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008156 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008157 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008159 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008160 postponed_split_flags = cmdmod.cmod_split;
8161 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8163 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008164 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 }
8166}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167
Bram Moolenaar843ee412004-06-30 16:16:41 +00008168#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169/*
8170 * ":winpos".
8171 */
8172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008173ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174{
8175 int x, y;
8176 char_u *arg = eap->arg;
8177 char_u *p;
8178
8179 if (*arg == NUL)
8180 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008181# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008182# ifdef VIMDLL
8183 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8184 mch_get_winpos(&x, &y) != FAIL)
8185# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008187# else
8188 if (mch_get_winpos(&x, &y) != FAIL)
8189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {
8191 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008192 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 }
8194 else
8195# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008196 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 }
8198 else
8199 {
8200 x = getdigits(&arg);
8201 arg = skipwhite(arg);
8202 p = arg;
8203 y = getdigits(&arg);
8204 if (*p == NUL || *arg != NUL)
8205 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008206 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 return;
8208 }
8209# ifdef FEAT_GUI
8210 if (gui.in_use)
8211 gui_mch_set_winpos(x, y);
8212 else if (gui.starting)
8213 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008214 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 gui_win_x = x;
8216 gui_win_y = y;
8217 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008218# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 else
8220# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008221# endif
8222# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008223 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224# endif
8225# ifdef HAVE_TGETENT
8226 if (*T_CWP)
8227 term_set_winpos(x, y);
8228# endif
8229 }
8230}
8231#endif
8232
8233/*
8234 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8235 */
8236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008237ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238{
8239 oparg_T oa;
8240
8241 clear_oparg(&oa);
8242 oa.regname = eap->regname;
8243 oa.start.lnum = eap->line1;
8244 oa.end.lnum = eap->line2;
8245 oa.line_count = eap->line2 - eap->line1 + 1;
8246 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008248 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {
8250 setpcmark();
8251 curwin->w_cursor.lnum = eap->line1;
8252 beginline(BL_SOL | BL_FIX);
8253 }
8254
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008255 if (VIsual_active)
8256 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008257
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 switch (eap->cmdidx)
8259 {
8260 case CMD_delete:
8261 oa.op_type = OP_DELETE;
8262 op_delete(&oa);
8263 break;
8264
8265 case CMD_yank:
8266 oa.op_type = OP_YANK;
8267 (void)op_yank(&oa, FALSE, TRUE);
8268 break;
8269
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008270 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008271 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008273 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8274#else
8275 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008277 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 oa.op_type = OP_RSHIFT;
8279 else
8280 oa.op_type = OP_LSHIFT;
8281 op_shift(&oa, FALSE, eap->amount);
8282 break;
8283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008285 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286}
8287
8288/*
8289 * ":put".
8290 */
8291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008292ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008294 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 if (eap->line2 == 0)
8296 {
8297 eap->line2 = 1;
8298 eap->forceit = TRUE;
8299 }
8300 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008301 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008302 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008303 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304}
8305
8306/*
8307 * Handle ":copy" and ":move".
8308 */
8309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008310ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311{
8312 long n;
8313
Bram Moolenaar491799b2020-08-01 19:23:43 +02008314#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008315 if (not_in_vim9(eap) == FAIL)
8316 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008317#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008318 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008319 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {
8321 eap->nextcmd = NULL;
8322 return;
8323 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008324 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
8326 /*
8327 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8328 */
8329 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8330 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008331 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 return;
8333 }
8334
8335 if (eap->cmdidx == CMD_move)
8336 {
8337 if (do_move(eap->line1, eap->line2, n) == FAIL)
8338 return;
8339 }
8340 else
8341 ex_copy(eap->line1, eap->line2, n);
8342 u_clearline();
8343 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008344 ex_may_print(eap);
8345}
8346
8347/*
8348 * Print the current line if flags were given to the Ex command.
8349 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008350 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008351ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008352{
8353 if (eap->flags != 0)
8354 {
8355 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8356 (eap->flags & EXFLAG_LIST));
8357 ex_no_reprint = TRUE;
8358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359}
8360
8361/*
8362 * ":smagic" and ":snomagic".
8363 */
8364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008365ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008367 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008369 magic_overruled = eap->cmdidx == CMD_smagic
8370 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008371 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008372 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373}
8374
8375/*
8376 * ":join".
8377 */
8378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008379ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380{
8381 curwin->w_cursor.lnum = eap->line1;
8382 if (eap->line1 == eap->line2)
8383 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008384 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 return;
8386 if (eap->line2 == curbuf->b_ml.ml_line_count)
8387 {
8388 beep_flush();
8389 return;
8390 }
8391 ++eap->line2;
8392 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008393 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008395 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396}
8397
8398/*
8399 * ":[addr]@r" or ":[addr]*r": execute register
8400 */
8401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008402ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008405 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406
8407 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008408 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409
8410#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008411 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412#endif
8413
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008414 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 c = *eap->arg;
8416 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8417 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008418 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008419 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8420 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008423 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008426 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008428 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008430 /*
8431 * Execute from the typeahead buffer.
8432 * Continue until the stuff buffer is empty and all added characters
8433 * have been consumed.
8434 */
8435 while (!stuff_empty() || typebuf.tb_len > prev_len)
8436 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8437
8438 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439}
8440
8441/*
8442 * ":!".
8443 */
8444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008445ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446{
8447 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8448}
8449
8450/*
8451 * ":undo".
8452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008454ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008456 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008457 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008458 else
8459 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460}
8461
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008462#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008464ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008465{
8466 char_u hash[UNDO_HASH_SIZE];
8467
8468 u_compute_hash(hash);
8469 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8470}
8471
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008473ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008474{
8475 char_u hash[UNDO_HASH_SIZE];
8476
8477 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008478 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008479}
8480#endif
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482/*
8483 * ":redo".
8484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008486ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487{
8488 u_redo(1);
8489}
8490
8491/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008492 * ":earlier" and ":later".
8493 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008495ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008496{
8497 long count = 0;
8498 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008499 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008500 char_u *p = eap->arg;
8501
8502 if (*p == NUL)
8503 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008504 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008505 {
8506 count = getdigits(&p);
8507 switch (*p)
8508 {
8509 case 's': ++p; sec = TRUE; break;
8510 case 'm': ++p; sec = TRUE; count *= 60; break;
8511 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008512 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8513 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008514 }
8515 }
8516
8517 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008518 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008519 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008520 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8521 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008522}
8523
8524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 * ":redir": start/stop redirection.
8526 */
8527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008528ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529{
8530 char *mode;
8531 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008532 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533
Bram Moolenaarba768492016-07-08 15:32:54 +02008534#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008535 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008536 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008537 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008538 return;
8539 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008540#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008541
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 if (STRICMP(eap->arg, "END") == 0)
8543 close_redir();
8544 else
8545 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008546 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008548 ++arg;
8549 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008551 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 mode = "a";
8553 }
8554 else
8555 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008556 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
8558 close_redir();
8559
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008560 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008561 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 if (fname == NULL)
8563 return;
8564#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008565 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 {
8567 char_u *browseFile;
8568
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008569 browseFile = do_browse(BROWSE_SAVE,
8570 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008571 fname, NULL, NULL,
8572 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008574 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 vim_free(fname);
8576 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008577 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 }
8579#endif
8580
8581 redir_fd = open_exfile(fname, eap->forceit, mode);
8582 vim_free(fname);
8583 }
8584#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008585 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008587 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008589 ++arg;
8590 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008592 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008593 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008595 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008597 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008598 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008599 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008600 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008602 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008603 if (*arg == '>')
8604 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008605 // Make register empty when not using @A-@Z and the
8606 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008607 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008608 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008610 }
8611 if (*arg != NUL)
8612 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008613 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008614 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008615 }
8616 }
8617 else if (*arg == '=' && arg[1] == '>')
8618 {
8619 int append;
8620
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008621 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008622 close_redir();
8623 arg += 2;
8624
8625 if (*arg == '>')
8626 {
8627 ++arg;
8628 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 }
8630 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008631 append = FALSE;
8632
8633 if (var_redir_start(skipwhite(arg), append) == OK)
8634 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 }
8636#endif
8637
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008638 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008641 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008643
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008644 // Make sure redirection is not off. Can happen for cmdline completion
8645 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008646 if (redir_fd != NULL
8647#ifdef FEAT_EVAL
8648 || redir_reg || redir_vname
8649#endif
8650 )
8651 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652}
8653
8654/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008655 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008657 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008658ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008660 redraw_cmd(eap->forceit);
8661}
8662
8663/*
8664 * ":redraw": force redraw, with clear if "clear" is TRUE.
8665 */
8666 void
8667redraw_cmd(int clear)
8668{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008669 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008671
8672 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008674
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008675 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008677 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 if (need_maketitle)
8679 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008680#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8681# ifdef VIMDLL
8682 if (!gui.in_use)
8683# endif
8684 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008685#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008686 RedrawingDisabled = save_RedrawingDisabled;
8687 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
Bram Moolenaar5017c662022-04-07 18:06:08 +01008689 // After drawing the statusline screen_attr may still be set.
8690 screen_stop_highlight();
8691
Bram Moolenaara2a89732022-08-31 14:46:18 +01008692 // Reset msg_didout, so that a message that's there is overwritten.
8693 msg_didout = FALSE;
8694 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008696 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008697 need_wait_return = FALSE;
8698
Bram Moolenaara653e532022-04-19 11:38:24 +01008699 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008700 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008701 redrawcmdline();
8702
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 out_flush();
8704}
8705
8706/*
8707 * ":redrawstatus": force redraw of status line(s)
8708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008710ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 if (eap->forceit)
8713 status_redraw_all();
8714 else
8715 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008716 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008717 return; // redraw later
8718
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008719 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008720 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008721
8722 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008723 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008724
zeertzjq320d9102022-09-20 17:12:13 +01008725 if (State & MODE_CMDLINE)
8726 redraw_statuslines();
8727 else
8728 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008729 RedrawingDisabled = save_RedrawingDisabled;
8730 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732}
8733
Bram Moolenaare12bab32019-01-08 22:02:56 +01008734/*
8735 * ":redrawtabline": force redraw of the tabline
8736 */
8737 static void
8738ex_redrawtabline(exarg_T *eap UNUSED)
8739{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008740 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008741 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008742
8743 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008744 p_lz = FALSE;
8745
8746 draw_tabline();
8747
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008748 RedrawingDisabled = save_RedrawingDisabled;
8749 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008750 out_flush();
8751}
8752
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008754close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755{
8756 if (redir_fd != NULL)
8757 {
8758 fclose(redir_fd);
8759 redir_fd = NULL;
8760 }
8761#ifdef FEAT_EVAL
8762 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008763 if (redir_vname)
8764 {
8765 var_redir_stop();
8766 redir_vname = 0;
8767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768#endif
8769}
8770
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008771#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008772 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008773vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008774{
8775 if (vim_mkdir(name, prot) != 0)
8776 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008777 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008778 return FAIL;
8779 }
8780 return OK;
8781}
8782#endif
8783
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784/*
8785 * Open a file for writing for an Ex command, with some checks.
8786 * Return file descriptor, or NULL on failure.
8787 */
8788 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008789open_exfile(
8790 char_u *fname,
8791 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008792 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
8794 FILE *fd;
8795
8796#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008797 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 if (mch_isdir(fname))
8799 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008800 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 return NULL;
8802 }
8803#endif
8804 if (!forceit && *mode != 'a' && vim_fexists(fname))
8805 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008806 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 return NULL;
8808 }
8809
8810 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008811 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812
8813 return fd;
8814}
8815
8816/*
8817 * ":mark" and ":k".
8818 */
8819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008820ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
8822 pos_T pos;
8823
Bram Moolenaar10b94212021-02-19 21:42:57 +01008824#ifdef FEAT_EVAL
8825 if (not_in_vim9(eap) == FAIL)
8826 return;
8827#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008828 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008830 emsg(_(e_argument_required));
8831 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008833
8834 if (eap->arg[1] != NUL) // more than one character?
8835 {
8836 semsg(_(e_trailing_characters_str), eap->arg);
8837 return;
8838 }
8839
8840 pos = curwin->w_cursor; // save curwin->w_cursor
8841 curwin->w_cursor.lnum = eap->line2;
8842 beginline(BL_WHITE | BL_FIX);
8843 if (setmark(*eap->arg) == FAIL) // set mark
8844 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
8845 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846}
8847
8848/*
8849 * Update w_topline, w_leftcol and the cursor position.
8850 */
8851 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008852update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008854 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 update_topline();
8856 if (!curwin->w_p_wrap)
8857 validate_cursor();
8858 update_curswant();
8859}
8860
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008862 * Save the current State and go to Normal mode.
8863 * Return TRUE if the typeahead could be saved.
8864 */
8865 int
8866save_current_state(save_state_T *sst)
8867{
8868 sst->save_msg_scroll = msg_scroll;
8869 sst->save_restart_edit = restart_edit;
8870 sst->save_msg_didout = msg_didout;
8871 sst->save_State = State;
8872 sst->save_insertmode = p_im;
8873 sst->save_finish_op = finish_op;
8874 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008875 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008876 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008877
Bram Moolenaar4324d872020-12-01 20:12:24 +01008878 msg_scroll = FALSE; // no msg scrolling in Normal mode
8879 restart_edit = 0; // don't go to Insert mode
8880 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008881
Bram Moolenaara452b802020-12-01 21:47:59 +01008882 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008883 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008884
8885 /*
8886 * Save the current typeahead. This is required to allow using ":normal"
8887 * from an event handler and makes sure we don't hang when the argument
8888 * ends with half a command.
8889 */
8890 save_typeahead(&sst->tabuf);
8891 return sst->tabuf.typebuf_valid;
8892}
8893
8894 void
8895restore_current_state(save_state_T *sst)
8896{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008897 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008898 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008899
8900 msg_scroll = sst->save_msg_scroll;
8901 restart_edit = sst->save_restart_edit;
8902 p_im = sst->save_insertmode;
8903 finish_op = sst->save_finish_op;
8904 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008905 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008906 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008907 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008908 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008909
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008910 // Restore the state (needed when called from a function executed for
8911 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008912 State = sst->save_State;
8913#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008914 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008915#endif
8916}
8917
8918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 * ":normal[!] {commands}": Execute normal mode commands.
8920 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008921 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008922ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008924 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 char_u *arg = NULL;
8926 int l;
8927 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008929 if (ex_normal_lock > 0)
8930 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008931 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008932 return;
8933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 if (ex_normal_busy >= p_mmd)
8935 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008936 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 return;
8938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 /*
8941 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8942 * this for the K_SPECIAL leading byte, otherwise special keys will not
8943 * work.
8944 */
8945 if (has_mbyte)
8946 {
8947 int len = 0;
8948
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008949 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 for (p = eap->arg; *p != NUL; ++p)
8951 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008952#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008953 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008955#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008956 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008957 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008958#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 )
8962 len += 2;
8963 }
8964 if (len > 0)
8965 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008966 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 if (arg != NULL)
8968 {
8969 len = 0;
8970 for (p = eap->arg; *p != NUL; ++p)
8971 {
8972 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008973#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 if (*p == CSI)
8975 {
8976 arg[len++] = KS_EXTRA;
8977 arg[len++] = (int)KE_CSI;
8978 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008979#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008980 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 {
8982 arg[len++] = *++p;
8983 if (*p == K_SPECIAL)
8984 {
8985 arg[len++] = KS_SPECIAL;
8986 arg[len++] = KE_FILLER;
8987 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008988#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 else if (*p == CSI)
8990 {
8991 arg[len++] = KS_EXTRA;
8992 arg[len++] = (int)KE_CSI;
8993 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 }
8996 arg[len] = NUL;
8997 }
8998 }
8999 }
9000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009002 ++ex_normal_busy;
9003 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 {
9005 /*
9006 * Repeat the :normal command for each line in the range. When no
9007 * range given, execute it just once, without positioning the cursor
9008 * first.
9009 */
9010 do
9011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 if (eap->addr_count != 0)
9013 {
9014 curwin->w_cursor.lnum = eap->line1++;
9015 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02009016 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 }
9018
Bram Moolenaar13505972019-01-24 15:04:48 +01009019 exec_normal_cmd(arg != NULL
9020 ? arg
9021 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 }
9023 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9024 }
9025
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009026 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 update_topline_cursor();
9028
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009029 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009031 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009032#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009033 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009034#endif
9035
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037}
9038
9039/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009040 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 */
9042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009043ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009045 if (eap->forceit)
9046 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009047 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009048 if (!curwin->w_cursor.lnum)
9049 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009050 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009051 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009052#ifdef FEAT_TERMINAL
9053 // Ignore this when running in an active terminal.
9054 if (term_job_running(curbuf->b_term))
9055 return;
9056#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009057
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009058 // Ignore the command when already in Insert mode. Inserting an
9059 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009060 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009061 return;
9062
Bram Moolenaar64969662005-12-14 21:59:55 +00009063 if (eap->cmdidx == CMD_startinsert)
9064 restart_edit = 'a';
9065 else if (eap->cmdidx == CMD_startreplace)
9066 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009068 restart_edit = 'V';
9069
9070 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009072 if (eap->cmdidx == CMD_startinsert)
9073 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009074 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009076
9077 if (VIsual_active)
9078 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079}
9080
9081/*
9082 * ":stopinsert"
9083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009085ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086{
9087 restart_edit = 0;
9088 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009089 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009092/*
9093 * Execute normal mode command "cmd".
9094 * "remap" can be REMAP_NONE or REMAP_YES.
9095 */
9096 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009097exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009098{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009099 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009100 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009101 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009102}
Bram Moolenaar25281632016-01-21 23:32:32 +01009103
Bram Moolenaar25281632016-01-21 23:32:32 +01009104/*
9105 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009106 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009107 */
9108 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009109exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009110{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009111 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009112 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009113
Bram Moolenaar905dd902019-04-07 14:21:47 +02009114 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9115 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009116 clear_oparg(&oa);
9117 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009118 while ((!stuff_empty()
9119 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009120 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009121 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009122 {
9123 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009124#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009125 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009126 && oa.op_type == OP_NOP && oa.regname == NUL
9127 && !VIsual_active)
9128 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009129 // If terminal_loop() returns OK we got a key that is handled
9130 // in Normal model. With FAIL we first need to position the
9131 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009132 if (terminal_loop(TRUE) == OK)
9133 normal_cmd(&oa, TRUE);
9134 }
9135 else
9136#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009137 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009138 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009139 }
9140}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009141
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142#ifdef FEAT_FIND_ID
9143 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009144ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9147 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009148 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149}
9150
Bram Moolenaar4033c552017-09-16 20:54:51 +02009151#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152/*
9153 * ":psearch"
9154 */
9155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009156ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
9158 g_do_tagpreview = p_pvh;
9159 ex_findpat(eap);
9160 g_do_tagpreview = 0;
9161}
9162#endif
9163
9164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009165ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166{
9167 int whole = TRUE;
9168 long n;
9169 char_u *p;
9170 int action;
9171
9172 switch (cmdnames[eap->cmdidx].cmd_name[2])
9173 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009174 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9176 action = ACTION_GOTO;
9177 else
9178 action = ACTION_SHOW;
9179 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009180 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 action = ACTION_SHOW_ALL;
9182 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009183 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 action = ACTION_GOTO;
9185 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009186 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 action = ACTION_SPLIT;
9188 break;
9189 }
9190
9191 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009192 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 {
9194 n = getdigits(&eap->arg);
9195 eap->arg = skipwhite(eap->arg);
9196 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009197 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 {
9199 whole = FALSE;
9200 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009201 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 if (*p)
9203 {
9204 *p++ = NUL;
9205 p = skipwhite(p);
9206
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009207 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009208 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009209 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009211 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 }
9213 }
9214 if (!eap->skip)
9215 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9216 whole, !eap->forceit,
9217 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009218 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219}
9220#endif
9221
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222
Bram Moolenaar4033c552017-09-16 20:54:51 +02009223#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224/*
9225 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9226 */
9227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009228ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009230 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9232}
9233
9234/*
9235 * ":pedit"
9236 */
9237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009238ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239{
9240 win_T *curwin_save = curwin;
9241
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009242 if (ERROR_IF_ANY_POPUP_WINDOW)
9243 return;
9244
Bram Moolenaar026587b2019-08-17 15:08:00 +02009245 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009247 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009248
Bram Moolenaar026587b2019-08-17 15:08:00 +02009249 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009251
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 if (curwin != curwin_save && win_valid(curwin_save))
9253 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009254 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009256 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 win_enter(curwin_save, TRUE);
9258 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009259# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009260 else if (WIN_IS_POPUP(curwin))
9261 {
9262 // can't keep focus in popup window
9263 win_enter(firstwin, TRUE);
9264 }
9265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 g_do_tagpreview = 0;
9267}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269
9270/*
9271 * ":stag", ":stselect" and ":stjump".
9272 */
9273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009274ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
9276 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009277 postponed_split_flags = cmdmod.cmod_split;
9278 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9280 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009281 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283
9284/*
9285 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9286 */
9287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009288ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289{
9290 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9291}
9292
9293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009294ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295{
9296 int cmd;
9297
9298 switch (name[1])
9299 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009300 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009302 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009304 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009306 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009308 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009310 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009312 case 'f': // ":tfirst"
9313 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009315 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009317 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009319 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009321 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 return;
9323 }
9324#endif
9325 cmd = DT_TAG;
9326 break;
9327 }
9328
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009329 if (name[0] == 'l')
9330 {
9331#ifndef FEAT_QUICKFIX
9332 ex_ni(eap);
9333 return;
9334#else
9335 cmd = DT_LTAG;
9336#endif
9337 }
9338
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9340 eap->forceit, TRUE);
9341}
9342
John Marriotted908f72024-04-18 22:46:56 +02009343enum {
9344 SPEC_PERC = 0,
9345 SPEC_HASH,
9346 SPEC_CWORD, // cursor word
9347 SPEC_CCWORD, // cursor WORD
9348 SPEC_CEXPR, // expr under cursor
9349 SPEC_CFILE, // cursor path name
9350 SPEC_SFILE, // ":so" file name
9351 SPEC_SLNUM, // ":so" file line number
9352 SPEC_STACK, // call stack
9353 SPEC_SCRIPT, // script file name
9354 SPEC_AFILE, // autocommand file name
9355 SPEC_ABUF, // autocommand buffer number
9356 SPEC_AMATCH, // autocommand match name
9357 SPEC_SFLNUM, // script file line number
9358 SPEC_SID // script ID: <SNR>123_
9359#ifdef FEAT_CLIENTSERVER
9360 , SPEC_CLIENT
9361#endif
9362};
9363
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009365 * Check "str" for starting with a special cmdline variable.
9366 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9367 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9368 */
9369 int
Mike Williams51024bb2024-05-30 07:46:30 +02009370find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009371{
John Marriotted908f72024-04-18 22:46:56 +02009372 // must be sorted by the 'value' field because it is used by bsearch()!
9373 static keyvalue_T spec_str_tab[] = {
9374 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9375 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9376 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9377 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9378 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9379 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9380 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009381#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009382 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009383#endif
John Marriotted908f72024-04-18 22:46:56 +02009384 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9385 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9386 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9387 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9388 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9389 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009390 };
John Marriotted908f72024-04-18 22:46:56 +02009391 keyvalue_T target;
9392 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009393
John Marriotted908f72024-04-18 22:46:56 +02009394 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009395 {
John Marriotted908f72024-04-18 22:46:56 +02009396 case '%':
9397 *usedlen = 1;
9398 return SPEC_PERC;
9399
9400 case '#':
9401 *usedlen = 1;
9402 return SPEC_HASH;
9403
9404 case '<':
9405 target.key = 0;
9406 target.value = (char *)src + 1; // skip over '<'
9407 target.length = 0; // not used, see cmp_keyvalue_value_n()
9408
9409 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab, ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]), cmp_keyvalue_value_n);
9410 if (entry == NULL)
9411 return -1;
9412
9413 *usedlen = entry->length + 1;
9414 return entry->key;
9415
9416 default:
9417 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009418 }
John Marriotted908f72024-04-18 22:46:56 +02009419
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009420 return -1;
9421}
9422
9423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 * Evaluate cmdline variables.
9425 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009426 * change "%" to curbuf->b_ffname
9427 * "#" to curwin->w_alt_fnum
9428 * "%%" to curwin->w_alt_fnum in Vim9 script
9429 * "<cword>" to word under the cursor
9430 * "<cWORD>" to WORD under the cursor
9431 * "<cexpr>" to C-expression under the cursor
9432 * "<cfile>" to path name under the cursor
9433 * "<sfile>" to sourced file name
9434 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009435 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009436 * "<slnum>" to sourced file line number
9437 * "<afile>" to file name for autocommand
9438 * "<abuf>" to buffer number for autocommand
9439 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 *
9441 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9442 * "" for error without a message) and NULL is returned.
9443 * Returns an allocated string if a valid match was found.
9444 * Returns NULL if no match was found. "usedlen" then still contains the
9445 * number of characters to skip.
9446 */
9447 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009448eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009449 char_u *src, // pointer into commandline
9450 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009451 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009452 linenr_T *lnump, // line number for :e command, or NULL
9453 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009454 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009455 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009456 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457{
9458 int i;
9459 char_u *s;
9460 char_u *result;
9461 char_u *resultbuf = NULL;
9462 int resultlen;
9463 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009464 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009466 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
9470 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009471 if (escaped != NULL)
9472 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473
9474 /*
9475 * Check if there is something to do.
9476 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009477 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009478 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 {
9480 *usedlen = 1;
9481 return NULL;
9482 }
9483
9484 /*
9485 * Skip when preceded with a backslash "\%" and "\#".
9486 * Note: In "\\%" the % is also not recognized!
9487 */
9488 if (src > srcstart && src[-1] == '\\')
9489 {
9490 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009491 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 return NULL;
9493 }
9494
9495 /*
9496 * word or WORD under cursor
9497 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009498 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9499 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009501 resultlen = find_ident_under_cursor(&result,
9502 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9503 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9504 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 if (resultlen == 0)
9506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009507 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 return NULL;
9509 }
9510 }
9511
9512 /*
9513 * '#': Alternate file name
9514 * '%': Current file name
9515 * File name under the cursor
9516 * File name for autocommand
9517 * and following modifiers
9518 */
9519 else
9520 {
Mike Williams51024bb2024-05-30 07:46:30 +02009521 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009522
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 switch (spec_idx)
9524 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009525 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009526#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009527 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009530 // '%': current file
9531 if (curbuf->b_fname == NULL)
9532 {
9533 result = (char_u *)"";
9534 valid = 0; // Must have ":p:h" to be valid
9535 }
9536 else
9537 {
9538 result = curbuf->b_fname;
9539 tilde_file = STRCMP(result, "~") == 0;
9540 }
9541 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009543#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009544 // "%%" alternate file
9545 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009546#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009547 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009548 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009549 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009551 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 result = arg_all();
9553 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009554 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009555 if (escaped != NULL)
9556 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 break;
9559 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009560 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009561 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009562 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009564 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009565 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009566 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009567 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009569 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009571 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009572 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009573 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009574 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009575 return NULL;
9576 }
9577#ifdef FEAT_EVAL
9578 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9579 (long)i);
9580 if (result == NULL)
9581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009582 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009583 return NULL;
9584 }
9585#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009586 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 }
9590 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009591 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009592 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9593 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009594 buf = buflist_findnr(i);
9595 if (buf == NULL)
9596 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009597 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009598 return NULL;
9599 }
9600 if (lnump != NULL)
9601 *lnump = ECMD_LAST;
9602 if (buf->b_fname == NULL)
9603 {
9604 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009605 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009606 }
9607 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009608 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009609 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009610 tilde_file = STRCMP(result, "~") == 0;
9611 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 break;
9614
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009615 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009616 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 if (result == NULL)
9618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009619 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 return NULL;
9621 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009622 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009625 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009627 if (result != NULL && !autocmd_fname_full)
9628 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009629 // Still need to turn the fname into a full path. It is
9630 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009631 autocmd_fname_full = TRUE;
9632 result = FullName_save(autocmd_fname, FALSE);
9633 vim_free(autocmd_fname);
9634 autocmd_fname = result;
9635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 if (result == NULL)
9637 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009638 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 return NULL;
9640 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009641 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 break;
9643
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009644 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 if (autocmd_bufnr <= 0)
9646 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009647 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 return NULL;
9649 }
9650 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9651 result = strbuf;
9652 break;
9653
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009654 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 result = autocmd_match;
9656 if (result == NULL)
9657 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009658 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 return NULL;
9660 }
9661 break;
9662
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009663 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009664 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 if (result == NULL)
9666 {
LemonBoy6013d002022-04-09 21:42:10 +01009667 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9668 return NULL;
9669 }
9670 resultbuf = result; // remember allocated string
9671 break;
9672 case SPEC_STACK: // call stack
9673 result = estack_sfile(ESTACK_STACK);
9674 if (result == NULL)
9675 {
9676 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9677 return NULL;
9678 }
9679 resultbuf = result; // remember allocated string
9680 break;
9681 case SPEC_SCRIPT: // script file name
9682 result = estack_sfile(ESTACK_SCRIPT);
9683 if (result == NULL)
9684 {
9685 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 return NULL;
9687 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009688 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009691 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009692 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009693 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009694 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009695 return NULL;
9696 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009697 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009698 result = strbuf;
9699 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009700
9701#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009702 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009703 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009704 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009705 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009706 return NULL;
9707 }
9708 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009709 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009710 result = strbuf;
9711 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009712
Bram Moolenaar90944302020-08-01 20:45:11 +02009713 case SPEC_SID:
9714 if (current_sctx.sc_sid <= 0)
9715 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009716 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009717 return NULL;
9718 }
9719 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9720 result = strbuf;
9721 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009722#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009723
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009724#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009725 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009726 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9727 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 result = strbuf;
9729 break;
9730#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009731
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009732 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009733 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009734 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 }
9736
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009737 resultlen = (int)STRLEN(result); // length of new string
9738 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 {
9740 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 resultlen = (int)(s - result);
9743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 else if (!skip_mod)
9745 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009746 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 &resultlen);
9748 if (result == NULL)
9749 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009750 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 return NULL;
9752 }
9753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 }
9755
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009756 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009758 if (empty_is_error)
9759 {
9760 if (valid != VALID_HEAD + VALID_PATH)
9761 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9762 else
9763 *errormsg = _(e_evaluates_to_an_empty_string);
9764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 result = NULL;
9766 }
9767 else
9768 result = vim_strnsave(result, resultlen);
9769 vim_free(resultbuf);
9770 return result;
9771}
9772
9773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 * Expand the <sfile> string in "arg".
9775 *
9776 * Returns an allocated string, or NULL for any error.
9777 */
9778 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009779expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009781 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +02009783 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +02009785 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +02009787 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +02009788 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 char_u *p;
9790
John Marriotted908f72024-04-18 22:46:56 +02009791 resultlen = STRLEN(arg);
9792 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 if (result == NULL)
9794 return NULL;
9795
9796 for (p = result; *p; )
9797 {
9798 if (STRNCMP(p, "<sfile>", 7) != 0)
9799 ++p;
9800 else
9801 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009802 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009803 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 if (errormsg != NULL)
9805 {
9806 if (*errormsg)
9807 emsg(errormsg);
9808 vim_free(result);
9809 return NULL;
9810 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009811 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 {
9813 p += srclen;
9814 continue;
9815 }
John Marriotted908f72024-04-18 22:46:56 +02009816 repllen = STRLEN(repl);
9817 resultlen += repllen - srclen;
9818 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 if (newres == NULL)
9820 {
9821 vim_free(repl);
9822 vim_free(result);
9823 return NULL;
9824 }
John Marriotted908f72024-04-18 22:46:56 +02009825 len = p - result;
9826 mch_memmove(newres, result, len);
9827 STRCPY(newres + len, repl);
9828 len += repllen;
9829 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 vim_free(repl);
9831 vim_free(result);
9832 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009833 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 }
9835 }
9836
9837 return result;
9838}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009841/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009842 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009843 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009846dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 if (fname == NULL)
9849 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009850 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851}
9852#endif
9853
9854/*
9855 * ":behave {mswin,xterm}"
9856 */
9857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009858ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859{
9860 if (STRCMP(eap->arg, "mswin") == 0)
9861 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009862 set_option_value_give_err((char_u *)"selection",
9863 0L, (char_u *)"exclusive", 0);
9864 set_option_value_give_err((char_u *)"selectmode",
9865 0L, (char_u *)"mouse,key", 0);
9866 set_option_value_give_err((char_u *)"mousemodel",
9867 0L, (char_u *)"popup", 0);
9868 set_option_value_give_err((char_u *)"keymodel",
9869 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 }
9871 else if (STRCMP(eap->arg, "xterm") == 0)
9872 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009873 set_option_value_give_err((char_u *)"selection",
9874 0L, (char_u *)"inclusive", 0);
9875 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9876 set_option_value_give_err((char_u *)"mousemodel",
9877 0L, (char_u *)"extend", 0);
9878 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 }
9880 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009881 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882}
9883
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884static int filetype_detect = FALSE;
9885static int filetype_plugin = FALSE;
9886static int filetype_indent = FALSE;
9887
9888/*
9889 * ":filetype [plugin] [indent] {on,off,detect}"
9890 * on: Load the filetype.vim file to install autocommands for file types.
9891 * off: Load the ftoff.vim file to remove all autocommands for file types.
9892 * plugin on: load filetype.vim and ftplugin.vim
9893 * plugin off: load ftplugof.vim
9894 * indent on: load filetype.vim and indent.vim
9895 * indent off: load indoff.vim
9896 */
9897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009898ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899{
9900 char_u *arg = eap->arg;
9901 int plugin = FALSE;
9902 int indent = FALSE;
9903
9904 if (*eap->arg == NUL)
9905 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009906 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009907 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 filetype_detect ? "ON" : "OFF",
9909 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9910 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9911 return;
9912 }
9913
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009914 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 for (;;)
9916 {
9917 if (STRNCMP(arg, "plugin", 6) == 0)
9918 {
9919 plugin = TRUE;
9920 arg = skipwhite(arg + 6);
9921 continue;
9922 }
9923 if (STRNCMP(arg, "indent", 6) == 0)
9924 {
9925 indent = TRUE;
9926 arg = skipwhite(arg + 6);
9927 continue;
9928 }
9929 break;
9930 }
9931 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9932 {
9933 if (*arg == 'o' || !filetype_detect)
9934 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009935 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 filetype_detect = TRUE;
9937 if (plugin)
9938 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009939 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 filetype_plugin = TRUE;
9941 }
9942 if (indent)
9943 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009944 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 filetype_indent = TRUE;
9946 }
9947 }
9948 if (*arg == 'd')
9949 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009950 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009951 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 }
9953 }
9954 else if (STRCMP(arg, "off") == 0)
9955 {
9956 if (plugin || indent)
9957 {
9958 if (plugin)
9959 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009960 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 filetype_plugin = FALSE;
9962 }
9963 if (indent)
9964 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009965 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 filetype_indent = FALSE;
9967 }
9968 }
9969 else
9970 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009971 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 filetype_detect = FALSE;
9973 }
9974 }
9975 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009976 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977}
9978
9979/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009980 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 */
9982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009983ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984{
zeertzjq5bf6c212024-03-31 18:41:27 +02009985 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009986 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009987
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009988 char_u *arg = eap->arg;
9989 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9990 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009991
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009992 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9993 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +02009994 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009998ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999{
10000#ifdef FEAT_DIGRAPHS
10001 if (*eap->arg != NUL)
10002 putdigraph(eap->arg);
10003 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010004 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005#else
mityu61065042021-07-19 20:07:21 +020010006 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007#endif
10008}
10009
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010010#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10011 void
10012set_no_hlsearch(int flag)
10013{
10014 no_hlsearch = flag;
10015# ifdef FEAT_EVAL
10016 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10017# endif
10018}
10019
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020/*
10021 * ":nohlsearch"
10022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010024ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010026 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +010010027 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029#endif
10030
10031#ifdef FEAT_CRYPT
10032/*
10033 * ":X": Get crypt key
10034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010036ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010038 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010039 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040}
10041#endif
10042
10043#ifdef FEAT_FOLDING
10044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010045ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046{
10047 if (foldManualAllowed(TRUE))
10048 foldCreate(eap->line1, eap->line2);
10049}
10050
10051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010052ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053{
10054 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10055 eap->forceit, FALSE);
10056}
10057
10058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010059ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060{
10061 linenr_T lnum;
10062
Bram Moolenaar4facea32019-10-12 20:17:40 +020010063# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010064 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010065# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010066
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010067 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10069 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10070 ml_setmarked(lnum);
10071
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010072 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010074 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010075# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010076 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078}
10079#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010081#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010082/*
10083 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10084 * instead of a quickfix command.
10085 */
10086 int
10087is_loclist_cmd(int cmdidx)
10088{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010089 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010090 return FALSE;
10091 return cmdnames[cmdidx].cmd_name[0] == 'l';
10092}
10093#endif
10094
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010095 int
10096get_pressedreturn(void)
10097{
10098 return ex_pressedreturn;
10099}
10100
10101 void
10102set_pressedreturn(int val)
10103{
10104 ex_pressedreturn = val;
10105}