blob: 53f9d1af2c10643c95897231278906059cc5a241 [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
zeertzjqd9be94c2024-07-14 10:20:20 +02002863 // range to after the modifiers. The command will be "'<,'>cmdmod cmd",
2864 // parse "cmdmod cmd" and then put back "'<,'>" before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002865 eap->cmd += 5;
2866 cmd_start = eap->cmd;
2867 has_visual_range = TRUE;
2868 }
2869
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002870 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002871 for (;;)
2872 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002873 char_u *p;
2874
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002876 {
2877 if (*eap->cmd == ':')
2878 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002879 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002880 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002881
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002882 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002883 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002884 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2885 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2887 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002888 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002889 if (!skip_only)
2890 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002891 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002892 }
2893
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002894 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002895 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002896 {
2897 // a comment ends at a NL
2898 if (eap->nextcmd == NULL)
2899 {
2900 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2901 if (eap->nextcmd != NULL)
2902 ++eap->nextcmd;
2903 }
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002904 if (vim9script)
2905 {
2906 if (has_cmdmod(cmod, FALSE))
2907 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002908#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002909 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2910 && eap->cmd[2] != '{')
2911 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002912#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002913 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002915 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002916 if (*eap->cmd == NUL)
2917 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002918 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002919 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002920 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002921 if (vim9script && has_cmdmod(cmod, FALSE))
2922 *errormsg = _(e_command_modifier_without_command);
2923 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002924 return FAIL;
2925 }
2926
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002927 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002928
2929 // In Vim9 script a variable can shadow a command modifier:
2930 // verbose = 123
2931 // verbose += 123
2932 // silent! verbose = func()
2933 // verbose.member = 2
2934 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002935 // But not:
2936 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002937 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002938 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002939 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002940
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002941 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002942 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002943 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002944 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2945 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002946 break;
2947 }
2948
Bram Moolenaareffed932018-08-14 13:38:17 +02002949 switch (*p)
2950 {
John Marriotted908f72024-04-18 22:46:56 +02002951 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002952 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002953 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002954 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002955 continue;
2956
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002957 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002959 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002960 continue;
2961 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002962 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 {
2964#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002965 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002966#endif
2967 continue;
2968 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002969 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002970 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002971 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002972 continue;
2973
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002974 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002975 break;
2976#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002977 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002978#endif
2979 continue;
2980
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002981 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002982 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002983 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002984 continue;
2985 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002986 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 continue;
2990 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002991 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002992 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002993 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 continue;
2995 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002996 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002998 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 continue;
3000
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003001 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003003 char_u *reg_pat;
3004 char_u *nulp = NULL;
3005 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003006
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003007 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003008 || *p == NUL
3009 || (ends_excmd(*p)
3010#ifdef FEAT_EVAL
3011 // in ":filter #pat# cmd" # does not
3012 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003013 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003014#endif
3015 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003016 break;
3017 if (*p == '!')
3018 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003019 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003020 p = skipwhite(p + 1);
3021 if (*p == NUL || ends_excmd(*p))
3022 break;
3023 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003024#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003025 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003026 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003027 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003028#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003029 if (skip_only)
3030 p = skip_vimgrep_pat(p, NULL, NULL);
3031 else
3032 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003033 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3034 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003035 if (p == NULL || *p == NUL)
3036 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003037 if (!skip_only)
3038 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003039 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003040 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003041 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003042 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003043 // restore the character overwritten by NUL
3044 if (nulp != NULL)
3045 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003046 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003047 eap->cmd = p;
3048 continue;
3049 }
3050
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003051 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3052 {
3053 cmod->cmod_split |= WSP_HOR;
3054 continue;
3055 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003056 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003057 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003058 || *p == NUL || ends_excmd(*p))
3059 break;
3060 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003061 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003062 continue;
3063
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003064 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003065 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003066 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003067 continue;
3068 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003069 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3070 {
3071 if (ends_excmd2(p, eap->cmd))
3072 {
3073 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003074 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003075 return FAIL;
3076 }
3077 cmod->cmod_flags |= CMOD_LEGACY;
3078 continue;
3079 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003080
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003081 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003082 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003083 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003084 continue;
3085
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003086 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003087 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003088 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003089 continue;
3090 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003091 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003092 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003093 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003094 continue;
3095
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003096 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003097 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003098 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003099 continue;
3100
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003101 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003102 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003103 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003104 continue;
3105 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003106 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003107 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003108 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003109 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3110 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003111 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003112 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003113 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003114 }
3115 continue;
3116
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003117 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003118 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003119 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003120 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003121 long tabnr = get_address(eap, &eap->cmd,
3122 ADDR_TABS, eap->skip,
3123 skip_only, FALSE, 1);
3124 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003125 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003126 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003127 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003128 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3129 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003130 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003131 return FAIL;
3132 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003133 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003134 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003135 }
3136 eap->cmd = p;
3137 continue;
3138 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003139 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003140 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003141 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003142 continue;
3143
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003144 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003145 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003146 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003147 continue;
3148
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003149 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003150 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003151 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003152 continue;
3153 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003154 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003155 {
3156 if (ends_excmd2(p, eap->cmd))
3157 {
3158 *errormsg =
3159 _(e_vim9cmd_must_be_followed_by_command);
3160 return FAIL;
3161 }
3162 cmod->cmod_flags |= CMOD_VIM9CMD;
3163 continue;
3164 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003165 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003166 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003167 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003168 {
zeertzjqcd749632022-06-14 13:30:35 +01003169 // zero means not set, one is verbose == 0, etc.
3170 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003171 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003172 else
zeertzjqcd749632022-06-14 13:30:35 +01003173 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003174 eap->cmd = p;
3175 continue;
3176 }
3177 break;
3178 }
3179
Bram Moolenaar1501b632022-03-27 16:56:21 +01003180 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003181 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003182 if (eap->cmd > cmd_start)
3183 {
3184 // Move the '<,'> range to after the modifiers and insert a colon.
3185 // Since the modifiers have been parsed put the colon on top of the
3186 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3187 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003188 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003189 {
3190 size_t len = STRLEN(cmd_start);
3191
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003192 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003193 // "'<,'>mods" -> "mods *+
3194 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003195 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003196 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003197 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003198 }
3199 else
3200 {
3201 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3202 eap->cmd -= 5;
3203 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3204 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003205 }
3206 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003207 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003208 // Special case: change empty command to "+".
3209 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003210 eap->cmd = (char_u *)"'<,'>+";
3211 else
3212 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003213 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003214 else if (use_plus_cmd)
3215 eap->cmd = (char_u *)"+";
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003216
Bram Moolenaareffed932018-08-14 13:38:17 +02003217 return OK;
3218}
3219
3220/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003221 * Return TRUE if "cmod" has anything set.
3222 */
3223 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003224has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003225{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003226 return (cmod->cmod_flags != 0 && (!ignore_silent
3227 || (cmod->cmod_flags
3228 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003229 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003230 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003231 || cmod->cmod_tab != 0
3232 || cmod->cmod_filter_regmatch.regprog != NULL;
3233}
3234
Bram Moolenaar8991be22022-02-14 21:51:46 +00003235#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003236/*
3237 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3238 */
3239 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003240cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003241{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003242 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003243 {
3244 emsg(_(e_misplaced_command_modifier));
3245 return TRUE;
3246 }
3247 return FALSE;
3248}
Dominique Pelle748b3082022-01-08 12:41:16 +00003249#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003250
3251/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003252 * Apply the command modifiers. Saves current state in "cmdmod", call
3253 * undo_cmdmod() later.
3254 */
3255 void
3256apply_cmdmod(cmdmod_T *cmod)
3257{
3258#ifdef HAVE_SANDBOX
3259 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3260 {
3261 ++sandbox;
3262 cmod->cmod_did_sandbox = TRUE;
3263 }
3264#endif
zeertzjqcd749632022-06-14 13:30:35 +01003265 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003266 {
3267 if (cmod->cmod_verbose_save == 0)
3268 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003269 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003270 }
3271
3272 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3273 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003274 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003275 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003276 cmod->cmod_save_msg_scroll = msg_scroll;
3277 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003278 if (cmod->cmod_flags & CMOD_SILENT)
3279 ++msg_silent;
3280 if (cmod->cmod_flags & CMOD_UNSILENT)
3281 msg_silent = 0;
3282
3283 if (cmod->cmod_flags & CMOD_ERRSILENT)
3284 {
3285 ++emsg_silent;
3286 ++cmod->cmod_did_esilent;
3287 }
3288
Bram Moolenaare1004402020-10-24 20:49:43 +02003289 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003290 {
3291 // Set 'eventignore' to "all".
3292 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003293 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003294 set_string_option_direct((char_u *)"ei", -1,
3295 (char_u *)"all", OPT_FREE, SID_NONE);
3296 }
3297}
3298
3299/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003300 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003301 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003302 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003303undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003304{
Bram Moolenaare1004402020-10-24 20:49:43 +02003305 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003306 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003307 p_verbose = cmod->cmod_verbose_save - 1;
3308 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003309 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003310
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003311#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003312 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003313 {
3314 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003315 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003316 }
3317#endif
3318
Bram Moolenaare1004402020-10-24 20:49:43 +02003319 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003320 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003321 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003322 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3323 OPT_FREE, SID_NONE);
3324 free_string_option(cmod->cmod_save_ei);
3325 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003326 }
3327
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003328 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003329
Bram Moolenaare1004402020-10-24 20:49:43 +02003330 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003331 {
3332 // messages could be enabled for a serious error, need to check if the
3333 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003334 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3335 msg_silent = cmod->cmod_save_msg_silent - 1;
3336 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003337 if (emsg_silent < 0)
3338 emsg_silent = 0;
3339 // Restore msg_scroll, it's set by file I/O commands, even when no
3340 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003341 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003342
3343 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3344 // somewhere in the line. Put it back in the first column.
3345 if (redirecting())
3346 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003347
Bram Moolenaare1004402020-10-24 20:49:43 +02003348 cmod->cmod_save_msg_silent = 0;
3349 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003350 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003351}
3352
3353/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003354 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003355 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003356 * Return FAIL and set "errormsg" or return OK.
3357 */
3358 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003359parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003360{
3361 int address_count = 1;
3362 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003363 int need_check_cursor = FALSE;
3364 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003365
3366 // Repeat for all ',' or ';' separated addresses.
3367 for (;;)
3368 {
3369 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003370 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003371 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003372 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003373 eap->addr_count == 0, address_count++);
3374 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003375 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003376 if (lnum == MAXLNUM)
3377 {
3378 if (*eap->cmd == '%') // '%' - all lines
3379 {
3380 ++eap->cmd;
3381 switch (eap->addr_type)
3382 {
3383 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003384 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003385 eap->line1 = 1;
3386 eap->line2 = curbuf->b_ml.ml_line_count;
3387 break;
3388 case ADDR_LOADED_BUFFERS:
3389 {
3390 buf_T *buf = firstbuf;
3391
3392 while (buf->b_next != NULL
3393 && buf->b_ml.ml_mfp == NULL)
3394 buf = buf->b_next;
3395 eap->line1 = buf->b_fnum;
3396 buf = lastbuf;
3397 while (buf->b_prev != NULL
3398 && buf->b_ml.ml_mfp == NULL)
3399 buf = buf->b_prev;
3400 eap->line2 = buf->b_fnum;
3401 break;
3402 }
3403 case ADDR_BUFFERS:
3404 eap->line1 = firstbuf->b_fnum;
3405 eap->line2 = lastbuf->b_fnum;
3406 break;
3407 case ADDR_WINDOWS:
3408 case ADDR_TABS:
3409 if (IS_USER_CMDIDX(eap->cmdidx))
3410 {
3411 eap->line1 = 1;
3412 eap->line2 = eap->addr_type == ADDR_WINDOWS
3413 ? LAST_WIN_NR : LAST_TAB_NR;
3414 }
3415 else
3416 {
3417 // there is no Vim command which uses '%' and
3418 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003419 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003420 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003421 }
3422 break;
3423 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003424 case ADDR_UNSIGNED:
3425 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003426 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003427 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003428 case ADDR_ARGUMENTS:
3429 if (ARGCOUNT == 0)
3430 eap->line1 = eap->line2 = 0;
3431 else
3432 {
3433 eap->line1 = 1;
3434 eap->line2 = ARGCOUNT;
3435 }
3436 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003437 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003438#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003439 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003440 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003441 if (eap->line2 == 0)
3442 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003443#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003444 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003445 case ADDR_NONE:
3446 // Will give an error later if a range is found.
3447 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003448 }
3449 ++eap->addr_count;
3450 }
3451 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3452 {
3453 pos_T *fp;
3454
3455 // '*' - visual area
3456 if (eap->addr_type != ADDR_LINES)
3457 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003458 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003459 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003460 }
3461
3462 ++eap->cmd;
3463 if (!eap->skip)
3464 {
3465 fp = getmark('<', FALSE);
3466 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003467 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003468 eap->line1 = fp->lnum;
3469 fp = getmark('>', FALSE);
3470 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003471 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003472 eap->line2 = fp->lnum;
3473 ++eap->addr_count;
3474 }
3475 }
3476 }
3477 else
3478 eap->line2 = lnum;
3479 eap->addr_count++;
3480
3481 if (*eap->cmd == ';')
3482 {
3483 if (!eap->skip)
3484 {
3485 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003486
Bram Moolenaar0e717042020-04-27 19:29:01 +02003487 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003488 // accept zero as address, so 0;/PATTERN/ works correctly
3489 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003490 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003491 if (eap->line2 > 0)
3492 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003493 else
3494 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003495 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003496 }
3497 }
3498 else if (*eap->cmd != ',')
3499 break;
3500 ++eap->cmd;
3501 }
3502
3503 // One address given: set start and end lines.
3504 if (eap->addr_count == 1)
3505 {
3506 eap->line1 = eap->line2;
3507 // ... but only implicit: really no address given
3508 if (lnum == MAXLNUM)
3509 eap->addr_count = 0;
3510 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003511 ret = OK;
3512
3513theend:
3514 if (need_check_cursor)
3515 check_cursor();
3516 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003517}
3518
3519/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003520 * Append "cmd" to the error message in IObuff.
3521 * Takes care of limiting the length and handling 0xa0, which would be
3522 * invisible otherwise.
3523 */
3524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003525append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003526{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003527 size_t len = STRLEN(IObuff);
3528 char_u *s = cmd;
3529 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003530
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003531 if (len > IOSIZE - 100)
3532 {
3533 // Not enough space, truncate and put in "...".
3534 d = IObuff + IOSIZE - 100;
3535 d -= mb_head_off(IObuff, d);
3536 STRCPY(d, "...");
3537 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003538 STRCAT(IObuff, ": ");
3539 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003540 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003541 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003542 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003543 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003544 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003545 STRCPY(d, "<a0>");
3546 d += 4;
3547 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003548 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3549 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003550 else
3551 MB_COPY_CHAR(s, d);
3552 }
3553 *d = NUL;
3554}
3555
Dominique Pelle748b3082022-01-08 12:41:16 +00003556#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003557/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003558 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3559 * the name. Otherwise just return "start".
3560 */
3561 char_u *
3562skip_option_env_lead(char_u *start)
3563{
3564 char_u *name = start;
3565
3566 if (*start == '&')
3567 {
3568 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3569 name += 3;
3570 else
3571 name += 1;
3572 }
3573 else if (*start == '$')
3574 name += 1;
3575 return name;
3576}
Dominique Pelle748b3082022-01-08 12:41:16 +00003577#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003578
3579/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003580 * Return TRUE and set "*idx" if "p" points to a one letter command.
3581 * If not in Vim9 script:
3582 * - The 'k' command can directly be followed by any character.
3583 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3584 * but :sre[wind] is another command, as are :scr[iptnames],
3585 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3586 */
3587 static int
3588one_letter_cmd(char_u *p, cmdidx_T *idx)
3589{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003590 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003591 return FALSE;
3592 if (*p == 'k')
3593 {
3594 *idx = CMD_k;
3595 return TRUE;
3596 }
3597 if (p[0] == 's'
3598 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3599 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3600 || p[1] == 'g'
3601 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3602 || p[1] == 'I'
3603 || (p[1] == 'r' && p[2] != 'e')))
3604 {
3605 *idx = CMD_substitute;
3606 return TRUE;
3607 }
3608 return FALSE;
3609}
3610
Dominique Pellee764d1b2023-03-12 21:20:59 +00003611#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003612/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003613 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3614 * call.
3615 */
3616 int
3617number_method(char_u *cmd)
3618{
3619 char_u *p = skipdigits(cmd);
3620
3621 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3622}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003623#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003624
3625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003627 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003628 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003629 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 *
3631 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3632 * assignment without "let". Sets eap->cmdidx to the command while returning
3633 * "eap->cmd".
3634 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 * Returns NULL for an ambiguous user command.
3636 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 char_u *
3638find_ex_command(
3639 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003640 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003641 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003642 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
3644 int len;
3645 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003646 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003647#ifndef FEAT_EVAL
3648 int vim9 = FALSE;
3649#else
3650 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 /*
3653 * Recognize a Vim9 script function/method call and assignment:
3654 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3655 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003656 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003657 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003659 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003661 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003662 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3663 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003665 int oplen;
3666 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003667 char_u *swp;
3668
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003669 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003670 || (eap->cmd[0] == '$'
3671 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003672 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003673 && (valid_yank_reg(eap->cmd[1], FALSE)
3674 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003675 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003676 if (*eap->cmd == '&')
3677 {
3678 p = eap->cmd + 1;
3679 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3680 p += 2;
3681 p = to_name_end(p, FALSE);
3682 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003683 else if (*eap->cmd == '$')
3684 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003685 else
3686 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003687 if (ends_excmd(*skipwhite(p)))
3688 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003689 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3690 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003691 eap->cmdidx = CMD_eval;
3692 return eap->cmd;
3693 }
3694 // "&option" can be followed by "->" or "=", check below
3695 }
3696
3697 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003699 if (
3700 // "(..." is an expression.
3701 // "funcname(" is always a function call.
3702 *p == '('
3703 || (p == eap->cmd
3704 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003705 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003706 *eap->cmd == '{'
3707 // "'string'->func()" is an expression.
3708 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003709 // '"string"->func()' is an expression.
3710 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003711 // '$"string"->func()' is an expression.
3712 // "$'string'->func()" is an expression.
3713 || (eap->cmd[0] == '$'
3714 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3715 // '0z1234->func()' is an expression.
3716 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003717 // "g:varname" is an expression.
3718 || eap->cmd[1] == ':'
3719 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003720 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003721 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003722 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003723 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3724 {
3725 // "{" by itself is the start of a block.
3726 eap->cmdidx = CMD_block;
3727 return eap->cmd + 1;
3728 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003729 eap->cmdidx = CMD_eval;
3730 return eap->cmd;
3731 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003732
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003733 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003734 // "varname[]" is an expression.
3735 *p == '['
3736 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003737 || (*p == '.'
3738 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3739 // g:[key] is an expression
3740 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003741 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003742 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003743
3744 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003745 // Skip over the whole thing, it can be:
3746 // name.member = val
3747 // name[a : b] = val
3748 // name[idx] = val
3749 // name[idx].member = val
3750 // etc.
3751 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003752 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003753 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003754 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003755 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003756 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003757 || (after[0] == '.' && after[1] == '.'
3758 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003759 eap->cmdidx = CMD_var;
3760 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003761 --emsg_silent;
3762 return eap->cmd;
3763 }
3764
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003765 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3766 // an assignment.
3767 // If there is no line break inside the "[...]" then "p" is
3768 // advanced to after the "]" by to_name_const_end(): check if a "="
3769 // follows.
3770 // If "[...]" has a line break "p" still points at the "[" and it
3771 // can't be an assignment.
3772 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003773 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003774 char_u *eq;
3775
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003776 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003777 if (p == eap->cmd && *p == '[')
3778 {
3779 int count = 0;
3780 int semicolon = FALSE;
3781
3782 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3783 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003784 eq = p;
3785 if (eq != NULL)
3786 {
3787 eq = skipwhite(eq);
3788 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3789 ++eq;
3790 }
3791 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003792 {
3793 eap->cmdidx = CMD_eval;
3794 return eap->cmd;
3795 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003796 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003797 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003798 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003799 return eap->cmd;
3800 }
3801 }
3802
3803 // Recognize an assignment if we recognize the variable name:
3804 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003805 // "@r = expr"
3806 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003807 // "var = expr" where "var" is a variable name or we are skipping
3808 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003809 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003810 oplen = assignment_len(skipwhite(p), &heredoc);
3811 if (oplen > 0)
3812 {
3813 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3814 || *eap->cmd == '&'
3815 || *eap->cmd == '$'
3816 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003817 || (eap->skip && IS_WHITE_OR_NUL(
3818 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003819 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003820 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003821 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003822 return eap->cmd;
3823 }
3824 }
3825
Bram Moolenaar9510d222022-09-11 15:14:05 +01003826 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003827 // "w:varname: number = 123".
3828 if (eap->cmd[1] == ':' && *p == ':')
3829 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003830 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003831 return eap->cmd;
3832 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003833 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003834
Bram Moolenaar31d99482022-05-26 22:24:43 +01003835 // 1234->func() is a method call
3836 if (number_method(eap->cmd))
3837 {
3838 eap->cmdidx = CMD_eval;
3839 return eap->cmd;
3840 }
3841
Bram Moolenaar7b829262021-10-13 15:04:34 +01003842 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3843 // an expression. A global/substitute/list command needs to use a
3844 // longer name.
3845 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3846 {
3847 eap->cmdidx = CMD_eval;
3848 return eap->cmd;
3849 }
3850
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003851 // If it is an ID it might be a variable with an operator on the next
3852 // line, if the variable exists it can't be an Ex command.
3853 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003854 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003855 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3856 {
3857 eap->cmdidx = CMD_eval;
3858 return eap->cmd;
3859 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003860
3861 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003862 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3863 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003864 {
3865 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3866 return eap->cmd + 2;
3867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 }
3869#endif
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 /*
3872 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 */
3874 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003875 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 ++p;
3878 }
3879 else
3880 {
3881 while (ASCII_ISALPHA(*p))
3882 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003883 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003884 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003886 while (ASCII_ISALNUM(*p))
3887 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 }
3889 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3890 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003891 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 ++p;
3893 while (ASCII_ISALPHA(*p))
3894 ++p;
3895 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003896
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003897 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003898 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 ++p;
3900 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003901 // The "d" command can directly be followed by 'l' or 'p' flag, when
3902 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003903 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003904 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003905 // Check for ":dl", ":dell", etc. to ":deletel": that's
3906 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003907 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003908 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003909 break;
3910 if (i == len - 1)
3911 {
3912 --len;
3913 if (p[-1] == 'l')
3914 eap->flags |= EXFLAG_LIST;
3915 else
3916 eap->flags |= EXFLAG_PRINT;
3917 }
3918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003920 if (ASCII_ISLOWER(eap->cmd[0]))
3921 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003922 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003923 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003924
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003925 if (command_count != (int)CMD_SIZE)
3926 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003927 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003928 getout(1);
3929 }
3930
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003931 // Use a precomputed index for fast look-up in cmdnames[]
3932 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003933 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3934 if (ASCII_ISLOWER(c2))
3935 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3936 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003937 else if (ASCII_ISUPPER(eap->cmd[0]))
3938 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003940 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
3942 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3943 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3944 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3945 (size_t)len) == 0)
3946 {
3947#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003948 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 *full = TRUE;
3950#endif
3951 break;
3952 }
3953
Bram Moolenaar204852a2022-03-05 12:56:44 +00003954 // :Print and :mode are not supported in Vim9 script.
3955 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003956 if (vim9 && eap->cmdidx != CMD_SIZE)
3957 {
3958 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3959 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003960 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003961 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003962 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003963 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003964 eap->cmdidx = CMD_SIZE;
3965 }
3966 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003967
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003968 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003969 // 'cpoptions'.
3970 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3971 p = eap->cmd;
3972
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003973 // Look for a user defined command as a last resort. Let ":Print" be
3974 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003975 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3976 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003978 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 while (ASCII_ISALNUM(*p))
3980 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003981 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003983 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 eap->cmdidx = CMD_SIZE;
3985 }
3986
Bram Moolenaar204852a2022-03-05 12:56:44 +00003987 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3988 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003989 eap->cmdidx = CMD_finally;
3990
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003991#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003992 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003993 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02003994 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003995 && (eap->cmdidx < 0 ||
3996 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003997 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003998 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3999
4000 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004001 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004002 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004003 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004004#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 return p;
4007}
4008
4009#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004010typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004011{
4012 char *name;
4013 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004014 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004015} cmdmod_info_T;
4016
4017static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004018 {"aboveleft", 3, FALSE},
4019 {"belowright", 3, FALSE},
4020 {"botright", 2, FALSE},
4021 {"browse", 3, FALSE},
4022 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004023 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004024 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004025 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004026 {"keepalt", 5, FALSE},
4027 {"keepjumps", 5, FALSE},
4028 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004029 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004030 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004031 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004032 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004033 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004034 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004035 {"rightbelow", 6, FALSE},
4036 {"sandbox", 3, FALSE},
4037 {"silent", 3, FALSE},
4038 {"tab", 3, TRUE},
4039 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004040 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004041 {"verbose", 4, TRUE},
4042 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004043 {"vim9cmd", 4, FALSE}
4044}; // cmdmod_info_tab
4045
4046// compare two cmdmod_info_T structs by case sensitive name with length
4047 static int
4048cmp_cmdmod_info(const void *a, const void *b)
4049{
4050 cmdmod_info_T *cm1 = (cmdmod_info_T *)a;
4051 cmdmod_info_T *cm2 = (cmdmod_info_T *)b;
4052
4053 return STRNCMP(cm1->name, cm2->name, cm2->minlen);
4054}
Bram Moolenaared53fb92007-11-24 20:50:24 +00004055
4056/*
4057 * Return length of a command modifier (including optional count).
4058 * Return zero when it's not a modifier.
4059 */
4060 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004061modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004062{
Bram Moolenaared53fb92007-11-24 20:50:24 +00004063 char_u *p = cmd;
John Marriotted908f72024-04-18 22:46:56 +02004064 cmdmod_info_T target;
4065 cmdmod_info_T *entry;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004066
4067 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004068 p = skipwhite(skipdigits(cmd + 1));
John Marriotted908f72024-04-18 22:46:56 +02004069
4070 // only lowercase characters can match
4071 if (!ASCII_ISLOWER(*p))
4072 return 0;
4073
4074 target.name = (char *)p;
4075 target.minlen = 0; // not used, see cmp_cmdmod_info()
4076 target.has_count = FALSE;
4077
4078 entry = (cmdmod_info_T *)bsearch(&target, &cmdmod_info_tab, ARRAY_LENGTH(cmdmod_info_tab), sizeof(cmdmod_info_tab[0]), cmp_cmdmod_info);
4079 if (entry != NULL)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004080 {
John Marriotted908f72024-04-18 22:46:56 +02004081 int i;
4082
4083 for (i = entry->minlen; p[i] != NUL; ++i)
4084 {
4085 if (p[i] != entry->name[i])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004086 break;
John Marriotted908f72024-04-18 22:46:56 +02004087 }
4088
4089 if (!ASCII_ISALPHA(p[i]) && i >= entry->minlen && (p == cmd || entry->has_count))
4090 return i + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004091 }
John Marriotted908f72024-04-18 22:46:56 +02004092
Bram Moolenaared53fb92007-11-24 20:50:24 +00004093 return 0;
4094}
4095
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096/*
4097 * Return > 0 if an Ex command "name" exists.
4098 * Return 2 if there is an exact match.
4099 * Return 3 if there is an ambiguous match.
4100 */
4101 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004102cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103{
4104 exarg_T ea;
4105 int full = FALSE;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004106 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
John Marriotted908f72024-04-18 22:46:56 +02004108 // only lowercase characters can match
4109 if (ASCII_ISLOWER(*name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
John Marriotted908f72024-04-18 22:46:56 +02004111 cmdmod_info_T target;
4112 cmdmod_info_T *entry;
4113
4114 target.name = (char *)name;
4115 target.minlen = 0; // not used, see cmp_cmdmod_info()
4116 target.has_count = FALSE;
4117
4118 // Check command modifiers.
4119 entry = (cmdmod_info_T *)bsearch(&target, &cmdmod_info_tab, ARRAY_LENGTH(cmdmod_info_tab), sizeof(cmdmod_info_tab[0]), cmp_cmdmod_info);
4120 if (entry != NULL)
4121 {
4122 int i;
4123
4124 for (i = entry->minlen; name[i] != NUL; ++i)
4125 {
4126 if (name[i] != entry->name[i])
4127 break;
4128 }
4129
4130 if (name[i] == NUL && i >= entry->minlen)
4131 return (entry->name[i] == NUL ? 2 : 1);
4132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 }
4134
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004135 // Check built-in commands and user defined commands.
4136 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004137 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004139 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004141 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004143 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4144 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004145 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004146 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4148}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004149
4150/*
4151 * "fullcommand" function
4152 */
4153 void
4154f_fullcommand(typval_T *argvars, typval_T *rettv)
4155{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004156 exarg_T ea;
4157 char_u *name;
4158 char_u *p;
4159 int vim9script = in_vim9script();
4160 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004161
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004162 rettv->v_type = VAR_STRING;
4163 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004164
Bram Moolenaaraa534142022-09-15 21:46:02 +01004165 if (in_vim9script()
4166 && (check_for_string_arg(argvars, 0) == FAIL
4167 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004168 return;
4169
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004170 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004171 if (name == NULL)
4172 return;
4173
Bram Moolenaaraa534142022-09-15 21:46:02 +01004174 if (argvars[1].v_type != VAR_UNKNOWN)
4175 {
4176 vim9script = tv_get_bool(&argvars[1]);
4177 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4178 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4179 }
4180
zeertzjqc024ed92022-01-04 16:22:52 +00004181 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004182 name++;
4183 name = skip_range(name, TRUE, NULL);
4184
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004185 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4186 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004187 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004188 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004189 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004190 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004191 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004192 goto theend;
4193
4194 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004195 {
4196 int res;
4197
4198 ++emsg_silent;
4199 res = not_in_vim9(&ea);
4200 --emsg_silent;
4201
4202 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004203 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004204 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004205
4206 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004207 ? get_user_command_name(ea.useridx, ea.cmdidx)
4208 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004209theend:
4210 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004211}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212#endif
4213
Bram Moolenaard0190392019-08-23 21:17:35 +02004214 cmdidx_T
4215excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
Bram Moolenaard0190392019-08-23 21:17:35 +02004217 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004219 if (!one_letter_cmd(cmd, &idx))
4220 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4221 idx = (cmdidx_T)((int)idx + 1))
4222 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4223 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
Bram Moolenaard0190392019-08-23 21:17:35 +02004225 return idx;
4226}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
Bram Moolenaard0190392019-08-23 21:17:35 +02004228 long
4229excmd_get_argt(cmdidx_T idx)
4230{
4231 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232}
4233
4234/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004235 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 *
4237 * Backslashed delimiters after / or ? will be skipped, and commands will
4238 * not be expanded between /'s and ?'s or after "'".
4239 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004240 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * Returns the "cmd" pointer advanced to beyond the range.
4242 */
4243 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004244skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004245 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004246 int skip_star, // skip "*" used for Visual range
4247 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004249 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004250 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004252 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004254 if (*cmd == '\\')
4255 {
4256 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4257 ++cmd;
4258 else
4259 break;
4260 }
4261 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004263 char_u *p = cmd;
4264
4265 // a quote is only valid at the start or after a separator
4266 while (p > cmd_start)
4267 {
4268 --p;
4269 if (!VIM_ISWHITE(*p))
4270 break;
4271 }
4272 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 if (*++cmd == NUL && ctx != NULL)
4275 *ctx = EXPAND_NOTHING;
4276 }
4277 else if (*cmd == '/' || *cmd == '?')
4278 {
4279 delim = *cmd++;
4280 while (*cmd != NUL && *cmd != delim)
4281 if (*cmd++ == '\\' && *cmd != NUL)
4282 ++cmd;
4283 if (*cmd == NUL && ctx != NULL)
4284 *ctx = EXPAND_NOTHING;
4285 }
4286 if (*cmd != NUL)
4287 ++cmd;
4288 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004289
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004290 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004291 while (*cmd == ':')
4292 cmd = skipwhite(cmd + 1);
4293
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004294 // Skip "*" used for Visual range.
4295 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4296 cmd = skipwhite(cmd + 1);
4297
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 return cmd;
4299}
4300
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004301 static void
4302addr_error(cmd_addr_T addr_type)
4303{
4304 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004305 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004306 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004307 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004308}
4309
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004311 * Return the default address for an address type.
4312 */
4313 static linenr_T
4314default_address(exarg_T *eap)
4315{
4316 linenr_T lnum = 0;
4317
4318 switch (eap->addr_type)
4319 {
4320 case ADDR_LINES:
4321 case ADDR_OTHER:
4322 // Default is the cursor line number. Avoid using an invalid
4323 // line number though.
4324 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4325 lnum = curbuf->b_ml.ml_line_count;
4326 else
4327 lnum = curwin->w_cursor.lnum;
4328 break;
4329 case ADDR_WINDOWS:
4330 lnum = CURRENT_WIN_NR;
4331 break;
4332 case ADDR_ARGUMENTS:
4333 lnum = curwin->w_arg_idx + 1;
4334 if (lnum > ARGCOUNT)
4335 lnum = ARGCOUNT;
4336 break;
4337 case ADDR_LOADED_BUFFERS:
4338 case ADDR_BUFFERS:
4339 lnum = curbuf->b_fnum;
4340 break;
4341 case ADDR_TABS:
4342 lnum = CURRENT_TAB_NR;
4343 break;
4344 case ADDR_TABS_RELATIVE:
4345 case ADDR_UNSIGNED:
4346 lnum = 1;
4347 break;
4348 case ADDR_QUICKFIX:
4349#ifdef FEAT_QUICKFIX
4350 lnum = qf_get_cur_idx(eap);
4351#endif
4352 break;
4353 case ADDR_QUICKFIX_VALID:
4354#ifdef FEAT_QUICKFIX
4355 lnum = qf_get_cur_valid_idx(eap);
4356#endif
4357 break;
4358 case ADDR_NONE:
4359 // Will give an error later if a range is found.
4360 break;
4361 }
4362 return lnum;
4363}
4364
4365/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004366 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 *
4368 * Set ptr to the next character after the part that was interpreted.
4369 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004370 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 *
4372 * Return MAXLNUM when no Ex address was found.
4373 */
4374 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004375get_address(
4376 exarg_T *eap UNUSED,
4377 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004378 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004379 int skip, // only skip the address, don't use it
4380 int silent, // no errors or side effects
4381 int to_other_file, // flag: may jump to other file
4382 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
4384 int c;
4385 int i;
4386 long n;
4387 char_u *cmd;
4388 pos_T pos;
4389 pos_T *fp;
4390 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004391 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 cmd = skipwhite(*ptr);
4394 lnum = MAXLNUM;
4395 do
4396 {
4397 switch (*cmd)
4398 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004399 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 ++cmd;
4401 switch (addr_type)
4402 {
4403 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004404 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 lnum = curwin->w_cursor.lnum;
4406 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004407 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004408 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004409 break;
4410 case ADDR_ARGUMENTS:
4411 lnum = curwin->w_arg_idx + 1;
4412 break;
4413 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004414 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 lnum = curbuf->b_fnum;
4416 break;
4417 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004418 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004419 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004420 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004421 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004422 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004423 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004424 cmd = NULL;
4425 goto error;
4426 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004427 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004428#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004429 lnum = qf_get_cur_idx(eap);
4430#endif
4431 break;
4432 case ADDR_QUICKFIX_VALID:
4433#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004434 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004435#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004436 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004437 }
4438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004440 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004441 ++cmd;
4442 switch (addr_type)
4443 {
4444 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004445 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 lnum = curbuf->b_ml.ml_line_count;
4447 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004449 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 break;
4451 case ADDR_ARGUMENTS:
4452 lnum = ARGCOUNT;
4453 break;
4454 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004455 buf = lastbuf;
4456 while (buf->b_ml.ml_mfp == NULL)
4457 {
4458 if (buf->b_prev == NULL)
4459 break;
4460 buf = buf->b_prev;
4461 }
4462 lnum = buf->b_fnum;
4463 break;
4464 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004465 lnum = lastbuf->b_fnum;
4466 break;
4467 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004468 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004469 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004470 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004471 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004472 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004473 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004474 cmd = NULL;
4475 goto error;
4476 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004477 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004478#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004479 lnum = qf_get_size(eap);
4480 if (lnum == 0)
4481 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004482#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004483 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004484 case ADDR_QUICKFIX_VALID:
4485#ifdef FEAT_QUICKFIX
4486 lnum = qf_get_valid_size(eap);
4487 if (lnum == 0)
4488 lnum = 1;
4489#endif
4490 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 }
4492 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004494 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 if (*++cmd == NUL)
4496 {
4497 cmd = NULL;
4498 goto error;
4499 }
4500 if (addr_type != ADDR_LINES)
4501 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004502 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004503 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004504 goto error;
4505 }
4506 if (skip)
4507 ++cmd;
4508 else
4509 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004510 // Only accept a mark in another file when it is
4511 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004512 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4513 ++cmd;
4514 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004515 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004516 lnum = curwin->w_cursor.lnum;
4517 else
4518 {
4519 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
4521 cmd = NULL;
4522 goto error;
4523 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004524 lnum = fp->lnum;
4525 }
4526 }
4527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
4529 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004530 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004531 c = *cmd++;
4532 if (addr_type != ADDR_LINES)
4533 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004534 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004535 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004536 goto error;
4537 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004538 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004539 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004540 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004541 if (*cmd == c)
4542 ++cmd;
4543 }
4544 else
4545 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004546 int flags;
4547
4548 pos = curwin->w_cursor; // save curwin->w_cursor
4549
4550 // When '/' or '?' follows another address, start from
4551 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004552 if (lnum > 0 && lnum != MAXLNUM)
4553 curwin->w_cursor.lnum =
4554 lnum > curbuf->b_ml.ml_line_count
4555 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004556
4557 // Start a forward search at the end of the line (unless
4558 // before the first line).
4559 // Start a backward search at the start of the line.
4560 // This makes sure we never match in the current
4561 // line, and can match anywhere in the
4562 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004563 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004564 curwin->w_cursor.col = MAXCOL;
4565 else
4566 curwin->w_cursor.col = 0;
4567 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004568 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004569 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004570 {
4571 curwin->w_cursor = pos;
4572 cmd = NULL;
4573 goto error;
4574 }
4575 lnum = curwin->w_cursor.lnum;
4576 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004577 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004578 cmd += searchcmdlen;
4579 }
4580 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004582 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004583 ++cmd;
4584 if (addr_type != ADDR_LINES)
4585 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004586 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004587 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004588 goto error;
4589 }
4590 if (*cmd == '&')
4591 i = RE_SUBST;
4592 else if (*cmd == '?' || *cmd == '/')
4593 i = RE_SEARCH;
4594 else
4595 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004596 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004597 cmd = NULL;
4598 goto error;
4599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004601 if (!skip)
4602 {
4603 /*
4604 * When search follows another address, start from
4605 * there.
4606 */
4607 if (lnum != MAXLNUM)
4608 pos.lnum = lnum;
4609 else
4610 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004612 /*
4613 * Start the search just like for the above
4614 * do_search().
4615 */
4616 if (*cmd != '?')
4617 pos.col = MAXCOL;
4618 else
4619 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004620 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004621 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004622 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004623 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004624 lnum = pos.lnum;
4625 else
4626 {
4627 cmd = NULL;
4628 goto error;
4629 }
4630 }
4631 ++cmd;
4632 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
4634 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004635 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004636 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
4638
4639 for (;;)
4640 {
4641 cmd = skipwhite(cmd);
4642 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4643 break;
4644
4645 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004646 {
4647 switch (addr_type)
4648 {
4649 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004650 case ADDR_OTHER:
4651 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004652 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004653 break;
4654 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004655 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004656 break;
4657 case ADDR_ARGUMENTS:
4658 lnum = curwin->w_arg_idx + 1;
4659 break;
4660 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004661 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004662 lnum = curbuf->b_fnum;
4663 break;
4664 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004665 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004666 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004667 case ADDR_TABS_RELATIVE:
4668 lnum = 1;
4669 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004670 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004671#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004672 lnum = qf_get_cur_idx(eap);
4673#endif
4674 break;
4675 case ADDR_QUICKFIX_VALID:
4676#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004677 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004678#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004679 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004680 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004681 case ADDR_UNSIGNED:
4682 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004683 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004684 }
4685 }
4686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004688 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 else
4690 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004691 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 n = 1;
4693 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004694 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004695 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004697 if (n == MAXLNUM)
4698 {
4699 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004700 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004701 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));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004731 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004732 goto error;
4733 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004734 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004735 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
4738 } while (*cmd == '/' || *cmd == '?');
4739
4740error:
4741 *ptr = cmd;
4742 return lnum;
4743}
4744
4745/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004746 * Set eap->line1 and eap->line2 to the whole range.
4747 * Used for commands with the EX_DFLALL flag and no range given.
4748 */
4749 static void
4750address_default_all(exarg_T *eap)
4751{
4752 eap->line1 = 1;
4753 switch (eap->addr_type)
4754 {
4755 case ADDR_LINES:
4756 case ADDR_OTHER:
4757 eap->line2 = curbuf->b_ml.ml_line_count;
4758 break;
4759 case ADDR_LOADED_BUFFERS:
4760 {
4761 buf_T *buf = firstbuf;
4762
4763 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4764 buf = buf->b_next;
4765 eap->line1 = buf->b_fnum;
4766 buf = lastbuf;
4767 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4768 buf = buf->b_prev;
4769 eap->line2 = buf->b_fnum;
4770 }
4771 break;
4772 case ADDR_BUFFERS:
4773 eap->line1 = firstbuf->b_fnum;
4774 eap->line2 = lastbuf->b_fnum;
4775 break;
4776 case ADDR_WINDOWS:
4777 eap->line2 = LAST_WIN_NR;
4778 break;
4779 case ADDR_TABS:
4780 eap->line2 = LAST_TAB_NR;
4781 break;
4782 case ADDR_TABS_RELATIVE:
4783 eap->line2 = 1;
4784 break;
4785 case ADDR_ARGUMENTS:
4786 if (ARGCOUNT == 0)
4787 eap->line1 = eap->line2 = 0;
4788 else
4789 eap->line2 = ARGCOUNT;
4790 break;
4791 case ADDR_QUICKFIX_VALID:
4792#ifdef FEAT_QUICKFIX
4793 eap->line2 = qf_get_valid_size(eap);
4794 if (eap->line2 == 0)
4795 eap->line2 = 1;
4796#endif
4797 break;
4798 case ADDR_NONE:
4799 case ADDR_UNSIGNED:
4800 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004801 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004802 break;
4803 }
4804}
4805
4806
4807/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004808 * Get flags from an Ex command argument.
4809 */
4810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004811get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004812{
4813 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4814 {
4815 if (*eap->arg == 'l')
4816 eap->flags |= EXFLAG_LIST;
4817 else if (*eap->arg == 'p')
4818 eap->flags |= EXFLAG_PRINT;
4819 else
4820 eap->flags |= EXFLAG_NR;
4821 eap->arg = skipwhite(eap->arg + 1);
4822 }
4823}
4824
4825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 * Function called for command which is Not Implemented. NI!
4827 */
4828 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004829ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830{
4831 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004832 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004833 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834}
4835
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004836#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837/*
4838 * Function called for script command which is Not Implemented. NI!
4839 * Skips over ":perl <<EOF" constructs.
4840 */
4841 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004842ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843{
4844 if (!eap->skip)
4845 ex_ni(eap);
4846 else
4847 vim_free(script_get(eap, eap->arg));
4848}
4849#endif
4850
4851/*
4852 * Check range in Ex command for validity.
4853 * Return NULL when valid, error message when invalid.
4854 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004855 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004856invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004858 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004859
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if ( eap->line1 < 0
4861 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004862 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004863 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004864
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004865 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004866 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004867 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004868 {
4869 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004870 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004871#ifdef FEAT_DIFF
4872 + (eap->cmdidx == CMD_diffget)
4873#endif
4874 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004875 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004876 break;
4877 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004878 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004879 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004880 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004881 break;
4882 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004883 // Only a boundary check, not whether the buffers actually
4884 // exist.
4885 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004886 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004887 break;
4888 case ADDR_LOADED_BUFFERS:
4889 buf = firstbuf;
4890 while (buf->b_ml.ml_mfp == NULL)
4891 {
4892 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004893 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004894 buf = buf->b_next;
4895 }
4896 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004897 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004898 buf = lastbuf;
4899 while (buf->b_ml.ml_mfp == NULL)
4900 {
4901 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004902 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004903 buf = buf->b_prev;
4904 }
4905 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004906 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004907 break;
4908 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004909 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004910 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004911 break;
4912 case ADDR_TABS:
4913 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004914 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004915 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004916 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004917 case ADDR_OTHER:
4918 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004919 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004920 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004921#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004922 // No error for value that is too big, will use the last entry.
4923 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004924 {
4925 if (eap->addr_count == 0)
4926 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004927 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004928 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004929#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004930 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004931 case ADDR_QUICKFIX_VALID:
4932#ifdef FEAT_QUICKFIX
4933 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4934 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004935 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004936#endif
4937 break;
4938 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004939 case ADDR_NONE:
4940 // Will give an error elsewhere.
4941 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004942 }
4943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 return NULL;
4945}
4946
4947/*
4948 * Correct the range for zero line number, if required.
4949 */
4950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004951correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004953 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
4955 if (eap->line1 == 0)
4956 eap->line1 = 1;
4957 if (eap->line2 == 0)
4958 eap->line2 = 1;
4959 }
4960}
4961
Bram Moolenaar748bf032005-02-02 23:04:36 +00004962#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004963/*
4964 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4965 * pattern. Otherwise return eap->arg.
4966 */
4967 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004968skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004969{
4970 char_u *p = eap->arg;
4971
Bram Moolenaara37420f2006-02-04 22:37:47 +00004972 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4973 || eap->cmdidx == CMD_vimgrepadd
4974 || eap->cmdidx == CMD_lvimgrepadd
4975 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004976 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004977 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004978 if (p == NULL)
4979 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004980 }
4981 return p;
4982}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004983
4984/*
4985 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4986 * in the command line, so that things like % get expanded.
4987 */
4988 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004989replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004990{
4991 char_u *new_cmdline;
4992 char_u *program;
4993 char_u *pos;
4994 char_u *ptr;
4995 int len;
4996 int i;
4997
4998 /*
4999 * Don't do it when ":vimgrep" is used for ":grep".
5000 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00005001 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
5002 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
5003 || eap->cmdidx == CMD_grepadd
5004 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005005 && !grep_internal(eap->cmdidx))
5006 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00005007 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
5008 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005009 {
5010 if (*curbuf->b_p_gp == NUL)
5011 program = p_gp;
5012 else
5013 program = curbuf->b_p_gp;
5014 }
5015 else
5016 {
5017 if (*curbuf->b_p_mp == NUL)
5018 program = p_mp;
5019 else
5020 program = curbuf->b_p_mp;
5021 }
5022
5023 p = skipwhite(p);
5024
5025 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5026 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005027 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005028 i = 1;
5029 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
5030 ++i;
5031 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005032 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005033 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005034 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005035 ptr = new_cmdline;
5036 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5037 {
5038 i = (int)(pos - program);
5039 STRNCPY(ptr, program, i);
5040 STRCPY(ptr += i, p);
5041 ptr += len;
5042 program = pos + 2;
5043 }
5044 STRCPY(ptr, program);
5045 }
5046 else
5047 {
John Marriotted908f72024-04-18 22:46:56 +02005048 size_t program_len = STRLEN(program);
5049
5050 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005051 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005052 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005053 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005054 STRCPY(new_cmdline + program_len, " ");
5055 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005056 }
5057 msg_make(p);
5058
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005059 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005060 vim_free(*cmdlinep);
5061 *cmdlinep = new_cmdline;
5062 p = new_cmdline;
5063 }
5064 return p;
5065}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005066#endif
5067
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068/*
5069 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005070 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 * Return FAIL for failure, OK otherwise.
5072 */
5073 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005074expand_filename(
5075 exarg_T *eap,
5076 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005077 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005079 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005081 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 char_u *p;
5083 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005084 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085
Bram Moolenaar748bf032005-02-02 23:04:36 +00005086#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005087 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005088 p = skip_grep_pat(eap);
5089#else
5090 p = eap->arg;
5091#endif
5092
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 /*
5094 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5095 * the file name contains a wildcard it should not cause expanding.
5096 * (it will be expanded anyway if there is a wildcard before replacing).
5097 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005098 has_wildcards = mch_has_wildcard(p);
5099 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005101#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005102 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005103 if (p[0] == '`' && p[1] == '=')
5104 {
5105 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005106 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005107 if (*p == '`')
5108 ++p;
5109 continue;
5110 }
5111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 /*
5113 * Quick check if this cannot be the start of a special string.
5114 * Also removes backslash before '%', '#' and '<'.
5115 */
5116 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5117 {
5118 ++p;
5119 continue;
5120 }
5121
5122 /*
5123 * Try to find a match at this position.
5124 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005125 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005126 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005127 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005129 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
5131 p += srclen;
5132 continue;
5133 }
5134
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005135 // Wildcards won't be expanded below, the replacement is taken
5136 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005137 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5138 {
5139 char_u *l = repl;
5140
5141 repl = expand_env_save(repl);
5142 vim_free(l);
5143 }
5144
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005145 // Need to escape white space et al. with a backslash.
5146 // Don't do this for:
5147 // - replacement that already has been escaped: "##"
5148 // - shell commands (may have to use quotes instead).
5149 // - non-unix systems when there is a single argument (spaces don't
5150 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005152 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 && eap->cmdidx != CMD_grep
5155 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005156 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005157 && eap->cmdidx != CMD_lgrep
5158 && eap->cmdidx != CMD_lgrepadd
5159 && eap->cmdidx != CMD_lmake
5160 && eap->cmdidx != CMD_make
5161 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005163 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164#endif
5165 )
5166 {
5167 char_u *l;
5168#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005169 // Don't escape a backslash here, because rem_backslash() doesn't
5170 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 static char_u *nobslash = (char_u *)" \t\"|";
5172# define ESCAPE_CHARS nobslash
5173#else
5174# define ESCAPE_CHARS escape_chars
5175#endif
5176
5177 for (l = repl; *l; ++l)
5178 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5179 {
5180 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5181 if (l != NULL)
5182 {
5183 vim_free(repl);
5184 repl = l;
5185 }
5186 break;
5187 }
5188 }
5189
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005190 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005191 if ((eap->usefilter || eap->cmdidx == CMD_bang
5192 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005193 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
5195 char_u *l;
5196
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005197 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (l != NULL)
5199 {
5200 vim_free(repl);
5201 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
5203 }
5204
Mike Williams51024bb2024-05-30 07:46:30 +02005205 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 vim_free(repl);
5207 if (p == NULL)
5208 return FAIL;
5209 }
5210
5211 /*
5212 * One file argument: Expand wildcards.
5213 * Don't do this with ":r !command" or ":w !command".
5214 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005215 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 {
5217 /*
5218 * May do this twice:
5219 * 1. Replace environment variables.
5220 * 2. Replace any other wildcards, remove backslashes.
5221 */
5222 for (n = 1; n <= 2; ++n)
5223 {
5224 if (n == 2)
5225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 /*
5227 * Halve the number of backslashes (this is Vi compatible).
5228 * For Unix and OS/2, when wildcards are expanded, this is
5229 * done by ExpandOne() below.
5230 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005231#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 if (!has_wildcards)
5233#endif
5234 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 }
5236
5237 if (has_wildcards)
5238 {
5239 if (n == 1)
5240 {
5241 /*
5242 * First loop: May expand environment variables. This
5243 * can be done much faster with expand_env() than with
5244 * something else (e.g., calling a shell).
5245 * After expanding environment variables, check again
5246 * if there are still wildcards present.
5247 */
5248 if (vim_strchr(eap->arg, '$') != NULL
5249 || vim_strchr(eap->arg, '~') != NULL)
5250 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005251 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005252 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 has_wildcards = mch_has_wildcard(NameBuff);
5254 p = NameBuff;
5255 }
5256 else
5257 p = NULL;
5258 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005259 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
5261 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005262 int options = WILD_LIST_NOTFOUND
5263 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
5265 ExpandInit(&xpc);
5266 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005267 if (p_wic)
5268 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005270 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 if (p == NULL)
5272 return FAIL;
5273 }
5274 if (p != NULL)
5275 {
John Marriotted908f72024-04-18 22:46:56 +02005276 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005278 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 vim_free(p);
5280 }
5281 }
5282 }
5283 }
5284 return OK;
5285}
5286
5287/*
5288 * Replace part of the command line, keeping eap->cmd, eap->arg and
5289 * eap->nextcmd correct.
5290 * "src" points to the part that is to be replaced, of length "srclen".
5291 * "repl" is the replacement string.
5292 * Returns a pointer to the character after the replaced string.
5293 * Returns NULL for failure.
5294 */
5295 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005296repl_cmdline(
5297 exarg_T *eap,
5298 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005299 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005300 char_u *repl,
5301 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
John Marriotted908f72024-04-18 22:46:56 +02005303 size_t repllen;
5304 size_t taillen;
5305 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005307 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308
5309 /*
5310 * The new command line is build in new_cmdline[].
5311 * First allocate it.
5312 * Careful: a "+cmd" argument may have been NUL terminated.
5313 */
John Marriotted908f72024-04-18 22:46:56 +02005314 repllen = STRLEN(repl);
5315 taillen = STRLEN(src + srclen);
5316 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005317 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005318 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005319 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005320 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321
5322 /*
5323 * Copy the stuff before the expanded part.
5324 * Copy the expanded stuff.
5325 * Copy what came after the expanded part.
5326 * Copy the next commands, if there are any.
5327 */
John Marriotted908f72024-04-18 22:46:56 +02005328 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5329 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005330
John Marriotted908f72024-04-18 22:46:56 +02005331 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5332 new_cmdlinelen += repllen; // remember the end of the string
5333 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5334 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005336 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
John Marriotted908f72024-04-18 22:46:56 +02005338 new_cmdlinelen += taillen + 1;
5339 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5340 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5343 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5344 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5345 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5346 vim_free(*cmdlinep);
5347 *cmdlinep = new_cmdline;
5348
5349 return src;
5350}
5351
5352/*
5353 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005354 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 */
5356 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005357separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358{
5359 char_u *p;
5360
Bram Moolenaar86b68352004-12-27 21:59:20 +00005361#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005362 p = skip_grep_pat(eap);
5363#else
5364 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005365#endif
5366
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005367 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 {
5369 if (*p == Ctrl_V)
5370 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005371 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005372 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005374 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005375 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005376 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 break;
5378 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005379
5380#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005381 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005382 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005383 {
5384 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005385 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005386 if (*p == NUL) // stop at NUL after CTRL-V
5387 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005388 }
5389#endif
5390
Bram Moolenaareda29c92022-10-02 12:59:00 +01005391 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005392 // :@" and :*" do not start a comment!
5393 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005394 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005395 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005396 && !(eap->argt & EX_NOTRLCOM)
5397 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5398 || p != eap->arg)
5399 && (eap->cmdidx != CMD_redir
5400 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005401 || (*p == '#'
5402 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005403 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005404 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005405 || (*p == '|'
5406 && eap->cmdidx != CMD_append
5407 && eap->cmdidx != CMD_change
5408 && eap->cmdidx != CMD_insert)
5409 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 {
5411 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005412 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 * AND 'b' is present in 'cpoptions'.
5414 */
5415 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005416 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005418 if (!keep_backslash)
5419 {
5420 STRMOVE(p - 1, p); // remove the '\'
5421 --p;
5422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
5424 else
5425 {
5426 eap->nextcmd = check_nextcmd(p);
5427 *p = NUL;
5428 break;
5429 }
5430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005433 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 del_trailing_spaces(eap->arg);
5435}
5436
5437/*
5438 * get + command from ex argument
5439 */
5440 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005441getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
5443 char_u *arg = *argp;
5444 char_u *command = NULL;
5445
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005446 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 {
5448 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005449 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 command = dollar_command;
5451 else
5452 {
5453 command = arg;
5454 arg = skip_cmd_arg(command, TRUE);
5455 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005456 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
5458
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005459 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 *argp = arg;
5461 }
5462 return command;
5463}
5464
5465/*
5466 * Find end of "+command" argument. Skip over "\ " and "\\".
5467 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005468 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005469skip_cmd_arg(
5470 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005471 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
5473 while (*p && !vim_isspace(*p))
5474 {
5475 if (*p == '\\' && p[1] != NUL)
5476 {
5477 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005478 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 else
5480 ++p;
5481 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005482 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484 return p;
5485}
5486
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005487 int
5488get_bad_opt(char_u *p, exarg_T *eap)
5489{
5490 if (STRICMP(p, "keep") == 0)
5491 eap->bad_char = BAD_KEEP;
5492 else if (STRICMP(p, "drop") == 0)
5493 eap->bad_char = BAD_DROP;
5494 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5495 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005496 else
5497 return FAIL;
5498 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005499}
5500
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005502 * Function given to ExpandGeneric() to obtain the list of bad= names.
5503 */
5504 static char_u *
5505get_bad_name(expand_T *xp UNUSED, int idx)
5506{
5507 // Note: Keep this in sync with getargopt.
5508 static char *(p_bad_values[]) =
5509 {
5510 "?",
5511 "keep",
5512 "drop",
5513 };
5514
John Marriotted908f72024-04-18 22:46:56 +02005515 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5516 return NULL;
5517
5518 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005519}
5520
5521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 * Get "++opt=arg" argument.
5523 * Return FAIL or OK.
5524 */
5525 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005526getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
5528 char_u *arg = eap->arg + 2;
5529 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005530 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005533 // Note: Keep this in sync with get_argopt_name.
5534
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005535 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5537 {
5538 if (*arg == 'n')
5539 {
5540 arg += 2;
5541 eap->force_bin = FORCE_NOBIN;
5542 }
5543 else
5544 eap->force_bin = FORCE_BIN;
5545 if (!checkforcmd(&arg, "binary", 3))
5546 return FAIL;
5547 eap->arg = skipwhite(arg);
5548 return OK;
5549 }
5550
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005551 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005552 if (STRNCMP(arg, "edit", 4) == 0)
5553 {
5554 eap->read_edit = TRUE;
5555 eap->arg = skipwhite(arg + 4);
5556 return OK;
5557 }
5558
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 if (STRNCMP(arg, "ff", 2) == 0)
5560 {
5561 arg += 2;
5562 pp = &eap->force_ff;
5563 }
5564 else if (STRNCMP(arg, "fileformat", 10) == 0)
5565 {
5566 arg += 10;
5567 pp = &eap->force_ff;
5568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 else if (STRNCMP(arg, "enc", 3) == 0)
5570 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005571 if (STRNCMP(arg, "encoding", 8) == 0)
5572 arg += 8;
5573 else
5574 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 pp = &eap->force_enc;
5576 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005577 else if (STRNCMP(arg, "bad", 3) == 0)
5578 {
5579 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005580 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582
5583 if (pp == NULL || *arg != '=')
5584 return FAIL;
5585
5586 ++arg;
5587 *pp = (int)(arg - eap->cmd);
5588 arg = skip_cmd_arg(arg, FALSE);
5589 eap->arg = skipwhite(arg);
5590 *arg = NUL;
5591
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 if (pp == &eap->force_ff)
5593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5595 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005596 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005598 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005600 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5602 *p = TOLOWER_ASC(*p);
5603 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005604 else
5605 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005606 // Check ++bad= argument. Must be a single-byte character, "keep" or
5607 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005608 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005609 return FAIL;
5610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
5612 return OK;
5613}
5614
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005615/*
5616 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5617 */
5618 static char_u *
5619get_argopt_name(expand_T *xp UNUSED, int idx)
5620{
5621 // Note: Keep this in sync with getargopt.
5622 static char *(p_opt_values[]) =
5623 {
5624 "fileformat=",
5625 "encoding=",
5626 "binary",
5627 "nobinary",
5628 "bad=",
5629 "edit",
5630 };
5631
John Marriotted908f72024-04-18 22:46:56 +02005632 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5633 return NULL;
5634
5635 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005636}
5637
5638/*
5639 * Command-line expansion for ++opt=name.
5640 */
5641 int
5642expand_argopt(
5643 char_u *pat,
5644 expand_T *xp,
5645 regmatch_T *rmp,
5646 char_u ***matches,
5647 int *numMatches)
5648{
5649 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5650 {
5651 char_u *(*cb)(expand_T *, int) = NULL;
5652
5653 char_u *name_end = xp->xp_pattern - 1;
5654 if (name_end - xp->xp_line >= 2
5655 && STRNCMP(name_end - 2, "ff", 2) == 0)
5656 cb = get_fileformat_name;
5657 else if (name_end - xp->xp_line >= 10
5658 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5659 cb = get_fileformat_name;
5660 else if (name_end - xp->xp_line >= 3
5661 && STRNCMP(name_end - 3, "enc", 3) == 0)
5662 cb = get_encoding_name;
5663 else if (name_end - xp->xp_line >= 8
5664 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5665 cb = get_encoding_name;
5666 else if (name_end - xp->xp_line >= 3
5667 && STRNCMP(name_end - 3, "bad", 3) == 0)
5668 cb = get_bad_name;
5669
5670 if (cb != NULL)
5671 {
5672 return ExpandGeneric(
5673 pat,
5674 xp,
5675 rmp,
5676 matches,
5677 numMatches,
5678 cb,
5679 FALSE);
5680 }
5681 return FAIL;
5682 }
5683
5684 // Special handling of "ff" which acts as a short form of
5685 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005686 if (xp->xp_pattern_len == 2
5687 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005688 {
5689 *matches = ALLOC_MULT(char_u *, 1);
5690 if (*matches == NULL)
5691 return FAIL;
5692 *numMatches = 1;
5693 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5694 return OK;
5695 }
5696
5697 return ExpandGeneric(
5698 pat,
5699 xp,
5700 rmp,
5701 matches,
5702 numMatches,
5703 get_argopt_name,
5704 FALSE);
5705}
5706
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005708ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
5710 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005711 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 * directory for security reasons.
5713 */
5714 if (secure)
5715 {
5716 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005717 eap->errmsg =
5718 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005721 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 else
5723 do_augroup(eap->arg, eap->forceit);
5724}
5725
5726/*
5727 * ":doautocmd": Apply the automatic commands to the current buffer.
5728 */
5729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005730ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005732 char_u *arg = eap->arg;
5733 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005734 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005735
Bram Moolenaar1610d052016-06-09 22:53:01 +02005736 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005737 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005738 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005739 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742/*
5743 * :[N]bunload[!] [N] [bufname] unload buffer
5744 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5745 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5746 */
5747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005748ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005750 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005751 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 eap->errmsg = do_bufdel(
5753 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5754 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5755 : DOBUF_UNLOAD, eap->arg,
5756 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5757}
5758
5759/*
5760 * :[N]buffer [N] to buffer N
5761 * :[N]sbuffer [N] to buffer N
5762 */
5763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005764ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005766 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005767 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005769 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 else
5771 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005772 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5774 else
5775 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005776 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005777 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
5779}
5780
5781/*
5782 * :[N]bmodified [N] to next mod. buffer
5783 * :[N]sbmodified [N] to next mod. buffer
5784 */
5785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005786ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
5788 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005789 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005790 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791}
5792
5793/*
5794 * :[N]bnext [N] to next buffer
5795 * :[N]sbnext [N] split and to next buffer
5796 */
5797 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005798ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005800 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005801 return;
5802
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005804 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005805 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806}
5807
5808/*
5809 * :[N]bNext [N] to previous buffer
5810 * :[N]bprevious [N] to previous buffer
5811 * :[N]sbNext [N] split and to previous buffer
5812 * :[N]sbprevious [N] split and to previous buffer
5813 */
5814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005815ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005817 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005818 return;
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005821 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005822 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823}
5824
5825/*
5826 * :brewind to first buffer
5827 * :bfirst to first buffer
5828 * :sbrewind split and to first buffer
5829 * :sbfirst split and to first buffer
5830 */
5831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005832ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005834 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005835 return;
5836
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005838 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005839 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840}
5841
5842/*
5843 * :blast to last buffer
5844 * :sblast split and to last buffer
5845 */
5846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005847ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005849 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005850 return;
5851
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005853 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005854 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
Bram Moolenaar7a092242020-04-16 22:10:49 +02005857/*
5858 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005859 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005862ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005864 int comment_char = '"';
5865
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005866 if (in_vim9script())
5867 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005868 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005869}
5870
5871/*
5872 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5873 * to "cmd_start" or has a white space character before it.
5874 */
5875 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005876ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005877{
5878 int c = *cmd;
5879
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005880 if (c == NUL || c == '|' || c == '\n')
5881 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005882 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005883 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5884 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005885 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005886 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887}
5888
5889#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5890 || defined(PROTO)
5891/*
5892 * Return the next command, after the first '|' or '\n'.
5893 * Return NULL if not found.
5894 */
5895 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005896find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
5898 while (*p != '|' && *p != '\n')
5899 {
5900 if (*p == NUL)
5901 return NULL;
5902 ++p;
5903 }
5904 return (p + 1);
5905}
5906#endif
5907
5908/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005909 * Check if *p is a separator between Ex commands, skipping over white space.
5910 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 */
5912 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005913check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005915 char_u *s = skipwhite(p);
5916
5917 if (*s == '|' || *s == '\n')
5918 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 else
5920 return NULL;
5921}
5922
5923/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005924 * If "eap->nextcmd" is not set, check for a next command at "p".
5925 */
5926 void
5927set_nextcmd(exarg_T *eap, char_u *arg)
5928{
5929 char_u *p = check_nextcmd(arg);
5930
5931 if (eap->nextcmd == NULL)
5932 eap->nextcmd = p;
5933 else if (p != NULL)
5934 // cannot use "| command" inside a {} block
5935 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5936}
5937
5938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 * - if there are more files to edit
5940 * - and this is the last window
5941 * - and forceit not used
5942 * - and not repeated twice on a row
5943 * return FAIL and give error message if 'message' TRUE
5944 * return OK otherwise
5945 */
5946 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005947check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005948 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005949 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5952
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005953 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005954 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 {
5956 if (message)
5957 {
5958#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005959 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5960 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005962 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005964 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5965 NGETTEXT("%d more file to edit. Quit anyway?",
5966 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5968 return OK;
5969 return FAIL;
5970 }
5971#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005972 semsg(NGETTEXT(e_nr_more_file_to_edit,
5973 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005974 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 }
5976 return FAIL;
5977 }
5978 return OK;
5979}
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981/*
5982 * Function given to ExpandGeneric() to obtain the list of command names.
5983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005985get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986{
5987 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005988 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 return cmdnames[idx].cmd_name;
5990}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005993ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
Bram Moolenaare6850792010-05-14 15:28:44 +02005995 if (*eap->arg == NUL)
5996 {
5997#ifdef FEAT_EVAL
5998 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5999 char_u *p = NULL;
6000
6001 if (expr != NULL)
6002 {
6003 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006004 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02006005 --emsg_off;
6006 vim_free(expr);
6007 }
6008 if (p != NULL)
6009 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006010 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02006011 vim_free(p);
6012 }
6013 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006014 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02006015#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006016 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02006017#endif
6018 }
6019 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00006020 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006021
6022#ifdef FEAT_VTP
6023 else if (has_vtp_working())
6024 {
6025 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006026 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006027 redrawcmd();
6028 }
6029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030}
6031
6032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006033ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034{
6035 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006036 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 do_highlight(eap->arg, eap->forceit, FALSE);
6038}
6039
6040
6041/*
6042 * Call this function if we thought we were going to exit, but we won't
6043 * (because of an error). May need to restore the terminal mode.
6044 */
6045 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006046not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047{
6048 exiting = FALSE;
6049 settmode(TMODE_RAW);
6050}
6051
Bram Moolenaar3552e742021-05-29 12:21:58 +02006052 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006053before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6054{
6055 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6056
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006057 // Bail out when autocommands closed the window.
6058 // Refuse to quit when the buffer in the last window is being closed (can
6059 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006060 if (!win_valid(wp)
6061 || curbuf_locked()
6062 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6063 return TRUE;
6064
6065 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6066 {
6067 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006068 // Refuse to quit when locked or when the window was closed or the
6069 // buffer in the last window is being closed (can only happen in
6070 // autocommands).
6071 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006072 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6073 return TRUE;
6074 }
6075
6076 return FALSE;
6077}
6078
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006080 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006081 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006082 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006084 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006085ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006087 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006088
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 if (cmdwin_type != 0)
6090 {
6091 cmdwin_result = Ctrl_C;
6092 return;
6093 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006094 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006095 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006096 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006097 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006098 return;
6099 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006100 if (eap->addr_count > 0)
6101 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006102 int wnr = eap->line2;
6103
6104 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6105 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006106 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006107 }
6108 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006109 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006110
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006111 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006112 if (curbuf_locked())
6113 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006115 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006116 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006117 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
6119#ifdef FEAT_NETBEANS_INTG
6120 netbeansForcedQuit = eap->forceit;
6121#endif
6122
6123 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006124 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 */
6126 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6127 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006128 if ((!buf_hide(wp->w_buffer)
6129 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006130 | (eap->forceit ? CCGD_FORCEIT : 0)
6131 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006133 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 {
6135 not_exiting();
6136 }
6137 else
6138 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006139 // quit last window
6140 // Note: only_one_window() returns true, even so a help window is
6141 // still open. In that case only quit, if no address has been
6142 // specified. Example:
6143 // :h|wincmd w|1q - don't quit
6144 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006145 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006147 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006148#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006150#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006151 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006152 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 }
6154}
6155
6156/*
6157 * ":cquit".
6158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006160ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006162 // this does not always pass on the exit code to the Manx compiler. why?
6163 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164}
6165
6166/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006167 * Do preparations for "qall" and "wqall".
6168 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006170 int
6171before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 if (cmdwin_type != 0)
6174 {
6175 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006176 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 else
6178 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006179 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006181
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006182 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006183 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006184 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006185 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006186 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006187 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006188
6189 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006190 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006191
Bram Moolenaar411da642023-05-10 16:53:27 +01006192 return OK;
6193}
6194
6195/*
6196 * ":qall": try to quit all windows
6197 */
6198 static void
6199ex_quit_all(exarg_T *eap)
6200{
6201 if (before_quit_all(eap) == FAIL)
6202 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006204 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 getout(0);
6206 not_exiting();
6207}
6208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209/*
6210 * ":close": close current window, unless it is the last one
6211 */
6212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006213ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006215 win_T *win;
6216 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006218 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006220 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006221 {
6222 if (eap->addr_count == 0)
6223 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006224 else
6225 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006226 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006227 {
6228 winnr++;
6229 if (winnr == eap->line2)
6230 break;
6231 }
6232 if (win == NULL)
6233 win = lastwin;
6234 ex_win_close(eap->forceit, win, NULL);
6235 }
6236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237}
6238
Bram Moolenaar4033c552017-09-16 20:54:51 +02006239#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006240/*
6241 * ":pclose": Close any preview window.
6242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006244ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006245{
6246 win_T *win;
6247
Bram Moolenaar79648732019-07-18 21:43:07 +02006248 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006249 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006250 if (win->w_p_pvw)
6251 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006252 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006253 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006254 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006255# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006256 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006257 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006258# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006259}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006260#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006261
Bram Moolenaarf740b292006-02-16 22:11:02 +00006262/*
6263 * Close window "win" and take care of handling closing the last window for a
6264 * modified buffer.
6265 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006267ex_win_close(
6268 int forceit,
6269 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006270 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271{
6272 int need_hide;
6273 buf_T *buf = win->w_buffer;
6274
Bram Moolenaarcf844172020-06-26 19:44:06 +02006275 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006276 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006277 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006278 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006279 return;
6280 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006281 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006282 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006283
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006285 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006287#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006288 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006290# ifdef FEAT_TERMINAL
6291 if (term_job_running(buf->b_term))
6292 {
6293 if (term_confirm_stop(buf) == FAIL)
6294 return;
6295 // Manually kill the terminal here because this command will
6296 // hide it otherwise.
6297 free_terminal(buf);
6298 need_hide = FALSE;
6299 }
6300 else
6301# endif
6302 {
6303 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006304
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006305 set_bufref(&bufref, buf);
6306 dialog_changed(buf, FALSE);
6307 if (bufref_valid(&bufref) && bufIsChanged(buf))
6308 return;
6309 need_hide = FALSE;
6310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 }
6312 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006315 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 return;
6317 }
6318 }
6319
Bram Moolenaar4033c552017-09-16 20:54:51 +02006320#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006322#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006323
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006324 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006325 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006326 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006327 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006328 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329}
6330
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006332 * Handle the argument for a tabpage related ex command.
6333 * Returns a tabpage number.
6334 * When an error is encountered then eap->errmsg is set.
6335 */
6336 static int
6337get_tabpage_arg(exarg_T *eap)
6338{
6339 int tab_number;
6340 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6341
6342 if (eap->arg && *eap->arg != NUL)
6343 {
6344 char_u *p = eap->arg;
6345 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006346 int relative = 0; // argument +N/-N means: go to N places to the
6347 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006348
6349 if (*p == '-')
6350 {
6351 relative = -1;
6352 p++;
6353 }
6354 else if (*p == '+')
6355 {
6356 relative = 1;
6357 p++;
6358 }
6359
6360 p_save = p;
6361 tab_number = getdigits(&p);
6362
6363 if (relative == 0)
6364 {
6365 if (STRCMP(p, "$") == 0)
6366 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006367 else if (STRCMP(p, "#") == 0)
6368 if (valid_tabpage(lastused_tabpage))
6369 tab_number = tabpage_index(lastused_tabpage);
6370 else
6371 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006372 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6373 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006374 tab_number = 0;
6375 goto theend;
6376 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006377 else if (p == p_save || *p_save == '-' || *p != NUL
6378 || tab_number > LAST_TAB_NR)
6379 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006380 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006381 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006382 goto theend;
6383 }
6384 }
6385 else
6386 {
6387 if (*p_save == NUL)
6388 tab_number = 1;
6389 else if (p == p_save || *p_save == '-' || *p != NUL
6390 || tab_number == 0)
6391 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006392 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006393 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006394 goto theend;
6395 }
6396 tab_number = tab_number * relative + tabpage_index(curtab);
6397 if (!unaccept_arg0 && relative == -1)
6398 --tab_number;
6399 }
6400 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006401 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006402 }
6403 else if (eap->addr_count > 0)
6404 {
6405 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006406 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006407 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006408 tab_number = 0;
6409 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006410 else
6411 {
6412 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006413 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006414 {
zeertzjq076faac2024-03-25 16:41:06 +01006415 char_u *cmdp = eap->cmd;
6416
6417 while (--cmdp > *eap->cmdlinep
6418 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6419 ;
6420 if (*cmdp == '-')
6421 {
6422 --tab_number;
6423 if (tab_number < unaccept_arg0)
6424 eap->errmsg = _(e_invalid_range);
6425 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006426 }
6427 }
6428 }
6429 else
6430 {
6431 switch (eap->cmdidx)
6432 {
6433 case CMD_tabnext:
6434 tab_number = tabpage_index(curtab) + 1;
6435 if (tab_number > LAST_TAB_NR)
6436 tab_number = 1;
6437 break;
6438 case CMD_tabmove:
6439 tab_number = LAST_TAB_NR;
6440 break;
6441 default:
6442 tab_number = tabpage_index(curtab);
6443 }
6444 }
6445
6446theend:
6447 return tab_number;
6448}
6449
6450/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006451 * ":tabclose": close current tab page, unless it is the last one.
6452 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 */
6454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006455ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006457 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006458 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006459
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006460 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006461 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006462 cmdwin_result = K_IGNORE;
6463 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006464 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006465
6466 if (first_tabpage->tp_next == NULL)
6467 {
6468 emsg(_(e_cannot_close_last_tab_page));
6469 return;
6470 }
6471
6472 if (window_layout_locked(CMD_tabclose))
6473 return;
6474
6475 tab_number = get_tabpage_arg(eap);
6476 if (eap->errmsg != NULL)
6477 return;
6478
6479 tp = find_tabpage(tab_number);
6480 if (tp == NULL)
6481 {
6482 beep_flush();
6483 return;
6484 }
6485 if (tp != curtab)
6486 {
6487 tabpage_close_other(tp, eap->forceit);
6488 return;
6489 }
6490 else if (!text_locked() && !curbuf_locked())
6491 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006492}
6493
6494/*
6495 * ":tabonly": close all tab pages except the current one
6496 */
6497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006498ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006499{
6500 tabpage_T *tp;
6501 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006502 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006503
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006504 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006505 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006506 cmdwin_result = K_IGNORE;
6507 return;
6508 }
6509
6510 if (first_tabpage->tp_next == NULL)
6511 {
6512 msg(_("Already only one tab page"));
6513 return;
6514 }
6515
6516 if (window_layout_locked(CMD_tabonly))
6517 return;
6518
6519 tab_number = get_tabpage_arg(eap);
6520 if (eap->errmsg != NULL)
6521 return;
6522
6523 goto_tabpage(tab_number);
6524 // Repeat this up to a 1000 times, because autocommands may
6525 // mess up the lists.
6526 for (done = 0; done < 1000; ++done)
6527 {
6528 FOR_ALL_TABPAGES(tp)
6529 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006530 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006531 tabpage_close_other(tp, eap->forceit);
6532 // if we failed to close it quit
6533 if (valid_tabpage(tp))
6534 done = 1000;
6535 // start over, "tp" is now invalid
6536 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006537 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006538 if (first_tabpage->tp_next == NULL)
6539 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542
6543/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006544 * Close the current tab page.
6545 */
6546 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006547tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006548{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006549 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006550 return;
6551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006552 // First close all the windows but the current one. If that worked then
6553 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006554 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006555 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006556 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006557 ex_win_close(forceit, curwin, NULL);
6558# ifdef FEAT_GUI
6559 need_mouse_correct = TRUE;
6560# endif
6561}
6562
6563/*
6564 * Close tab page "tp", which is not the current tab page.
6565 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006566 * Also takes care of the tab pages line disappearing when closing the
6567 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006568 */
6569 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006570tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006571{
6572 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006573 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006574
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006575 // Limit to 1000 windows, autocommands may add a window while we close
6576 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006577 while (++done < 1000)
6578 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006579 wp = tp->tp_firstwin;
6580 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006581
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006582 // Autocommands may delete the tab page under our fingers and we may
6583 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006584 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006585 break;
6586 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006587
Bram Moolenaar29323592016-07-24 22:04:11 +02006588 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006589}
6590
6591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 * ":only".
6593 */
6594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006595ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006597 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006598 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599# ifdef FEAT_GUI
6600 need_mouse_correct = TRUE;
6601# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006602 if (eap->addr_count > 0)
6603 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006604 win_T *wp;
6605 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006606 for (wp = firstwin; --wnr > 0; )
6607 {
6608 if (wp->w_next == NULL)
6609 break;
6610 else
6611 wp = wp->w_next;
6612 }
6613 win_goto(wp);
6614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 close_others(TRUE, eap->forceit);
6616}
6617
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006619ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006621 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006622 if (eap->skip)
6623 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006624
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006625 if (window_layout_locked(CMD_hide))
6626 return;
6627#ifdef FEAT_GUI
6628 need_mouse_correct = TRUE;
6629#endif
6630 if (eap->addr_count == 0)
6631 win_close(curwin, FALSE); // don't free buffer
6632 else
6633 {
6634 int winnr = 0;
6635 win_T *win;
6636
6637 FOR_ALL_WINDOWS(win)
6638 {
6639 winnr++;
6640 if (winnr == eap->line2)
6641 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006643 if (win == NULL)
6644 win = lastwin;
6645 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 }
6647}
6648
6649/*
6650 * ":stop" and ":suspend": Suspend Vim.
6651 */
dbivolaruab16ad32021-12-29 19:41:47 +00006652 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006653ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
6655 /*
6656 * Disallow suspending for "rvim".
6657 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006658 if (check_restricted())
6659 return;
6660
6661 if (!eap->forceit)
6662 autowrite_all();
6663 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6664 windgoto((int)Rows - 1, 0);
6665 out_char('\n');
6666 out_flush();
6667 stoptermcap();
6668 out_flush(); // needed for SUN to restore xterm buffer
6669 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6670 ui_suspend(); // call machine specific function
6671 maketitle();
6672 resettitle(); // force updating the title
6673 starttermcap();
6674 scroll_start(); // scroll screen before redrawing
6675 redraw_later_clear();
6676 shell_resized(); // may have resized window
6677 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678}
6679
6680/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006681 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 */
6683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006684ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006686#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006687 if (not_in_vim9(eap) == FAIL)
6688 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 if (cmdwin_type != 0)
6691 {
6692 cmdwin_result = Ctrl_C;
6693 return;
6694 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006695 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006696 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006697 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006698 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006699 return;
6700 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006701
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006703 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 */
6705 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6706 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006707
6708 // Write the buffer for ":wq" or when it was changed.
6709 // Trigger QuitPre and ExitPre.
6710 // Check if we can exit now, after autocommands have changed things.
6711 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6712 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006714 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {
6716 not_exiting();
6717 }
6718 else
6719 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006720 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006722 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723# ifdef FEAT_GUI
6724 need_mouse_correct = TRUE;
6725# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006726 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006727 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 }
6729}
6730
6731/*
6732 * ":print", ":list", ":number".
6733 */
6734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006735ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006737 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006738 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006739 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006741 for ( ;!got_int; ui_breakcheck())
6742 {
6743 print_line(eap->line1,
6744 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6745 || (eap->flags & EXFLAG_NR)),
6746 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6747 if (++eap->line1 > eap->line2)
6748 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006749 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006750 }
6751 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006752 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006753 curwin->w_cursor.lnum = eap->line2;
6754 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 }
6756
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758}
6759
6760#ifdef FEAT_BYTEOFF
6761 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006762ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763{
6764 goto_byte(eap->line2);
6765}
6766#endif
6767
6768/*
6769 * ":shell".
6770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006772ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773{
6774 do_shell(NULL, 0);
6775}
6776
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006777#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006779static int drop_busy = FALSE;
6780static int drop_filec;
6781static char_u **drop_filev = NULL;
6782static int drop_split;
6783static void (*drop_callback)(void *);
6784static void *drop_cookie;
6785
6786 static void
6787handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
6789 exarg_T ea;
6790 int save_msg_scroll = msg_scroll;
6791
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006792 // Setting the argument list may cause screen updates and being called
6793 // recursively. Avoid that by setting drop_busy.
6794 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006796 // Check whether the current buffer is changed. If so, we will need
6797 // to split the current window or data could be lost.
6798 // We don't need to check if the 'hidden' option is set, as in this
6799 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006800 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
6802 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006803 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 --emsg_off;
6805 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006806 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 if (win_split(0, 0) == FAIL)
6809 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006810 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006812 // When splitting the window, create a new alist. Otherwise the
6813 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 alist_unlink(curwin->w_alist);
6815 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 }
6817
6818 /*
6819 * Set up the new argument list.
6820 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006821 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822
6823 /*
6824 * Move to the first file.
6825 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006826 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006827 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 ea.cmd = (char_u *)"next";
6829 do_argfile(&ea, 0);
6830
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006831 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6832 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 need_start_insertmode = FALSE;
6834
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006835 // Restore msg_scroll, otherwise a following command may cause scrolling
6836 // unexpectedly. The screen will be redrawn by the caller, thus
6837 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006839
6840 if (drop_callback != NULL)
6841 drop_callback(drop_cookie);
6842
6843 drop_filev = NULL;
6844 drop_busy = FALSE;
6845}
6846
6847/*
6848 * Handle a file drop. The code is here because a drop is *nearly* like an
6849 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006850 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006851 * code is very similar to :args and hence needs access to a lot of the static
6852 * functions in this file.
6853 *
6854 * The "filev" list must have been allocated using alloc(), as should each item
6855 * in the list. This function takes over responsibility for freeing the "filev"
6856 * list.
6857 */
6858 void
6859handle_drop(
6860 int filec, // the number of files dropped
6861 char_u **filev, // the list of files dropped
6862 int split, // force splitting the window
6863 void (*callback)(void *), // to be called after setting the argument
6864 // list
6865 void *cookie) // argument for "callback" (allocated)
6866{
6867 // Cannot handle recursive drops, finish the pending one.
6868 if (drop_busy)
6869 {
6870 FreeWild(filec, filev);
6871 vim_free(cookie);
6872 return;
6873 }
6874
6875 // When calling handle_drop() more than once in a row we only use the last
6876 // one.
6877 if (drop_filev != NULL)
6878 {
6879 FreeWild(drop_filec, drop_filev);
6880 vim_free(drop_cookie);
6881 }
6882
6883 drop_filec = filec;
6884 drop_filev = filev;
6885 drop_split = split;
6886 drop_callback = callback;
6887 drop_cookie = cookie;
6888
6889 // Postpone this when:
6890 // - editing the command line
6891 // - not possible to change the current buffer
6892 // - updating the screen
6893 // As it may change buffers and window structures that are in use and cause
6894 // freed memory to be used.
6895 if (text_locked() || curbuf_locked() || updating_screen)
6896 return;
6897
6898 handle_drop_internal();
6899}
6900
6901/*
6902 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6903 * reset: Handle a postponed drop.
6904 */
6905 void
6906handle_any_postponed_drop(void)
6907{
6908 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006909 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006910 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911}
6912#endif
6913
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 * ":preserve".
6916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006918ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006920 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 ml_preserve(curbuf, TRUE);
6922}
6923
6924/*
6925 * ":recover".
6926 */
6927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006928ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006930 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006932 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6933 | CCGD_MULTWIN
6934 | (eap->forceit ? CCGD_FORCEIT : 0)
6935 | CCGD_EXCMD)
6936
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 && (*eap->arg == NUL
6938 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006939 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 recoverymode = FALSE;
6941}
6942
6943/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006944 * Command modifier used in a wrong way. Also for other commands that can't
6945 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 */
6947 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006948ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006950 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951}
6952
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953/*
6954 * :sview [+command] file split window with new file, read-only
6955 * :split [[+command] file] split window with current or new file
6956 * :vsplit [[+command] file] split window vertically with current or new file
6957 * :new [[+command] file] split window with no or new file
6958 * :vnew [[+command] file] split vertically window with no or new file
6959 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006960 *
6961 * :tabedit open new Tab page with empty window
6962 * :tabedit [+command] file open new Tab page and edit "file"
6963 * :tabnew [[+command] file] just like :tabedit
6964 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 */
6966 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006967ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006969 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006971#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006972 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006973 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006974#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006975 int use_tab = eap->cmdidx == CMD_tabedit
6976 || eap->cmdidx == CMD_tabfind
6977 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006979 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006980 return;
6981
Bram Moolenaar4033c552017-09-16 20:54:51 +02006982#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
Bram Moolenaar4033c552017-09-16 20:54:51 +02006986#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006987 // A ":split" in the quickfix window works like ":new". Don't want two
6988 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006989 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {
6991 if (eap->cmdidx == CMD_split)
6992 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (eap->cmdidx == CMD_vsplit)
6994 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006998 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007000 char_u *file_to_find = NULL;
7001 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007003 FNAME_MESS, TRUE, curbuf->b_ffname,
7004 &file_to_find, &search_ctx);
7005 vim_free(file_to_find);
7006 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (fname == NULL)
7008 goto theend;
7009 eap->arg = fname;
7010 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007011# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007012 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 && eap->cmdidx != CMD_new)
7015 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007016 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007017# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007018 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007019# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007020 au_has_group((char_u *)"FileExplorer"))
7021 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007022 // No browsing supported but we do have the file explorer:
7023 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007024 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007025 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007026 }
7027 else
7028 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02007029 fname = do_browse(0, (char_u *)(use_tab
7030 ? _("Edit File in new tab page")
7031 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007033 if (fname == NULL)
7034 goto theend;
7035 eap->arg = fname;
7036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007038 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007041 /*
7042 * Either open new tab page or split the window.
7043 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007044 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007045 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007046 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007047 : eap->addr_count == 0 ? 0
7048 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007049 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007050 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007051
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007052 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007053 if (curwin != old_curwin
7054 && win_valid(old_curwin)
7055 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007056 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007057 old_curwin->w_alt_fnum = curbuf->b_fnum;
7058 }
7059 }
7060 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7062 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007063 // Reset 'scrollbind' when editing another file, but keep it when
7064 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007065 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007066 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 else
7068 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 do_exedit(eap, old_curwin);
7070 }
7071
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007072# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007073 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076theend:
7077 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007079
7080/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007081 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007082 */
7083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007084tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007085{
7086 exarg_T ea;
7087
Bram Moolenaara80faa82020-04-12 19:37:17 +02007088 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007089 ea.cmdidx = CMD_tabnew;
7090 ea.cmd = (char_u *)"tabn";
7091 ea.arg = (char_u *)"";
7092 ex_splitview(&ea);
7093}
7094
7095/*
7096 * :tabnext command
7097 */
7098 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007099ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007100{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007101 int tab_number;
7102
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007103 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007104 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007105 switch (eap->cmdidx)
7106 {
7107 case CMD_tabfirst:
7108 case CMD_tabrewind:
7109 goto_tabpage(1);
7110 break;
7111 case CMD_tablast:
7112 goto_tabpage(9999);
7113 break;
7114 case CMD_tabprevious:
7115 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007116 if (eap->arg && *eap->arg != NUL)
7117 {
7118 char_u *p = eap->arg;
7119 char_u *p_save = p;
7120
7121 tab_number = getdigits(&p);
7122 if (p == p_save || *p_save == '-' || *p != NUL
7123 || tab_number == 0)
7124 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007125 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007126 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007127 return;
7128 }
7129 }
7130 else
7131 {
7132 if (eap->addr_count == 0)
7133 tab_number = 1;
7134 else
7135 {
7136 tab_number = eap->line2;
7137 if (tab_number < 1)
7138 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007139 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007140 return;
7141 }
7142 }
7143 }
7144 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007145 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007146 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007147 tab_number = get_tabpage_arg(eap);
7148 if (eap->errmsg == NULL)
7149 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007150 break;
7151 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007152}
7153
7154/*
7155 * :tabmove command
7156 */
7157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007158ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007159{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007160 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007161
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007162 tab_number = get_tabpage_arg(eap);
7163 if (eap->errmsg == NULL)
7164 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007165}
7166
7167/*
7168 * :tabs command: List tabs and their contents.
7169 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007171ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007172{
7173 tabpage_T *tp;
7174 win_T *wp;
7175 int tabcount = 1;
7176
7177 msg_start();
7178 msg_scroll = TRUE;
7179 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7180 {
7181 msg_putchar('\n');
7182 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007183 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007184 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007185 ui_breakcheck();
7186
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007187 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007188 wp = firstwin;
7189 else
7190 wp = tp->tp_firstwin;
7191 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7192 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007193 msg_putchar('\n');
7194 msg_putchar(wp == curwin ? '>' : ' ');
7195 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007196 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7197 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007198 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007199 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007200 else
7201 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7202 IObuff, IOSIZE, TRUE);
7203 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007204 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007205 ui_breakcheck();
7206 }
7207 }
7208}
7209
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210/*
7211 * ":mode": Set screen mode.
7212 * If no argument given, just get the screen size and redraw.
7213 */
7214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007215ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 if (*eap->arg == NUL)
7218 shell_resized();
7219 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007220 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221}
7222
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223/*
7224 * ":resize".
7225 * set, increment or decrement current window height
7226 */
7227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007228ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230 int n;
7231 win_T *wp = curwin;
7232
7233 if (eap->addr_count > 0)
7234 {
7235 n = eap->line2;
7236 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7237 ;
7238 }
7239
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007240# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007244 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 {
7246 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007247 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007248 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007250 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 }
7252 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 {
7254 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007255 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007256 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007258 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 }
7260}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
7262/*
7263 * ":find [+command] <file>" command.
7264 */
7265 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007266ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267{
Colin Kennedy21570352024-03-03 16:16:47 +01007268 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007269 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007270
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 char_u *fname;
7272 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007273 char_u *file_to_find = NULL;
7274 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275
7276 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007277 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 if (eap->addr_count > 0)
7279 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007280 // Repeat finding the file "count" times. This matters when it appears
7281 // several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 count = eap->line2;
7283 while (fname != NULL && --count > 0)
7284 {
7285 vim_free(fname);
7286 fname = find_file_in_path(NULL, 0, FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007287 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 }
7289 }
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007290 VIM_CLEAR(file_to_find);
7291 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007293 if (fname == NULL)
7294 return;
7295
7296 eap->arg = fname;
7297 do_exedit(eap, NULL);
7298 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299}
7300
7301/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007302 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007303 */
7304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007305ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007306{
7307 regmatch_T regmatch;
7308 char_u *p;
7309
Bram Moolenaar65088802021-03-13 21:07:21 +01007310#ifdef FEAT_EVAL
7311 if (not_in_vim9(eap) == FAIL)
7312 return;
7313#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007314 curwin->w_cursor.lnum = eap->line2;
7315 beginline(BL_SOL | BL_FIX);
7316 if (*eap->arg == '/')
7317 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007318 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007319 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007320 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007321 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007322 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007323 if (regmatch.regprog != NULL)
7324 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007325 // make a copy of the line, when searching for a mark it might be
7326 // flushed
7327 char_u *line = vim_strsave(ml_get_curline());
7328
Bram Moolenaardf177f62005-02-22 08:39:57 +00007329 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007330 if (vim_regexec(&regmatch, line, (colnr_T)0))
7331 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007332 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007333 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007334 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007335 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007336 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007337 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007338 eap->arg += STRLEN(eap->arg);
7339 }
7340 check_cursor();
7341
7342 eap->cmdidx = CMD_visual;
7343 do_exedit(eap, NULL);
7344}
7345
7346/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007347 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 */
7349 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007350ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007352 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7353
Colin Kennedy21570352024-03-03 16:16:47 +01007354 // Exclude commands which keep the window's current buffer
7355 if (
7356 eap->cmdidx != CMD_badd
7357 && eap->cmdidx != CMD_balt
7358 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007359 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7360 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007361 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007362
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 do_exedit(eap, NULL);
7364}
7365
7366/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007367 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007370do_exedit(
7371 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007372 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373{
7374 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007376 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007378 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7379 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007380 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 /*
7382 * ":vi" command ends Ex mode.
7383 */
7384 if (exmode_active && (eap->cmdidx == CMD_visual
7385 || eap->cmdidx == CMD_view))
7386 {
7387 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007388 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007391 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007392 if (global_busy)
7393 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007394 if (eap->nextcmd != NULL)
7395 {
7396 stuffReadbuff(eap->nextcmd);
7397 eap->nextcmd = NULL;
7398 }
7399
7400 if (exmode_was != EXMODE_VIM)
7401 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007402 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007403 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007404 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007405 no_wait_return = 0;
7406 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007407 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007408 msg_scroll = 0;
7409#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007410 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007411 hold_gui_events = 0;
7412#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007413 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007414 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007415
7416 main_loop(FALSE, TRUE);
7417
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007418 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007419 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007420 no_wait_return = save_nwr;
7421 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007422#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007423 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007424#endif
7425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
7429
7430 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007431 || eap->cmdidx == CMD_tabnew
7432 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007433 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007435 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 setpcmark();
7437 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007438 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7439 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007441 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 || *eap->arg != NUL
7443#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007444 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445#endif
7446 )
7447 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007448 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7449 // Only ":edit" or ":script" can bring us here, others are stopped
7450 // earlier.
7451 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007452 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007453
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 n = readonlymode;
7455 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7456 readonlymode = TRUE;
7457 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007458 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7459 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007460 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7461 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7463 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007464 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7466 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7467 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007468 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007470 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007471 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007472 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7473 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007474 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007476 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 if (old_curwin != NULL)
7478 {
7479 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007480 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007482#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007483 cleanup_T cs;
7484
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007485 // Reset the error/interrupt/exception state here so that
7486 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007487 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007488#endif
7489#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007491#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007492 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007493
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007494#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007495 // Restore the error/interrupt/exception state if not
7496 // discarded by a new aborting error, interrupt, or
7497 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007498 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 }
7501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
7503 else if (readonlymode && curbuf->b_nwindows == 1)
7504 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007505 // When editing an already visited buffer, 'readonly' won't be set
7506 // but the previous value is kept. With ":view" and ":sview" we
7507 // want the file to be readonly, except when another window is
7508 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 curbuf->b_p_ro = TRUE;
7510 }
7511 readonlymode = n;
7512 }
7513 else
7514 {
7515 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007516 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 if (n != curwin->w_arg_idx_invalid)
7520 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
7522
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 /*
7524 * if ":split file" worked, set alternate file name in old window to new
7525 * file
7526 */
7527 if (old_curwin != NULL
7528 && *eap->arg != NUL
7529 && curwin != old_curwin
7530 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007531 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007532 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534
7535 ex_no_reprint = TRUE;
7536}
7537
7538#ifndef FEAT_GUI
7539/*
7540 * ":gui" and ":gvim" when there is no GUI.
7541 */
7542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007543ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007545 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546}
7547#endif
7548
Bram Moolenaar4f974752019-02-17 17:44:42 +01007549#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007551ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
7553 gui_make_tearoff(eap->arg);
7554}
7555#endif
7556
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007557#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7558 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007560ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007562# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7563 if (gui.in_use)
7564 gui_make_popup(eap->arg, eap->forceit);
7565# ifdef FEAT_TERM_POPUP_MENU
7566 else
7567# endif
7568# endif
7569# ifdef FEAT_TERM_POPUP_MENU
7570 pum_make_popup(eap->arg, eap->forceit);
7571# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572}
7573#endif
7574
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007576ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577{
7578 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007579 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007581 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582}
7583
7584/*
7585 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7586 * offset.
7587 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007590ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007593 win_T *save_curwin = curwin;
7594 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 long topline;
7596 long y;
7597 linenr_T old_linenr = curwin->w_cursor.lnum;
7598
7599 setpcmark();
7600
7601 /*
7602 * determine max topline
7603 */
7604 if (curwin->w_p_scb)
7605 {
7606 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007607 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 {
7609 if (wp->w_p_scb && wp->w_buffer)
7610 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007611 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 if (topline > y)
7613 topline = y;
7614 }
7615 }
7616 if (topline < 1)
7617 topline = 1;
7618 }
7619 else
7620 {
7621 topline = 1;
7622 }
7623
7624
7625 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007626 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007628 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {
7630 if (curwin->w_p_scb)
7631 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007632 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 y = topline - curwin->w_topline;
7634 if (y > 0)
7635 scrollup(y, TRUE);
7636 else
7637 scrolldown(-y, TRUE);
7638 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007639 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 }
7643 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007644 curwin = save_curwin;
7645 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 if (curwin->w_p_scb)
7647 {
7648 did_syncbind = TRUE;
7649 checkpcmark();
7650 if (old_linenr != curwin->w_cursor.lnum)
7651 {
7652 char_u ctrl_o[2];
7653
7654 ctrl_o[0] = Ctrl_O;
7655 ctrl_o[1] = 0;
7656 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7657 }
7658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659}
7660
7661
7662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007663ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007665 int i;
7666 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7667 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007669 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007671 do_bang(1, eap, FALSE, FALSE, TRUE);
7672 return;
7673 }
7674
7675 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7676 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007679 if (cmdmod.cmod_flags & CMOD_BROWSE)
7680 {
7681 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007683 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7684 NULL, NULL, NULL, curbuf);
7685 if (browseFile != NULL)
7686 {
7687 i = readfile(browseFile, NULL,
7688 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7689 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 }
7691 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007692 i = OK;
7693 }
7694 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007696 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007698 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 return;
7700 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007701 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 }
7703 else
7704 {
7705 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7706 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7707 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007708 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709
7710 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007711 if (i != OK)
7712 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007713#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007714 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007716 semsg(_(e_cant_open_file_str), eap->arg);
7717 }
7718 else
7719 {
7720 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007721 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007722 // Delete the empty line that remains. Historically ex does
7723 // this but vi doesn't.
7724 if (eap->line2 == 0)
7725 lnum = curbuf->b_ml.ml_line_count;
7726 else
7727 lnum = 1;
7728 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007729 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007730 ml_delete(lnum);
7731 if (curwin->w_cursor.lnum > 1
7732 && curwin->w_cursor.lnum >= lnum)
7733 --curwin->w_cursor.lnum;
7734 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007735 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007736 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007737 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
7739}
7740
Bram Moolenaarea408852005-06-25 22:49:46 +00007741static char_u *prev_dir = NULL;
7742
7743#if defined(EXITFREE) || defined(PROTO)
7744 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007745free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007746{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007747 VIM_CLEAR(prev_dir);
7748 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007749}
7750#endif
7751
Bram Moolenaarf4258302013-06-02 18:20:17 +02007752/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007753 * Get the previous directory for the given chdir scope.
7754 */
7755 static char_u *
7756get_prevdir(cdscope_T scope)
7757{
7758 if (scope == CDSCOPE_WINDOW)
7759 return curwin->w_prevdir;
7760 else if (scope == CDSCOPE_TABPAGE)
7761 return curtab->tp_prevdir;
7762 return prev_dir;
7763}
7764
7765/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007766 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007767 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7768 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007769 */
7770 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007771post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007772{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007773 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007774 // Clear tab local directory for both :cd and :tcd
7775 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007776 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007777 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007778 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007779 char_u *pdir = get_prevdir(scope);
7780
7781 // If still in the global directory, need to remember current
7782 // directory as the global directory.
7783 if (globaldir == NULL && pdir != NULL)
7784 globaldir = vim_strsave(pdir);
7785
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007786 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007787 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007788 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007789 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007790 curtab->tp_localdir = vim_strsave(NameBuff);
7791 else
7792 curwin->w_localdir = vim_strsave(NameBuff);
7793 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007794 }
7795 else
7796 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007797 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007798 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007799 }
7800
zeertzjq64be6aa2021-11-19 11:59:08 +00007801 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007802 shorten_fnames(TRUE);
7803}
7804
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007805/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007806 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7807 */
7808 void
7809trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7810{
7811#ifdef FEAT_EVAL
7812 dict_T *v_event;
7813 save_v_event_T save_v_event;
7814
7815 v_event = get_v_event(&save_v_event);
7816 (void)dict_add_string(v_event, "directory", new_dir);
7817 dict_set_items_ro(v_event);
7818#endif
7819 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7820#ifdef FEAT_EVAL
7821 restore_v_event(v_event, &save_v_event);
7822#endif
7823}
7824
7825/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007826 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007827 * chdir() function.
7828 * scope == CDSCOPE_WINDOW: changes the window-local directory
7829 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7830 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007831 * Returns TRUE if the directory is successfully changed.
7832 */
7833 int
7834changedir_func(
7835 char_u *new_dir,
7836 int forceit,
7837 cdscope_T scope)
7838{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007839 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007840 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007841 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007842 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007843 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007844
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007845 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007846 return FALSE;
7847
7848 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7849 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007850 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007851 return FALSE;
7852 }
7853
7854 // ":cd -": Change to previous directory
7855 if (STRCMP(new_dir, "-") == 0)
7856 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007857 pdir = get_prevdir(scope);
7858 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007859 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007860 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007861 return FALSE;
7862 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007863 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007864 }
7865
7866 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007867 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007868 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007869 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007870 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007871
Bakudankun29f3a452021-12-11 12:28:08 +00007872 // For UNIX ":cd" means: go to home directory.
7873 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007874#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007875 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007876#else
7877 if (*new_dir == NUL && p_cdh)
7878#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007879 {
7880 // use NameBuff for home directory name
7881# ifdef VMS
7882 char_u *p;
7883
7884 p = mch_getenv((char_u *)"SYS$LOGIN");
7885 if (p == NULL || *p == NUL) // empty is the same as not set
7886 NameBuff[0] = NUL;
7887 else
7888 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7889# else
7890 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7891# endif
7892 new_dir = NameBuff;
7893 }
zeertzjqf38aad82021-12-30 13:45:57 +00007894 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007895 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7896 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007897 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007898 if (scope == CDSCOPE_WINDOW)
7899 acmd_fname = (char_u *)"window";
7900 else if (scope == CDSCOPE_TABPAGE)
7901 acmd_fname = (char_u *)"tabpage";
7902 else
7903 acmd_fname = (char_u *)"global";
7904 trigger_DirChangedPre(acmd_fname, new_dir);
7905
7906 if (vim_chdir(new_dir))
7907 {
7908 emsg(_(e_command_failed));
7909 vim_free(pdir);
7910 return FALSE;
7911 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007912 }
zeertzjq73257142022-02-02 13:16:37 +00007913
7914 if (scope == CDSCOPE_WINDOW)
7915 pp = &curwin->w_prevdir;
7916 else if (scope == CDSCOPE_TABPAGE)
7917 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007918 else
zeertzjq73257142022-02-02 13:16:37 +00007919 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007920 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007921 *pp = pdir;
7922
7923 post_chdir(scope);
7924
7925 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007926 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007927 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007928 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007929}
Bram Moolenaarea408852005-06-25 22:49:46 +00007930
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007932 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007934 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007935ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007937 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938
7939 new_dir = eap->arg;
7940#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007941 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7942 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007944 ex_pwd(NULL);
7945 return;
7946 }
7947#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007948
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007949 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007950
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007951 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7952 scope = CDSCOPE_WINDOW;
7953 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7954 scope = CDSCOPE_TABPAGE;
7955
7956 if (changedir_func(new_dir, eap->forceit, scope))
7957 {
7958 // Echo the new current directory if the command was typed.
7959 if (KeyTyped || p_verbose >= 5)
7960 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 }
7962}
7963
7964/*
7965 * ":pwd".
7966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007968ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7971 {
7972#ifdef BACKSLASH_IN_FILENAME
7973 slash_adjust(NameBuff);
7974#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007975 if (p_verbose > 0)
7976 {
7977 char *context = "global";
7978
Bram Moolenaar05268152021-11-18 18:53:45 +00007979 if (last_chdir_reason != NULL)
7980 context = last_chdir_reason;
7981 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007982 context = "window";
7983 else if (curtab->tp_localdir != NULL)
7984 context = "tabpage";
7985 smsg("[%s] %s", context, (char *)NameBuff);
7986 }
7987 else
7988 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 }
7990 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007991 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992}
7993
7994/*
7995 * ":=".
7996 */
7997 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007998ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008000 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008001 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002}
8003
8004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008005ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008007 int n;
8008 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009
8010 if (cursor_valid())
8011 {
8012 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8013 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00008014 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008016
8017 len = eap->line2;
8018 switch (*eap->arg)
8019 {
8020 case 'm': break;
8021 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008022 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008023 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008024
8025 // Hide the cursor if invoked with !
8026 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027}
8028
8029/*
8030 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008031 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 */
8033 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008034do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035{
Bram Moolenaar52953192019-08-16 10:27:13 +02008036 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008037 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008038# ifdef ELAPSED_FUNC
8039 elapsed_T start_tv;
8040
8041 // Remember at what time we started, so that we know how much longer we
8042 // should wait after waiting for a bit.
8043 ELAPSED_INIT(start_tv);
8044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008046 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008047 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008048 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008049 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008050
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008051 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008052 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008054 wait_now = msec - done > 1000L ? 1000L : msec - done;
8055#ifdef FEAT_TIMERS
8056 {
8057 long due_time = check_due_timer();
8058
8059 if (due_time > 0 && due_time < wait_now)
8060 wait_now = due_time;
8061 }
8062#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008063#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008064 if (has_any_channel() && wait_now > 20L)
8065 wait_now = 20L;
8066#endif
8067#ifdef FEAT_SOUND
8068 if (has_any_sound_callback() && wait_now > 20L)
8069 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008070#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008071 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008072
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008073#ifdef FEAT_JOB_CHANNEL
8074 if (has_any_channel())
8075 ui_breakcheck_force(TRUE);
8076 else
8077#endif
8078 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008079#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008080 // Process the netbeans and clientserver messages that may have been
8081 // received in the call to ui_breakcheck() when the GUI is in use. This
8082 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008083 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008084#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008085
8086# ifdef ELAPSED_FUNC
8087 // actual time passed
8088 done = ELAPSED_FUNC(start_tv);
8089# else
8090 // guestimate time passed (will actually be more)
8091 done += wait_now;
8092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008094
8095 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8096 // input buffer, otherwise a following call to input() fails.
8097 if (got_int)
8098 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008099
8100 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008101 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102}
8103
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104/*
8105 * ":winsize" command (obsolete).
8106 */
8107 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008108ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109{
8110 int w, h;
8111 char_u *arg = eap->arg;
8112 char_u *p;
8113
Keith Thompson184f71c2024-01-04 21:19:04 +01008114 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008115 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008116 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008117 return;
8118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 w = getdigits(&arg);
8120 arg = skipwhite(arg);
8121 p = arg;
8122 h = getdigits(&arg);
8123 if (*p != NUL && *arg == NUL)
8124 set_shellsize(w, h, TRUE);
8125 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008126 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127}
8128
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008130ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131{
8132 int xchar = NUL;
8133 char_u *p;
8134
8135 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8136 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008137 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 if (eap->arg[1] == NUL)
8139 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008140 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 return;
8142 }
8143 xchar = eap->arg[1];
8144 p = eap->arg + 2;
8145 }
8146 else
8147 p = eap->arg + 1;
8148
Bram Moolenaar63b91732021-08-05 20:40:03 +02008149 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008151 if (*p != NUL && *p != (
8152#ifdef FEAT_EVAL
8153 in_vim9script() ? '#' :
8154#endif
8155 '"')
8156 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008157 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008158 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008160 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008161 postponed_split_flags = cmdmod.cmod_split;
8162 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8164 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008165 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 }
8167}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168
Bram Moolenaar843ee412004-06-30 16:16:41 +00008169#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170/*
8171 * ":winpos".
8172 */
8173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008174ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175{
8176 int x, y;
8177 char_u *arg = eap->arg;
8178 char_u *p;
8179
8180 if (*arg == NUL)
8181 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008182# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008183# ifdef VIMDLL
8184 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8185 mch_get_winpos(&x, &y) != FAIL)
8186# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008188# else
8189 if (mch_get_winpos(&x, &y) != FAIL)
8190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {
8192 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008193 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 }
8195 else
8196# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008197 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 }
8199 else
8200 {
8201 x = getdigits(&arg);
8202 arg = skipwhite(arg);
8203 p = arg;
8204 y = getdigits(&arg);
8205 if (*p == NUL || *arg != NUL)
8206 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008207 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 return;
8209 }
8210# ifdef FEAT_GUI
8211 if (gui.in_use)
8212 gui_mch_set_winpos(x, y);
8213 else if (gui.starting)
8214 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008215 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 gui_win_x = x;
8217 gui_win_y = y;
8218 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008219# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 else
8221# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008222# endif
8223# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008224 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225# endif
8226# ifdef HAVE_TGETENT
8227 if (*T_CWP)
8228 term_set_winpos(x, y);
8229# endif
8230 }
8231}
8232#endif
8233
8234/*
8235 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8236 */
8237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008238ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239{
8240 oparg_T oa;
8241
8242 clear_oparg(&oa);
8243 oa.regname = eap->regname;
8244 oa.start.lnum = eap->line1;
8245 oa.end.lnum = eap->line2;
8246 oa.line_count = eap->line2 - eap->line1 + 1;
8247 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008249 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {
8251 setpcmark();
8252 curwin->w_cursor.lnum = eap->line1;
8253 beginline(BL_SOL | BL_FIX);
8254 }
8255
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008256 if (VIsual_active)
8257 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008258
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 switch (eap->cmdidx)
8260 {
8261 case CMD_delete:
8262 oa.op_type = OP_DELETE;
8263 op_delete(&oa);
8264 break;
8265
8266 case CMD_yank:
8267 oa.op_type = OP_YANK;
8268 (void)op_yank(&oa, FALSE, TRUE);
8269 break;
8270
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008271 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008272 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008274 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8275#else
8276 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008278 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 oa.op_type = OP_RSHIFT;
8280 else
8281 oa.op_type = OP_LSHIFT;
8282 op_shift(&oa, FALSE, eap->amount);
8283 break;
8284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008286 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287}
8288
8289/*
8290 * ":put".
8291 */
8292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008293ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008295 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 if (eap->line2 == 0)
8297 {
8298 eap->line2 = 1;
8299 eap->forceit = TRUE;
8300 }
8301 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008302 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008303 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008304 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305}
8306
8307/*
8308 * Handle ":copy" and ":move".
8309 */
8310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008311ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312{
8313 long n;
8314
Bram Moolenaar491799b2020-08-01 19:23:43 +02008315#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008316 if (not_in_vim9(eap) == FAIL)
8317 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008318#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008319 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008320 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {
8322 eap->nextcmd = NULL;
8323 return;
8324 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008325 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326
8327 /*
8328 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8329 */
8330 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8331 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008332 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 return;
8334 }
8335
8336 if (eap->cmdidx == CMD_move)
8337 {
8338 if (do_move(eap->line1, eap->line2, n) == FAIL)
8339 return;
8340 }
8341 else
8342 ex_copy(eap->line1, eap->line2, n);
8343 u_clearline();
8344 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008345 ex_may_print(eap);
8346}
8347
8348/*
8349 * Print the current line if flags were given to the Ex command.
8350 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008351 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008352ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008353{
8354 if (eap->flags != 0)
8355 {
8356 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8357 (eap->flags & EXFLAG_LIST));
8358 ex_no_reprint = TRUE;
8359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360}
8361
8362/*
8363 * ":smagic" and ":snomagic".
8364 */
8365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008366ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008368 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008370 magic_overruled = eap->cmdidx == CMD_smagic
8371 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008372 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008373 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374}
8375
8376/*
8377 * ":join".
8378 */
8379 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008380ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381{
8382 curwin->w_cursor.lnum = eap->line1;
8383 if (eap->line1 == eap->line2)
8384 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008385 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 return;
8387 if (eap->line2 == curbuf->b_ml.ml_line_count)
8388 {
8389 beep_flush();
8390 return;
8391 }
8392 ++eap->line2;
8393 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008394 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008396 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397}
8398
8399/*
8400 * ":[addr]@r" or ":[addr]*r": execute register
8401 */
8402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008403ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
8405 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008406 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407
8408 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008409 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410
8411#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008412 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413#endif
8414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008415 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 c = *eap->arg;
8417 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8418 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008419 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008420 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8421 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008422 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008424 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008427 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008429 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008431 /*
8432 * Execute from the typeahead buffer.
8433 * Continue until the stuff buffer is empty and all added characters
8434 * have been consumed.
8435 */
8436 while (!stuff_empty() || typebuf.tb_len > prev_len)
8437 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8438
8439 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440}
8441
8442/*
8443 * ":!".
8444 */
8445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008446ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8449}
8450
8451/*
8452 * ":undo".
8453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008455ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008457 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008458 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008459 else
8460 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461}
8462
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008463#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008465ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008466{
8467 char_u hash[UNDO_HASH_SIZE];
8468
8469 u_compute_hash(hash);
8470 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8471}
8472
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008474ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008475{
8476 char_u hash[UNDO_HASH_SIZE];
8477
8478 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008479 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008480}
8481#endif
8482
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483/*
8484 * ":redo".
8485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008487ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488{
8489 u_redo(1);
8490}
8491
8492/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008493 * ":earlier" and ":later".
8494 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008495 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008496ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008497{
8498 long count = 0;
8499 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008500 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008501 char_u *p = eap->arg;
8502
8503 if (*p == NUL)
8504 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008505 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008506 {
8507 count = getdigits(&p);
8508 switch (*p)
8509 {
8510 case 's': ++p; sec = TRUE; break;
8511 case 'm': ++p; sec = TRUE; count *= 60; break;
8512 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008513 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8514 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008515 }
8516 }
8517
8518 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008519 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008520 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008521 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8522 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008523}
8524
8525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 * ":redir": start/stop redirection.
8527 */
8528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008529ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 char *mode;
8532 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008533 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534
Bram Moolenaarba768492016-07-08 15:32:54 +02008535#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008536 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008537 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008538 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008539 return;
8540 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008541#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008542
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 if (STRICMP(eap->arg, "END") == 0)
8544 close_redir();
8545 else
8546 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008547 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008549 ++arg;
8550 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008552 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 mode = "a";
8554 }
8555 else
8556 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008557 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
8559 close_redir();
8560
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008561 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008562 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 if (fname == NULL)
8564 return;
8565#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008566 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 {
8568 char_u *browseFile;
8569
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008570 browseFile = do_browse(BROWSE_SAVE,
8571 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008572 fname, NULL, NULL,
8573 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008575 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 vim_free(fname);
8577 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008578 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 }
8580#endif
8581
8582 redir_fd = open_exfile(fname, eap->forceit, mode);
8583 vim_free(fname);
8584 }
8585#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008586 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008588 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008590 ++arg;
8591 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008593 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008594 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008596 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008598 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008599 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008600 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008603 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008604 if (*arg == '>')
8605 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008606 // Make register empty when not using @A-@Z and the
8607 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008608 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008609 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008611 }
8612 if (*arg != NUL)
8613 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008614 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008615 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008616 }
8617 }
8618 else if (*arg == '=' && arg[1] == '>')
8619 {
8620 int append;
8621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008622 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008623 close_redir();
8624 arg += 2;
8625
8626 if (*arg == '>')
8627 {
8628 ++arg;
8629 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 }
8631 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008632 append = FALSE;
8633
8634 if (var_redir_start(skipwhite(arg), append) == OK)
8635 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 }
8637#endif
8638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008639 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008642 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008645 // Make sure redirection is not off. Can happen for cmdline completion
8646 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008647 if (redir_fd != NULL
8648#ifdef FEAT_EVAL
8649 || redir_reg || redir_vname
8650#endif
8651 )
8652 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653}
8654
8655/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008656 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008658 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008659ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008661 redraw_cmd(eap->forceit);
8662}
8663
8664/*
8665 * ":redraw": force redraw, with clear if "clear" is TRUE.
8666 */
8667 void
8668redraw_cmd(int clear)
8669{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008670 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008672
8673 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008675
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008676 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008678 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 if (need_maketitle)
8680 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008681#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8682# ifdef VIMDLL
8683 if (!gui.in_use)
8684# endif
8685 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008686#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008687 RedrawingDisabled = save_RedrawingDisabled;
8688 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689
Bram Moolenaar5017c662022-04-07 18:06:08 +01008690 // After drawing the statusline screen_attr may still be set.
8691 screen_stop_highlight();
8692
Bram Moolenaara2a89732022-08-31 14:46:18 +01008693 // Reset msg_didout, so that a message that's there is overwritten.
8694 msg_didout = FALSE;
8695 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008697 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008698 need_wait_return = FALSE;
8699
Bram Moolenaara653e532022-04-19 11:38:24 +01008700 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008701 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008702 redrawcmdline();
8703
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 out_flush();
8705}
8706
8707/*
8708 * ":redrawstatus": force redraw of status line(s)
8709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008711ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 if (eap->forceit)
8714 status_redraw_all();
8715 else
8716 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008717 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008718 return; // redraw later
8719
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008720 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008721 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008722
8723 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008724 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008725
zeertzjq320d9102022-09-20 17:12:13 +01008726 if (State & MODE_CMDLINE)
8727 redraw_statuslines();
8728 else
8729 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008730 RedrawingDisabled = save_RedrawingDisabled;
8731 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733}
8734
Bram Moolenaare12bab32019-01-08 22:02:56 +01008735/*
8736 * ":redrawtabline": force redraw of the tabline
8737 */
8738 static void
8739ex_redrawtabline(exarg_T *eap UNUSED)
8740{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008741 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008742 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008743
8744 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008745 p_lz = FALSE;
8746
8747 draw_tabline();
8748
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008749 RedrawingDisabled = save_RedrawingDisabled;
8750 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008751 out_flush();
8752}
8753
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008755close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
8757 if (redir_fd != NULL)
8758 {
8759 fclose(redir_fd);
8760 redir_fd = NULL;
8761 }
8762#ifdef FEAT_EVAL
8763 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008764 if (redir_vname)
8765 {
8766 var_redir_stop();
8767 redir_vname = 0;
8768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769#endif
8770}
8771
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008772#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008773 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008774vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008775{
8776 if (vim_mkdir(name, prot) != 0)
8777 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008778 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008779 return FAIL;
8780 }
8781 return OK;
8782}
8783#endif
8784
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785/*
8786 * Open a file for writing for an Ex command, with some checks.
8787 * Return file descriptor, or NULL on failure.
8788 */
8789 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008790open_exfile(
8791 char_u *fname,
8792 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008793 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
8795 FILE *fd;
8796
8797#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008798 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 if (mch_isdir(fname))
8800 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008801 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 return NULL;
8803 }
8804#endif
8805 if (!forceit && *mode != 'a' && vim_fexists(fname))
8806 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008807 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 return NULL;
8809 }
8810
8811 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008812 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813
8814 return fd;
8815}
8816
8817/*
8818 * ":mark" and ":k".
8819 */
8820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008821ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822{
8823 pos_T pos;
8824
Bram Moolenaar10b94212021-02-19 21:42:57 +01008825#ifdef FEAT_EVAL
8826 if (not_in_vim9(eap) == FAIL)
8827 return;
8828#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008829 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008831 emsg(_(e_argument_required));
8832 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008834
8835 if (eap->arg[1] != NUL) // more than one character?
8836 {
8837 semsg(_(e_trailing_characters_str), eap->arg);
8838 return;
8839 }
8840
8841 pos = curwin->w_cursor; // save curwin->w_cursor
8842 curwin->w_cursor.lnum = eap->line2;
8843 beginline(BL_WHITE | BL_FIX);
8844 if (setmark(*eap->arg) == FAIL) // set mark
8845 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
8846 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847}
8848
8849/*
8850 * Update w_topline, w_leftcol and the cursor position.
8851 */
8852 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008853update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008855 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 update_topline();
8857 if (!curwin->w_p_wrap)
8858 validate_cursor();
8859 update_curswant();
8860}
8861
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008863 * Save the current State and go to Normal mode.
8864 * Return TRUE if the typeahead could be saved.
8865 */
8866 int
8867save_current_state(save_state_T *sst)
8868{
8869 sst->save_msg_scroll = msg_scroll;
8870 sst->save_restart_edit = restart_edit;
8871 sst->save_msg_didout = msg_didout;
8872 sst->save_State = State;
8873 sst->save_insertmode = p_im;
8874 sst->save_finish_op = finish_op;
8875 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008876 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008877 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008878
Bram Moolenaar4324d872020-12-01 20:12:24 +01008879 msg_scroll = FALSE; // no msg scrolling in Normal mode
8880 restart_edit = 0; // don't go to Insert mode
8881 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008882
Bram Moolenaara452b802020-12-01 21:47:59 +01008883 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008884 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008885
8886 /*
8887 * Save the current typeahead. This is required to allow using ":normal"
8888 * from an event handler and makes sure we don't hang when the argument
8889 * ends with half a command.
8890 */
8891 save_typeahead(&sst->tabuf);
8892 return sst->tabuf.typebuf_valid;
8893}
8894
8895 void
8896restore_current_state(save_state_T *sst)
8897{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008898 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008899 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008900
8901 msg_scroll = sst->save_msg_scroll;
8902 restart_edit = sst->save_restart_edit;
8903 p_im = sst->save_insertmode;
8904 finish_op = sst->save_finish_op;
8905 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008906 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008907 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008908 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008909 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008910
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008911 // Restore the state (needed when called from a function executed for
8912 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008913 State = sst->save_State;
8914#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008915 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008916#endif
8917}
8918
8919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 * ":normal[!] {commands}": Execute normal mode commands.
8921 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008922 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008923ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008925 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 char_u *arg = NULL;
8927 int l;
8928 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008930 if (ex_normal_lock > 0)
8931 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008932 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008933 return;
8934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 if (ex_normal_busy >= p_mmd)
8936 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008937 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 return;
8939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 /*
8942 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8943 * this for the K_SPECIAL leading byte, otherwise special keys will not
8944 * work.
8945 */
8946 if (has_mbyte)
8947 {
8948 int len = 0;
8949
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008950 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 for (p = eap->arg; *p != NUL; ++p)
8952 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008953#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008954 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008956#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008957 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008958 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008959#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 )
8963 len += 2;
8964 }
8965 if (len > 0)
8966 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008967 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 if (arg != NULL)
8969 {
8970 len = 0;
8971 for (p = eap->arg; *p != NUL; ++p)
8972 {
8973 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008974#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 if (*p == CSI)
8976 {
8977 arg[len++] = KS_EXTRA;
8978 arg[len++] = (int)KE_CSI;
8979 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008980#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008981 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 {
8983 arg[len++] = *++p;
8984 if (*p == K_SPECIAL)
8985 {
8986 arg[len++] = KS_SPECIAL;
8987 arg[len++] = KE_FILLER;
8988 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008989#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 else if (*p == CSI)
8991 {
8992 arg[len++] = KS_EXTRA;
8993 arg[len++] = (int)KE_CSI;
8994 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 }
8997 arg[len] = NUL;
8998 }
8999 }
9000 }
9001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009003 ++ex_normal_busy;
9004 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 {
9006 /*
9007 * Repeat the :normal command for each line in the range. When no
9008 * range given, execute it just once, without positioning the cursor
9009 * first.
9010 */
9011 do
9012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 if (eap->addr_count != 0)
9014 {
9015 curwin->w_cursor.lnum = eap->line1++;
9016 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02009017 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 }
9019
Bram Moolenaar13505972019-01-24 15:04:48 +01009020 exec_normal_cmd(arg != NULL
9021 ? arg
9022 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 }
9024 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9025 }
9026
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009027 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 update_topline_cursor();
9029
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009030 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009032 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009033#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009034 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009035#endif
9036
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038}
9039
9040/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009041 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 */
9043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009044ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009046 if (eap->forceit)
9047 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009048 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009049 if (!curwin->w_cursor.lnum)
9050 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009051 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009052 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009053#ifdef FEAT_TERMINAL
9054 // Ignore this when running in an active terminal.
9055 if (term_job_running(curbuf->b_term))
9056 return;
9057#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009058
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009059 // Ignore the command when already in Insert mode. Inserting an
9060 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009061 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009062 return;
9063
Bram Moolenaar64969662005-12-14 21:59:55 +00009064 if (eap->cmdidx == CMD_startinsert)
9065 restart_edit = 'a';
9066 else if (eap->cmdidx == CMD_startreplace)
9067 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009069 restart_edit = 'V';
9070
9071 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009073 if (eap->cmdidx == CMD_startinsert)
9074 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009075 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009077
9078 if (VIsual_active)
9079 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080}
9081
9082/*
9083 * ":stopinsert"
9084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009086ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087{
9088 restart_edit = 0;
9089 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009090 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009093/*
9094 * Execute normal mode command "cmd".
9095 * "remap" can be REMAP_NONE or REMAP_YES.
9096 */
9097 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009098exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009099{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009100 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009101 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009102 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009103}
Bram Moolenaar25281632016-01-21 23:32:32 +01009104
Bram Moolenaar25281632016-01-21 23:32:32 +01009105/*
9106 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009107 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009108 */
9109 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009110exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009111{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009112 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009113 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009114
Bram Moolenaar905dd902019-04-07 14:21:47 +02009115 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9116 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009117 clear_oparg(&oa);
9118 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009119 while ((!stuff_empty()
9120 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009121 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009122 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009123 {
9124 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009125#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009126 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009127 && oa.op_type == OP_NOP && oa.regname == NUL
9128 && !VIsual_active)
9129 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009130 // If terminal_loop() returns OK we got a key that is handled
9131 // in Normal model. With FAIL we first need to position the
9132 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009133 if (terminal_loop(TRUE) == OK)
9134 normal_cmd(&oa, TRUE);
9135 }
9136 else
9137#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009138 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009139 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009140 }
9141}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009142
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#ifdef FEAT_FIND_ID
9144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009145ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
9147 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9148 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009149 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150}
9151
Bram Moolenaar4033c552017-09-16 20:54:51 +02009152#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153/*
9154 * ":psearch"
9155 */
9156 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009157ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158{
9159 g_do_tagpreview = p_pvh;
9160 ex_findpat(eap);
9161 g_do_tagpreview = 0;
9162}
9163#endif
9164
9165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009166ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
9168 int whole = TRUE;
9169 long n;
9170 char_u *p;
9171 int action;
9172
9173 switch (cmdnames[eap->cmdidx].cmd_name[2])
9174 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009175 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9177 action = ACTION_GOTO;
9178 else
9179 action = ACTION_SHOW;
9180 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009181 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 action = ACTION_SHOW_ALL;
9183 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009184 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 action = ACTION_GOTO;
9186 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009187 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 action = ACTION_SPLIT;
9189 break;
9190 }
9191
9192 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009193 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 {
9195 n = getdigits(&eap->arg);
9196 eap->arg = skipwhite(eap->arg);
9197 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009198 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 {
9200 whole = FALSE;
9201 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009202 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 if (*p)
9204 {
9205 *p++ = NUL;
9206 p = skipwhite(p);
9207
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009208 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009209 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009210 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009212 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 }
9214 }
9215 if (!eap->skip)
9216 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9217 whole, !eap->forceit,
9218 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009219 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220}
9221#endif
9222
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223
Bram Moolenaar4033c552017-09-16 20:54:51 +02009224#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225/*
9226 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9227 */
9228 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009229ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009231 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9233}
9234
9235/*
9236 * ":pedit"
9237 */
9238 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009239ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240{
9241 win_T *curwin_save = curwin;
9242
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009243 if (ERROR_IF_ANY_POPUP_WINDOW)
9244 return;
9245
Bram Moolenaar026587b2019-08-17 15:08:00 +02009246 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009248 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009249
Bram Moolenaar026587b2019-08-17 15:08:00 +02009250 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009252
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 if (curwin != curwin_save && win_valid(curwin_save))
9254 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009255 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009257 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 win_enter(curwin_save, TRUE);
9259 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009260# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009261 else if (WIN_IS_POPUP(curwin))
9262 {
9263 // can't keep focus in popup window
9264 win_enter(firstwin, TRUE);
9265 }
9266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 g_do_tagpreview = 0;
9268}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270
9271/*
9272 * ":stag", ":stselect" and ":stjump".
9273 */
9274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009275ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276{
9277 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009278 postponed_split_flags = cmdmod.cmod_split;
9279 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9281 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009282 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284
9285/*
9286 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9287 */
9288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009289ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290{
9291 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9292}
9293
9294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009295ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296{
9297 int cmd;
9298
9299 switch (name[1])
9300 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009301 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009303 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009305 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009307 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009309 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009311 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009313 case 'f': // ":tfirst"
9314 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009316 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009318 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009320 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009322 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 return;
9324 }
9325#endif
9326 cmd = DT_TAG;
9327 break;
9328 }
9329
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009330 if (name[0] == 'l')
9331 {
9332#ifndef FEAT_QUICKFIX
9333 ex_ni(eap);
9334 return;
9335#else
9336 cmd = DT_LTAG;
9337#endif
9338 }
9339
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9341 eap->forceit, TRUE);
9342}
9343
John Marriotted908f72024-04-18 22:46:56 +02009344enum {
9345 SPEC_PERC = 0,
9346 SPEC_HASH,
9347 SPEC_CWORD, // cursor word
9348 SPEC_CCWORD, // cursor WORD
9349 SPEC_CEXPR, // expr under cursor
9350 SPEC_CFILE, // cursor path name
9351 SPEC_SFILE, // ":so" file name
9352 SPEC_SLNUM, // ":so" file line number
9353 SPEC_STACK, // call stack
9354 SPEC_SCRIPT, // script file name
9355 SPEC_AFILE, // autocommand file name
9356 SPEC_ABUF, // autocommand buffer number
9357 SPEC_AMATCH, // autocommand match name
9358 SPEC_SFLNUM, // script file line number
9359 SPEC_SID // script ID: <SNR>123_
9360#ifdef FEAT_CLIENTSERVER
9361 , SPEC_CLIENT
9362#endif
9363};
9364
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009366 * Check "str" for starting with a special cmdline variable.
9367 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9368 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9369 */
9370 int
Mike Williams51024bb2024-05-30 07:46:30 +02009371find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009372{
John Marriotted908f72024-04-18 22:46:56 +02009373 // must be sorted by the 'value' field because it is used by bsearch()!
9374 static keyvalue_T spec_str_tab[] = {
9375 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9376 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9377 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9378 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9379 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9380 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9381 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009382#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009383 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009384#endif
John Marriotted908f72024-04-18 22:46:56 +02009385 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9386 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9387 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9388 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9389 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9390 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009391 };
John Marriotted908f72024-04-18 22:46:56 +02009392 keyvalue_T target;
9393 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009394
John Marriotted908f72024-04-18 22:46:56 +02009395 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009396 {
John Marriotted908f72024-04-18 22:46:56 +02009397 case '%':
9398 *usedlen = 1;
9399 return SPEC_PERC;
9400
9401 case '#':
9402 *usedlen = 1;
9403 return SPEC_HASH;
9404
9405 case '<':
9406 target.key = 0;
9407 target.value = (char *)src + 1; // skip over '<'
9408 target.length = 0; // not used, see cmp_keyvalue_value_n()
9409
9410 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab, ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]), cmp_keyvalue_value_n);
9411 if (entry == NULL)
9412 return -1;
9413
9414 *usedlen = entry->length + 1;
9415 return entry->key;
9416
9417 default:
9418 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009419 }
John Marriotted908f72024-04-18 22:46:56 +02009420
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009421 return -1;
9422}
9423
9424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 * Evaluate cmdline variables.
9426 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009427 * change "%" to curbuf->b_ffname
9428 * "#" to curwin->w_alt_fnum
9429 * "%%" to curwin->w_alt_fnum in Vim9 script
9430 * "<cword>" to word under the cursor
9431 * "<cWORD>" to WORD under the cursor
9432 * "<cexpr>" to C-expression under the cursor
9433 * "<cfile>" to path name under the cursor
9434 * "<sfile>" to sourced file name
9435 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009436 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009437 * "<slnum>" to sourced file line number
9438 * "<afile>" to file name for autocommand
9439 * "<abuf>" to buffer number for autocommand
9440 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 *
9442 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9443 * "" for error without a message) and NULL is returned.
9444 * Returns an allocated string if a valid match was found.
9445 * Returns NULL if no match was found. "usedlen" then still contains the
9446 * number of characters to skip.
9447 */
9448 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009449eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009450 char_u *src, // pointer into commandline
9451 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009452 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009453 linenr_T *lnump, // line number for :e command, or NULL
9454 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009455 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009456 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009457 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458{
9459 int i;
9460 char_u *s;
9461 char_u *result;
9462 char_u *resultbuf = NULL;
9463 int resultlen;
9464 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009465 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009467 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470
9471 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009472 if (escaped != NULL)
9473 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474
9475 /*
9476 * Check if there is something to do.
9477 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009478 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009479 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 {
9481 *usedlen = 1;
9482 return NULL;
9483 }
9484
9485 /*
9486 * Skip when preceded with a backslash "\%" and "\#".
9487 * Note: In "\\%" the % is also not recognized!
9488 */
9489 if (src > srcstart && src[-1] == '\\')
9490 {
9491 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009492 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 return NULL;
9494 }
9495
9496 /*
9497 * word or WORD under cursor
9498 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009499 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9500 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009502 resultlen = find_ident_under_cursor(&result,
9503 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9504 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9505 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 if (resultlen == 0)
9507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009508 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 return NULL;
9510 }
9511 }
9512
9513 /*
9514 * '#': Alternate file name
9515 * '%': Current file name
9516 * File name under the cursor
9517 * File name for autocommand
9518 * and following modifiers
9519 */
9520 else
9521 {
Mike Williams51024bb2024-05-30 07:46:30 +02009522 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009523
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 switch (spec_idx)
9525 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009526 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009527#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009528 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009531 // '%': current file
9532 if (curbuf->b_fname == NULL)
9533 {
9534 result = (char_u *)"";
9535 valid = 0; // Must have ":p:h" to be valid
9536 }
9537 else
9538 {
9539 result = curbuf->b_fname;
9540 tilde_file = STRCMP(result, "~") == 0;
9541 }
9542 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009544#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009545 // "%%" alternate file
9546 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009547#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009548 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009549 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009550 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009552 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 result = arg_all();
9554 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009555 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009556 if (escaped != NULL)
9557 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 break;
9560 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009561 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009562 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009563 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009565 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009566 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009567 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009568 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009570 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009572 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009573 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009574 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009575 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009576 return NULL;
9577 }
9578#ifdef FEAT_EVAL
9579 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9580 (long)i);
9581 if (result == NULL)
9582 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009583 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009584 return NULL;
9585 }
9586#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009587 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 }
9591 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009592 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009593 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9594 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009595 buf = buflist_findnr(i);
9596 if (buf == NULL)
9597 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009598 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009599 return NULL;
9600 }
9601 if (lnump != NULL)
9602 *lnump = ECMD_LAST;
9603 if (buf->b_fname == NULL)
9604 {
9605 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009606 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009607 }
9608 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009609 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009610 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009611 tilde_file = STRCMP(result, "~") == 0;
9612 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 break;
9615
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009616 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009617 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 if (result == NULL)
9619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009620 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 return NULL;
9622 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009623 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009626 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009628 if (result != NULL && !autocmd_fname_full)
9629 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009630 // Still need to turn the fname into a full path. It is
9631 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009632 autocmd_fname_full = TRUE;
9633 result = FullName_save(autocmd_fname, FALSE);
9634 vim_free(autocmd_fname);
9635 autocmd_fname = result;
9636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 if (result == NULL)
9638 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009639 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 return NULL;
9641 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009642 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 break;
9644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009645 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 if (autocmd_bufnr <= 0)
9647 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009648 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 return NULL;
9650 }
9651 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9652 result = strbuf;
9653 break;
9654
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009655 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 result = autocmd_match;
9657 if (result == NULL)
9658 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009659 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 return NULL;
9661 }
9662 break;
9663
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009664 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009665 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 if (result == NULL)
9667 {
LemonBoy6013d002022-04-09 21:42:10 +01009668 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9669 return NULL;
9670 }
9671 resultbuf = result; // remember allocated string
9672 break;
9673 case SPEC_STACK: // call stack
9674 result = estack_sfile(ESTACK_STACK);
9675 if (result == NULL)
9676 {
9677 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9678 return NULL;
9679 }
9680 resultbuf = result; // remember allocated string
9681 break;
9682 case SPEC_SCRIPT: // script file name
9683 result = estack_sfile(ESTACK_SCRIPT);
9684 if (result == NULL)
9685 {
9686 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 return NULL;
9688 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009689 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009691
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009692 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009693 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009694 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009695 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009696 return NULL;
9697 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009698 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009699 result = strbuf;
9700 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009701
9702#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009703 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009704 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009705 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009706 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009707 return NULL;
9708 }
9709 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009710 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009711 result = strbuf;
9712 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009713
Bram Moolenaar90944302020-08-01 20:45:11 +02009714 case SPEC_SID:
9715 if (current_sctx.sc_sid <= 0)
9716 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009717 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009718 return NULL;
9719 }
9720 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9721 result = strbuf;
9722 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009723#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009724
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009725#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009726 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009727 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9728 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 result = strbuf;
9730 break;
9731#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009732
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009733 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009734 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009735 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 }
9737
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009738 resultlen = (int)STRLEN(result); // length of new string
9739 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 {
9741 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 resultlen = (int)(s - result);
9744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 else if (!skip_mod)
9746 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009747 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 &resultlen);
9749 if (result == NULL)
9750 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009751 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 return NULL;
9753 }
9754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 }
9756
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009757 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009759 if (empty_is_error)
9760 {
9761 if (valid != VALID_HEAD + VALID_PATH)
9762 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9763 else
9764 *errormsg = _(e_evaluates_to_an_empty_string);
9765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 result = NULL;
9767 }
9768 else
9769 result = vim_strnsave(result, resultlen);
9770 vim_free(resultbuf);
9771 return result;
9772}
9773
9774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 * Expand the <sfile> string in "arg".
9776 *
9777 * Returns an allocated string, or NULL for any error.
9778 */
9779 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009780expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009782 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +02009784 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +02009786 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +02009788 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +02009789 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 char_u *p;
9791
John Marriotted908f72024-04-18 22:46:56 +02009792 resultlen = STRLEN(arg);
9793 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 if (result == NULL)
9795 return NULL;
9796
9797 for (p = result; *p; )
9798 {
9799 if (STRNCMP(p, "<sfile>", 7) != 0)
9800 ++p;
9801 else
9802 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009803 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009804 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 if (errormsg != NULL)
9806 {
9807 if (*errormsg)
9808 emsg(errormsg);
9809 vim_free(result);
9810 return NULL;
9811 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009812 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 {
9814 p += srclen;
9815 continue;
9816 }
John Marriotted908f72024-04-18 22:46:56 +02009817 repllen = STRLEN(repl);
9818 resultlen += repllen - srclen;
9819 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 if (newres == NULL)
9821 {
9822 vim_free(repl);
9823 vim_free(result);
9824 return NULL;
9825 }
John Marriotted908f72024-04-18 22:46:56 +02009826 len = p - result;
9827 mch_memmove(newres, result, len);
9828 STRCPY(newres + len, repl);
9829 len += repllen;
9830 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 vim_free(repl);
9832 vim_free(result);
9833 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009834 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 }
9836 }
9837
9838 return result;
9839}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009842/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009843 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009844 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009847dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 if (fname == NULL)
9850 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009851 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852}
9853#endif
9854
9855/*
9856 * ":behave {mswin,xterm}"
9857 */
9858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009859ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860{
9861 if (STRCMP(eap->arg, "mswin") == 0)
9862 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009863 set_option_value_give_err((char_u *)"selection",
9864 0L, (char_u *)"exclusive", 0);
9865 set_option_value_give_err((char_u *)"selectmode",
9866 0L, (char_u *)"mouse,key", 0);
9867 set_option_value_give_err((char_u *)"mousemodel",
9868 0L, (char_u *)"popup", 0);
9869 set_option_value_give_err((char_u *)"keymodel",
9870 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 }
9872 else if (STRCMP(eap->arg, "xterm") == 0)
9873 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009874 set_option_value_give_err((char_u *)"selection",
9875 0L, (char_u *)"inclusive", 0);
9876 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9877 set_option_value_give_err((char_u *)"mousemodel",
9878 0L, (char_u *)"extend", 0);
9879 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 }
9881 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009882 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883}
9884
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885static int filetype_detect = FALSE;
9886static int filetype_plugin = FALSE;
9887static int filetype_indent = FALSE;
9888
9889/*
9890 * ":filetype [plugin] [indent] {on,off,detect}"
9891 * on: Load the filetype.vim file to install autocommands for file types.
9892 * off: Load the ftoff.vim file to remove all autocommands for file types.
9893 * plugin on: load filetype.vim and ftplugin.vim
9894 * plugin off: load ftplugof.vim
9895 * indent on: load filetype.vim and indent.vim
9896 * indent off: load indoff.vim
9897 */
9898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009899ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900{
9901 char_u *arg = eap->arg;
9902 int plugin = FALSE;
9903 int indent = FALSE;
9904
9905 if (*eap->arg == NUL)
9906 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009907 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009908 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 filetype_detect ? "ON" : "OFF",
9910 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9911 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9912 return;
9913 }
9914
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009915 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 for (;;)
9917 {
9918 if (STRNCMP(arg, "plugin", 6) == 0)
9919 {
9920 plugin = TRUE;
9921 arg = skipwhite(arg + 6);
9922 continue;
9923 }
9924 if (STRNCMP(arg, "indent", 6) == 0)
9925 {
9926 indent = TRUE;
9927 arg = skipwhite(arg + 6);
9928 continue;
9929 }
9930 break;
9931 }
9932 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9933 {
9934 if (*arg == 'o' || !filetype_detect)
9935 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009936 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 filetype_detect = TRUE;
9938 if (plugin)
9939 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009940 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 filetype_plugin = TRUE;
9942 }
9943 if (indent)
9944 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009945 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 filetype_indent = TRUE;
9947 }
9948 }
9949 if (*arg == 'd')
9950 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009951 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009952 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 }
9954 }
9955 else if (STRCMP(arg, "off") == 0)
9956 {
9957 if (plugin || indent)
9958 {
9959 if (plugin)
9960 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009961 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 filetype_plugin = FALSE;
9963 }
9964 if (indent)
9965 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009966 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 filetype_indent = FALSE;
9968 }
9969 }
9970 else
9971 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009972 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 filetype_detect = FALSE;
9974 }
9975 }
9976 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009977 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978}
9979
9980/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009981 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 */
9983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009984ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985{
zeertzjq5bf6c212024-03-31 18:41:27 +02009986 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009987 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009988
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009989 char_u *arg = eap->arg;
9990 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9991 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009992
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009993 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9994 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +02009995 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009999ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000{
10001#ifdef FEAT_DIGRAPHS
10002 if (*eap->arg != NUL)
10003 putdigraph(eap->arg);
10004 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010005 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006#else
mityu61065042021-07-19 20:07:21 +020010007 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008#endif
10009}
10010
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010011#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10012 void
10013set_no_hlsearch(int flag)
10014{
10015 no_hlsearch = flag;
10016# ifdef FEAT_EVAL
10017 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10018# endif
10019}
10020
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021/*
10022 * ":nohlsearch"
10023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010025ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010027 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +010010028 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030#endif
10031
10032#ifdef FEAT_CRYPT
10033/*
10034 * ":X": Get crypt key
10035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010037ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010039 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010040 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041}
10042#endif
10043
10044#ifdef FEAT_FOLDING
10045 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010046ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047{
10048 if (foldManualAllowed(TRUE))
10049 foldCreate(eap->line1, eap->line2);
10050}
10051
10052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010053ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054{
10055 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10056 eap->forceit, FALSE);
10057}
10058
10059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010060ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061{
10062 linenr_T lnum;
10063
Bram Moolenaar4facea32019-10-12 20:17:40 +020010064# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010065 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010066# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010067
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010068 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10070 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10071 ml_setmarked(lnum);
10072
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010073 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010075 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010076# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010077 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010078# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079}
10080#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010082#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010083/*
10084 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10085 * instead of a quickfix command.
10086 */
10087 int
10088is_loclist_cmd(int cmdidx)
10089{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010090 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010091 return FALSE;
10092 return cmdnames[cmdidx].cmd_name[0] == 'l';
10093}
10094#endif
10095
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010096 int
10097get_pressedreturn(void)
10098{
10099 return ex_pressedreturn;
10100}
10101
10102 void
10103set_pressedreturn(int val)
10104{
10105 ex_pressedreturn = val;
10106}