blob: 0e69d5c384048e8cebe50089f0765299920a7fe3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058# define ex_cclose ex_ni
59# define ex_copen ex_ni
60# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020061# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000063#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
64# define ex_cexpr ex_ni
65#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020067static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020068static 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 +020069static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010070static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020071#if !defined(FEAT_PERL) \
72 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
73 || !defined(FEAT_TCL) \
74 || !defined(FEAT_RUBY) \
75 || !defined(FEAT_LUA) \
76 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000077# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010080static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000082#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#endif
John Marriotted908f72024-04-18 22:46:56 +020085static 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 +010086static void ex_highlight(exarg_T *eap);
87static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void ex_cquit(exarg_T *eap);
89static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_close(exarg_T *eap);
91static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
92static void ex_only(exarg_T *eap);
93static void ex_resize(exarg_T *eap);
94static void ex_stag(exarg_T *eap);
95static void ex_tabclose(exarg_T *eap);
96static void ex_tabonly(exarg_T *eap);
97static void ex_tabnext(exarg_T *eap);
98static void ex_tabmove(exarg_T *eap);
99static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100101static void ex_pclose(exarg_T *eap);
102static void ex_ptag(exarg_T *eap);
103static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#else
105# define ex_pclose ex_ni
106# define ex_ptag ex_ni
107# define ex_pedit ex_ni
108#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_hide(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100110static void ex_exit(exarg_T *eap);
111static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100113static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#else
115# define ex_goto ex_ni
116#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100117static void ex_shell(exarg_T *eap);
118static void ex_preserve(exarg_T *eap);
119static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_mode(exarg_T *eap);
121static void ex_wrongmodifier(exarg_T *eap);
122static void ex_find(exarg_T *eap);
123static void ex_open(exarg_T *eap);
124static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#ifndef FEAT_GUI
126# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100127static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100129#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#else
132# define ex_tearoff ex_ni
133#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100134#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
135 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100136static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#else
138# define ex_popup ex_ni
139#endif
140#ifndef FEAT_GUI_MSWIN
141# define ex_simalt ex_ni
142#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000143#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144# define gui_mch_find_dialog ex_ni
145# define gui_mch_replace_dialog ex_ni
146#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000147#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148# define ex_helpfind ex_ni
149#endif
150#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100151# define ex_cscope ex_ni
152# define ex_scscope ex_ni
153# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#endif
155#ifndef FEAT_SYN_HL
156# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200157# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000158#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200159#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200160# define ex_syntime ex_ni
161#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000162#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000163# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000164# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000165# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000166# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000167# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000168#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200169#ifndef FEAT_PERSISTENT_UNDO
170# define ex_rundo ex_ni
171# define ex_wundo ex_ni
172#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200173#ifndef FEAT_LUA
174# define ex_lua ex_script_ni
175# define ex_luado ex_ni
176# define ex_luafile ex_ni
177#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000178#ifndef FEAT_MZSCHEME
179# define ex_mzscheme ex_script_ni
180# define ex_mzfile ex_ni
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#ifndef FEAT_PERL
183# define ex_perl ex_script_ni
184# define ex_perldo ex_ni
185#endif
186#ifndef FEAT_PYTHON
187# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200188# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189# define ex_pyfile ex_ni
190#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200191#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200192# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200193# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194# define ex_py3file ex_ni
195#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100196#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
197# define ex_pyx ex_script_ni
198# define ex_pyxdo ex_ni
199# define ex_pyxfile ex_ni
200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifndef FEAT_TCL
202# define ex_tcl ex_script_ni
203# define ex_tcldo ex_ni
204# define ex_tclfile ex_ni
205#endif
206#ifndef FEAT_RUBY
207# define ex_ruby ex_script_ni
208# define ex_rubydo ex_ni
209# define ex_rubyfile ex_ni
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211#ifndef FEAT_KEYMAP
212# define ex_loadkeymap ex_ni
213#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100214static void ex_swapname(exarg_T *eap);
215static void ex_syncbind(exarg_T *eap);
216static void ex_read(exarg_T *eap);
217static void ex_pwd(exarg_T *eap);
218static void ex_equal(exarg_T *eap);
219static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100220static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000222#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#else
225# define ex_winpos ex_ni
226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ex_operators(exarg_T *eap);
228static void ex_put(exarg_T *eap);
229static void ex_copymove(exarg_T *eap);
230static void ex_submagic(exarg_T *eap);
231static void ex_join(exarg_T *eap);
232static void ex_at(exarg_T *eap);
233static void ex_bang(exarg_T *eap);
234static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200235#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ex_wundo(exarg_T *eap);
237static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_redo(exarg_T *eap);
240static void ex_later(exarg_T *eap);
241static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100243static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_startinsert(exarg_T *eap);
247static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_checkpath(exarg_T *eap);
250static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#else
252# define ex_findpat ex_ni
253# define ex_checkpath ex_ni
254#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200255#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#else
258# define ex_psearch ex_ni
259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static void ex_tag(exarg_T *eap);
261static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200263# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200264# define ex_break ex_ni
265# define ex_breakadd ex_ni
266# define ex_breakdel ex_ni
267# define ex_breaklist ex_ni
268# define ex_call ex_ni
269# define ex_catch ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000270# define ex_class ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200272# define ex_continue ex_ni
273# define ex_debug ex_ni
274# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200275# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200281# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200282# define ex_endfunction ex_ni
283# define ex_endif ex_ni
284# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285# define ex_endwhile ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000286# define ex_enum ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
289# define ex_finally ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000290# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200291# define ex_finish ex_ni
292# define ex_function ex_ni
293# define ex_if ex_ni
294# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200295# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200296# define ex_lockvar ex_ni
297# define ex_oldfiles ex_ni
298# define ex_options ex_ni
299# define ex_packadd ex_ni
300# define ex_packloadall ex_ni
301# define ex_return ex_ni
302# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_throw ex_ni
304# define ex_try ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000305# define ex_type ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200307# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_import ex_ni
309# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200311#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_loadview ex_ni
313#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200314#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315# define ex_viminfo ex_ni
316#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_filetype(exarg_T *eap);
319static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000321# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322# define ex_diffpatch ex_ni
323# define ex_diffgetput ex_ni
324# define ex_diffsplit ex_ni
325# define ex_diffthis ex_ni
326# define ex_diffupdate ex_ni
327#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#else
332# define ex_nohlsearch ex_ni
333# define ex_match ex_ni
334#endif
335#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_X ex_ni
339#endif
340#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_fold(exarg_T *eap);
342static void ex_foldopen(exarg_T *eap);
343static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#else
345# define ex_fold ex_ni
346# define ex_foldopen ex_ni
347# define ex_folddo ex_ni
348#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100349#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350# define ex_language ex_ni
351#endif
352#ifndef FEAT_SIGNS
353# define ex_sign ex_ni
354#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar05159a02005-02-26 23:04:13 +0000361#ifndef FEAT_PROFILE
362# define ex_profile ex_ni
363#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200364#ifndef FEAT_TERMINAL
365# define ex_terminal ex_ni
366#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200367#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
368# define ex_xrestore ex_ni
369#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100370#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200371# define ex_popupclear ex_ni
372#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374/*
375 * Declare cmdnames[].
376 */
377#define DO_DECLARE_EXCMD
378#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200379#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381static char_u dollar_command[2] = {'$', 0};
382
383
384#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100385// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386typedef struct
387{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388 char_u *line; // command line
389 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390} wcmd_T;
391
392/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000393 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000395 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000397struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100399 garray_T *lines_gap; // growarray with line info
400 int current_line; // last read line from growarray
401 int repeating; // TRUE when looping a second time
402 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100403 char_u *(*lc_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 void *cookie;
405};
406
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200407static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static int store_loop_line(garray_T *gap, char_u *line);
409static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000410
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100411// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000412struct dbg_stuff
413{
414 int trylevel;
415 int force_abort;
416 except_T *caught_stack;
417 char_u *vv_exception;
418 char_u *vv_throwpoint;
419 int did_emsg;
420 int got_int;
421 int did_throw;
422 int need_rethrow;
423 int check_cstack;
424 except_T *current_exception;
425};
426
Bram Moolenaared203462004-06-16 11:19:22 +0000427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100428save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000429{
430 dsp->trylevel = trylevel; trylevel = 0;
431 dsp->force_abort = force_abort; force_abort = FALSE;
432 dsp->caught_stack = caught_stack; caught_stack = NULL;
433 dsp->vv_exception = v_exception(NULL);
434 dsp->vv_throwpoint = v_throwpoint(NULL);
435
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100436 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000437 dsp->did_emsg = did_emsg; did_emsg = FALSE;
438 dsp->got_int = got_int; got_int = FALSE;
439 dsp->did_throw = did_throw; did_throw = FALSE;
440 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
441 dsp->check_cstack = check_cstack; check_cstack = FALSE;
442 dsp->current_exception = current_exception; current_exception = NULL;
443}
444
445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100446restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000447{
448 suppress_errthrow = FALSE;
449 trylevel = dsp->trylevel;
450 force_abort = dsp->force_abort;
451 caught_stack = dsp->caught_stack;
452 (void)v_exception(dsp->vv_exception);
453 (void)v_throwpoint(dsp->vv_throwpoint);
454 did_emsg = dsp->did_emsg;
455 got_int = dsp->got_int;
456 did_throw = dsp->did_throw;
457 need_rethrow = dsp->need_rethrow;
458 check_cstack = dsp->check_cstack;
459 current_exception = dsp->current_exception;
460}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461#endif
462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463/*
Colin Kennedy65e580b2024-03-26 18:29:30 +0100464 * Check if ffname differs from fnum.
465 * fnum is a buffer number. 0 == current buffer, 1-or-more must be a valid buffer ID.
466 * ffname is a full path to where a buffer lives on-disk or would live on-disk.
467 *
468 */
469 static int
470is_other_file(int fnum, char_u *ffname)
471{
472 if (fnum != 0)
473 {
474 if (fnum == curbuf->b_fnum)
475 return FALSE;
476
477 return TRUE;
478 }
479
480 if (ffname == NULL)
481 return TRUE;
482
483 if (*ffname == NUL)
484 return FALSE;
485
486 // TODO: Need a reliable way to know whether a buffer is meant to live on-disk
487 // !curbuf->b_dev_valid is not always available (example: missing on Windows)
488 if (curbuf->b_sfname != NULL
489 && *curbuf->b_sfname != NUL)
490 // This occurs with unsaved buffers. In which case `ffname`
491 // actually corresponds to curbuf->b_sfname
492 return fnamecmp(ffname, curbuf->b_sfname) != 0;
493
494 return otherfile(ffname);
495}
496
497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
499 * command is given.
500 */
501 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100502do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100503 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504{
505 int save_msg_scroll;
506 int prev_msg_row;
507 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100508 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000509
510 if (improved)
511 exmode_active = EXMODE_VIM;
512 else
513 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100514 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100515 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000516
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100517 // When using ":global /pat/ visual" and then "Q" we return to continue
518 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000519 if (global_busy)
520 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
522 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100523 ++RedrawingDisabled; // don't redisplay the window
524 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100526 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 ++hold_gui_events;
528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
Bram Moolenaar32526b32019-01-19 17:43:09 +0100530 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 while (exmode_active)
532 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100533 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000534 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
535 {
536 exmode_active = FALSE;
537 break;
538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 msg_scroll = TRUE;
540 need_wait_return = FALSE;
541 ex_pressedreturn = FALSE;
542 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100543 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 prev_msg_row = msg_row;
545 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 if (improved)
547 {
548 cmdline_row = msg_row;
549 do_cmdline(NULL, getexline, NULL, 0);
550 }
551 else
552 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
553 lines_left = Rows - 1;
554
Bram Moolenaardf177f62005-02-22 08:39:57 +0000555 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100556 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000558 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000559 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000560 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000562 if (ex_pressedreturn)
563 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100564 // go up one line, to overwrite the ":<CR>" line, so the
565 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000566 msg_row = prev_msg_row;
567 if (prev_msg_row == Rows - 1)
568 msg_row--;
569 }
570 msg_col = 0;
571 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
572 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100575 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000576 {
577 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000578 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000579 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000580 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
583
584#ifdef FEAT_GUI
585 --hold_gui_events;
586#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100587 if (RedrawingDisabled > 0)
588 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100590 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 need_wait_return = FALSE;
592 msg_scroll = save_msg_scroll;
593}
594
595/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200596 * Print the executed command for when 'verbose' is set.
597 * When "lnum" is 0 only print the command.
598 */
599 static void
600msg_verbose_cmd(linenr_T lnum, char_u *cmd)
601{
602 ++no_wait_return;
603 verbose_enter_scroll();
604
605 if (lnum == 0)
606 smsg(_("Executing: %s"), cmd);
607 else
608 smsg(_("line %ld: %s"), (long)lnum, cmd);
609 if (msg_silent == 0)
610 msg_puts("\n"); // don't overwrite this
611
612 verbose_leave_scroll();
613 --no_wait_return;
614}
615
616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 * Execute a simple command line. Used for translated commands like "*".
618 */
619 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100620do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 return do_cmdline(cmd, NULL, NULL,
623 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
624}
625
626/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100627 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
628 * This allows for using a range without ":" in Vim9 script.
629 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200630 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100631do_cmd_argument(char_u *cmd)
632{
633 return do_cmdline(cmd, NULL, NULL,
634 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
635}
636
637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 * do_cmdline(): execute one Ex command line
639 *
640 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100641 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 * 2. Split up in parts separated with '|'.
643 *
644 * This function can be called recursively!
645 *
646 * flags:
647 * DOCMD_VERBOSE - The command will be included in the error message.
648 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100649 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 * DOCMD_KEYTYPED - Don't reset KeyTyped.
651 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
652 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
653 *
654 * return FAIL if cmdline could not be executed, OK otherwise
655 */
656 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100657do_cmdline(
658 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200659 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100660 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100661 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100663 char_u *next_cmdline; // next cmd to execute
664 char_u *cmdline_copy = NULL; // copy of cmd line
665 int used_getline = FALSE; // used "fgetline" to obtain command
666 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 int count = 0; // line number count
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100669 int did_inc_RedrawingDisabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 int retval = OK;
671#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100672 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100673 garray_T lines_ga; // keep lines for ":while"/":for"
674 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200675 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 char_u *fname = NULL; // function or script name
677 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
678 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
679 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200681 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200682 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100684 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200685 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000689 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100691# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692# define cmd_cookie cookie
693#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100694 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200695#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100696 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
697 // location for storing error messages to be converted to an exception.
698 // This ensures that the do_errthrow() call in do_one_cmd() does not
699 // combine the messages stored by an earlier invocation of do_one_cmd()
700 // with the command name of the later one. This would happen when
701 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 saved_msg_list = msg_list;
703 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#endif
705
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100706 // It's possible to create an endless loop with ":execute", catch that
707 // here. The value of 200 allows nested function calls, ":source", etc.
708 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100709 if (call_depth >= 200
710#ifdef FEAT_EVAL
711 && call_depth >= p_mfd
712#endif
713 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000715 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100717 // When converting to an exception, we do not include the command name
718 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100719 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 msg_list = saved_msg_list;
721#endif
722 return FAIL;
723 }
724 ++call_depth;
725
726#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100727 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000729 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100731 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100733 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100734 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000735 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 ++ex_nesting_level;
737
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100738 // Get the function or script name and the address where the next breakpoint
739 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000740 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {
742 fname = func_name(real_cookie);
743 breakpoint = func_breakpoint(real_cookie);
744 dbg_tick = func_dbg_tick(real_cookie);
745 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100746 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100748 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 breakpoint = source_breakpoint(real_cookie);
750 dbg_tick = source_dbg_tick(real_cookie);
751 }
752
753 /*
754 * Initialize "force_abort" and "suppress_errthrow" at the top level.
755 */
756 if (!recursive)
757 {
758 force_abort = FALSE;
759 suppress_errthrow = FALSE;
760 }
761
762 /*
763 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000764 * exception handling (used when debugging). Otherwise clear it to avoid
765 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000767 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000768 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000769 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200770 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 initial_trylevel = trylevel;
773
774 /*
775 * "did_throw" will be set to TRUE when an exception is being thrown.
776 */
777 did_throw = FALSE;
778#endif
779 /*
780 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * If force_abort is set, we cancel everything.
783 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100784#ifdef FEAT_EVAL
785 did_emsg_cumul += did_emsg;
786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 did_emsg = FALSE;
788
789 /*
790 * KeyTyped is only set when calling vgetc(). Reset it here when not
791 * calling vgetc() (sourced command lines).
792 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100793 if (!(flags & DOCMD_KEYTYPED)
794 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 KeyTyped = FALSE;
796
797 /*
798 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000799 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 * - for multiple commands on one line, separated with '|'
801 * - when repeating until there are no more lines (for ":source")
802 */
803 next_cmdline = cmdline;
804 do
805 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100807 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#endif
809
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100810 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (next_cmdline == NULL
812#ifdef FEAT_EVAL
813 && !force_abort
814 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816#endif
817 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100818 {
819#ifdef FEAT_EVAL
820 did_emsg_cumul += did_emsg;
821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000826 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 * 3. If a line is given: Make a copy, so we can mess with it.
829 */
830
831#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000833 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100835 // Each '|' separated command is stored separately in lines_ga, to
836 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100837 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100839 // Check if a function has returned or, unless it has an unclosed
840 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000841 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000844 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845 func_line_end(real_cookie);
846# endif
847 if (func_has_ended(real_cookie))
848 {
849 retval = FAIL;
850 break;
851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000854 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100855 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856 script_line_end();
857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100859 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100860 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
862 retval = FAIL;
863 break;
864 }
865
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100866 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (breakpoint != NULL && dbg_tick != NULL
868 && *dbg_tick != debug_tick)
869 {
870 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100872 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 *dbg_tick = debug_tick;
874 }
875
876 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100877 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100879 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100881 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100883 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100884 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100886 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100887 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 *dbg_tick = debug_tick;
889 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000891 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000892 {
893 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100894 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100895 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896 script_line_start();
897 }
898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#endif
901
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100902 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (next_cmdline == NULL)
904 {
905 /*
906 * Need to set msg_didout for the first line after an ":if",
907 * otherwise the ":if" will be overwritten.
908 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100909 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100911 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912#ifdef FEAT_EVAL
913 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
914#else
915 0
916#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100917 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
918 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100920 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100921 // returned for the end of a sourced file or executed function
922 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (KeyTyped && !(flags & DOCMD_REPEAT))
924 need_wait_return = FALSE;
925 retval = FAIL;
926 break;
927 }
928 used_getline = TRUE;
929
930 /*
931 * Keep the first typed line. Clear it when more lines are typed.
932 */
933 if (flags & DOCMD_KEEPLINE)
934 {
935 vim_free(repeat_cmdline);
936 if (count == 0)
937 repeat_cmdline = vim_strsave(next_cmdline);
938 else
939 repeat_cmdline = NULL;
940 }
941 }
942
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100943 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 else if (cmdline_copy == NULL)
945 {
946 next_cmdline = vim_strsave(next_cmdline);
947 if (next_cmdline == NULL)
948 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200949 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 retval = FAIL;
951 break;
952 }
953 }
954 cmdline_copy = next_cmdline;
955
956#ifdef FEAT_EVAL
957 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200958 * Inside a while/for loop, and when the command looks like a ":while"
959 * or ":for", the line is stored, because we may need it later when
960 * looping.
961 *
962 * When there is a '|' and another command, it is stored separately,
963 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000964 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200965 *
966 * Pass a different "fgetline" function to do_one_cmd() below,
967 * that it stores lines in or reads them from "lines_ga". Makes it
968 * possible to define a function inside a while/for loop and handles
969 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200971 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200973 cmd_getline = get_loop_line;
974 cmd_cookie = (void *)&cmd_loop_cookie;
975 cmd_loop_cookie.lines_gap = &lines_ga;
976 cmd_loop_cookie.current_line = current_line;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100977 cmd_loop_cookie.lc_getline = fgetline;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200978 cmd_loop_cookie.cookie = cookie;
979 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
980
981 // Save the current line when encountering it the first time.
982 if (current_line == lines_ga.ga_len
983 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 {
985 retval = FAIL;
986 break;
987 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200988 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200990 else
991 {
992 cmd_getline = fgetline;
993 cmd_cookie = cookie;
994 }
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 did_endif = FALSE;
997#endif
998
999 if (count++ == 0)
1000 {
1001 /*
1002 * All output from the commands is put below each other, without
1003 * waiting for a return. Don't do this when executing commands
1004 * from a script or when being called recursive (e.g. for ":e
1005 * +command file").
1006 */
1007 if (!(flags & DOCMD_NOWAIT) && !recursive)
1008 {
1009 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001010 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001012 msg_scroll = TRUE; // put messages below each other
1013 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001015 did_inc_RedrawingDisabled = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 }
1018
Bram Moolenaar823654b2020-05-29 23:03:09 +02001019 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001020 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
1022 /*
1023 * 2. Execute one '|' separated command.
1024 * do_one_cmd() will return NULL if there is no trailing '|'.
1025 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1026 */
1027 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001028 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#ifdef FEAT_EVAL
1030 &cstack,
1031#endif
1032 cmd_getline, cmd_cookie);
1033 --recursive;
1034
1035#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001037 // Use "current_line" from "cmd_loop_cookie", it may have been
1038 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040#endif
1041
1042 if (next_cmdline == NULL)
1043 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001044 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 /*
1047 * If the command was typed, remember it for the ':' register.
1048 * Do this AFTER executing the command to make :@: work.
1049 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001050 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 && new_last_cmdline != NULL)
1052 {
1053 vim_free(last_cmdline);
1054 last_cmdline = new_last_cmdline;
1055 new_last_cmdline = NULL;
1056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 }
1058 else
1059 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001060 // need to copy the command after the '|' to cmdline_copy, for the
1061 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001062 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 next_cmdline = cmdline_copy;
1064 }
1065
1066
1067#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001068 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001070 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001072 {
1073 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {
1079 ++current_line;
1080
1081 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 * An ":endwhile", ":endfor" and ":continue" is handled here.
1083 * If we were executing commands, jump back to the ":while" or
1084 * ":for".
1085 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001087 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001089 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001091 // Jump back to the matching ":while" or ":for". Be careful
1092 // not to use a cs_line[] from an entry that isn't a ":while"
1093 // or ":for": It would make "current_line" invalid and can
1094 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 if (!did_emsg && !got_int && !did_throw
1096 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 && (cstack.cs_flags[cstack.cs_idx]
1098 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 && cstack.cs_line[cstack.cs_idx] >= 0
1100 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1101 {
1102 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001103 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001105 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001107 // Check for the next breakpoint at or after the ":while"
1108 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001109 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
1111 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001112 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 fname,
1114 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1115 *dbg_tick = debug_tick;
1116 }
1117 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001120 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001122 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1123 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 }
1125 }
1126
1127 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001133 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 }
1136
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001137 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001138 if (breakpoint != NULL && has_watchexpr())
1139 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001140 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001141 *dbg_tick = debug_tick;
1142 }
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 /*
1145 * When not inside any ":while" loop, clear remembered lines.
1146 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 if (lines_ga.ga_len > 0)
1150 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001151 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1153 free_cmdlines(&lines_ga);
1154 }
1155 current_line = 0;
1156 }
1157
1158 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001159 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1160 * being restored at the ":endtry". Reset them here and set the
1161 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1162 * This includes the case where a missing ":endif", ":endwhile" or
1163 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 cstack.cs_lflags &= ~CSL_HAD_FINA;
1168 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1169 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 did_throw ? (void *)current_exception : NULL);
1171 did_emsg = got_int = did_throw = FALSE;
1172 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1173 }
1174
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001175 // Update global "trylevel" for recursive calls to do_cmdline() from
1176 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 trylevel = initial_trylevel + cstack.cs_trylevel;
1178
1179 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001180 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 * files) is aborted because of an error, an interrupt, or an uncaught
1182 * exception, cancel everything. If it is left normally, reset
1183 * force_abort to get the non-EH compatible abortion behavior for
1184 * the rest of the script.
1185 */
1186 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1187 force_abort = FALSE;
1188
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001189 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001191#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
1193 }
1194 /*
1195 * Continue executing command lines when:
1196 * - no CTRL-C typed, no aborting error, no exception thrown or try
1197 * conditionals need to be checked for executing finally clauses or
1198 * catching an interrupt exception
1199 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 * looping for ":source" command or function call.
1202 */
1203 while (!((got_int
1204#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001205 || (did_emsg && (force_abort || in_vim9script()))
1206 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#endif
1208 )
1209#ifdef FEAT_EVAL
1210 && cstack.cs_trylevel == 0
1211#endif
1212 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001213 && !(did_emsg
1214#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001215 // Keep going when inside try/catch, so that the error can be
1216 // deal with, except when it is a syntax error, it may cause
1217 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001218 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1219#endif
1220 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001221 && (getline_equal(fgetline, cookie, getexmodeline)
1222 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 && (next_cmdline != NULL
1224#ifdef FEAT_EVAL
1225 || cstack.cs_idx >= 0
1226#endif
1227 || (flags & DOCMD_REPEAT)));
1228
1229 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001230 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231#ifdef FEAT_EVAL
1232 free_cmdlines(&lines_ga);
1233 ga_clear(&lines_ga);
1234
1235 if (cstack.cs_idx >= 0)
1236 {
1237 /*
1238 * If a sourced file or executed function ran to its end, report the
1239 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001240 * In Vim9 script do not give a second error, executing aborts after
1241 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001243 if (!got_int && !did_throw && !aborting()
1244 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001245 && ((getline_equal(fgetline, cookie, getsourceline)
1246 && !source_finished(fgetline, cookie))
1247 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 && !func_has_ended(real_cookie))))
1249 {
1250 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001251 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001253 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001254 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001255 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001257 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 }
1259
1260 /*
1261 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1262 * ":endtry" in a sourced file or executed function. If the try
1263 * conditional is in its finally clause, ignore anything pending.
1264 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001265 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 */
1267 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001268 {
1269 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1270
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001271 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001272 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001273 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1274 &cstack.cs_looplevel);
1275 }
1276 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 trylevel = initial_trylevel;
1278 }
1279
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001280 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1281 // lack was reported above and the error message is to be converted to an
1282 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001283 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 ? (char_u *)"endfunction" : (char_u *)NULL);
1285
1286 if (trylevel == 0)
1287 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001288 // Just in case did_throw got set but current_exception wasn't.
1289 if (current_exception == NULL)
1290 did_throw = FALSE;
1291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 /*
1293 * When an exception is being thrown out of the outermost try
1294 * conditional, discard the uncaught exception, disable the conversion
1295 * of interrupts or errors to exceptions, and ensure that no more
1296 * commands are executed.
1297 */
1298 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001299 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300
1301 /*
1302 * On an interrupt or an aborting error not converted to an exception,
1303 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001304 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 * when force_abort is set and did_emsg unset in case of an interrupt
1306 * from a finally clause after an error.
1307 */
1308 else if (got_int || (did_emsg && force_abort))
1309 suppress_errthrow = TRUE;
1310 }
1311
1312 /*
1313 * The current cstack will be freed when do_cmdline() returns. An uncaught
1314 * exception will have to be rethrown in the previous cstack. If a function
1315 * has just returned or a script file was just finished and the previous
1316 * cstack belongs to the same function or, respectively, script file, it
1317 * will have to be checked for finally clauses to be executed due to the
1318 * ":return" or ":finish". This is done in do_one_cmd().
1319 */
1320 if (did_throw)
1321 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001324 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 && ex_nesting_level > func_level(real_cookie) + 1))
1326 {
1327 if (!did_throw)
1328 check_cstack = TRUE;
1329 }
1330 else
1331 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001332 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 --ex_nesting_level;
1335 /*
1336 * Go to debug mode when returning from a function in which we are
1337 * single-stepping.
1338 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 if ((getline_equal(fgetline, cookie, getsourceline)
1340 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001342 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 ? (char_u *)_("End of sourced file")
1344 : (char_u *)_("End of function"));
1345 }
1346
1347 /*
1348 * Restore the exception environment (done after returning from the
1349 * debugger).
1350 */
1351 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001352 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
1354 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001355
1356 // Cleanup if "cs_emsg_silent_list" remains.
1357 if (cstack.cs_emsg_silent_list != NULL)
1358 {
1359 eslist_T *elem, *temp;
1360
1361 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1362 {
1363 temp = elem->next;
1364 vim_free(elem);
1365 }
1366 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001367#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 /*
1370 * If there was too much output to fit on the command line, ask the user to
1371 * hit return before redrawing the screen. With the ":global" command we do
1372 * this only once after the command is finished.
1373 */
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001374 if (did_inc_RedrawingDisabled)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001376 if (RedrawingDisabled > 0)
1377 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 --no_wait_return;
1379 msg_scroll = FALSE;
1380
1381 /*
1382 * When just finished an ":if"-":else" which was typed, no need to
1383 * wait for hit-return. Also for an error situation.
1384 */
1385 if (retval == FAIL
1386#ifdef FEAT_EVAL
1387 || (did_endif && KeyTyped && !did_emsg)
1388#endif
1389 )
1390 {
1391 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001392 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394 else if (need_wait_return)
1395 {
1396 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001397 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 * here should not overwrite the command that may be shown before
1399 * doing that.
1400 */
1401 msg_didout |= msg_didout_before_start;
1402 wait_return(FALSE);
1403 }
1404 }
1405
Bram Moolenaar2a942252012-11-28 23:03:07 +01001406#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001407 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001408#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /*
1410 * Reset if_level, in case a sourced script file contains more ":if" than
1411 * ":endif" (could be ":if x | foo | endif").
1412 */
1413 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001414#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 --call_depth;
1417 return retval;
1418}
1419
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001420#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001421/*
1422 * Handle when "did_throw" is set after executing commands.
1423 */
1424 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001425handle_did_throw(void)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001426{
1427 char *p = NULL;
1428 msglist_T *messages = NULL;
ichizok7e5fe382023-04-15 13:17:50 +01001429 ESTACK_CHECK_DECLARATION;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001430
1431 /*
1432 * If the uncaught exception is a user exception, report it as an
1433 * error. If it is an error exception, display the saved error
1434 * message now. For an interrupt exception, do nothing; the
1435 * interrupt message is given elsewhere.
1436 */
1437 switch (current_exception->type)
1438 {
1439 case ET_USER:
1440 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001441 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001442 current_exception->value);
1443 p = (char *)vim_strsave(IObuff);
1444 break;
1445 case ET_ERROR:
1446 messages = current_exception->messages;
1447 current_exception->messages = NULL;
1448 break;
1449 case ET_INTERRUPT:
1450 break;
1451 }
1452
1453 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1454 current_exception->throw_lnum);
ichizok7e5fe382023-04-15 13:17:50 +01001455 ESTACK_CHECK_SETUP;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001456 current_exception->throw_name = NULL;
1457
1458 discard_current_exception(); // uses IObuff if 'verbose'
LemonBoy749ba0f2024-07-04 19:23:16 +02001459
1460 // If "silent!" is active the uncaught exception is not fatal.
1461 if (emsg_silent == 0)
1462 {
1463 suppress_errthrow = TRUE;
1464 force_abort = TRUE;
1465 }
Bram Moolenaar620c9592021-07-31 21:32:31 +02001466
1467 if (messages != NULL)
1468 {
1469 do
1470 {
1471 msglist_T *next = messages->next;
1472 int save_compiling = estack_compiling;
1473
1474 estack_compiling = messages->msg_compiling;
1475 emsg(messages->msg);
1476 vim_free(messages->msg);
1477 vim_free(messages->sfile);
1478 vim_free(messages);
1479 messages = next;
1480 estack_compiling = save_compiling;
1481 }
1482 while (messages != NULL);
1483 }
1484 else if (p != NULL)
1485 {
1486 emsg(p);
1487 vim_free(p);
1488 }
1489 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01001490 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001491 estack_pop();
1492}
1493
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001495 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 */
1497 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001498get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001500 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 wcmd_T *wp;
1502 char_u *line;
1503
1504 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1505 {
1506 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001507 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001509 // First time inside the ":while"/":for": get line normally.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001510 if (cp->lc_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001511 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001513 line = cp->lc_getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001514 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 ++cp->current_line;
1516
1517 return line;
1518 }
1519
1520 KeyTyped = FALSE;
1521 ++cp->current_line;
1522 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001523 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 return vim_strsave(wp->line);
1525}
1526
1527/*
1528 * Store a line in "gap" so that a ":while" loop can execute it again.
1529 */
1530 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001531store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 if (ga_grow(gap, 1) == FAIL)
1534 return FAIL;
1535 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001536 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 return OK;
1539}
1540
1541/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001542 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 */
1544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001545free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
1547 while (gap->ga_len > 0)
1548 {
1549 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1550 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 }
1552}
1553#endif
1554
1555/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001556 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1557 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001560getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001561 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001562 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001563 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001566 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001569 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1570 // function that's originally used to obtain the lines. This may be
1571 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001572 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001573 cp = (struct loop_cookie *)cookie;
1574 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001576 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 cp = cp->cookie;
1578 }
1579 return gp == func;
1580#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001581 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582#endif
1583}
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001586 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 * getline function. Otherwise return "cookie".
1588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001590getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001591 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001592 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593{
Bram Moolenaar13505972019-01-24 15:04:48 +01001594#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001595 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001596 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001598 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1599 // cookie that's originally used to obtain the lines. This may be nested
1600 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001601 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001602 cp = (struct loop_cookie *)cookie;
1603 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001605 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 cp = cp->cookie;
1607 }
1608 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001609#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001614#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001615/*
1616 * Get the next line source line without advancing.
1617 */
1618 char_u *
1619getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001620 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001621 void *cookie) // argument for fgetline()
1622{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001623 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001624 struct loop_cookie *cp;
1625 wcmd_T *wp;
1626
1627 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1628 // cookie that's originally used to obtain the lines. This may be nested
1629 // several levels.
1630 gp = fgetline;
1631 cp = (struct loop_cookie *)cookie;
1632 while (gp == get_loop_line)
1633 {
1634 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1635 {
1636 // executing lines a second time, use the stored copy
1637 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1638 return wp->line;
1639 }
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001640 gp = cp->lc_getline;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001641 cp = cp->cookie;
1642 }
1643 if (gp == getsourceline)
1644 return source_nextline(cp);
1645 return NULL;
1646}
1647#endif
1648
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001649
1650/*
1651 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1652 * ":bwipeout", etc.
1653 * Returns the buffer number.
1654 */
1655 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001656compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001657{
1658 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001659 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001660 int count = offset;
1661
1662 buf = firstbuf;
1663 while (buf->b_next != NULL && buf->b_fnum < lnum)
1664 buf = buf->b_next;
1665 while (count != 0)
1666 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001667 count += (offset < 0) ? 1 : -1;
1668 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1669 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001670 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001671 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001672 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001673 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001674 while (buf->b_ml.ml_mfp == NULL)
1675 {
1676 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1677 if (nextbuf == NULL)
1678 break;
1679 buf = nextbuf;
1680 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001681 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001682 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001683 if (addr_type == ADDR_LOADED_BUFFERS)
1684 while (buf->b_ml.ml_mfp == NULL)
1685 {
1686 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1687 if (nextbuf == NULL)
1688 break;
1689 buf = nextbuf;
1690 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001691 return buf->b_fnum;
1692}
1693
Bram Moolenaarb7316892019-05-01 18:08:42 +02001694/*
1695 * Return the window number of "win".
1696 * When "win" is NULL return the number of windows.
1697 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001698 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001699current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001700{
1701 win_T *wp;
1702 int nr = 0;
1703
Bram Moolenaar29323592016-07-24 22:04:11 +02001704 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001705 {
1706 ++nr;
1707 if (wp == win)
1708 break;
1709 }
1710 return nr;
1711}
1712
1713 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001714current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001715{
1716 tabpage_T *tp;
1717 int nr = 0;
1718
Bram Moolenaar29323592016-07-24 22:04:11 +02001719 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001720 {
1721 ++nr;
1722 if (tp == tab)
1723 break;
1724 }
1725 return nr;
1726}
1727
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001728 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001729comment_start(char_u *p, int starts_with_colon UNUSED)
1730{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001731 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001732 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001733 return *p == '"';
1734}
1735
Bram Moolenaarf240e182014-11-27 18:33:02 +01001736# define CURRENT_WIN_NR current_win_nr(curwin)
1737# define LAST_WIN_NR current_win_nr(NULL)
1738# define CURRENT_TAB_NR current_tab_nr(curtab)
1739# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741/*
1742 * Execute one Ex command.
1743 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001744 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1745 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 *
1747 * 1. skip comment lines and leading space
1748 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001749 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001750 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001751 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001752 * 6. parse arguments
1753 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001755 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 *
1757 * This function may be called recursively!
1758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001760do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001761 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001762 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001764 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001766 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001767 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001769 char_u *p;
1770 linenr_T lnum;
1771 long n;
1772 char *errormsg = NULL; // error message
1773 char_u *after_modifier = NULL;
1774 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001775 cmdmod_T save_cmdmod;
1776 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001777 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001778 int ni; // set when Not Implemented
1779 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001780 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001781 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001782#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001783 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001784#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001785 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001786 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
Bram Moolenaara80faa82020-04-12 19:37:17 +02001788 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 ea.line1 = 1;
1790 ea.line2 = 1;
1791#ifdef FEAT_EVAL
1792 ++ex_nesting_level;
1793#endif
1794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001795 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (quitmore
1797#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001798 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001799 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001800#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001801 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001802 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 --quitmore;
1804
1805 /*
1806 * Reset browse, confirm, etc.. They are restored when returning, for
1807 * recursive calls.
1808 */
1809 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001811 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001812 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1813 goto doend;
1814
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001815/*
1816 * 1. Skip comment lines and leading white space and colons.
1817 * 2. Handle command modifiers.
1818 */
1819 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001821 ea.cmdlinep = cmdlinep;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001822 ea.ea_getline = fgetline;
Bram Moolenaareffed932018-08-14 13:38:17 +02001823 ea.cookie = cookie;
1824#ifdef FEAT_EVAL
1825 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001826 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001828 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001829 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001830 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001831 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
1833#ifdef FEAT_EVAL
1834 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1835 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1836#else
1837 ea.skip = (if_level > 0);
1838#endif
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001841 * 3. Skip over the range to find the command. Let "p" point to after it.
1842 *
1843 * We need the command to know what kind of range it uses.
1844 */
1845 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001846
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001847 // In Vim9 script a colon is required before the range. This may also be
1848 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001849 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001850 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001851 {
1852 may_have_range = FALSE;
1853 for (p = ea.cmd; p >= *cmdlinep; --p)
1854 {
1855 if (*p == ':')
1856 may_have_range = TRUE;
1857 if (p < ea.cmd && !VIM_ISWHITE(*p))
1858 break;
1859 }
1860 }
1861 else
1862 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001863 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001864 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001866#ifdef FEAT_EVAL
1867 // Handle ":export" - it functions almost like a command modifier.
1868 // ":export var Name: type"
1869 // ":export def Name(..."
1870 // etc.
1871 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1872 is_export = TRUE;
1873#endif
1874
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001875 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001876 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001877 if (ea.cmd == cmd + 1 && *cmd == '$')
1878 // should be "$VAR = val"
1879 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001880#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001881 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001882#else
1883 p = find_ex_command(&ea, NULL, NULL, NULL);
1884#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001885 if (ea.cmdidx == CMD_SIZE)
1886 {
1887 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1888
1889 // If a ':' before the range is missing, give a clearer error
1890 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001891 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001892 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001893 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001894 goto doend;
1895 }
1896 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001900
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001901#ifdef FEAT_EVAL
1902# ifdef FEAT_PROFILE
1903 // Count this line for profiling if skip is TRUE.
1904 if (do_profiling == PROF_YES
1905 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1906 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1907 {
1908 int skip = did_emsg || got_int || did_throw;
1909
1910 if (ea.cmdidx == CMD_catch)
1911 skip = !skip && !(cstack->cs_idx >= 0
1912 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1913 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1914 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1915 skip = skip || !(cstack->cs_idx >= 0
1916 && !(cstack->cs_flags[cstack->cs_idx]
1917 & (CSF_ACTIVE | CSF_TRUE)));
1918 else if (ea.cmdidx == CMD_finally)
1919 skip = FALSE;
1920 else if (ea.cmdidx != CMD_endif
1921 && ea.cmdidx != CMD_endfor
1922 && ea.cmdidx != CMD_endtry
1923 && ea.cmdidx != CMD_endwhile)
1924 skip = ea.skip;
1925
1926 if (!skip)
1927 {
1928 if (getline_equal(fgetline, cookie, get_func_line))
1929 func_line_exec(getline_cookie(fgetline, cookie));
1930 else if (getline_equal(fgetline, cookie, getsourceline))
1931 script_line_exec();
1932 }
1933 }
1934# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001935#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001936
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001937 ea.cmd = cmd;
1938
1939#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001940 // May go to debug mode. If this happens and the ">quit" debug command is
1941 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001942 dbg_check_breakpoint(&ea);
1943 if (!ea.skip && got_int)
1944 {
1945 ea.skip = TRUE;
1946 (void)do_intthrow(cstack);
1947 }
1948#endif
1949
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001950/*
1951 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 *
1953 * where 'addr' is:
1954 *
1955 * % (entire file)
1956 * $ [+-NUM]
1957 * 'x [+-NUM] (where x denotes a currently defined mark)
1958 * . [+-NUM]
1959 * [+-NUM]..
1960 * NUM
1961 *
1962 * The ea.cmd pointer is updated to point to the first character following the
1963 * range spec. If an initial address is found, but no second, the upper bound
1964 * is equal to the lower.
1965 */
1966
Bram Moolenaar25190db2019-05-04 15:05:28 +02001967 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001968 if (!IS_USER_CMDIDX(ea.cmdidx))
1969 {
1970 if (ea.cmdidx != CMD_SIZE)
1971 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1972 else
1973 ea.addr_type = ADDR_LINES;
1974
Bram Moolenaar25190db2019-05-04 15:05:28 +02001975 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001976 if (ea.cmdidx == CMD_wincmd && p != NULL)
1977 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001978#ifdef FEAT_QUICKFIX
1979 // :.cc in quickfix window uses line number
1980 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1981 ea.addr_type = ADDR_OTHER;
1982#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001983 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001984
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001985 if (!may_have_range)
1986 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001987 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1988 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001991 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 */
1993
1994 /*
1995 * Skip ':' and any white space
1996 */
1997 ea.cmd = skipwhite(ea.cmd);
1998 while (*ea.cmd == ':')
1999 ea.cmd = skipwhite(ea.cmd + 1);
2000
2001 /*
2002 * If we got a line, but no command, then go to the line.
2003 * If we find a '|' or '\n' we set ea.nextcmd.
2004 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002005 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
2006 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 /*
2009 * strange vi behaviour:
2010 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01002011 * ":3|..." prints line 3 (not in Vim9 script)
2012 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002014 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002016 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 goto doend;
2018 }
2019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 // If this looks like an undefined user command and there are CmdUndefined
2021 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002022 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2023 && ASCII_ISUPPER(*ea.cmd)
2024 && has_cmdundefined())
2025 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002026 int ret;
2027
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002028 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002029 while (ASCII_ISALNUM(*p))
2030 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002031 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002032 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2033 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // If the autocommands did something and didn't cause an error, try
2035 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002036 p = (ret
2037#ifdef FEAT_EVAL
2038 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002039#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002041 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (p == NULL)
2044 {
2045 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002046 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 goto doend;
2048 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002049 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002050 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2051 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
2053 errormsg = uc_fun_cmd();
2054 goto doend;
2055 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (ea.cmdidx == CMD_SIZE)
2058 {
2059 if (!ea.skip)
2060 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002061 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002063 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002064 // If the modifier was parsed OK the error must be in the
2065 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002066 if (after_modifier != NULL)
2067 append_command(after_modifier);
2068 else
2069 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002070 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002071 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002072 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002073 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075 goto doend;
2076 }
2077
Bram Moolenaar958636c2014-10-21 20:01:58 +02002078 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2079 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002080#ifdef HAVE_EX_SCRIPT_NI
2081 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2082#endif
2083 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
2085#ifndef FEAT_EVAL
2086 /*
2087 * When the expression evaluation is disabled, recognize the ":if" and
2088 * ":endif" commands and ignore everything in between it.
2089 */
2090 if (ea.cmdidx == CMD_if)
2091 ++if_level;
2092 if (if_level)
2093 {
2094 if (ea.cmdidx == CMD_endif)
2095 --if_level;
2096 goto doend;
2097 }
2098
2099#endif
2100
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002101 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002102 if (*p == '!' && ea.cmdidx != CMD_substitute
2103 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
2105 ++p;
2106 ea.forceit = TRUE;
2107 }
2108 else
2109 ea.forceit = FALSE;
2110
2111/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002112 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002114 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002115 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117 if (!ea.skip)
2118 {
2119#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002120 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002122 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002123 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 goto doend;
2125 }
2126#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002127 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002128 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002129 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002130 goto doend;
2131 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002132 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002134 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002135 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 goto doend;
2137 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002138
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002139 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002141 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2142 {
2143 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002144 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002145 goto doend;
2146 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002147 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2148 {
2149 // Command not allowed when text is locked
2150 errormsg = _(get_text_locked_msg());
2151 goto doend;
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002155 // Disallow editing another buffer when "curbuf_lock" is set.
2156 // Do allow ":checktime" (it is postponed).
2157 // Do allow ":edit" (check for an argument later).
2158 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002159 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002160 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002161 && ea.cmdidx != CMD_edit
2162 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002163 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002164 && curbuf_locked())
2165 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002167 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002169 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 goto doend;
2171 }
2172 }
2173
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002174 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002176 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 goto doend;
2178 }
2179
2180 /*
2181 * Don't complain about the range if it is not used
2182 * (could happen if line_count is accidentally set to 0).
2183 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002184 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
2186 /*
2187 * If the range is backwards, ask for confirmation and, if given, swap
2188 * ea.line1 & ea.line2 so it's forwards again.
2189 * When global command is busy, don't ask, will fail below.
2190 */
2191 if (!global_busy && ea.line1 > ea.line2)
2192 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002193 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002195 if (sourcing || exmode_active)
2196 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002197 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002198 goto doend;
2199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 if (ask_yesno((char_u *)
2201 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002202 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204 lnum = ea.line1;
2205 ea.line1 = ea.line2;
2206 ea.line2 = lnum;
2207 }
2208 if ((errormsg = invalid_range(&ea)) != NULL)
2209 goto doend;
2210 }
2211
Bram Moolenaarb7316892019-05-01 18:08:42 +02002212 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2213 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 ea.line2 = 1;
2215
2216 correct_range(&ea);
2217
2218#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002219 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002220 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002222 // Put the first line at the start of a closed fold, put the last line
2223 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 (void)hasFolding(ea.line1, &ea.line1, NULL);
2225 (void)hasFolding(ea.line2, NULL, &ea.line2);
2226 }
2227#endif
2228
2229#ifdef FEAT_QUICKFIX
2230 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002231 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 * option here, so things like % get expanded.
2233 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002234 p = replace_makeprg(&ea, p, cmdlinep);
2235 if (p == NULL)
2236 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#endif
2238
2239 /*
2240 * Skip to start of argument.
2241 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2242 */
2243 if (ea.cmdidx == CMD_bang)
2244 ea.arg = p;
2245 else
2246 ea.arg = skipwhite(p);
2247
Bram Moolenaar379fb762018-08-30 15:58:28 +02002248 // ":file" cannot be run with an argument when "curbuf_lock" is set
2249 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2250 goto doend;
2251
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 /*
2253 * Check for "++opt=val" argument.
2254 * Must be first, allow ":w ++enc=utf8 !cmd"
2255 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002256 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2258 if (getargopt(&ea) == FAIL && !ni)
2259 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002260 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 goto doend;
2262 }
2263
2264 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2265 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002266 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002268 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002270 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 goto doend;
2272 }
2273 ea.arg = skipwhite(ea.arg + 1);
2274 ea.append = TRUE;
2275 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002276 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
2278 ++ea.arg;
2279 ea.usefilter = TRUE;
2280 }
2281 }
2282
2283 if (ea.cmdidx == CMD_read)
2284 {
2285 if (ea.forceit)
2286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002287 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 ea.forceit = FALSE;
2289 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002290 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 ++ea.arg;
2293 ea.usefilter = TRUE;
2294 }
2295 }
2296
2297 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2298 {
2299 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002300 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 ++ea.arg;
2303 ++ea.amount;
2304 }
2305 ea.arg = skipwhite(ea.arg);
2306 }
2307
2308 /*
2309 * Check for "+command" argument, before checking for next command.
2310 * Don't do this for ":read !cmd" and ":write !cmd".
2311 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002312 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2314
2315 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002316 * For commands that do not use '|' inside their argument: Check for '|' to
2317 * separate commands and '"' or '#' to start comments.
2318 *
2319 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002320 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002321 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002322 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002324 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2325 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002326 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002327 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002328 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002329 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002330 || ea.cmdidx == CMD_global
2331 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002332 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002333#ifdef FEAT_EVAL
2334 || inside_block(&ea)
2335#endif
2336 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
2338 for (p = ea.arg; *p; ++p)
2339 {
Mohamed Akramf3daa452024-07-06 17:12:09 +02002340 // Remove one backslash before a newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002342 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002343 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 ea.nextcmd = p + 1;
2346 *p = NUL;
2347 break;
2348 }
2349 }
2350 }
2351
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002352 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002353 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002355 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002356 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002358 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002359 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002362#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002363 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002364 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002366 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002367 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002370 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2371 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002372 {
2373 ea.regname = *ea.arg++;
2374#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002375 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002376 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2377 {
kuuote08d7b1c2021-10-04 22:17:36 +01002378 if (!ea.skip)
2379 {
2380 set_expr_line(vim_strsave(ea.arg), &ea);
2381 did_set_expr_line = TRUE;
2382 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002383 ea.arg += STRLEN(ea.arg);
2384 }
2385#endif
2386 ea.arg = skipwhite(ea.arg);
2387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
2389
2390 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002391 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 * count, it's a buffer name.
2393 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002394 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002395 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002396 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002398 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002400 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002402 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 goto doend;
2404 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002405 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
2407 ea.line2 = n;
2408 if (ea.addr_count == 0)
2409 ea.addr_count = 1;
2410 }
2411 else
2412 {
2413 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002414 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002415 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002416 else
2417 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 ++ea.addr_count;
2419 /*
2420 * Be vi compatible: no error message for out of range.
2421 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002422 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 ea.line2 = curbuf->b_ml.ml_line_count;
2424 }
2425 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002426
2427 /*
2428 * Check for flags: 'l', 'p' and '#'.
2429 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002430 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002431 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002432 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2433 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002435 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002436 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 goto doend;
2438 }
2439
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002440 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002442 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 goto doend;
2444 }
2445
2446#ifdef FEAT_EVAL
2447 /*
2448 * Skip the command when it's not going to be executed.
2449 * The commands like :if, :endif, etc. always need to be executed.
2450 * Also make an exception for commands that handle a trailing command
2451 * themselves.
2452 */
2453 if (ea.skip)
2454 {
2455 switch (ea.cmdidx)
2456 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002457 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_while:
2459 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002460 case CMD_for:
2461 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_if:
2463 case CMD_elseif:
2464 case CMD_else:
2465 case CMD_endif:
2466 case CMD_try:
2467 case CMD_catch:
2468 case CMD_finally:
2469 case CMD_endtry:
2470 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002471 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 break;
2473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002474 // Commands that handle '|' themselves. Check: A command should
2475 // either have the EX_TRLBAR flag, appear in this list or appear in
2476 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 case CMD_aboveleft:
2478 case CMD_and:
2479 case CMD_belowright:
2480 case CMD_botright:
2481 case CMD_browse:
2482 case CMD_call:
2483 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002484 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 case CMD_delfunction:
2486 case CMD_djump:
2487 case CMD_dlist:
2488 case CMD_dsearch:
2489 case CMD_dsplit:
2490 case CMD_echo:
2491 case CMD_echoerr:
2492 case CMD_echomsg:
2493 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002494 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002496 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002497 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 case CMD_help:
2499 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002500 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 case CMD_ijump:
2502 case CMD_ilist:
2503 case CMD_isearch:
2504 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002505 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 case CMD_keepjumps:
2507 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002508 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_leftabove:
2510 case CMD_let:
2511 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002512 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002513 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002515 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002516 case CMD_noautocmd:
2517 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 case CMD_perl:
2519 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002520 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002521 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002522 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 case CMD_return:
2524 case CMD_rightbelow:
2525 case CMD_ruby:
2526 case CMD_silent:
2527 case CMD_smagic:
2528 case CMD_snomagic:
2529 case CMD_substitute:
2530 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002531 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 case CMD_tcl:
2533 case CMD_throw:
2534 case CMD_tilde:
2535 case CMD_topleft:
2536 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002537 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002538 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 case CMD_verbose:
2540 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002541 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 break;
2543
2544 default: goto doend;
2545 }
2546 }
2547#endif
2548
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002549 if ((ea.argt & EX_XFILE)
2550 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2551 goto doend;
2552
2553#ifdef FEAT_EVAL
2554 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002556 emsg(_(e_invalid_command_after_export));
2557 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 /*
2562 * Accept buffer name. Cannot be used at the same time with a buffer
2563 * number. Don't do this for a user command.
2564 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002565 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002566 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
2568 /*
2569 * :bdelete, :bwipeout and :bunload take several arguments, separated
2570 * by spaces: find next space (skipping over escaped characters).
2571 * The others take one argument: ignore trailing spaces.
2572 */
2573 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2574 || ea.cmdidx == CMD_bunload)
2575 p = skiptowhite_esc(ea.arg);
2576 else
2577 {
2578 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002579 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 --p;
2581 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002582 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002583 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002584 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 goto doend;
2586 ea.addr_count = 1;
2587 ea.arg = skipwhite(p);
2588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002590 // The :try command saves the emsg_silent flag, reset it here when
2591 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002592 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002593 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002594 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002595 if (emsg_silent < 0)
2596 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002597 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002598 }
2599
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002601 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
Bram Moolenaar958636c2014-10-21 20:01:58 +02002604 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
2606 /*
2607 * Execute a user-defined command.
2608 */
2609 do_ucmd(&ea);
2610 }
2611 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
2613 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002614 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2617 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002618 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620
2621#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002622 // A command will reset "is_export" when exporting an item. If it is still
LemonBoy90c27b22023-08-27 19:28:15 +02002623 // set something went wrong or the command was never executed.
2624 if (!ea.skip && is_export)
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002625 {
2626 if (errormsg == NULL)
2627 errormsg = _(e_export_with_invalid_argument);
2628 is_export = FALSE;
2629 }
2630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002632 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002633 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002634 && current_sctx.sc_sid > 0
2635 && ea.cmdidx != CMD_endif
2636 && (cstack->cs_idx < 0
2637 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002638 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 /*
2641 * If the command just executed called do_cmdline(), any throw or ":return"
2642 * or ":finish" encountered there must also check the cstack of the still
2643 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2644 * exception, or reanimate a returned function or finished script file and
2645 * return or finish it again.
2646 */
2647 if (need_rethrow)
2648 do_throw(cstack);
2649 else if (check_cstack)
2650 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002651 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002653 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 && current_func_returned())
2655 do_return(&ea, TRUE, FALSE, NULL);
2656 }
2657 need_rethrow = check_cstack = FALSE;
2658#endif
2659
2660doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002661 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002664 curwin->w_cursor.col = 0;
2665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
2667 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2668 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002669 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002671 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
2673 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002674 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002676 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678 emsg(errormsg);
2679 }
2680#ifdef FEAT_EVAL
2681 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002682 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2683 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002684
2685 if (did_set_expr_line)
2686 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002687 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688#endif
2689
Bram Moolenaare1004402020-10-24 20:49:43 +02002690 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002692 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002693 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002695 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 ea.nextcmd = NULL;
2697
2698#ifdef FEAT_EVAL
2699 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002700 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701#endif
2702
2703 return ea.nextcmd;
2704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002706static char ex_error_buf[MSG_BUF_LEN];
2707
2708/*
2709 * Return an error message with argument included.
2710 * Uses a static buffer, only the last error will be kept.
2711 * "msg" will be translated, caller should use N_().
2712 */
2713 char *
2714ex_errmsg(char *msg, char_u *arg)
2715{
2716 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2717 return ex_error_buf;
2718}
2719
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720/*
zeertzjq7d664bf2024-07-14 10:22:54 +02002721 * The "+" string used in place of an empty command in Ex mode.
2722 * This string is used in pointer comparison.
2723 */
2724static char exmode_plus[] = "+";
2725
2726/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002727 * Handle a range without a command.
2728 * Returns an error message on failure.
2729 */
2730 char *
2731ex_range_without_command(exarg_T *eap)
2732{
2733 char *errormsg = NULL;
2734
zeertzjq7d664bf2024-07-14 10:22:54 +02002735 if ((*eap->cmd == '|' ||
2736 (exmode_active && eap->cmd != (char_u *)exmode_plus + 1))
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002737#ifdef FEAT_EVAL
2738 && !in_vim9script()
2739#endif
2740 )
2741 {
2742 eap->cmdidx = CMD_print;
2743 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2744 if ((errormsg = invalid_range(eap)) == NULL)
2745 {
2746 correct_range(eap);
2747 ex_print(eap);
2748 }
2749 }
2750 else if (eap->addr_count != 0)
2751 {
2752 if (eap->line2 > curbuf->b_ml.ml_line_count)
2753 {
2754 // With '-' in 'cpoptions' a line number past the file is an
2755 // error, otherwise put it at the end of the file.
2756 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2757 eap->line2 = -1;
2758 else
2759 eap->line2 = curbuf->b_ml.ml_line_count;
2760 }
2761
2762 if (eap->line2 < 0)
2763 errormsg = _(e_invalid_range);
2764 else
2765 {
2766 if (eap->line2 == 0)
2767 curwin->w_cursor.lnum = 1;
2768 else
2769 curwin->w_cursor.lnum = eap->line2;
2770 beginline(BL_SOL | BL_FIX);
2771 }
2772 }
2773 return errormsg;
2774}
2775
2776/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002777 * Check for an Ex command with optional tail.
2778 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002779 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002780 */
2781 static int
2782checkforcmd_opt(
2783 char_u **pp, // start of command
2784 char *cmd, // name of command
2785 int len, // required length
2786 int noparen)
2787{
2788 int i;
2789
2790 for (i = 0; cmd[i] != NUL; ++i)
2791 if (((char_u *)cmd)[i] != (*pp)[i])
2792 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002793 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002794 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002795 {
2796 *pp = skipwhite(*pp + i);
2797 return TRUE;
2798 }
2799 return FALSE;
2800}
2801
2802/*
2803 * Check for an Ex command with optional tail.
2804 * If there is a match advance "pp" to the argument and return TRUE.
2805 */
2806 int
2807checkforcmd(
2808 char_u **pp, // start of command
2809 char *cmd, // name of command
2810 int len) // required length
2811{
2812 return checkforcmd_opt(pp, cmd, len, FALSE);
2813}
2814
2815/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002816 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002817 * If there is a match advance "pp" to the argument and return TRUE.
2818 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002819 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002820checkforcmd_noparen(
2821 char_u **pp, // start of command
2822 char *cmd, // name of command
2823 int len) // required length
2824{
2825 return checkforcmd_opt(pp, cmd, len, TRUE);
2826}
2827
2828/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002829 * Parse and skip over command modifiers:
2830 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002831 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002833 * When "skip_only" is TRUE the global variables are not changed, except for
2834 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002835 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2836 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002837 * Call apply_cmdmod() to get the side effects of the modifiers:
2838 * - Increment "sandbox" for ":sandbox"
2839 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002840 * - set msg_silent for ":silent"
2841 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 * Return FAIL when the command is not to be executed.
2843 * May set "errormsg" to an error message.
2844 */
2845 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002846parse_command_modifiers(
2847 exarg_T *eap,
2848 char **errormsg,
2849 cmdmod_T *cmod,
2850 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002851{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002852 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002853 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002854 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002855 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002856 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002857 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002858
Bram Moolenaare1004402020-10-24 20:49:43 +02002859 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002860 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002861
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002862 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2863 {
2864 // The automatically inserted Visual area range is skipped, so that
2865 // typing ":cmdmod cmd" in Visual mode works without having to move the
zeertzjqd9be94c2024-07-14 10:20:20 +02002866 // range to after the modifiers. The command will be "'<,'>cmdmod cmd",
2867 // parse "cmdmod cmd" and then put back "'<,'>" before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002868 eap->cmd += 5;
2869 cmd_start = eap->cmd;
2870 has_visual_range = TRUE;
2871 }
2872
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002873 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002874 for (;;)
2875 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002876 char_u *p;
2877
Bram Moolenaareffed932018-08-14 13:38:17 +02002878 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002879 {
2880 if (*eap->cmd == ':')
2881 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002883 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002884
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002885 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002887 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2888 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2890 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002891 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002892 if (!skip_only)
2893 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002894 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002895 }
2896
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002897 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002898 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002899 {
2900 // a comment ends at a NL
2901 if (eap->nextcmd == NULL)
2902 {
2903 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2904 if (eap->nextcmd != NULL)
2905 ++eap->nextcmd;
2906 }
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002907 if (vim9script)
2908 {
2909 if (has_cmdmod(cmod, FALSE))
2910 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002911#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002912 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2913 && eap->cmd[2] != '{')
2914 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002915#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002916 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002917 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002918 }
zeertzjq2432b4a2024-09-03 22:58:30 +02002919 if (eap->nextcmd == NULL && *eap->cmd == '\n')
2920 {
2921 eap->nextcmd = eap->cmd + 1;
2922 return FAIL;
2923 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002924 if (*eap->cmd == NUL)
2925 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002926 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002927 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002928 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002929 if (vim9script && has_cmdmod(cmod, FALSE))
2930 *errormsg = _(e_command_modifier_without_command);
2931 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002932 return FAIL;
2933 }
2934
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002935 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002936
2937 // In Vim9 script a variable can shadow a command modifier:
2938 // verbose = 123
2939 // verbose += 123
2940 // silent! verbose = func()
2941 // verbose.member = 2
2942 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002943 // But not:
2944 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002945 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002946 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002947 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002948
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002949 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002950 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002951 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002952 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2953 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002954 break;
2955 }
2956
Bram Moolenaareffed932018-08-14 13:38:17 +02002957 switch (*p)
2958 {
John Marriotted908f72024-04-18 22:46:56 +02002959 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002960 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002961 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002962 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 continue;
2964
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002965 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002966 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002967 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002968 continue;
2969 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002970 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002971 {
2972#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002973 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002974#endif
2975 continue;
2976 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002977 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002978 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002979 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002980 continue;
2981
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002982 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002983 break;
2984#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002985 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002986#endif
2987 continue;
2988
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002989 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002990 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002991 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002992 continue;
2993 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002994 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002995 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002996 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 continue;
2998 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002999 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003000 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003001 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 continue;
3003 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003004 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003005 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003006 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 continue;
3008
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003009 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003010 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003011 char_u *reg_pat;
3012 char_u *nulp = NULL;
3013 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003014
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003015 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003016 || *p == NUL
3017 || (ends_excmd(*p)
3018#ifdef FEAT_EVAL
3019 // in ":filter #pat# cmd" # does not
3020 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003021 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003022#endif
3023 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003024 break;
3025 if (*p == '!')
3026 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003027 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003028 p = skipwhite(p + 1);
3029 if (*p == NUL || ends_excmd(*p))
3030 break;
3031 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003032#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003033 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003034 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003035 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003036#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003037 if (skip_only)
3038 p = skip_vimgrep_pat(p, NULL, NULL);
3039 else
3040 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003041 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3042 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003043 if (p == NULL || *p == NUL)
3044 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003045 if (!skip_only)
3046 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003047 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003048 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003049 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003050 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003051 // restore the character overwritten by NUL
3052 if (nulp != NULL)
3053 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003054 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003055 eap->cmd = p;
3056 continue;
3057 }
3058
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003059 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3060 {
3061 cmod->cmod_split |= WSP_HOR;
3062 continue;
3063 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003064 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003065 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003066 || *p == NUL || ends_excmd(*p))
3067 break;
3068 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003069 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003070 continue;
3071
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003072 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003073 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003074 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003075 continue;
3076 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003077 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3078 {
3079 if (ends_excmd2(p, eap->cmd))
3080 {
3081 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003082 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003083 return FAIL;
3084 }
3085 cmod->cmod_flags |= CMOD_LEGACY;
3086 continue;
3087 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003088
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003089 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003090 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003091 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003092 continue;
3093
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003094 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003095 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003096 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003097 continue;
3098 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003099 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003100 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003101 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003102 continue;
3103
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003104 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003105 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003106 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003107 continue;
3108
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003109 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003110 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003111 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003112 continue;
3113 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003114 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003115 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003116 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003117 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3118 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003119 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003120 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003121 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003122 }
3123 continue;
3124
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003125 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003126 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003127 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003128 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003129 long tabnr = get_address(eap, &eap->cmd,
3130 ADDR_TABS, eap->skip,
3131 skip_only, FALSE, 1);
3132 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003133 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003134 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003135 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003136 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3137 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003138 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003139 return FAIL;
3140 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003141 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003142 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003143 }
3144 eap->cmd = p;
3145 continue;
3146 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003147 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003148 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003149 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003150 continue;
3151
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003152 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003153 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003154 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003155 continue;
3156
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003157 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003158 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003159 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003160 continue;
3161 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003162 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003163 {
3164 if (ends_excmd2(p, eap->cmd))
3165 {
3166 *errormsg =
3167 _(e_vim9cmd_must_be_followed_by_command);
3168 return FAIL;
3169 }
3170 cmod->cmod_flags |= CMOD_VIM9CMD;
3171 continue;
3172 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003173 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003174 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003175 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003176 {
zeertzjqcd749632022-06-14 13:30:35 +01003177 // zero means not set, one is verbose == 0, etc.
3178 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003179 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003180 else
zeertzjqcd749632022-06-14 13:30:35 +01003181 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003182 eap->cmd = p;
3183 continue;
3184 }
3185 break;
3186 }
3187
Bram Moolenaar1501b632022-03-27 16:56:21 +01003188 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003189 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003190 if (eap->cmd > cmd_start)
3191 {
3192 // Move the '<,'> range to after the modifiers and insert a colon.
3193 // Since the modifiers have been parsed put the colon on top of the
3194 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3195 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003196 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003197 {
3198 size_t len = STRLEN(cmd_start);
3199
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003200 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003201 // "'<,'>mods" -> "mods *+
3202 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003203 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003204 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003205 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003206 }
3207 else
3208 {
3209 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3210 eap->cmd -= 5;
3211 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3212 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003213 }
3214 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003215 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003216 // Special case: change empty command to "+".
3217 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003218 eap->cmd = (char_u *)"'<,'>+";
3219 else
3220 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003221 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003222 else if (use_plus_cmd)
zeertzjq7d664bf2024-07-14 10:22:54 +02003223 eap->cmd = (char_u *)exmode_plus;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003224
Bram Moolenaareffed932018-08-14 13:38:17 +02003225 return OK;
3226}
3227
3228/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003229 * Return TRUE if "cmod" has anything set.
3230 */
3231 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003232has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003233{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003234 return (cmod->cmod_flags != 0 && (!ignore_silent
3235 || (cmod->cmod_flags
3236 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003237 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003238 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003239 || cmod->cmod_tab != 0
3240 || cmod->cmod_filter_regmatch.regprog != NULL;
3241}
3242
Bram Moolenaar8991be22022-02-14 21:51:46 +00003243#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003244/*
3245 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3246 */
3247 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003248cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003249{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003250 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003251 {
3252 emsg(_(e_misplaced_command_modifier));
3253 return TRUE;
3254 }
3255 return FALSE;
3256}
Dominique Pelle748b3082022-01-08 12:41:16 +00003257#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003258
3259/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003260 * Apply the command modifiers. Saves current state in "cmdmod", call
3261 * undo_cmdmod() later.
3262 */
3263 void
3264apply_cmdmod(cmdmod_T *cmod)
3265{
3266#ifdef HAVE_SANDBOX
3267 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3268 {
3269 ++sandbox;
3270 cmod->cmod_did_sandbox = TRUE;
3271 }
3272#endif
zeertzjqcd749632022-06-14 13:30:35 +01003273 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003274 {
3275 if (cmod->cmod_verbose_save == 0)
3276 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003277 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003278 }
3279
3280 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3281 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003282 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003283 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003284 cmod->cmod_save_msg_scroll = msg_scroll;
3285 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003286 if (cmod->cmod_flags & CMOD_SILENT)
3287 ++msg_silent;
3288 if (cmod->cmod_flags & CMOD_UNSILENT)
3289 msg_silent = 0;
3290
3291 if (cmod->cmod_flags & CMOD_ERRSILENT)
3292 {
3293 ++emsg_silent;
3294 ++cmod->cmod_did_esilent;
3295 }
3296
Bram Moolenaare1004402020-10-24 20:49:43 +02003297 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003298 {
3299 // Set 'eventignore' to "all".
3300 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003301 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003302 set_string_option_direct((char_u *)"ei", -1,
3303 (char_u *)"all", OPT_FREE, SID_NONE);
3304 }
3305}
3306
3307/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003308 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003309 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003310 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003311undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003312{
Bram Moolenaare1004402020-10-24 20:49:43 +02003313 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003314 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003315 p_verbose = cmod->cmod_verbose_save - 1;
3316 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003317 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003318
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003319#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003320 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003321 {
3322 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003323 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003324 }
3325#endif
3326
Bram Moolenaare1004402020-10-24 20:49:43 +02003327 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003328 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003329 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003330 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3331 OPT_FREE, SID_NONE);
3332 free_string_option(cmod->cmod_save_ei);
3333 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003334 }
3335
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003336 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003337
Bram Moolenaare1004402020-10-24 20:49:43 +02003338 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003339 {
3340 // messages could be enabled for a serious error, need to check if the
3341 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003342 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3343 msg_silent = cmod->cmod_save_msg_silent - 1;
3344 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003345 if (emsg_silent < 0)
3346 emsg_silent = 0;
3347 // Restore msg_scroll, it's set by file I/O commands, even when no
3348 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003349 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003350
3351 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3352 // somewhere in the line. Put it back in the first column.
3353 if (redirecting())
3354 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003355
Bram Moolenaare1004402020-10-24 20:49:43 +02003356 cmod->cmod_save_msg_silent = 0;
3357 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003358 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003359}
3360
3361/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003362 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003363 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003364 * Return FAIL and set "errormsg" or return OK.
3365 */
3366 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003367parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003368{
3369 int address_count = 1;
3370 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003371 int need_check_cursor = FALSE;
3372 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003373
3374 // Repeat for all ',' or ';' separated addresses.
3375 for (;;)
3376 {
3377 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003378 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003379 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003380 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003381 eap->addr_count == 0, address_count++);
3382 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003383 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003384 if (lnum == MAXLNUM)
3385 {
3386 if (*eap->cmd == '%') // '%' - all lines
3387 {
3388 ++eap->cmd;
3389 switch (eap->addr_type)
3390 {
3391 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003392 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003393 eap->line1 = 1;
3394 eap->line2 = curbuf->b_ml.ml_line_count;
3395 break;
3396 case ADDR_LOADED_BUFFERS:
3397 {
3398 buf_T *buf = firstbuf;
3399
3400 while (buf->b_next != NULL
3401 && buf->b_ml.ml_mfp == NULL)
3402 buf = buf->b_next;
3403 eap->line1 = buf->b_fnum;
3404 buf = lastbuf;
3405 while (buf->b_prev != NULL
3406 && buf->b_ml.ml_mfp == NULL)
3407 buf = buf->b_prev;
3408 eap->line2 = buf->b_fnum;
3409 break;
3410 }
3411 case ADDR_BUFFERS:
3412 eap->line1 = firstbuf->b_fnum;
3413 eap->line2 = lastbuf->b_fnum;
3414 break;
3415 case ADDR_WINDOWS:
3416 case ADDR_TABS:
3417 if (IS_USER_CMDIDX(eap->cmdidx))
3418 {
3419 eap->line1 = 1;
3420 eap->line2 = eap->addr_type == ADDR_WINDOWS
3421 ? LAST_WIN_NR : LAST_TAB_NR;
3422 }
3423 else
3424 {
3425 // there is no Vim command which uses '%' and
3426 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003427 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003428 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003429 }
3430 break;
3431 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003432 case ADDR_UNSIGNED:
3433 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003434 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003435 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003436 case ADDR_ARGUMENTS:
3437 if (ARGCOUNT == 0)
3438 eap->line1 = eap->line2 = 0;
3439 else
3440 {
3441 eap->line1 = 1;
3442 eap->line2 = ARGCOUNT;
3443 }
3444 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003445 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003446#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003447 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003448 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003449 if (eap->line2 == 0)
3450 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003451#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003452 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003453 case ADDR_NONE:
3454 // Will give an error later if a range is found.
3455 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003456 }
3457 ++eap->addr_count;
3458 }
3459 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3460 {
3461 pos_T *fp;
3462
3463 // '*' - visual area
3464 if (eap->addr_type != ADDR_LINES)
3465 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003466 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003467 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003468 }
3469
3470 ++eap->cmd;
3471 if (!eap->skip)
3472 {
3473 fp = getmark('<', FALSE);
3474 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003475 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003476 eap->line1 = fp->lnum;
3477 fp = getmark('>', FALSE);
3478 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003479 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003480 eap->line2 = fp->lnum;
3481 ++eap->addr_count;
3482 }
3483 }
3484 }
3485 else
3486 eap->line2 = lnum;
3487 eap->addr_count++;
3488
3489 if (*eap->cmd == ';')
3490 {
3491 if (!eap->skip)
3492 {
3493 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003494
Bram Moolenaar0e717042020-04-27 19:29:01 +02003495 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003496 // accept zero as address, so 0;/PATTERN/ works correctly
3497 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003498 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003499 if (eap->line2 > 0)
3500 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003501 else
3502 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003503 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003504 }
3505 }
3506 else if (*eap->cmd != ',')
3507 break;
3508 ++eap->cmd;
3509 }
3510
3511 // One address given: set start and end lines.
3512 if (eap->addr_count == 1)
3513 {
3514 eap->line1 = eap->line2;
3515 // ... but only implicit: really no address given
3516 if (lnum == MAXLNUM)
3517 eap->addr_count = 0;
3518 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003519 ret = OK;
3520
3521theend:
3522 if (need_check_cursor)
3523 check_cursor();
3524 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003525}
3526
3527/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003528 * Append "cmd" to the error message in IObuff.
3529 * Takes care of limiting the length and handling 0xa0, which would be
3530 * invisible otherwise.
3531 */
3532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003533append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003534{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003535 size_t len = STRLEN(IObuff);
3536 char_u *s = cmd;
3537 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003538
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003539 if (len > IOSIZE - 100)
3540 {
3541 // Not enough space, truncate and put in "...".
3542 d = IObuff + IOSIZE - 100;
3543 d -= mb_head_off(IObuff, d);
3544 STRCPY(d, "...");
3545 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003546 STRCAT(IObuff, ": ");
3547 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003548 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003549 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003550 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003551 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003552 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003553 STRCPY(d, "<a0>");
3554 d += 4;
3555 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003556 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3557 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003558 else
3559 MB_COPY_CHAR(s, d);
3560 }
3561 *d = NUL;
3562}
3563
Dominique Pelle748b3082022-01-08 12:41:16 +00003564#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003565/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003566 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3567 * the name. Otherwise just return "start".
3568 */
3569 char_u *
3570skip_option_env_lead(char_u *start)
3571{
3572 char_u *name = start;
3573
3574 if (*start == '&')
3575 {
3576 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3577 name += 3;
3578 else
3579 name += 1;
3580 }
3581 else if (*start == '$')
3582 name += 1;
3583 return name;
3584}
Dominique Pelle748b3082022-01-08 12:41:16 +00003585#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003586
3587/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003588 * Return TRUE and set "*idx" if "p" points to a one letter command.
3589 * If not in Vim9 script:
3590 * - The 'k' command can directly be followed by any character.
3591 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3592 * but :sre[wind] is another command, as are :scr[iptnames],
3593 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3594 */
3595 static int
3596one_letter_cmd(char_u *p, cmdidx_T *idx)
3597{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003598 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003599 return FALSE;
3600 if (*p == 'k')
3601 {
3602 *idx = CMD_k;
3603 return TRUE;
3604 }
3605 if (p[0] == 's'
3606 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3607 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3608 || p[1] == 'g'
3609 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3610 || p[1] == 'I'
3611 || (p[1] == 'r' && p[2] != 'e')))
3612 {
3613 *idx = CMD_substitute;
3614 return TRUE;
3615 }
3616 return FALSE;
3617}
3618
Dominique Pellee764d1b2023-03-12 21:20:59 +00003619#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003620/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003621 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3622 * call.
3623 */
3624 int
3625number_method(char_u *cmd)
3626{
3627 char_u *p = skipdigits(cmd);
3628
3629 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3630}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003631#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003632
3633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003636 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003637 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 *
3639 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3640 * assignment without "let". Sets eap->cmdidx to the command while returning
3641 * "eap->cmd".
3642 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 * Returns NULL for an ambiguous user command.
3644 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 char_u *
3646find_ex_command(
3647 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003648 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003649 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003650 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651{
3652 int len;
3653 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003654 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003655#ifndef FEAT_EVAL
3656 int vim9 = FALSE;
3657#else
3658 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 /*
3661 * Recognize a Vim9 script function/method call and assignment:
3662 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3663 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003664 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003665 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003667 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003669 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003670 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3671 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003673 int oplen;
3674 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003675 char_u *swp;
3676
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003677 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003678 || (eap->cmd[0] == '$'
3679 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003680 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003681 && (valid_yank_reg(eap->cmd[1], FALSE)
3682 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003683 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003684 if (*eap->cmd == '&')
3685 {
3686 p = eap->cmd + 1;
3687 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3688 p += 2;
3689 p = to_name_end(p, FALSE);
3690 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003691 else if (*eap->cmd == '$')
3692 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003693 else
3694 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003695 if (ends_excmd(*skipwhite(p)))
3696 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003697 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3698 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003699 eap->cmdidx = CMD_eval;
3700 return eap->cmd;
3701 }
3702 // "&option" can be followed by "->" or "=", check below
3703 }
3704
3705 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003707 if (
3708 // "(..." is an expression.
3709 // "funcname(" is always a function call.
3710 *p == '('
3711 || (p == eap->cmd
3712 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003713 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003714 *eap->cmd == '{'
3715 // "'string'->func()" is an expression.
3716 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003717 // '"string"->func()' is an expression.
3718 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003719 // '$"string"->func()' is an expression.
3720 // "$'string'->func()" is an expression.
3721 || (eap->cmd[0] == '$'
3722 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3723 // '0z1234->func()' is an expression.
3724 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003725 // "g:varname" is an expression.
3726 || eap->cmd[1] == ':'
3727 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003728 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003729 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003730 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003731 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3732 {
3733 // "{" by itself is the start of a block.
3734 eap->cmdidx = CMD_block;
3735 return eap->cmd + 1;
3736 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003737 eap->cmdidx = CMD_eval;
3738 return eap->cmd;
3739 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003740
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003741 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003742 // "varname[]" is an expression.
3743 *p == '['
3744 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003745 || (*p == '.'
3746 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3747 // g:[key] is an expression
3748 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003749 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003750 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003751
3752 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003753 // Skip over the whole thing, it can be:
3754 // name.member = val
3755 // name[a : b] = val
3756 // name[idx] = val
3757 // name[idx].member = val
3758 // etc.
3759 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003760 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003761 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003762 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003763 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003764 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003765 || (after[0] == '.' && after[1] == '.'
3766 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003767 eap->cmdidx = CMD_var;
3768 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003769 --emsg_silent;
3770 return eap->cmd;
3771 }
3772
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003773 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3774 // an assignment.
3775 // If there is no line break inside the "[...]" then "p" is
3776 // advanced to after the "]" by to_name_const_end(): check if a "="
3777 // follows.
3778 // If "[...]" has a line break "p" still points at the "[" and it
3779 // can't be an assignment.
3780 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003781 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003782 char_u *eq;
3783
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003784 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003785 if (p == eap->cmd && *p == '[')
3786 {
3787 int count = 0;
3788 int semicolon = FALSE;
3789
3790 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3791 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003792 eq = p;
3793 if (eq != NULL)
3794 {
3795 eq = skipwhite(eq);
3796 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3797 ++eq;
3798 }
3799 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003800 {
3801 eap->cmdidx = CMD_eval;
3802 return eap->cmd;
3803 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003804 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003805 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003806 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003807 return eap->cmd;
3808 }
3809 }
3810
3811 // Recognize an assignment if we recognize the variable name:
3812 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003813 // "@r = expr"
3814 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003815 // "var = expr" where "var" is a variable name or we are skipping
3816 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003817 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003818 oplen = assignment_len(skipwhite(p), &heredoc);
3819 if (oplen > 0)
3820 {
3821 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3822 || *eap->cmd == '&'
3823 || *eap->cmd == '$'
3824 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003825 || (eap->skip && IS_WHITE_OR_NUL(
3826 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003827 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003828 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003829 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003830 return eap->cmd;
3831 }
3832 }
3833
Bram Moolenaar9510d222022-09-11 15:14:05 +01003834 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003835 // "w:varname: number = 123".
3836 if (eap->cmd[1] == ':' && *p == ':')
3837 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003838 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003839 return eap->cmd;
3840 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003841 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003842
Bram Moolenaar31d99482022-05-26 22:24:43 +01003843 // 1234->func() is a method call
3844 if (number_method(eap->cmd))
3845 {
3846 eap->cmdidx = CMD_eval;
3847 return eap->cmd;
3848 }
3849
Bram Moolenaar7b829262021-10-13 15:04:34 +01003850 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3851 // an expression. A global/substitute/list command needs to use a
3852 // longer name.
3853 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3854 {
3855 eap->cmdidx = CMD_eval;
3856 return eap->cmd;
3857 }
3858
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003859 // If it is an ID it might be a variable with an operator on the next
3860 // line, if the variable exists it can't be an Ex command.
3861 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003862 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003863 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3864 {
3865 eap->cmdidx = CMD_eval;
3866 return eap->cmd;
3867 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003868
3869 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003870 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3871 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003872 {
3873 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3874 return eap->cmd + 2;
3875 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 }
3877#endif
3878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 /*
3880 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 */
3882 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003883 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 ++p;
3886 }
3887 else
3888 {
3889 while (ASCII_ISALPHA(*p))
3890 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003891 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003892 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003894 while (ASCII_ISALNUM(*p))
3895 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 }
3897 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3898 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003899 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 ++p;
3901 while (ASCII_ISALPHA(*p))
3902 ++p;
3903 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003904
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003905 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003906 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 ++p;
3908 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003909 // The "d" command can directly be followed by 'l' or 'p' flag, when
3910 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003911 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003912 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003913 // Check for ":dl", ":dell", etc. to ":deletel": that's
3914 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003915 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003916 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003917 break;
3918 if (i == len - 1)
3919 {
3920 --len;
3921 if (p[-1] == 'l')
3922 eap->flags |= EXFLAG_LIST;
3923 else
3924 eap->flags |= EXFLAG_PRINT;
3925 }
3926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003928 if (ASCII_ISLOWER(eap->cmd[0]))
3929 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003930 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003931 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003932
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003933 if (command_count != (int)CMD_SIZE)
3934 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003935 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003936 getout(1);
3937 }
3938
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003939 // Use a precomputed index for fast look-up in cmdnames[]
3940 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003941 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3942 if (ASCII_ISLOWER(c2))
3943 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3944 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003945 else if (ASCII_ISUPPER(eap->cmd[0]))
3946 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003948 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
3950 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3951 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3952 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3953 (size_t)len) == 0)
3954 {
3955#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003956 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 *full = TRUE;
3958#endif
3959 break;
3960 }
3961
Bram Moolenaar204852a2022-03-05 12:56:44 +00003962 // :Print and :mode are not supported in Vim9 script.
3963 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003964 if (vim9 && eap->cmdidx != CMD_SIZE)
3965 {
3966 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3967 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003968 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003969 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003970 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003971 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003972 eap->cmdidx = CMD_SIZE;
3973 }
3974 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003975
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003976 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003977 // 'cpoptions'.
3978 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3979 p = eap->cmd;
3980
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003981 // Look for a user defined command as a last resort. Let ":Print" be
3982 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003983 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3984 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003986 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 while (ASCII_ISALNUM(*p))
3988 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003989 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003991 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 eap->cmdidx = CMD_SIZE;
3993 }
3994
Bram Moolenaar204852a2022-03-05 12:56:44 +00003995 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3996 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003997 eap->cmdidx = CMD_finally;
3998
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003999#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004000 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004001 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02004002 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004003 && (eap->cmdidx < 0 ||
4004 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004005 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004006 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4007
4008 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004009 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004010 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004011 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004012#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004013
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 return p;
4015}
4016
4017#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004018typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004019{
4020 char *name;
4021 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004022 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004023} cmdmod_info_T;
4024
4025static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004026 {"aboveleft", 3, FALSE},
4027 {"belowright", 3, FALSE},
4028 {"botright", 2, FALSE},
4029 {"browse", 3, FALSE},
4030 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004031 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004032 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004033 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004034 {"keepalt", 5, FALSE},
4035 {"keepjumps", 5, FALSE},
4036 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004037 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004038 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004039 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004040 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004041 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004042 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004043 {"rightbelow", 6, FALSE},
4044 {"sandbox", 3, FALSE},
4045 {"silent", 3, FALSE},
4046 {"tab", 3, TRUE},
4047 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004048 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004049 {"verbose", 4, TRUE},
4050 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004051 {"vim9cmd", 4, FALSE}
4052}; // cmdmod_info_tab
4053
Bram Moolenaared53fb92007-11-24 20:50:24 +00004054/*
4055 * Return length of a command modifier (including optional count).
4056 * Return zero when it's not a modifier.
4057 */
4058 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004059modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004060{
Christian Brabandt70a11a62024-07-26 19:13:55 +02004061 int i, j;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004062 char_u *p = cmd;
4063
4064 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004065 p = skipwhite(skipdigits(cmd + 1));
Christian Brabandt70a11a62024-07-26 19:13:55 +02004066 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004067 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004068 for (j = 0; p[j] != NUL; ++j)
4069 if (p[j] != cmdmod_info_tab[i].name[j])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004070 break;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004071 if (!ASCII_ISALPHA(p[j]) && j >= cmdmod_info_tab[i].minlen
4072 && (p == cmd || cmdmod_info_tab[i].has_count))
4073 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004074 }
4075 return 0;
4076}
4077
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078/*
4079 * Return > 0 if an Ex command "name" exists.
4080 * Return 2 if there is an exact match.
4081 * Return 3 if there is an ambiguous match.
4082 */
4083 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004084cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085{
4086 exarg_T ea;
4087 int full = FALSE;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004088 int i;
4089 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004090 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
Christian Brabandt70a11a62024-07-26 19:13:55 +02004092 // Check command modifiers.
4093 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004095 for (j = 0; name[j] != NUL; ++j)
4096 if (name[j] != cmdmod_info_tab[i].name[j])
4097 break;
4098 if (name[j] == NUL && j >= cmdmod_info_tab[i].minlen)
4099 return (cmdmod_info_tab[i].name[j] == NUL ? 2 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004102 // Check built-in commands and user defined commands.
4103 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004104 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004106 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004108 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004110 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4111 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004112 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004113 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4115}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004116
4117/*
4118 * "fullcommand" function
4119 */
4120 void
4121f_fullcommand(typval_T *argvars, typval_T *rettv)
4122{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004123 exarg_T ea;
4124 char_u *name;
4125 char_u *p;
4126 int vim9script = in_vim9script();
4127 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004128
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004129 rettv->v_type = VAR_STRING;
4130 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004131
Bram Moolenaaraa534142022-09-15 21:46:02 +01004132 if (in_vim9script()
4133 && (check_for_string_arg(argvars, 0) == FAIL
4134 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004135 return;
4136
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004137 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004138 if (name == NULL)
4139 return;
4140
Bram Moolenaaraa534142022-09-15 21:46:02 +01004141 if (argvars[1].v_type != VAR_UNKNOWN)
4142 {
4143 vim9script = tv_get_bool(&argvars[1]);
4144 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4145 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4146 }
4147
zeertzjqc024ed92022-01-04 16:22:52 +00004148 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004149 name++;
4150 name = skip_range(name, TRUE, NULL);
4151
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004152 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4153 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004154 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004155 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004156 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004157 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004158 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004159 goto theend;
4160
4161 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004162 {
4163 int res;
4164
4165 ++emsg_silent;
4166 res = not_in_vim9(&ea);
4167 --emsg_silent;
4168
4169 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004170 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004171 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004172
4173 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004174 ? get_user_command_name(ea.useridx, ea.cmdidx)
4175 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004176theend:
4177 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004178}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179#endif
4180
Bram Moolenaard0190392019-08-23 21:17:35 +02004181 cmdidx_T
4182excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183{
Bram Moolenaard0190392019-08-23 21:17:35 +02004184 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004186 if (!one_letter_cmd(cmd, &idx))
4187 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4188 idx = (cmdidx_T)((int)idx + 1))
4189 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4190 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
Bram Moolenaard0190392019-08-23 21:17:35 +02004192 return idx;
4193}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
Bram Moolenaard0190392019-08-23 21:17:35 +02004195 long
4196excmd_get_argt(cmdidx_T idx)
4197{
4198 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199}
4200
4201/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004202 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 *
4204 * Backslashed delimiters after / or ? will be skipped, and commands will
4205 * not be expanded between /'s and ?'s or after "'".
4206 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004207 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 * Returns the "cmd" pointer advanced to beyond the range.
4209 */
4210 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004211skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004212 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004213 int skip_star, // skip "*" used for Visual range
4214 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004216 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004217 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004219 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004221 if (*cmd == '\\')
4222 {
4223 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4224 ++cmd;
4225 else
4226 break;
4227 }
4228 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004230 char_u *p = cmd;
4231
4232 // a quote is only valid at the start or after a separator
4233 while (p > cmd_start)
4234 {
4235 --p;
4236 if (!VIM_ISWHITE(*p))
4237 break;
4238 }
4239 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4240 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 if (*++cmd == NUL && ctx != NULL)
4242 *ctx = EXPAND_NOTHING;
4243 }
4244 else if (*cmd == '/' || *cmd == '?')
4245 {
4246 delim = *cmd++;
4247 while (*cmd != NUL && *cmd != delim)
4248 if (*cmd++ == '\\' && *cmd != NUL)
4249 ++cmd;
4250 if (*cmd == NUL && ctx != NULL)
4251 *ctx = EXPAND_NOTHING;
4252 }
4253 if (*cmd != NUL)
4254 ++cmd;
4255 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004256
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004257 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004258 while (*cmd == ':')
4259 cmd = skipwhite(cmd + 1);
4260
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004261 // Skip "*" used for Visual range.
4262 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4263 cmd = skipwhite(cmd + 1);
4264
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 return cmd;
4266}
4267
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004268 static void
4269addr_error(cmd_addr_T addr_type)
4270{
4271 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004272 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004273 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004274 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004275}
4276
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004278 * Return the default address for an address type.
4279 */
4280 static linenr_T
4281default_address(exarg_T *eap)
4282{
4283 linenr_T lnum = 0;
4284
4285 switch (eap->addr_type)
4286 {
4287 case ADDR_LINES:
4288 case ADDR_OTHER:
4289 // Default is the cursor line number. Avoid using an invalid
4290 // line number though.
4291 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4292 lnum = curbuf->b_ml.ml_line_count;
4293 else
4294 lnum = curwin->w_cursor.lnum;
4295 break;
4296 case ADDR_WINDOWS:
4297 lnum = CURRENT_WIN_NR;
4298 break;
4299 case ADDR_ARGUMENTS:
4300 lnum = curwin->w_arg_idx + 1;
4301 if (lnum > ARGCOUNT)
4302 lnum = ARGCOUNT;
4303 break;
4304 case ADDR_LOADED_BUFFERS:
4305 case ADDR_BUFFERS:
4306 lnum = curbuf->b_fnum;
4307 break;
4308 case ADDR_TABS:
4309 lnum = CURRENT_TAB_NR;
4310 break;
4311 case ADDR_TABS_RELATIVE:
4312 case ADDR_UNSIGNED:
4313 lnum = 1;
4314 break;
4315 case ADDR_QUICKFIX:
4316#ifdef FEAT_QUICKFIX
4317 lnum = qf_get_cur_idx(eap);
4318#endif
4319 break;
4320 case ADDR_QUICKFIX_VALID:
4321#ifdef FEAT_QUICKFIX
4322 lnum = qf_get_cur_valid_idx(eap);
4323#endif
4324 break;
4325 case ADDR_NONE:
4326 // Will give an error later if a range is found.
4327 break;
4328 }
4329 return lnum;
4330}
4331
4332/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004333 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 *
4335 * Set ptr to the next character after the part that was interpreted.
4336 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004337 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 *
4339 * Return MAXLNUM when no Ex address was found.
4340 */
4341 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004342get_address(
4343 exarg_T *eap UNUSED,
4344 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004345 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004346 int skip, // only skip the address, don't use it
4347 int silent, // no errors or side effects
4348 int to_other_file, // flag: may jump to other file
4349 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350{
4351 int c;
4352 int i;
4353 long n;
4354 char_u *cmd;
4355 pos_T pos;
4356 pos_T *fp;
4357 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004358 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
4360 cmd = skipwhite(*ptr);
4361 lnum = MAXLNUM;
4362 do
4363 {
4364 switch (*cmd)
4365 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004366 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004367 ++cmd;
4368 switch (addr_type)
4369 {
4370 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004371 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 lnum = curwin->w_cursor.lnum;
4373 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004374 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004375 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004376 break;
4377 case ADDR_ARGUMENTS:
4378 lnum = curwin->w_arg_idx + 1;
4379 break;
4380 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004381 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004382 lnum = curbuf->b_fnum;
4383 break;
4384 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004385 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004387 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004388 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004389 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004390 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004391 cmd = NULL;
4392 goto error;
4393 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004394 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004395#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004396 lnum = qf_get_cur_idx(eap);
4397#endif
4398 break;
4399 case ADDR_QUICKFIX_VALID:
4400#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004401 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004402#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004403 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004404 }
4405 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004407 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004408 ++cmd;
4409 switch (addr_type)
4410 {
4411 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004412 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 lnum = curbuf->b_ml.ml_line_count;
4414 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004416 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 break;
4418 case ADDR_ARGUMENTS:
4419 lnum = ARGCOUNT;
4420 break;
4421 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004422 buf = lastbuf;
4423 while (buf->b_ml.ml_mfp == NULL)
4424 {
4425 if (buf->b_prev == NULL)
4426 break;
4427 buf = buf->b_prev;
4428 }
4429 lnum = buf->b_fnum;
4430 break;
4431 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004432 lnum = lastbuf->b_fnum;
4433 break;
4434 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004435 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004436 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004437 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004438 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004439 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004440 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004441 cmd = NULL;
4442 goto error;
4443 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004444 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004445#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004446 lnum = qf_get_size(eap);
4447 if (lnum == 0)
4448 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004449#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004450 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004451 case ADDR_QUICKFIX_VALID:
4452#ifdef FEAT_QUICKFIX
4453 lnum = qf_get_valid_size(eap);
4454 if (lnum == 0)
4455 lnum = 1;
4456#endif
4457 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004458 }
4459 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004461 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004462 if (*++cmd == NUL)
4463 {
4464 cmd = NULL;
4465 goto error;
4466 }
4467 if (addr_type != ADDR_LINES)
4468 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004469 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004470 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004471 goto error;
4472 }
4473 if (skip)
4474 ++cmd;
4475 else
4476 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004477 // Only accept a mark in another file when it is
4478 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004479 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4480 ++cmd;
4481 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004482 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004483 lnum = curwin->w_cursor.lnum;
4484 else
4485 {
4486 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
4488 cmd = NULL;
4489 goto error;
4490 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 lnum = fp->lnum;
4492 }
4493 }
4494 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
4496 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004497 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004498 c = *cmd++;
4499 if (addr_type != ADDR_LINES)
4500 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004501 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004502 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 goto error;
4504 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004505 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004506 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004507 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004508 if (*cmd == c)
4509 ++cmd;
4510 }
4511 else
4512 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004513 int flags;
4514
4515 pos = curwin->w_cursor; // save curwin->w_cursor
4516
4517 // When '/' or '?' follows another address, start from
4518 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004519 if (lnum > 0 && lnum != MAXLNUM)
4520 curwin->w_cursor.lnum =
4521 lnum > curbuf->b_ml.ml_line_count
4522 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004523
4524 // Start a forward search at the end of the line (unless
4525 // before the first line).
4526 // Start a backward search at the start of the line.
4527 // This makes sure we never match in the current
4528 // line, and can match anywhere in the
4529 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004530 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004531 curwin->w_cursor.col = MAXCOL;
4532 else
4533 curwin->w_cursor.col = 0;
4534 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004535 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004536 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004537 {
4538 curwin->w_cursor = pos;
4539 cmd = NULL;
4540 goto error;
4541 }
4542 lnum = curwin->w_cursor.lnum;
4543 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004544 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 cmd += searchcmdlen;
4546 }
4547 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004549 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004550 ++cmd;
4551 if (addr_type != ADDR_LINES)
4552 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004553 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004554 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004555 goto error;
4556 }
4557 if (*cmd == '&')
4558 i = RE_SUBST;
4559 else if (*cmd == '?' || *cmd == '/')
4560 i = RE_SEARCH;
4561 else
4562 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004563 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004564 cmd = NULL;
4565 goto error;
4566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004568 if (!skip)
4569 {
4570 /*
4571 * When search follows another address, start from
4572 * there.
4573 */
4574 if (lnum != MAXLNUM)
4575 pos.lnum = lnum;
4576 else
4577 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 /*
4580 * Start the search just like for the above
4581 * do_search().
4582 */
4583 if (*cmd != '?')
4584 pos.col = MAXCOL;
4585 else
4586 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004587 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004588 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004589 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004590 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004591 lnum = pos.lnum;
4592 else
4593 {
4594 cmd = NULL;
4595 goto error;
4596 }
4597 }
4598 ++cmd;
4599 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
4601 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004602 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004603 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605
4606 for (;;)
4607 {
4608 cmd = skipwhite(cmd);
4609 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4610 break;
4611
4612 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004613 {
4614 switch (addr_type)
4615 {
4616 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004617 case ADDR_OTHER:
4618 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004619 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004620 break;
4621 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004622 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004623 break;
4624 case ADDR_ARGUMENTS:
4625 lnum = curwin->w_arg_idx + 1;
4626 break;
4627 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004628 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004629 lnum = curbuf->b_fnum;
4630 break;
4631 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004632 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004633 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004634 case ADDR_TABS_RELATIVE:
4635 lnum = 1;
4636 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004637 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004638#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004639 lnum = qf_get_cur_idx(eap);
4640#endif
4641 break;
4642 case ADDR_QUICKFIX_VALID:
4643#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004644 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004645#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004646 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004647 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004648 case ADDR_UNSIGNED:
4649 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004650 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004651 }
4652 }
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004655 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 else
4657 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004658 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 n = 1;
4660 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004661 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004662 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004664 if (n == MAXLNUM)
4665 {
4666 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004667 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004668 goto error;
4669 }
4670 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004671
4672 if (addr_type == ADDR_TABS_RELATIVE)
4673 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004674 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004675 cmd = NULL;
4676 goto error;
4677 }
4678 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004679 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004680 lnum = compute_buffer_local_count(
4681 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004683 {
4684#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004685 // Relative line addressing: need to adjust for lines in a
4686 // closed fold after the first address.
4687 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4688 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004689 (void)hasFolding(lnum, NULL, &lnum);
4690#endif
4691 if (i == '-')
4692 lnum -= n;
4693 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004694 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004695 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004696 {
4697 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004698 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004699 goto error;
4700 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004701 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004702 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705 } while (*cmd == '/' || *cmd == '?');
4706
4707error:
4708 *ptr = cmd;
4709 return lnum;
4710}
4711
4712/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004713 * Set eap->line1 and eap->line2 to the whole range.
4714 * Used for commands with the EX_DFLALL flag and no range given.
4715 */
4716 static void
4717address_default_all(exarg_T *eap)
4718{
4719 eap->line1 = 1;
4720 switch (eap->addr_type)
4721 {
4722 case ADDR_LINES:
4723 case ADDR_OTHER:
4724 eap->line2 = curbuf->b_ml.ml_line_count;
4725 break;
4726 case ADDR_LOADED_BUFFERS:
4727 {
4728 buf_T *buf = firstbuf;
4729
4730 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4731 buf = buf->b_next;
4732 eap->line1 = buf->b_fnum;
4733 buf = lastbuf;
4734 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4735 buf = buf->b_prev;
4736 eap->line2 = buf->b_fnum;
4737 }
4738 break;
4739 case ADDR_BUFFERS:
4740 eap->line1 = firstbuf->b_fnum;
4741 eap->line2 = lastbuf->b_fnum;
4742 break;
4743 case ADDR_WINDOWS:
4744 eap->line2 = LAST_WIN_NR;
4745 break;
4746 case ADDR_TABS:
4747 eap->line2 = LAST_TAB_NR;
4748 break;
4749 case ADDR_TABS_RELATIVE:
4750 eap->line2 = 1;
4751 break;
4752 case ADDR_ARGUMENTS:
4753 if (ARGCOUNT == 0)
4754 eap->line1 = eap->line2 = 0;
4755 else
4756 eap->line2 = ARGCOUNT;
4757 break;
4758 case ADDR_QUICKFIX_VALID:
4759#ifdef FEAT_QUICKFIX
4760 eap->line2 = qf_get_valid_size(eap);
4761 if (eap->line2 == 0)
4762 eap->line2 = 1;
4763#endif
4764 break;
4765 case ADDR_NONE:
4766 case ADDR_UNSIGNED:
4767 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004768 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004769 break;
4770 }
4771}
4772
4773
4774/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004775 * Get flags from an Ex command argument.
4776 */
4777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004778get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004779{
4780 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4781 {
4782 if (*eap->arg == 'l')
4783 eap->flags |= EXFLAG_LIST;
4784 else if (*eap->arg == 'p')
4785 eap->flags |= EXFLAG_PRINT;
4786 else
4787 eap->flags |= EXFLAG_NR;
4788 eap->arg = skipwhite(eap->arg + 1);
4789 }
4790}
4791
4792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 * Function called for command which is Not Implemented. NI!
4794 */
4795 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004796ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
4798 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004799 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004800 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801}
4802
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004803#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804/*
4805 * Function called for script command which is Not Implemented. NI!
4806 * Skips over ":perl <<EOF" constructs.
4807 */
4808 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004809ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
4811 if (!eap->skip)
4812 ex_ni(eap);
4813 else
4814 vim_free(script_get(eap, eap->arg));
4815}
4816#endif
4817
4818/*
4819 * Check range in Ex command for validity.
4820 * Return NULL when valid, error message when invalid.
4821 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004822 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004823invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004825 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004826
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 if ( eap->line1 < 0
4828 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004829 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004830 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004831
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004832 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004833 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004834 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004835 {
4836 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004837 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004838#ifdef FEAT_DIFF
4839 + (eap->cmdidx == CMD_diffget)
4840#endif
4841 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004842 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004843 break;
4844 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004845 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004846 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004847 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004848 break;
4849 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004850 // Only a boundary check, not whether the buffers actually
4851 // exist.
4852 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004853 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004854 break;
4855 case ADDR_LOADED_BUFFERS:
4856 buf = firstbuf;
4857 while (buf->b_ml.ml_mfp == NULL)
4858 {
4859 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004860 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004861 buf = buf->b_next;
4862 }
4863 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004864 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004865 buf = lastbuf;
4866 while (buf->b_ml.ml_mfp == NULL)
4867 {
4868 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004869 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004870 buf = buf->b_prev;
4871 }
4872 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004873 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004874 break;
4875 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004876 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004877 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004878 break;
4879 case ADDR_TABS:
4880 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004881 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004882 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004883 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004884 case ADDR_OTHER:
4885 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004886 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004887 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004888#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004889 // No error for value that is too big, will use the last entry.
4890 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004891 {
4892 if (eap->addr_count == 0)
4893 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004894 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004895 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004896#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004897 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004898 case ADDR_QUICKFIX_VALID:
4899#ifdef FEAT_QUICKFIX
4900 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4901 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004902 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004903#endif
4904 break;
4905 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004906 case ADDR_NONE:
4907 // Will give an error elsewhere.
4908 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004909 }
4910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 return NULL;
4912}
4913
4914/*
4915 * Correct the range for zero line number, if required.
4916 */
4917 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004918correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004920 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
4922 if (eap->line1 == 0)
4923 eap->line1 = 1;
4924 if (eap->line2 == 0)
4925 eap->line2 = 1;
4926 }
4927}
4928
Bram Moolenaar748bf032005-02-02 23:04:36 +00004929#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004930/*
4931 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4932 * pattern. Otherwise return eap->arg.
4933 */
4934 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004935skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004936{
4937 char_u *p = eap->arg;
4938
Bram Moolenaara37420f2006-02-04 22:37:47 +00004939 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4940 || eap->cmdidx == CMD_vimgrepadd
4941 || eap->cmdidx == CMD_lvimgrepadd
4942 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004943 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004944 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004945 if (p == NULL)
4946 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004947 }
4948 return p;
4949}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004950
4951/*
4952 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4953 * in the command line, so that things like % get expanded.
4954 */
4955 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004956replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004957{
4958 char_u *new_cmdline;
4959 char_u *program;
4960 char_u *pos;
4961 char_u *ptr;
4962 int len;
4963 int i;
4964
4965 /*
4966 * Don't do it when ":vimgrep" is used for ":grep".
4967 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004968 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4969 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4970 || eap->cmdidx == CMD_grepadd
4971 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004972 && !grep_internal(eap->cmdidx))
4973 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004974 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4975 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004976 {
4977 if (*curbuf->b_p_gp == NUL)
4978 program = p_gp;
4979 else
4980 program = curbuf->b_p_gp;
4981 }
4982 else
4983 {
4984 if (*curbuf->b_p_mp == NUL)
4985 program = p_mp;
4986 else
4987 program = curbuf->b_p_mp;
4988 }
4989
4990 p = skipwhite(p);
4991
4992 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4993 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004994 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004995 i = 1;
4996 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4997 ++i;
4998 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004999 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005000 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005001 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005002 ptr = new_cmdline;
5003 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5004 {
5005 i = (int)(pos - program);
5006 STRNCPY(ptr, program, i);
5007 STRCPY(ptr += i, p);
5008 ptr += len;
5009 program = pos + 2;
5010 }
5011 STRCPY(ptr, program);
5012 }
5013 else
5014 {
John Marriotted908f72024-04-18 22:46:56 +02005015 size_t program_len = STRLEN(program);
5016
5017 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005018 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005019 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005020 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005021 STRCPY(new_cmdline + program_len, " ");
5022 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005023 }
5024 msg_make(p);
5025
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005026 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005027 vim_free(*cmdlinep);
5028 *cmdlinep = new_cmdline;
5029 p = new_cmdline;
5030 }
5031 return p;
5032}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005033#endif
5034
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035/*
5036 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005037 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 * Return FAIL for failure, OK otherwise.
5039 */
5040 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005041expand_filename(
5042 exarg_T *eap,
5043 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005044 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005046 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005048 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 char_u *p;
5050 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005051 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
Bram Moolenaar748bf032005-02-02 23:04:36 +00005053#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005054 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005055 p = skip_grep_pat(eap);
5056#else
5057 p = eap->arg;
5058#endif
5059
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 /*
5061 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5062 * the file name contains a wildcard it should not cause expanding.
5063 * (it will be expanded anyway if there is a wildcard before replacing).
5064 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005065 has_wildcards = mch_has_wildcard(p);
5066 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005068#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005069 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005070 if (p[0] == '`' && p[1] == '=')
5071 {
5072 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005073 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005074 if (*p == '`')
5075 ++p;
5076 continue;
5077 }
5078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 /*
5080 * Quick check if this cannot be the start of a special string.
5081 * Also removes backslash before '%', '#' and '<'.
5082 */
5083 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5084 {
5085 ++p;
5086 continue;
5087 }
5088
5089 /*
5090 * Try to find a match at this position.
5091 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005092 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005093 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005094 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005096 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
5098 p += srclen;
5099 continue;
5100 }
5101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005102 // Wildcards won't be expanded below, the replacement is taken
5103 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005104 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5105 {
5106 char_u *l = repl;
5107
5108 repl = expand_env_save(repl);
5109 vim_free(l);
5110 }
5111
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005112 // Need to escape white space et al. with a backslash.
5113 // Don't do this for:
5114 // - replacement that already has been escaped: "##"
5115 // - shell commands (may have to use quotes instead).
5116 // - non-unix systems when there is a single argument (spaces don't
5117 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005119 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 && eap->cmdidx != CMD_grep
5122 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005123 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005124 && eap->cmdidx != CMD_lgrep
5125 && eap->cmdidx != CMD_lgrepadd
5126 && eap->cmdidx != CMD_lmake
5127 && eap->cmdidx != CMD_make
5128 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005130 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131#endif
5132 )
5133 {
5134 char_u *l;
5135#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005136 // Don't escape a backslash here, because rem_backslash() doesn't
5137 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 static char_u *nobslash = (char_u *)" \t\"|";
5139# define ESCAPE_CHARS nobslash
5140#else
5141# define ESCAPE_CHARS escape_chars
5142#endif
5143
5144 for (l = repl; *l; ++l)
5145 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5146 {
5147 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5148 if (l != NULL)
5149 {
5150 vim_free(repl);
5151 repl = l;
5152 }
5153 break;
5154 }
5155 }
5156
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005157 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005158 if ((eap->usefilter || eap->cmdidx == CMD_bang
5159 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005160 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
5162 char_u *l;
5163
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005164 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 if (l != NULL)
5166 {
5167 vim_free(repl);
5168 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170 }
5171
Mike Williams51024bb2024-05-30 07:46:30 +02005172 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 vim_free(repl);
5174 if (p == NULL)
5175 return FAIL;
5176 }
5177
5178 /*
5179 * One file argument: Expand wildcards.
5180 * Don't do this with ":r !command" or ":w !command".
5181 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005182 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
5184 /*
5185 * May do this twice:
5186 * 1. Replace environment variables.
5187 * 2. Replace any other wildcards, remove backslashes.
5188 */
5189 for (n = 1; n <= 2; ++n)
5190 {
5191 if (n == 2)
5192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 /*
5194 * Halve the number of backslashes (this is Vi compatible).
5195 * For Unix and OS/2, when wildcards are expanded, this is
5196 * done by ExpandOne() below.
5197 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005198#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 if (!has_wildcards)
5200#endif
5201 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
5203
5204 if (has_wildcards)
5205 {
5206 if (n == 1)
5207 {
5208 /*
5209 * First loop: May expand environment variables. This
5210 * can be done much faster with expand_env() than with
5211 * something else (e.g., calling a shell).
5212 * After expanding environment variables, check again
5213 * if there are still wildcards present.
5214 */
5215 if (vim_strchr(eap->arg, '$') != NULL
5216 || vim_strchr(eap->arg, '~') != NULL)
5217 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005218 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005219 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 has_wildcards = mch_has_wildcard(NameBuff);
5221 p = NameBuff;
5222 }
5223 else
5224 p = NULL;
5225 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005226 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
5228 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005229 int options = WILD_LIST_NOTFOUND
5230 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231
5232 ExpandInit(&xpc);
5233 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005234 if (p_wic)
5235 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005237 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 if (p == NULL)
5239 return FAIL;
5240 }
5241 if (p != NULL)
5242 {
John Marriotted908f72024-04-18 22:46:56 +02005243 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005245 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 vim_free(p);
5247 }
5248 }
5249 }
5250 }
5251 return OK;
5252}
5253
5254/*
5255 * Replace part of the command line, keeping eap->cmd, eap->arg and
5256 * eap->nextcmd correct.
5257 * "src" points to the part that is to be replaced, of length "srclen".
5258 * "repl" is the replacement string.
5259 * Returns a pointer to the character after the replaced string.
5260 * Returns NULL for failure.
5261 */
5262 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005263repl_cmdline(
5264 exarg_T *eap,
5265 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005266 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005267 char_u *repl,
5268 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269{
John Marriotted908f72024-04-18 22:46:56 +02005270 size_t repllen;
5271 size_t taillen;
5272 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005274 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
5276 /*
5277 * The new command line is build in new_cmdline[].
5278 * First allocate it.
5279 * Careful: a "+cmd" argument may have been NUL terminated.
5280 */
John Marriotted908f72024-04-18 22:46:56 +02005281 repllen = STRLEN(repl);
5282 taillen = STRLEN(src + srclen);
5283 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005284 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005285 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005286 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005287 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
5289 /*
5290 * Copy the stuff before the expanded part.
5291 * Copy the expanded stuff.
5292 * Copy what came after the expanded part.
5293 * Copy the next commands, if there are any.
5294 */
John Marriotted908f72024-04-18 22:46:56 +02005295 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5296 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005297
John Marriotted908f72024-04-18 22:46:56 +02005298 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5299 new_cmdlinelen += repllen; // remember the end of the string
5300 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5301 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005303 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
John Marriotted908f72024-04-18 22:46:56 +02005305 new_cmdlinelen += taillen + 1;
5306 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5307 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5310 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5311 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5312 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5313 vim_free(*cmdlinep);
5314 *cmdlinep = new_cmdline;
5315
5316 return src;
5317}
5318
5319/*
5320 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005321 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 */
5323 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005324separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
5326 char_u *p;
5327
Bram Moolenaar86b68352004-12-27 21:59:20 +00005328#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005329 p = skip_grep_pat(eap);
5330#else
5331 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005332#endif
5333
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005334 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 {
5336 if (*p == Ctrl_V)
5337 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005338 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005339 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005341 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005342 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005343 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 break;
5345 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005346
5347#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005348 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005349 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005350 {
5351 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005352 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005353 if (*p == NUL) // stop at NUL after CTRL-V
5354 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005355 }
5356#endif
5357
Bram Moolenaareda29c92022-10-02 12:59:00 +01005358 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005359 // :@" and :*" do not start a comment!
5360 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005361 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005362 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005363 && !(eap->argt & EX_NOTRLCOM)
5364 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5365 || p != eap->arg)
5366 && (eap->cmdidx != CMD_redir
5367 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005368 || (*p == '#'
5369 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005370 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005371 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005372 || (*p == '|'
5373 && eap->cmdidx != CMD_append
5374 && eap->cmdidx != CMD_change
5375 && eap->cmdidx != CMD_insert)
5376 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 {
5378 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005379 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 * AND 'b' is present in 'cpoptions'.
5381 */
5382 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005383 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005385 if (!keep_backslash)
5386 {
5387 STRMOVE(p - 1, p); // remove the '\'
5388 --p;
5389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391 else
5392 {
5393 eap->nextcmd = check_nextcmd(p);
5394 *p = NUL;
5395 break;
5396 }
5397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005399
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005400 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 del_trailing_spaces(eap->arg);
5402}
5403
5404/*
5405 * get + command from ex argument
5406 */
5407 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005408getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
5410 char_u *arg = *argp;
5411 char_u *command = NULL;
5412
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005413 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
5415 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005416 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 command = dollar_command;
5418 else
5419 {
5420 command = arg;
5421 arg = skip_cmd_arg(command, TRUE);
5422 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005423 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 }
5425
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005426 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 *argp = arg;
5428 }
5429 return command;
5430}
5431
5432/*
5433 * Find end of "+command" argument. Skip over "\ " and "\\".
5434 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005435 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005436skip_cmd_arg(
5437 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005438 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
5440 while (*p && !vim_isspace(*p))
5441 {
5442 if (*p == '\\' && p[1] != NUL)
5443 {
5444 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005445 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 else
5447 ++p;
5448 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005449 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 }
5451 return p;
5452}
5453
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005454 int
5455get_bad_opt(char_u *p, exarg_T *eap)
5456{
5457 if (STRICMP(p, "keep") == 0)
5458 eap->bad_char = BAD_KEEP;
5459 else if (STRICMP(p, "drop") == 0)
5460 eap->bad_char = BAD_DROP;
5461 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5462 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005463 else
5464 return FAIL;
5465 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005466}
5467
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005469 * Function given to ExpandGeneric() to obtain the list of bad= names.
5470 */
5471 static char_u *
5472get_bad_name(expand_T *xp UNUSED, int idx)
5473{
5474 // Note: Keep this in sync with getargopt.
5475 static char *(p_bad_values[]) =
5476 {
5477 "?",
5478 "keep",
5479 "drop",
5480 };
5481
John Marriotted908f72024-04-18 22:46:56 +02005482 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5483 return NULL;
5484
5485 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005486}
5487
5488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 * Get "++opt=arg" argument.
5490 * Return FAIL or OK.
5491 */
5492 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005493getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 char_u *arg = eap->arg + 2;
5496 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005497 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005500 // Note: Keep this in sync with get_argopt_name.
5501
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005502 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5504 {
5505 if (*arg == 'n')
5506 {
5507 arg += 2;
5508 eap->force_bin = FORCE_NOBIN;
5509 }
5510 else
5511 eap->force_bin = FORCE_BIN;
5512 if (!checkforcmd(&arg, "binary", 3))
5513 return FAIL;
5514 eap->arg = skipwhite(arg);
5515 return OK;
5516 }
5517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005518 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005519 if (STRNCMP(arg, "edit", 4) == 0)
5520 {
5521 eap->read_edit = TRUE;
5522 eap->arg = skipwhite(arg + 4);
5523 return OK;
5524 }
5525
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 if (STRNCMP(arg, "ff", 2) == 0)
5527 {
5528 arg += 2;
5529 pp = &eap->force_ff;
5530 }
5531 else if (STRNCMP(arg, "fileformat", 10) == 0)
5532 {
5533 arg += 10;
5534 pp = &eap->force_ff;
5535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 else if (STRNCMP(arg, "enc", 3) == 0)
5537 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005538 if (STRNCMP(arg, "encoding", 8) == 0)
5539 arg += 8;
5540 else
5541 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 pp = &eap->force_enc;
5543 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005544 else if (STRNCMP(arg, "bad", 3) == 0)
5545 {
5546 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005547 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550 if (pp == NULL || *arg != '=')
5551 return FAIL;
5552
5553 ++arg;
5554 *pp = (int)(arg - eap->cmd);
5555 arg = skip_cmd_arg(arg, FALSE);
5556 eap->arg = skipwhite(arg);
5557 *arg = NUL;
5558
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 if (pp == &eap->force_ff)
5560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5562 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005563 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005565 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005567 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5569 *p = TOLOWER_ASC(*p);
5570 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005571 else
5572 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005573 // Check ++bad= argument. Must be a single-byte character, "keep" or
5574 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005575 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005576 return FAIL;
5577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
5579 return OK;
5580}
5581
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005582/*
5583 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5584 */
5585 static char_u *
5586get_argopt_name(expand_T *xp UNUSED, int idx)
5587{
5588 // Note: Keep this in sync with getargopt.
5589 static char *(p_opt_values[]) =
5590 {
5591 "fileformat=",
5592 "encoding=",
5593 "binary",
5594 "nobinary",
5595 "bad=",
5596 "edit",
5597 };
5598
John Marriotted908f72024-04-18 22:46:56 +02005599 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5600 return NULL;
5601
5602 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005603}
5604
5605/*
5606 * Command-line expansion for ++opt=name.
5607 */
5608 int
5609expand_argopt(
5610 char_u *pat,
5611 expand_T *xp,
5612 regmatch_T *rmp,
5613 char_u ***matches,
5614 int *numMatches)
5615{
5616 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5617 {
5618 char_u *(*cb)(expand_T *, int) = NULL;
5619
5620 char_u *name_end = xp->xp_pattern - 1;
5621 if (name_end - xp->xp_line >= 2
5622 && STRNCMP(name_end - 2, "ff", 2) == 0)
5623 cb = get_fileformat_name;
5624 else if (name_end - xp->xp_line >= 10
5625 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5626 cb = get_fileformat_name;
5627 else if (name_end - xp->xp_line >= 3
5628 && STRNCMP(name_end - 3, "enc", 3) == 0)
5629 cb = get_encoding_name;
5630 else if (name_end - xp->xp_line >= 8
5631 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5632 cb = get_encoding_name;
5633 else if (name_end - xp->xp_line >= 3
5634 && STRNCMP(name_end - 3, "bad", 3) == 0)
5635 cb = get_bad_name;
5636
5637 if (cb != NULL)
5638 {
5639 return ExpandGeneric(
5640 pat,
5641 xp,
5642 rmp,
5643 matches,
5644 numMatches,
5645 cb,
5646 FALSE);
5647 }
5648 return FAIL;
5649 }
5650
5651 // Special handling of "ff" which acts as a short form of
5652 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005653 if (xp->xp_pattern_len == 2
5654 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005655 {
5656 *matches = ALLOC_MULT(char_u *, 1);
5657 if (*matches == NULL)
5658 return FAIL;
5659 *numMatches = 1;
5660 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5661 return OK;
5662 }
5663
5664 return ExpandGeneric(
5665 pat,
5666 xp,
5667 rmp,
5668 matches,
5669 numMatches,
5670 get_argopt_name,
5671 FALSE);
5672}
5673
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005675ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
5677 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005678 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 * directory for security reasons.
5680 */
5681 if (secure)
5682 {
5683 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005684 eap->errmsg =
5685 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005688 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 else
5690 do_augroup(eap->arg, eap->forceit);
5691}
5692
5693/*
5694 * ":doautocmd": Apply the automatic commands to the current buffer.
5695 */
5696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005697ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005699 char_u *arg = eap->arg;
5700 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005701 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005702
Bram Moolenaar1610d052016-06-09 22:53:01 +02005703 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005704 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005705 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005706 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709/*
5710 * :[N]bunload[!] [N] [bufname] unload buffer
5711 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5712 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5713 */
5714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005715ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005717 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005718 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 eap->errmsg = do_bufdel(
5720 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5721 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5722 : DOBUF_UNLOAD, eap->arg,
5723 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5724}
5725
5726/*
5727 * :[N]buffer [N] to buffer N
5728 * :[N]sbuffer [N] to buffer N
5729 */
5730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005731ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005733 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005734 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005736 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 else
5738 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005739 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5741 else
5742 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005743 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005744 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
5746}
5747
5748/*
5749 * :[N]bmodified [N] to next mod. buffer
5750 * :[N]sbmodified [N] to next mod. buffer
5751 */
5752 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005753ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754{
5755 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005756 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005757 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758}
5759
5760/*
5761 * :[N]bnext [N] to next buffer
5762 * :[N]sbnext [N] split and to next buffer
5763 */
5764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005765ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005767 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005768 return;
5769
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005771 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005772 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773}
5774
5775/*
5776 * :[N]bNext [N] to previous buffer
5777 * :[N]bprevious [N] to previous buffer
5778 * :[N]sbNext [N] split and to previous buffer
5779 * :[N]sbprevious [N] split and to previous buffer
5780 */
5781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005782ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005784 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005785 return;
5786
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005788 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005789 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790}
5791
5792/*
5793 * :brewind to first buffer
5794 * :bfirst to first buffer
5795 * :sbrewind split and to first buffer
5796 * :sbfirst split and to first buffer
5797 */
5798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005799ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005801 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005802 return;
5803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005805 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005806 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807}
5808
5809/*
5810 * :blast to last buffer
5811 * :sblast split and to last buffer
5812 */
5813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005814ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005816 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005817 return;
5818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005820 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005821 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
Bram Moolenaar7a092242020-04-16 22:10:49 +02005824/*
5825 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005826 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005829ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005831 int comment_char = '"';
5832
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005833 if (in_vim9script())
5834 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005835 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005836}
5837
5838/*
5839 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5840 * to "cmd_start" or has a white space character before it.
5841 */
5842 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005843ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005844{
5845 int c = *cmd;
5846
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005847 if (c == NUL || c == '|' || c == '\n')
5848 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005849 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005850 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5851 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005852 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005853 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854}
5855
5856#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5857 || defined(PROTO)
5858/*
5859 * Return the next command, after the first '|' or '\n'.
5860 * Return NULL if not found.
5861 */
5862 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005863find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
5865 while (*p != '|' && *p != '\n')
5866 {
5867 if (*p == NUL)
5868 return NULL;
5869 ++p;
5870 }
5871 return (p + 1);
5872}
5873#endif
5874
5875/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005876 * Check if *p is a separator between Ex commands, skipping over white space.
5877 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 */
5879 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005880check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005882 char_u *s = skipwhite(p);
5883
5884 if (*s == '|' || *s == '\n')
5885 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 else
5887 return NULL;
5888}
5889
5890/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005891 * If "eap->nextcmd" is not set, check for a next command at "p".
5892 */
5893 void
5894set_nextcmd(exarg_T *eap, char_u *arg)
5895{
5896 char_u *p = check_nextcmd(arg);
5897
5898 if (eap->nextcmd == NULL)
5899 eap->nextcmd = p;
5900 else if (p != NULL)
5901 // cannot use "| command" inside a {} block
5902 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5903}
5904
5905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 * - if there are more files to edit
5907 * - and this is the last window
5908 * - and forceit not used
5909 * - and not repeated twice on a row
5910 * return FAIL and give error message if 'message' TRUE
5911 * return OK otherwise
5912 */
5913 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005914check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005915 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005916 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917{
5918 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5919
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005920 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005921 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 {
5923 if (message)
5924 {
5925#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005926 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5927 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005929 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005931 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5932 NGETTEXT("%d more file to edit. Quit anyway?",
5933 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5935 return OK;
5936 return FAIL;
5937 }
5938#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005939 semsg(NGETTEXT(e_nr_more_file_to_edit,
5940 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005941 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 }
5943 return FAIL;
5944 }
5945 return OK;
5946}
5947
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948/*
5949 * Function given to ExpandGeneric() to obtain the list of command names.
5950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005952get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953{
5954 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005955 return expand_user_command_name(idx);
Christian Brabandt70a11a62024-07-26 19:13:55 +02005956 // the following are no real commands
5957 if (STRNCMP(cmdnames[idx].cmd_name, "{", 1) == 0 ||
5958 STRNCMP(cmdnames[idx].cmd_name, "}", 1) == 0)
5959 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 return cmdnames[idx].cmd_name;
5961}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005964ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965{
Bram Moolenaare6850792010-05-14 15:28:44 +02005966 if (*eap->arg == NUL)
5967 {
5968#ifdef FEAT_EVAL
5969 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5970 char_u *p = NULL;
5971
5972 if (expr != NULL)
5973 {
5974 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005975 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005976 --emsg_off;
5977 vim_free(expr);
5978 }
5979 if (p != NULL)
5980 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005981 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005982 vim_free(p);
5983 }
5984 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005985 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005986#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005987 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005988#endif
5989 }
5990 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005991 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005992
5993#ifdef FEAT_VTP
5994 else if (has_vtp_working())
5995 {
5996 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005997 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005998 redrawcmd();
5999 }
6000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001}
6002
6003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006004ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
6006 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006007 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 do_highlight(eap->arg, eap->forceit, FALSE);
6009}
6010
6011
6012/*
6013 * Call this function if we thought we were going to exit, but we won't
6014 * (because of an error). May need to restore the terminal mode.
6015 */
6016 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006017not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018{
6019 exiting = FALSE;
6020 settmode(TMODE_RAW);
6021}
6022
Bram Moolenaar3552e742021-05-29 12:21:58 +02006023 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006024before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6025{
6026 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6027
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006028 // Bail out when autocommands closed the window.
6029 // Refuse to quit when the buffer in the last window is being closed (can
6030 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006031 if (!win_valid(wp)
6032 || curbuf_locked()
6033 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6034 return TRUE;
6035
6036 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6037 {
6038 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006039 // Refuse to quit when locked or when the window was closed or the
6040 // buffer in the last window is being closed (can only happen in
6041 // autocommands).
6042 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006043 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6044 return TRUE;
6045 }
6046
6047 return FALSE;
6048}
6049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006051 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006052 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006053 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006055 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006056ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006058 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006059
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 if (cmdwin_type != 0)
6061 {
6062 cmdwin_result = Ctrl_C;
6063 return;
6064 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006065 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006066 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006067 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006068 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006069 return;
6070 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006071 if (eap->addr_count > 0)
6072 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006073 int wnr = eap->line2;
6074
6075 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6076 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006077 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006078 }
6079 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006080 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006082 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006083 if (curbuf_locked())
6084 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006085
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006086 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006087 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006088 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089
6090#ifdef FEAT_NETBEANS_INTG
6091 netbeansForcedQuit = eap->forceit;
6092#endif
6093
6094 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006095 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 */
6097 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6098 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006099 if ((!buf_hide(wp->w_buffer)
6100 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006101 | (eap->forceit ? CCGD_FORCEIT : 0)
6102 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006104 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 {
6106 not_exiting();
6107 }
6108 else
6109 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006110 // quit last window
6111 // Note: only_one_window() returns true, even so a help window is
6112 // still open. In that case only quit, if no address has been
6113 // specified. Example:
6114 // :h|wincmd w|1q - don't quit
6115 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006116 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006118 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006119#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006121#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006122 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006123 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 }
6125}
6126
6127/*
6128 * ":cquit".
6129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006131ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006133 // this does not always pass on the exit code to the Manx compiler. why?
6134 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135}
6136
6137/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006138 * Do preparations for "qall" and "wqall".
6139 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006141 int
6142before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (cmdwin_type != 0)
6145 {
6146 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006147 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 else
6149 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006150 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006152
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006153 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006154 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006155 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006156 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006157 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006158 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006159
6160 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006161 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006162
Bram Moolenaar411da642023-05-10 16:53:27 +01006163 return OK;
6164}
6165
6166/*
6167 * ":qall": try to quit all windows
6168 */
6169 static void
6170ex_quit_all(exarg_T *eap)
6171{
6172 if (before_quit_all(eap) == FAIL)
6173 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006175 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 getout(0);
6177 not_exiting();
6178}
6179
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180/*
6181 * ":close": close current window, unless it is the last one
6182 */
6183 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006184ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006186 win_T *win;
6187 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006189 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006191 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006192 {
6193 if (eap->addr_count == 0)
6194 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006195 else
6196 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006197 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006198 {
6199 winnr++;
6200 if (winnr == eap->line2)
6201 break;
6202 }
6203 if (win == NULL)
6204 win = lastwin;
6205 ex_win_close(eap->forceit, win, NULL);
6206 }
6207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208}
6209
Bram Moolenaar4033c552017-09-16 20:54:51 +02006210#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006211/*
6212 * ":pclose": Close any preview window.
6213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006215ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006216{
6217 win_T *win;
6218
Bram Moolenaar79648732019-07-18 21:43:07 +02006219 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006220 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006221 if (win->w_p_pvw)
6222 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006223 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006224 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006225 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006226# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006227 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006228 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006229# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006230}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006231#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232
Bram Moolenaarf740b292006-02-16 22:11:02 +00006233/*
6234 * Close window "win" and take care of handling closing the last window for a
6235 * modified buffer.
6236 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006238ex_win_close(
6239 int forceit,
6240 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006241 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243 int need_hide;
6244 buf_T *buf = win->w_buffer;
6245
Bram Moolenaarcf844172020-06-26 19:44:06 +02006246 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006247 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006248 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006249 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006250 return;
6251 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006252 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006253 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006254
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006256 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006258#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006259 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006261# ifdef FEAT_TERMINAL
6262 if (term_job_running(buf->b_term))
6263 {
6264 if (term_confirm_stop(buf) == FAIL)
6265 return;
6266 // Manually kill the terminal here because this command will
6267 // hide it otherwise.
6268 free_terminal(buf);
6269 need_hide = FALSE;
6270 }
6271 else
6272# endif
6273 {
6274 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006275
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006276 set_bufref(&bufref, buf);
6277 dialog_changed(buf, FALSE);
6278 if (bufref_valid(&bufref) && bufIsChanged(buf))
6279 return;
6280 need_hide = FALSE;
6281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 }
6283 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006286 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 return;
6288 }
6289 }
6290
Bram Moolenaar4033c552017-09-16 20:54:51 +02006291#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006293#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006294
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006295 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006296 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006297 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006298 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006299 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300}
6301
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006303 * Handle the argument for a tabpage related ex command.
6304 * Returns a tabpage number.
6305 * When an error is encountered then eap->errmsg is set.
6306 */
6307 static int
6308get_tabpage_arg(exarg_T *eap)
6309{
6310 int tab_number;
6311 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6312
6313 if (eap->arg && *eap->arg != NUL)
6314 {
6315 char_u *p = eap->arg;
6316 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006317 int relative = 0; // argument +N/-N means: go to N places to the
6318 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006319
6320 if (*p == '-')
6321 {
6322 relative = -1;
6323 p++;
6324 }
6325 else if (*p == '+')
6326 {
6327 relative = 1;
6328 p++;
6329 }
6330
6331 p_save = p;
6332 tab_number = getdigits(&p);
6333
6334 if (relative == 0)
6335 {
6336 if (STRCMP(p, "$") == 0)
6337 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006338 else if (STRCMP(p, "#") == 0)
6339 if (valid_tabpage(lastused_tabpage))
6340 tab_number = tabpage_index(lastused_tabpage);
6341 else
6342 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006343 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6344 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006345 tab_number = 0;
6346 goto theend;
6347 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006348 else if (p == p_save || *p_save == '-' || *p != NUL
6349 || tab_number > LAST_TAB_NR)
6350 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006351 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006352 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006353 goto theend;
6354 }
6355 }
6356 else
6357 {
6358 if (*p_save == NUL)
6359 tab_number = 1;
6360 else if (p == p_save || *p_save == '-' || *p != NUL
6361 || tab_number == 0)
6362 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006363 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006364 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006365 goto theend;
6366 }
6367 tab_number = tab_number * relative + tabpage_index(curtab);
6368 if (!unaccept_arg0 && relative == -1)
6369 --tab_number;
6370 }
6371 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006372 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006373 }
6374 else if (eap->addr_count > 0)
6375 {
6376 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006377 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006378 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006379 tab_number = 0;
6380 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006381 else
6382 {
6383 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006384 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006385 {
zeertzjq076faac2024-03-25 16:41:06 +01006386 char_u *cmdp = eap->cmd;
6387
6388 while (--cmdp > *eap->cmdlinep
6389 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6390 ;
6391 if (*cmdp == '-')
6392 {
6393 --tab_number;
6394 if (tab_number < unaccept_arg0)
6395 eap->errmsg = _(e_invalid_range);
6396 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006397 }
6398 }
6399 }
6400 else
6401 {
6402 switch (eap->cmdidx)
6403 {
6404 case CMD_tabnext:
6405 tab_number = tabpage_index(curtab) + 1;
6406 if (tab_number > LAST_TAB_NR)
6407 tab_number = 1;
6408 break;
6409 case CMD_tabmove:
6410 tab_number = LAST_TAB_NR;
6411 break;
6412 default:
6413 tab_number = tabpage_index(curtab);
6414 }
6415 }
6416
6417theend:
6418 return tab_number;
6419}
6420
6421/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006422 * ":tabclose": close current tab page, unless it is the last one.
6423 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 */
6425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006426ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006428 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006429 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006430
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006431 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006432 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006433 cmdwin_result = K_IGNORE;
6434 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006435 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006436
6437 if (first_tabpage->tp_next == NULL)
6438 {
6439 emsg(_(e_cannot_close_last_tab_page));
6440 return;
6441 }
6442
6443 if (window_layout_locked(CMD_tabclose))
6444 return;
6445
6446 tab_number = get_tabpage_arg(eap);
6447 if (eap->errmsg != NULL)
6448 return;
6449
6450 tp = find_tabpage(tab_number);
6451 if (tp == NULL)
6452 {
6453 beep_flush();
6454 return;
6455 }
6456 if (tp != curtab)
6457 {
6458 tabpage_close_other(tp, eap->forceit);
6459 return;
6460 }
6461 else if (!text_locked() && !curbuf_locked())
6462 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006463}
6464
6465/*
6466 * ":tabonly": close all tab pages except the current one
6467 */
6468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006469ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006470{
6471 tabpage_T *tp;
6472 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006473 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006474
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006475 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006476 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006477 cmdwin_result = K_IGNORE;
6478 return;
6479 }
6480
6481 if (first_tabpage->tp_next == NULL)
6482 {
6483 msg(_("Already only one tab page"));
6484 return;
6485 }
6486
6487 if (window_layout_locked(CMD_tabonly))
6488 return;
6489
6490 tab_number = get_tabpage_arg(eap);
6491 if (eap->errmsg != NULL)
6492 return;
6493
6494 goto_tabpage(tab_number);
6495 // Repeat this up to a 1000 times, because autocommands may
6496 // mess up the lists.
6497 for (done = 0; done < 1000; ++done)
6498 {
6499 FOR_ALL_TABPAGES(tp)
6500 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006501 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006502 tabpage_close_other(tp, eap->forceit);
6503 // if we failed to close it quit
6504 if (valid_tabpage(tp))
6505 done = 1000;
6506 // start over, "tp" is now invalid
6507 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006508 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006509 if (first_tabpage->tp_next == NULL)
6510 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
6514/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006515 * Close the current tab page.
6516 */
6517 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006518tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006519{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006520 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006521 return;
6522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006523 // First close all the windows but the current one. If that worked then
6524 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006525 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006526 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006527 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006528 ex_win_close(forceit, curwin, NULL);
6529# ifdef FEAT_GUI
6530 need_mouse_correct = TRUE;
6531# endif
6532}
6533
6534/*
6535 * Close tab page "tp", which is not the current tab page.
6536 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006537 * Also takes care of the tab pages line disappearing when closing the
6538 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006539 */
6540 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006541tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006542{
6543 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006544 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006545
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006546 // Limit to 1000 windows, autocommands may add a window while we close
6547 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006548 while (++done < 1000)
6549 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006550 wp = tp->tp_firstwin;
6551 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006553 // Autocommands may delete the tab page under our fingers and we may
6554 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006555 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006556 break;
6557 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006558
Bram Moolenaar29323592016-07-24 22:04:11 +02006559 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006560}
6561
6562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 * ":only".
6564 */
6565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006566ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006568 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006569 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570# ifdef FEAT_GUI
6571 need_mouse_correct = TRUE;
6572# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006573 if (eap->addr_count > 0)
6574 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006575 win_T *wp;
6576 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006577 for (wp = firstwin; --wnr > 0; )
6578 {
6579 if (wp->w_next == NULL)
6580 break;
6581 else
6582 wp = wp->w_next;
6583 }
6584 win_goto(wp);
6585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 close_others(TRUE, eap->forceit);
6587}
6588
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006590ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006592 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006593 if (eap->skip)
6594 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006595
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006596 if (window_layout_locked(CMD_hide))
6597 return;
6598#ifdef FEAT_GUI
6599 need_mouse_correct = TRUE;
6600#endif
6601 if (eap->addr_count == 0)
6602 win_close(curwin, FALSE); // don't free buffer
6603 else
6604 {
6605 int winnr = 0;
6606 win_T *win;
6607
6608 FOR_ALL_WINDOWS(win)
6609 {
6610 winnr++;
6611 if (winnr == eap->line2)
6612 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006614 if (win == NULL)
6615 win = lastwin;
6616 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 }
6618}
6619
6620/*
6621 * ":stop" and ":suspend": Suspend Vim.
6622 */
dbivolaruab16ad32021-12-29 19:41:47 +00006623 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006624ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 /*
6627 * Disallow suspending for "rvim".
6628 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006629 if (check_restricted())
6630 return;
6631
6632 if (!eap->forceit)
6633 autowrite_all();
6634 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6635 windgoto((int)Rows - 1, 0);
6636 out_char('\n');
6637 out_flush();
6638 stoptermcap();
6639 out_flush(); // needed for SUN to restore xterm buffer
6640 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6641 ui_suspend(); // call machine specific function
6642 maketitle();
6643 resettitle(); // force updating the title
6644 starttermcap();
6645 scroll_start(); // scroll screen before redrawing
6646 redraw_later_clear();
6647 shell_resized(); // may have resized window
6648 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649}
6650
6651/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006652 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 */
6654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006655ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006657#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006658 if (not_in_vim9(eap) == FAIL)
6659 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 if (cmdwin_type != 0)
6662 {
6663 cmdwin_result = Ctrl_C;
6664 return;
6665 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006666 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006667 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006668 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006669 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006670 return;
6671 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006672
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006674 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 */
6676 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6677 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006678
6679 // Write the buffer for ":wq" or when it was changed.
6680 // Trigger QuitPre and ExitPre.
6681 // Check if we can exit now, after autocommands have changed things.
6682 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6683 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006685 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
6687 not_exiting();
6688 }
6689 else
6690 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006691 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006693 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694# ifdef FEAT_GUI
6695 need_mouse_correct = TRUE;
6696# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006697 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006698 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 }
6700}
6701
6702/*
6703 * ":print", ":list", ":number".
6704 */
6705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006706ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006708 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006709 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006710 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006712 for ( ;!got_int; ui_breakcheck())
6713 {
6714 print_line(eap->line1,
6715 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6716 || (eap->flags & EXFLAG_NR)),
6717 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6718 if (++eap->line1 > eap->line2)
6719 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006720 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006721 }
6722 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006723 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006724 curwin->w_cursor.lnum = eap->line2;
6725 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 }
6727
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729}
6730
6731#ifdef FEAT_BYTEOFF
6732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006733ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 goto_byte(eap->line2);
6736}
6737#endif
6738
6739/*
6740 * ":shell".
6741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006743ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 do_shell(NULL, 0);
6746}
6747
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006748#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006750static int drop_busy = FALSE;
6751static int drop_filec;
6752static char_u **drop_filev = NULL;
6753static int drop_split;
6754static void (*drop_callback)(void *);
6755static void *drop_cookie;
6756
6757 static void
6758handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759{
6760 exarg_T ea;
6761 int save_msg_scroll = msg_scroll;
6762
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006763 // Setting the argument list may cause screen updates and being called
6764 // recursively. Avoid that by setting drop_busy.
6765 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006767 // Check whether the current buffer is changed. If so, we will need
6768 // to split the current window or data could be lost.
6769 // We don't need to check if the 'hidden' option is set, as in this
6770 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006771 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {
6773 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006774 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 --emsg_off;
6776 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006777 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if (win_split(0, 0) == FAIL)
6780 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006781 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006783 // When splitting the window, create a new alist. Otherwise the
6784 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 alist_unlink(curwin->w_alist);
6786 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 }
6788
6789 /*
6790 * Set up the new argument list.
6791 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006792 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793
6794 /*
6795 * Move to the first file.
6796 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006797 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006798 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 ea.cmd = (char_u *)"next";
6800 do_argfile(&ea, 0);
6801
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006802 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6803 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 need_start_insertmode = FALSE;
6805
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006806 // Restore msg_scroll, otherwise a following command may cause scrolling
6807 // unexpectedly. The screen will be redrawn by the caller, thus
6808 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006810
6811 if (drop_callback != NULL)
6812 drop_callback(drop_cookie);
6813
6814 drop_filev = NULL;
6815 drop_busy = FALSE;
6816}
6817
6818/*
6819 * Handle a file drop. The code is here because a drop is *nearly* like an
6820 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006821 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006822 * code is very similar to :args and hence needs access to a lot of the static
6823 * functions in this file.
6824 *
6825 * The "filev" list must have been allocated using alloc(), as should each item
6826 * in the list. This function takes over responsibility for freeing the "filev"
6827 * list.
6828 */
6829 void
6830handle_drop(
6831 int filec, // the number of files dropped
6832 char_u **filev, // the list of files dropped
6833 int split, // force splitting the window
6834 void (*callback)(void *), // to be called after setting the argument
6835 // list
6836 void *cookie) // argument for "callback" (allocated)
6837{
6838 // Cannot handle recursive drops, finish the pending one.
6839 if (drop_busy)
6840 {
6841 FreeWild(filec, filev);
6842 vim_free(cookie);
6843 return;
6844 }
6845
6846 // When calling handle_drop() more than once in a row we only use the last
6847 // one.
6848 if (drop_filev != NULL)
6849 {
6850 FreeWild(drop_filec, drop_filev);
6851 vim_free(drop_cookie);
6852 }
6853
6854 drop_filec = filec;
6855 drop_filev = filev;
6856 drop_split = split;
6857 drop_callback = callback;
6858 drop_cookie = cookie;
6859
6860 // Postpone this when:
6861 // - editing the command line
6862 // - not possible to change the current buffer
6863 // - updating the screen
6864 // As it may change buffers and window structures that are in use and cause
6865 // freed memory to be used.
6866 if (text_locked() || curbuf_locked() || updating_screen)
6867 return;
6868
6869 handle_drop_internal();
6870}
6871
6872/*
6873 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6874 * reset: Handle a postponed drop.
6875 */
6876 void
6877handle_any_postponed_drop(void)
6878{
6879 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006880 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006881 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882}
6883#endif
6884
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 * ":preserve".
6887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006889ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006891 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 ml_preserve(curbuf, TRUE);
6893}
6894
6895/*
6896 * ":recover".
6897 */
6898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006899ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006901 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006903 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6904 | CCGD_MULTWIN
6905 | (eap->forceit ? CCGD_FORCEIT : 0)
6906 | CCGD_EXCMD)
6907
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 && (*eap->arg == NUL
6909 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006910 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 recoverymode = FALSE;
6912}
6913
6914/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006915 * Command modifier used in a wrong way. Also for other commands that can't
6916 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 */
6918 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006919ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006921 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922}
6923
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924/*
6925 * :sview [+command] file split window with new file, read-only
6926 * :split [[+command] file] split window with current or new file
6927 * :vsplit [[+command] file] split window vertically with current or new file
6928 * :new [[+command] file] split window with no or new file
6929 * :vnew [[+command] file] split vertically window with no or new file
6930 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006931 *
6932 * :tabedit open new Tab page with empty window
6933 * :tabedit [+command] file open new Tab page and edit "file"
6934 * :tabnew [[+command] file] just like :tabedit
6935 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 */
6937 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006938ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006940 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006942#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006943 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006944 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006945#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006946 int use_tab = eap->cmdidx == CMD_tabedit
6947 || eap->cmdidx == CMD_tabfind
6948 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006950 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006951 return;
6952
Bram Moolenaar4033c552017-09-16 20:54:51 +02006953#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956
Bram Moolenaar4033c552017-09-16 20:54:51 +02006957#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006958 // A ":split" in the quickfix window works like ":new". Don't want two
6959 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006960 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {
6962 if (eap->cmdidx == CMD_split)
6963 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 if (eap->cmdidx == CMD_vsplit)
6965 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006969 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006971 char_u *file_to_find = NULL;
6972 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006974 FNAME_MESS, TRUE, curbuf->b_ffname,
6975 &file_to_find, &search_ctx);
6976 vim_free(file_to_find);
6977 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 if (fname == NULL)
6979 goto theend;
6980 eap->arg = fname;
6981 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006982# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006983 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 && eap->cmdidx != CMD_new)
6986 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006987 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006988# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006989 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006990# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006991 au_has_group((char_u *)"FileExplorer"))
6992 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006993 // No browsing supported but we do have the file explorer:
6994 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006995 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006996 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006997 }
6998 else
6999 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02007000 fname = do_browse(0, (char_u *)(use_tab
7001 ? _("Edit File in new tab page")
7002 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007004 if (fname == NULL)
7005 goto theend;
7006 eap->arg = fname;
7007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007009 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007012 /*
7013 * Either open new tab page or split the window.
7014 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007015 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007016 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007017 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007018 : eap->addr_count == 0 ? 0
7019 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007020 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007021 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007022
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007023 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007024 if (curwin != old_curwin
7025 && win_valid(old_curwin)
7026 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007027 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007028 old_curwin->w_alt_fnum = curbuf->b_fnum;
7029 }
7030 }
7031 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7033 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007034 // Reset 'scrollbind' when editing another file, but keep it when
7035 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007036 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007037 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 else
7039 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 do_exedit(eap, old_curwin);
7041 }
7042
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007043# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007044 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007045# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047theend:
7048 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007050
7051/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007052 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007053 */
7054 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007055tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007056{
7057 exarg_T ea;
7058
Bram Moolenaara80faa82020-04-12 19:37:17 +02007059 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007060 ea.cmdidx = CMD_tabnew;
7061 ea.cmd = (char_u *)"tabn";
7062 ea.arg = (char_u *)"";
7063 ex_splitview(&ea);
7064}
7065
7066/*
7067 * :tabnext command
7068 */
7069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007070ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007071{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007072 int tab_number;
7073
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007074 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007075 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007076 switch (eap->cmdidx)
7077 {
7078 case CMD_tabfirst:
7079 case CMD_tabrewind:
7080 goto_tabpage(1);
7081 break;
7082 case CMD_tablast:
7083 goto_tabpage(9999);
7084 break;
7085 case CMD_tabprevious:
7086 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007087 if (eap->arg && *eap->arg != NUL)
7088 {
7089 char_u *p = eap->arg;
7090 char_u *p_save = p;
7091
7092 tab_number = getdigits(&p);
7093 if (p == p_save || *p_save == '-' || *p != NUL
7094 || tab_number == 0)
7095 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007096 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007097 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007098 return;
7099 }
7100 }
7101 else
7102 {
7103 if (eap->addr_count == 0)
7104 tab_number = 1;
7105 else
7106 {
7107 tab_number = eap->line2;
7108 if (tab_number < 1)
7109 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007110 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007111 return;
7112 }
7113 }
7114 }
7115 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007116 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007117 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007118 tab_number = get_tabpage_arg(eap);
7119 if (eap->errmsg == NULL)
7120 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007121 break;
7122 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007123}
7124
7125/*
7126 * :tabmove command
7127 */
7128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007129ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007130{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007131 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007132
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007133 tab_number = get_tabpage_arg(eap);
7134 if (eap->errmsg == NULL)
7135 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007136}
7137
7138/*
7139 * :tabs command: List tabs and their contents.
7140 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007141 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007142ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007143{
7144 tabpage_T *tp;
7145 win_T *wp;
7146 int tabcount = 1;
7147
7148 msg_start();
7149 msg_scroll = TRUE;
7150 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7151 {
7152 msg_putchar('\n');
7153 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007154 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007155 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007156 ui_breakcheck();
7157
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007158 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007159 wp = firstwin;
7160 else
7161 wp = tp->tp_firstwin;
7162 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7163 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007164 msg_putchar('\n');
7165 msg_putchar(wp == curwin ? '>' : ' ');
7166 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007167 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7168 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007169 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007170 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007171 else
7172 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7173 IObuff, IOSIZE, TRUE);
7174 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007175 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007176 ui_breakcheck();
7177 }
7178 }
7179}
7180
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181/*
7182 * ":mode": Set screen mode.
7183 * If no argument given, just get the screen size and redraw.
7184 */
7185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007186ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
7188 if (*eap->arg == NUL)
7189 shell_resized();
7190 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007191 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192}
7193
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194/*
7195 * ":resize".
7196 * set, increment or decrement current window height
7197 */
7198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007199ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 int n;
7202 win_T *wp = curwin;
7203
7204 if (eap->addr_count > 0)
7205 {
7206 n = eap->line2;
7207 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7208 ;
7209 }
7210
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007211# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007215 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {
7217 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007218 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007219 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007221 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
7223 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {
7225 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007226 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007227 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007229 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 }
7231}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
7233/*
7234 * ":find [+command] <file>" command.
7235 */
7236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007237ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238{
Colin Kennedy21570352024-03-03 16:16:47 +01007239 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007240 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007241
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 char_u *fname;
7243 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007244 char_u *file_to_find = NULL;
7245 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
7247 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007248 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 if (eap->addr_count > 0)
7250 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007251 // Repeat finding the file "count" times. This matters when it appears
7252 // several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 count = eap->line2;
7254 while (fname != NULL && --count > 0)
7255 {
7256 vim_free(fname);
7257 fname = find_file_in_path(NULL, 0, FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007258 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 }
7260 }
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007261 VIM_CLEAR(file_to_find);
7262 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007264 if (fname == NULL)
7265 return;
7266
7267 eap->arg = fname;
7268 do_exedit(eap, NULL);
7269 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270}
7271
7272/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007273 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007274 */
7275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007276ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007277{
7278 regmatch_T regmatch;
7279 char_u *p;
7280
Bram Moolenaar65088802021-03-13 21:07:21 +01007281#ifdef FEAT_EVAL
7282 if (not_in_vim9(eap) == FAIL)
7283 return;
7284#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007285 curwin->w_cursor.lnum = eap->line2;
7286 beginline(BL_SOL | BL_FIX);
7287 if (*eap->arg == '/')
7288 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007289 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007290 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007291 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007292 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007293 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007294 if (regmatch.regprog != NULL)
7295 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007296 // make a copy of the line, when searching for a mark it might be
7297 // flushed
7298 char_u *line = vim_strsave(ml_get_curline());
7299
Bram Moolenaardf177f62005-02-22 08:39:57 +00007300 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007301 if (vim_regexec(&regmatch, line, (colnr_T)0))
7302 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007303 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007304 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007305 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007306 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007307 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007308 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007309 eap->arg += STRLEN(eap->arg);
7310 }
7311 check_cursor();
7312
7313 eap->cmdidx = CMD_visual;
7314 do_exedit(eap, NULL);
7315}
7316
7317/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007318 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 */
7320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007321ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007323 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7324
Colin Kennedy21570352024-03-03 16:16:47 +01007325 // Exclude commands which keep the window's current buffer
7326 if (
7327 eap->cmdidx != CMD_badd
7328 && eap->cmdidx != CMD_balt
7329 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007330 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7331 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007332 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007333
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 do_exedit(eap, NULL);
7335}
7336
7337/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007338 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007341do_exedit(
7342 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007343 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344{
7345 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007347 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007349 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7350 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007351 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 /*
7353 * ":vi" command ends Ex mode.
7354 */
7355 if (exmode_active && (eap->cmdidx == CMD_visual
7356 || eap->cmdidx == CMD_view))
7357 {
7358 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007359 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007361 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007362 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007363 if (global_busy)
7364 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007365 if (eap->nextcmd != NULL)
7366 {
7367 stuffReadbuff(eap->nextcmd);
7368 eap->nextcmd = NULL;
7369 }
7370
7371 if (exmode_was != EXMODE_VIM)
7372 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007373 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007374 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007375 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007376 no_wait_return = 0;
7377 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007378 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007379 msg_scroll = 0;
7380#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007381 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007382 hold_gui_events = 0;
7383#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007384 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007385 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007386
7387 main_loop(FALSE, TRUE);
7388
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007389 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007390 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007391 no_wait_return = save_nwr;
7392 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007393#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007394 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007395#endif
7396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 }
7400
7401 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007402 || eap->cmdidx == CMD_tabnew
7403 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007404 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007406 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 setpcmark();
7408 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007409 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7410 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007412 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 || *eap->arg != NUL
7414#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007415 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416#endif
7417 )
7418 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007419 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7420 // Only ":edit" or ":script" can bring us here, others are stopped
7421 // earlier.
7422 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007423 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007424
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 n = readonlymode;
7426 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7427 readonlymode = TRUE;
7428 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007429 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7430 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007431 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7432 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7434 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007435 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7437 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7438 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007439 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007441 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007442 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007443 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7444 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007445 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007447 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 if (old_curwin != NULL)
7449 {
7450 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007451 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007453#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007454 cleanup_T cs;
7455
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007456 // Reset the error/interrupt/exception state here so that
7457 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007458 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007459#endif
7460#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007462#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007463 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007464
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007465#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007466 // Restore the error/interrupt/exception state if not
7467 // discarded by a new aborting error, interrupt, or
7468 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007469 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 }
7472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 }
7474 else if (readonlymode && curbuf->b_nwindows == 1)
7475 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007476 // When editing an already visited buffer, 'readonly' won't be set
7477 // but the previous value is kept. With ":view" and ":sview" we
7478 // want the file to be readonly, except when another window is
7479 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 curbuf->b_p_ro = TRUE;
7481 }
7482 readonlymode = n;
7483 }
7484 else
7485 {
7486 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007487 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 if (n != curwin->w_arg_idx_invalid)
7491 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 }
7493
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 /*
7495 * if ":split file" worked, set alternate file name in old window to new
7496 * file
7497 */
7498 if (old_curwin != NULL
7499 && *eap->arg != NUL
7500 && curwin != old_curwin
7501 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007502 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007503 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505
7506 ex_no_reprint = TRUE;
7507}
7508
7509#ifndef FEAT_GUI
7510/*
7511 * ":gui" and ":gvim" when there is no GUI.
7512 */
7513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007514ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007516 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517}
7518#endif
7519
Bram Moolenaar4f974752019-02-17 17:44:42 +01007520#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007522ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523{
7524 gui_make_tearoff(eap->arg);
7525}
7526#endif
7527
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007528#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7529 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007531ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007533# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7534 if (gui.in_use)
7535 gui_make_popup(eap->arg, eap->forceit);
7536# ifdef FEAT_TERM_POPUP_MENU
7537 else
7538# endif
7539# endif
7540# ifdef FEAT_TERM_POPUP_MENU
7541 pum_make_popup(eap->arg, eap->forceit);
7542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543}
7544#endif
7545
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007547ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548{
7549 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007550 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007552 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553}
7554
7555/*
7556 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7557 * offset.
7558 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007561ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007564 win_T *save_curwin = curwin;
7565 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 long topline;
7567 long y;
7568 linenr_T old_linenr = curwin->w_cursor.lnum;
7569
7570 setpcmark();
7571
7572 /*
7573 * determine max topline
7574 */
7575 if (curwin->w_p_scb)
7576 {
7577 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007578 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 {
7580 if (wp->w_p_scb && wp->w_buffer)
7581 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007582 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 if (topline > y)
7584 topline = y;
7585 }
7586 }
7587 if (topline < 1)
7588 topline = 1;
7589 }
7590 else
7591 {
7592 topline = 1;
7593 }
7594
7595
7596 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007597 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007599 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 {
7601 if (curwin->w_p_scb)
7602 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007603 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 y = topline - curwin->w_topline;
7605 if (y > 0)
7606 scrollup(y, TRUE);
7607 else
7608 scrolldown(-y, TRUE);
7609 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007610 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 }
7614 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007615 curwin = save_curwin;
7616 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 if (curwin->w_p_scb)
7618 {
7619 did_syncbind = TRUE;
7620 checkpcmark();
7621 if (old_linenr != curwin->w_cursor.lnum)
7622 {
7623 char_u ctrl_o[2];
7624
7625 ctrl_o[0] = Ctrl_O;
7626 ctrl_o[1] = 0;
7627 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7628 }
7629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630}
7631
7632
7633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007634ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007636 int i;
7637 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7638 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007640 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007642 do_bang(1, eap, FALSE, FALSE, TRUE);
7643 return;
7644 }
7645
7646 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7647 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
7649#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007650 if (cmdmod.cmod_flags & CMOD_BROWSE)
7651 {
7652 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007654 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7655 NULL, NULL, NULL, curbuf);
7656 if (browseFile != NULL)
7657 {
7658 i = readfile(browseFile, NULL,
7659 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7660 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 }
7662 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007663 i = OK;
7664 }
7665 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007667 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007669 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 return;
7671 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007672 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 }
7674 else
7675 {
7676 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7677 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7678 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007679 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680
7681 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007682 if (i != OK)
7683 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007684#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007685 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007687 semsg(_(e_cant_open_file_str), eap->arg);
7688 }
7689 else
7690 {
7691 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007692 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007693 // Delete the empty line that remains. Historically ex does
7694 // this but vi doesn't.
7695 if (eap->line2 == 0)
7696 lnum = curbuf->b_ml.ml_line_count;
7697 else
7698 lnum = 1;
7699 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007700 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007701 ml_delete(lnum);
7702 if (curwin->w_cursor.lnum > 1
7703 && curwin->w_cursor.lnum >= lnum)
7704 --curwin->w_cursor.lnum;
7705 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007706 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007707 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007708 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
7710}
7711
Bram Moolenaarea408852005-06-25 22:49:46 +00007712static char_u *prev_dir = NULL;
7713
7714#if defined(EXITFREE) || defined(PROTO)
7715 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007716free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007717{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007718 VIM_CLEAR(prev_dir);
7719 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007720}
7721#endif
7722
Bram Moolenaarf4258302013-06-02 18:20:17 +02007723/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007724 * Get the previous directory for the given chdir scope.
7725 */
7726 static char_u *
7727get_prevdir(cdscope_T scope)
7728{
7729 if (scope == CDSCOPE_WINDOW)
7730 return curwin->w_prevdir;
7731 else if (scope == CDSCOPE_TABPAGE)
7732 return curtab->tp_prevdir;
7733 return prev_dir;
7734}
7735
7736/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007737 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007738 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7739 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007740 */
7741 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007742post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007743{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007744 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007745 // Clear tab local directory for both :cd and :tcd
7746 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007747 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007748 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007749 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007750 char_u *pdir = get_prevdir(scope);
7751
7752 // If still in the global directory, need to remember current
7753 // directory as the global directory.
7754 if (globaldir == NULL && pdir != NULL)
7755 globaldir = vim_strsave(pdir);
7756
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007757 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007758 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007759 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007760 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007761 curtab->tp_localdir = vim_strsave(NameBuff);
7762 else
7763 curwin->w_localdir = vim_strsave(NameBuff);
7764 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007765 }
7766 else
7767 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007768 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007769 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007770 }
7771
zeertzjq64be6aa2021-11-19 11:59:08 +00007772 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007773 shorten_fnames(TRUE);
7774}
7775
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007776/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007777 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7778 */
7779 void
7780trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7781{
7782#ifdef FEAT_EVAL
7783 dict_T *v_event;
7784 save_v_event_T save_v_event;
7785
7786 v_event = get_v_event(&save_v_event);
7787 (void)dict_add_string(v_event, "directory", new_dir);
7788 dict_set_items_ro(v_event);
7789#endif
7790 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7791#ifdef FEAT_EVAL
7792 restore_v_event(v_event, &save_v_event);
7793#endif
7794}
7795
7796/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007797 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007798 * chdir() function.
7799 * scope == CDSCOPE_WINDOW: changes the window-local directory
7800 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7801 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007802 * Returns TRUE if the directory is successfully changed.
7803 */
7804 int
7805changedir_func(
7806 char_u *new_dir,
7807 int forceit,
7808 cdscope_T scope)
7809{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007810 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007811 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007812 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007813 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007814 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007815
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007816 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007817 return FALSE;
7818
7819 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7820 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007821 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007822 return FALSE;
7823 }
7824
7825 // ":cd -": Change to previous directory
7826 if (STRCMP(new_dir, "-") == 0)
7827 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007828 pdir = get_prevdir(scope);
7829 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007830 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007831 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007832 return FALSE;
7833 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007834 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007835 }
7836
7837 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007838 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007839 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007840 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007841 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007842
Bakudankun29f3a452021-12-11 12:28:08 +00007843 // For UNIX ":cd" means: go to home directory.
7844 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007845#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007846 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007847#else
7848 if (*new_dir == NUL && p_cdh)
7849#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007850 {
7851 // use NameBuff for home directory name
7852# ifdef VMS
7853 char_u *p;
7854
7855 p = mch_getenv((char_u *)"SYS$LOGIN");
7856 if (p == NULL || *p == NUL) // empty is the same as not set
7857 NameBuff[0] = NUL;
7858 else
7859 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7860# else
7861 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7862# endif
7863 new_dir = NameBuff;
7864 }
zeertzjqf38aad82021-12-30 13:45:57 +00007865 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007866 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7867 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007868 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007869 if (scope == CDSCOPE_WINDOW)
7870 acmd_fname = (char_u *)"window";
7871 else if (scope == CDSCOPE_TABPAGE)
7872 acmd_fname = (char_u *)"tabpage";
7873 else
7874 acmd_fname = (char_u *)"global";
7875 trigger_DirChangedPre(acmd_fname, new_dir);
7876
7877 if (vim_chdir(new_dir))
7878 {
7879 emsg(_(e_command_failed));
7880 vim_free(pdir);
7881 return FALSE;
7882 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007883 }
zeertzjq73257142022-02-02 13:16:37 +00007884
7885 if (scope == CDSCOPE_WINDOW)
7886 pp = &curwin->w_prevdir;
7887 else if (scope == CDSCOPE_TABPAGE)
7888 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007889 else
zeertzjq73257142022-02-02 13:16:37 +00007890 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007891 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007892 *pp = pdir;
7893
7894 post_chdir(scope);
7895
7896 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007897 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007898 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007899 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007900}
Bram Moolenaarea408852005-06-25 22:49:46 +00007901
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007903 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007905 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007906ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007908 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909
7910 new_dir = eap->arg;
7911#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007912 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7913 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007915 ex_pwd(NULL);
7916 return;
7917 }
7918#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007919
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007920 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007921
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007922 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7923 scope = CDSCOPE_WINDOW;
7924 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7925 scope = CDSCOPE_TABPAGE;
7926
7927 if (changedir_func(new_dir, eap->forceit, scope))
7928 {
7929 // Echo the new current directory if the command was typed.
7930 if (KeyTyped || p_verbose >= 5)
7931 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 }
7933}
7934
7935/*
7936 * ":pwd".
7937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007939ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
7941 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7942 {
7943#ifdef BACKSLASH_IN_FILENAME
7944 slash_adjust(NameBuff);
7945#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007946 if (p_verbose > 0)
7947 {
7948 char *context = "global";
7949
Bram Moolenaar05268152021-11-18 18:53:45 +00007950 if (last_chdir_reason != NULL)
7951 context = last_chdir_reason;
7952 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007953 context = "window";
7954 else if (curtab->tp_localdir != NULL)
7955 context = "tabpage";
7956 smsg("[%s] %s", context, (char *)NameBuff);
7957 }
7958 else
7959 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 }
7961 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007962 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963}
7964
7965/*
7966 * ":=".
7967 */
7968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007969ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007971 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007972 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973}
7974
7975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007976ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007978 int n;
7979 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980
7981 if (cursor_valid())
7982 {
7983 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7984 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007985 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007987
7988 len = eap->line2;
7989 switch (*eap->arg)
7990 {
7991 case 'm': break;
7992 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007993 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007994 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007995
7996 // Hide the cursor if invoked with !
7997 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998}
7999
8000/*
8001 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008002 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 */
8004 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008005do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
Bram Moolenaar52953192019-08-16 10:27:13 +02008007 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008008 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008009# ifdef ELAPSED_FUNC
8010 elapsed_T start_tv;
8011
8012 // Remember at what time we started, so that we know how much longer we
8013 // should wait after waiting for a bit.
8014 ELAPSED_INIT(start_tv);
8015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008017 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008018 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008019 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008020 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008021
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008022 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008023 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008025 wait_now = msec - done > 1000L ? 1000L : msec - done;
8026#ifdef FEAT_TIMERS
8027 {
8028 long due_time = check_due_timer();
8029
8030 if (due_time > 0 && due_time < wait_now)
8031 wait_now = due_time;
8032 }
8033#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008034#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008035 if (has_any_channel() && wait_now > 20L)
8036 wait_now = 20L;
8037#endif
8038#ifdef FEAT_SOUND
8039 if (has_any_sound_callback() && wait_now > 20L)
8040 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008041#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008042 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008043
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008044#ifdef FEAT_JOB_CHANNEL
8045 if (has_any_channel())
8046 ui_breakcheck_force(TRUE);
8047 else
8048#endif
8049 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008050#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008051 // Process the netbeans and clientserver messages that may have been
8052 // received in the call to ui_breakcheck() when the GUI is in use. This
8053 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008054 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008055#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008056
8057# ifdef ELAPSED_FUNC
8058 // actual time passed
8059 done = ELAPSED_FUNC(start_tv);
8060# else
8061 // guestimate time passed (will actually be more)
8062 done += wait_now;
8063# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008065
8066 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8067 // input buffer, otherwise a following call to input() fails.
8068 if (got_int)
8069 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008070
8071 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008072 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073}
8074
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075/*
8076 * ":winsize" command (obsolete).
8077 */
8078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008079ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080{
8081 int w, h;
8082 char_u *arg = eap->arg;
8083 char_u *p;
8084
Keith Thompson184f71c2024-01-04 21:19:04 +01008085 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008086 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008087 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008088 return;
8089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 w = getdigits(&arg);
8091 arg = skipwhite(arg);
8092 p = arg;
8093 h = getdigits(&arg);
8094 if (*p != NUL && *arg == NUL)
8095 set_shellsize(w, h, TRUE);
8096 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008097 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098}
8099
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008101ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102{
8103 int xchar = NUL;
8104 char_u *p;
8105
8106 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8107 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008108 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 if (eap->arg[1] == NUL)
8110 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008111 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 return;
8113 }
8114 xchar = eap->arg[1];
8115 p = eap->arg + 2;
8116 }
8117 else
8118 p = eap->arg + 1;
8119
Bram Moolenaar63b91732021-08-05 20:40:03 +02008120 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008122 if (*p != NUL && *p != (
8123#ifdef FEAT_EVAL
8124 in_vim9script() ? '#' :
8125#endif
8126 '"')
8127 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008128 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008129 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008131 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008132 postponed_split_flags = cmdmod.cmod_split;
8133 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8135 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008136 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 }
8138}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139
Bram Moolenaar843ee412004-06-30 16:16:41 +00008140#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141/*
8142 * ":winpos".
8143 */
8144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008145ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146{
8147 int x, y;
8148 char_u *arg = eap->arg;
8149 char_u *p;
8150
8151 if (*arg == NUL)
8152 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008153# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008154# ifdef VIMDLL
8155 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8156 mch_get_winpos(&x, &y) != FAIL)
8157# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008159# else
8160 if (mch_get_winpos(&x, &y) != FAIL)
8161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {
8163 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008164 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 }
8166 else
8167# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008168 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 }
8170 else
8171 {
8172 x = getdigits(&arg);
8173 arg = skipwhite(arg);
8174 p = arg;
8175 y = getdigits(&arg);
8176 if (*p == NUL || *arg != NUL)
8177 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008178 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 return;
8180 }
8181# ifdef FEAT_GUI
8182 if (gui.in_use)
8183 gui_mch_set_winpos(x, y);
8184 else if (gui.starting)
8185 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008186 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 gui_win_x = x;
8188 gui_win_y = y;
8189 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008190# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 else
8192# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008193# endif
8194# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008195 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196# endif
8197# ifdef HAVE_TGETENT
8198 if (*T_CWP)
8199 term_set_winpos(x, y);
8200# endif
8201 }
8202}
8203#endif
8204
8205/*
8206 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8207 */
8208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008209ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210{
8211 oparg_T oa;
8212
8213 clear_oparg(&oa);
8214 oa.regname = eap->regname;
8215 oa.start.lnum = eap->line1;
8216 oa.end.lnum = eap->line2;
8217 oa.line_count = eap->line2 - eap->line1 + 1;
8218 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008220 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {
8222 setpcmark();
8223 curwin->w_cursor.lnum = eap->line1;
8224 beginline(BL_SOL | BL_FIX);
8225 }
8226
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008227 if (VIsual_active)
8228 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008229
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 switch (eap->cmdidx)
8231 {
8232 case CMD_delete:
8233 oa.op_type = OP_DELETE;
8234 op_delete(&oa);
8235 break;
8236
8237 case CMD_yank:
8238 oa.op_type = OP_YANK;
8239 (void)op_yank(&oa, FALSE, TRUE);
8240 break;
8241
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008242 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008243 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008245 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8246#else
8247 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008249 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 oa.op_type = OP_RSHIFT;
8251 else
8252 oa.op_type = OP_LSHIFT;
8253 op_shift(&oa, FALSE, eap->amount);
8254 break;
8255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008257 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258}
8259
8260/*
8261 * ":put".
8262 */
8263 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008264ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008266 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 if (eap->line2 == 0)
8268 {
8269 eap->line2 = 1;
8270 eap->forceit = TRUE;
8271 }
8272 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008273 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008274 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008275 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276}
8277
8278/*
8279 * Handle ":copy" and ":move".
8280 */
8281 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008282ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283{
8284 long n;
8285
Bram Moolenaar491799b2020-08-01 19:23:43 +02008286#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008287 if (not_in_vim9(eap) == FAIL)
8288 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008289#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008290 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008291 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {
8293 eap->nextcmd = NULL;
8294 return;
8295 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008296 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297
8298 /*
8299 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8300 */
8301 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8302 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008303 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 return;
8305 }
8306
8307 if (eap->cmdidx == CMD_move)
8308 {
8309 if (do_move(eap->line1, eap->line2, n) == FAIL)
8310 return;
8311 }
8312 else
8313 ex_copy(eap->line1, eap->line2, n);
8314 u_clearline();
8315 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008316 ex_may_print(eap);
8317}
8318
8319/*
8320 * Print the current line if flags were given to the Ex command.
8321 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008322 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008323ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008324{
8325 if (eap->flags != 0)
8326 {
8327 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8328 (eap->flags & EXFLAG_LIST));
8329 ex_no_reprint = TRUE;
8330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331}
8332
8333/*
8334 * ":smagic" and ":snomagic".
8335 */
8336 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008337ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008339 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008341 magic_overruled = eap->cmdidx == CMD_smagic
8342 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008343 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008344 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345}
8346
8347/*
8348 * ":join".
8349 */
8350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008351ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
8353 curwin->w_cursor.lnum = eap->line1;
8354 if (eap->line1 == eap->line2)
8355 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008356 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 return;
8358 if (eap->line2 == curbuf->b_ml.ml_line_count)
8359 {
8360 beep_flush();
8361 return;
8362 }
8363 ++eap->line2;
8364 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008365 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008367 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368}
8369
8370/*
8371 * ":[addr]@r" or ":[addr]*r": execute register
8372 */
8373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008374ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
8376 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008377 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
8379 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008380 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381
8382#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008383 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384#endif
8385
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008386 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 c = *eap->arg;
8388 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8389 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008390 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008391 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8392 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008395 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008398 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008400 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008402 /*
8403 * Execute from the typeahead buffer.
8404 * Continue until the stuff buffer is empty and all added characters
8405 * have been consumed.
8406 */
8407 while (!stuff_empty() || typebuf.tb_len > prev_len)
8408 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8409
8410 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411}
8412
8413/*
8414 * ":!".
8415 */
8416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008417ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418{
8419 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8420}
8421
8422/*
8423 * ":undo".
8424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008426ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008428 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008429 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008430 else
8431 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432}
8433
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008434#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008436ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008437{
8438 char_u hash[UNDO_HASH_SIZE];
8439
8440 u_compute_hash(hash);
8441 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8442}
8443
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008445ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008446{
8447 char_u hash[UNDO_HASH_SIZE];
8448
8449 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008450 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008451}
8452#endif
8453
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454/*
8455 * ":redo".
8456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008458ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459{
8460 u_redo(1);
8461}
8462
8463/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008464 * ":earlier" and ":later".
8465 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008467ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008468{
8469 long count = 0;
8470 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008471 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008472 char_u *p = eap->arg;
8473
8474 if (*p == NUL)
8475 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008476 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008477 {
8478 count = getdigits(&p);
8479 switch (*p)
8480 {
8481 case 's': ++p; sec = TRUE; break;
8482 case 'm': ++p; sec = TRUE; count *= 60; break;
8483 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008484 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8485 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008486 }
8487 }
8488
8489 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008490 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008491 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008492 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8493 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008494}
8495
8496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 * ":redir": start/stop redirection.
8498 */
8499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008500ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502 char *mode;
8503 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008504 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505
Bram Moolenaarba768492016-07-08 15:32:54 +02008506#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008507 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008508 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008509 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008510 return;
8511 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008512#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008513
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 if (STRICMP(eap->arg, "END") == 0)
8515 close_redir();
8516 else
8517 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008518 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008520 ++arg;
8521 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008523 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 mode = "a";
8525 }
8526 else
8527 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008528 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529
8530 close_redir();
8531
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008532 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008533 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 if (fname == NULL)
8535 return;
8536#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008537 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 {
8539 char_u *browseFile;
8540
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008541 browseFile = do_browse(BROWSE_SAVE,
8542 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008543 fname, NULL, NULL,
8544 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008546 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 vim_free(fname);
8548 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008549 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 }
8551#endif
8552
8553 redir_fd = open_exfile(fname, eap->forceit, mode);
8554 vim_free(fname);
8555 }
8556#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008557 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008559 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008561 ++arg;
8562 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008564 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008565 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008567 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008569 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008570 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008571 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008572 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008574 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008575 if (*arg == '>')
8576 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008577 // Make register empty when not using @A-@Z and the
8578 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008579 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008580 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008582 }
8583 if (*arg != NUL)
8584 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008585 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008586 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008587 }
8588 }
8589 else if (*arg == '=' && arg[1] == '>')
8590 {
8591 int append;
8592
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008593 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008594 close_redir();
8595 arg += 2;
8596
8597 if (*arg == '>')
8598 {
8599 ++arg;
8600 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 }
8602 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008603 append = FALSE;
8604
8605 if (var_redir_start(skipwhite(arg), append) == OK)
8606 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 }
8608#endif
8609
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008610 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008613 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008615
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008616 // Make sure redirection is not off. Can happen for cmdline completion
8617 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008618 if (redir_fd != NULL
8619#ifdef FEAT_EVAL
8620 || redir_reg || redir_vname
8621#endif
8622 )
8623 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624}
8625
8626/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008627 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008629 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008630ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008632 redraw_cmd(eap->forceit);
8633}
8634
8635/*
8636 * ":redraw": force redraw, with clear if "clear" is TRUE.
8637 */
8638 void
8639redraw_cmd(int clear)
8640{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008641 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008643
8644 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008646
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008647 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008649 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 if (need_maketitle)
8651 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008652#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8653# ifdef VIMDLL
8654 if (!gui.in_use)
8655# endif
8656 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008657#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008658 RedrawingDisabled = save_RedrawingDisabled;
8659 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660
Bram Moolenaar5017c662022-04-07 18:06:08 +01008661 // After drawing the statusline screen_attr may still be set.
8662 screen_stop_highlight();
8663
Bram Moolenaara2a89732022-08-31 14:46:18 +01008664 // Reset msg_didout, so that a message that's there is overwritten.
8665 msg_didout = FALSE;
8666 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008668 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008669 need_wait_return = FALSE;
8670
Bram Moolenaara653e532022-04-19 11:38:24 +01008671 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008672 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008673 redrawcmdline();
8674
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 out_flush();
8676}
8677
8678/*
8679 * ":redrawstatus": force redraw of status line(s)
8680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008682ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 if (eap->forceit)
8685 status_redraw_all();
8686 else
8687 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008688 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008689 return; // redraw later
8690
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008691 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008692 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008693
8694 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008695 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008696
zeertzjq320d9102022-09-20 17:12:13 +01008697 if (State & MODE_CMDLINE)
8698 redraw_statuslines();
8699 else
8700 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008701 RedrawingDisabled = save_RedrawingDisabled;
8702 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704}
8705
Bram Moolenaare12bab32019-01-08 22:02:56 +01008706/*
8707 * ":redrawtabline": force redraw of the tabline
8708 */
8709 static void
8710ex_redrawtabline(exarg_T *eap UNUSED)
8711{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008712 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008713 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008714
8715 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008716 p_lz = FALSE;
8717
8718 draw_tabline();
8719
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008720 RedrawingDisabled = save_RedrawingDisabled;
8721 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008722 out_flush();
8723}
8724
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008726close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727{
8728 if (redir_fd != NULL)
8729 {
8730 fclose(redir_fd);
8731 redir_fd = NULL;
8732 }
8733#ifdef FEAT_EVAL
8734 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008735 if (redir_vname)
8736 {
8737 var_redir_stop();
8738 redir_vname = 0;
8739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740#endif
8741}
8742
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008743#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008744 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008745vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008746{
8747 if (vim_mkdir(name, prot) != 0)
8748 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008749 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008750 return FAIL;
8751 }
8752 return OK;
8753}
8754#endif
8755
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756/*
8757 * Open a file for writing for an Ex command, with some checks.
8758 * Return file descriptor, or NULL on failure.
8759 */
8760 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008761open_exfile(
8762 char_u *fname,
8763 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008764 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765{
8766 FILE *fd;
8767
8768#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008769 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 if (mch_isdir(fname))
8771 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008772 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 return NULL;
8774 }
8775#endif
8776 if (!forceit && *mode != 'a' && vim_fexists(fname))
8777 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008778 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 return NULL;
8780 }
8781
8782 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008783 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784
8785 return fd;
8786}
8787
8788/*
8789 * ":mark" and ":k".
8790 */
8791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008792ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
8794 pos_T pos;
8795
Bram Moolenaar10b94212021-02-19 21:42:57 +01008796#ifdef FEAT_EVAL
8797 if (not_in_vim9(eap) == FAIL)
8798 return;
8799#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008800 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008802 emsg(_(e_argument_required));
8803 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008805
8806 if (eap->arg[1] != NUL) // more than one character?
8807 {
8808 semsg(_(e_trailing_characters_str), eap->arg);
8809 return;
8810 }
8811
8812 pos = curwin->w_cursor; // save curwin->w_cursor
8813 curwin->w_cursor.lnum = eap->line2;
8814 beginline(BL_WHITE | BL_FIX);
8815 if (setmark(*eap->arg) == FAIL) // set mark
8816 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
8817 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818}
8819
8820/*
8821 * Update w_topline, w_leftcol and the cursor position.
8822 */
8823 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008824update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008826 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 update_topline();
8828 if (!curwin->w_p_wrap)
8829 validate_cursor();
8830 update_curswant();
8831}
8832
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008834 * Save the current State and go to Normal mode.
8835 * Return TRUE if the typeahead could be saved.
8836 */
8837 int
8838save_current_state(save_state_T *sst)
8839{
8840 sst->save_msg_scroll = msg_scroll;
8841 sst->save_restart_edit = restart_edit;
8842 sst->save_msg_didout = msg_didout;
8843 sst->save_State = State;
8844 sst->save_insertmode = p_im;
8845 sst->save_finish_op = finish_op;
8846 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008847 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008848 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008849
Bram Moolenaar4324d872020-12-01 20:12:24 +01008850 msg_scroll = FALSE; // no msg scrolling in Normal mode
8851 restart_edit = 0; // don't go to Insert mode
8852 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008853
Bram Moolenaara452b802020-12-01 21:47:59 +01008854 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008855 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008856
8857 /*
8858 * Save the current typeahead. This is required to allow using ":normal"
8859 * from an event handler and makes sure we don't hang when the argument
8860 * ends with half a command.
8861 */
8862 save_typeahead(&sst->tabuf);
8863 return sst->tabuf.typebuf_valid;
8864}
8865
8866 void
8867restore_current_state(save_state_T *sst)
8868{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008869 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008870 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008871
8872 msg_scroll = sst->save_msg_scroll;
8873 restart_edit = sst->save_restart_edit;
8874 p_im = sst->save_insertmode;
8875 finish_op = sst->save_finish_op;
8876 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008877 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008878 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008879 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008880 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008881
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008882 // Restore the state (needed when called from a function executed for
8883 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008884 State = sst->save_State;
8885#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008886 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008887#endif
8888}
8889
8890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 * ":normal[!] {commands}": Execute normal mode commands.
8892 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008893 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008894ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008896 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 char_u *arg = NULL;
8898 int l;
8899 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008901 if (ex_normal_lock > 0)
8902 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008903 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008904 return;
8905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 if (ex_normal_busy >= p_mmd)
8907 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008908 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 return;
8910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 /*
8913 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8914 * this for the K_SPECIAL leading byte, otherwise special keys will not
8915 * work.
8916 */
8917 if (has_mbyte)
8918 {
8919 int len = 0;
8920
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008921 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 for (p = eap->arg; *p != NUL; ++p)
8923 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008924#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008925 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008927#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008928 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008929 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008930#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 )
8934 len += 2;
8935 }
8936 if (len > 0)
8937 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008938 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 if (arg != NULL)
8940 {
8941 len = 0;
8942 for (p = eap->arg; *p != NUL; ++p)
8943 {
8944 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008945#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 if (*p == CSI)
8947 {
8948 arg[len++] = KS_EXTRA;
8949 arg[len++] = (int)KE_CSI;
8950 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008951#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008952 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 {
8954 arg[len++] = *++p;
8955 if (*p == K_SPECIAL)
8956 {
8957 arg[len++] = KS_SPECIAL;
8958 arg[len++] = KE_FILLER;
8959 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008960#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 else if (*p == CSI)
8962 {
8963 arg[len++] = KS_EXTRA;
8964 arg[len++] = (int)KE_CSI;
8965 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 }
8968 arg[len] = NUL;
8969 }
8970 }
8971 }
8972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008974 ++ex_normal_busy;
8975 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 {
8977 /*
8978 * Repeat the :normal command for each line in the range. When no
8979 * range given, execute it just once, without positioning the cursor
8980 * first.
8981 */
8982 do
8983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 if (eap->addr_count != 0)
8985 {
8986 curwin->w_cursor.lnum = eap->line1++;
8987 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008988 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 }
8990
Bram Moolenaar13505972019-01-24 15:04:48 +01008991 exec_normal_cmd(arg != NULL
8992 ? arg
8993 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 }
8995 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8996 }
8997
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008998 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 update_topline_cursor();
9000
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009001 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009003 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009004#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009005 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009006#endif
9007
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009}
9010
9011/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009012 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 */
9014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009015ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009017 if (eap->forceit)
9018 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009019 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009020 if (!curwin->w_cursor.lnum)
9021 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009022 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009023 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009024#ifdef FEAT_TERMINAL
9025 // Ignore this when running in an active terminal.
9026 if (term_job_running(curbuf->b_term))
9027 return;
9028#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009029
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009030 // Ignore the command when already in Insert mode. Inserting an
9031 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009032 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009033 return;
9034
Bram Moolenaar64969662005-12-14 21:59:55 +00009035 if (eap->cmdidx == CMD_startinsert)
9036 restart_edit = 'a';
9037 else if (eap->cmdidx == CMD_startreplace)
9038 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009040 restart_edit = 'V';
9041
9042 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009044 if (eap->cmdidx == CMD_startinsert)
9045 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009046 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009048
9049 if (VIsual_active)
9050 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051}
9052
9053/*
9054 * ":stopinsert"
9055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009057ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058{
9059 restart_edit = 0;
9060 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009061 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009064/*
9065 * Execute normal mode command "cmd".
9066 * "remap" can be REMAP_NONE or REMAP_YES.
9067 */
9068 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009069exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009070{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009071 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009072 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009073 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009074}
Bram Moolenaar25281632016-01-21 23:32:32 +01009075
Bram Moolenaar25281632016-01-21 23:32:32 +01009076/*
9077 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009078 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009079 */
9080 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009081exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009082{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009083 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009084 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009085
Bram Moolenaar905dd902019-04-07 14:21:47 +02009086 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9087 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009088 clear_oparg(&oa);
9089 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009090 while ((!stuff_empty()
9091 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009092 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009093 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009094 {
9095 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009096#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009097 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009098 && oa.op_type == OP_NOP && oa.regname == NUL
9099 && !VIsual_active)
9100 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009101 // If terminal_loop() returns OK we got a key that is handled
9102 // in Normal model. With FAIL we first need to position the
9103 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009104 if (terminal_loop(TRUE) == OK)
9105 normal_cmd(&oa, TRUE);
9106 }
9107 else
9108#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009109 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009110 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009111 }
9112}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009113
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114#ifdef FEAT_FIND_ID
9115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009116ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117{
9118 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9119 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009120 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121}
9122
Bram Moolenaar4033c552017-09-16 20:54:51 +02009123#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124/*
9125 * ":psearch"
9126 */
9127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009128ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129{
9130 g_do_tagpreview = p_pvh;
9131 ex_findpat(eap);
9132 g_do_tagpreview = 0;
9133}
9134#endif
9135
9136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009137ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138{
9139 int whole = TRUE;
9140 long n;
9141 char_u *p;
9142 int action;
9143
9144 switch (cmdnames[eap->cmdidx].cmd_name[2])
9145 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009146 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9148 action = ACTION_GOTO;
9149 else
9150 action = ACTION_SHOW;
9151 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009152 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 action = ACTION_SHOW_ALL;
9154 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009155 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 action = ACTION_GOTO;
9157 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009158 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 action = ACTION_SPLIT;
9160 break;
9161 }
9162
9163 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009164 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 {
9166 n = getdigits(&eap->arg);
9167 eap->arg = skipwhite(eap->arg);
9168 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009169 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 {
9171 whole = FALSE;
9172 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009173 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 if (*p)
9175 {
9176 *p++ = NUL;
9177 p = skipwhite(p);
9178
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009179 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009180 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009181 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009183 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 }
9185 }
9186 if (!eap->skip)
9187 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9188 whole, !eap->forceit,
9189 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009190 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191}
9192#endif
9193
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194
Bram Moolenaar4033c552017-09-16 20:54:51 +02009195#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196/*
9197 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9198 */
9199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009200ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009202 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9204}
9205
9206/*
9207 * ":pedit"
9208 */
9209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009210ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211{
9212 win_T *curwin_save = curwin;
9213
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009214 if (ERROR_IF_ANY_POPUP_WINDOW)
9215 return;
9216
Bram Moolenaar026587b2019-08-17 15:08:00 +02009217 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009219 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009220
Bram Moolenaar026587b2019-08-17 15:08:00 +02009221 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009223
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 if (curwin != curwin_save && win_valid(curwin_save))
9225 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009226 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009228 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 win_enter(curwin_save, TRUE);
9230 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009231# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009232 else if (WIN_IS_POPUP(curwin))
9233 {
9234 // can't keep focus in popup window
9235 win_enter(firstwin, TRUE);
9236 }
9237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 g_do_tagpreview = 0;
9239}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241
9242/*
9243 * ":stag", ":stselect" and ":stjump".
9244 */
9245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009246ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247{
9248 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009249 postponed_split_flags = cmdmod.cmod_split;
9250 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9252 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009253 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255
9256/*
9257 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9258 */
9259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009260ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261{
9262 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9263}
9264
9265 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009266ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267{
9268 int cmd;
9269
9270 switch (name[1])
9271 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009272 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009274 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009276 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009278 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009280 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009282 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009284 case 'f': // ":tfirst"
9285 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009287 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009289 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009291 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009293 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 return;
9295 }
9296#endif
9297 cmd = DT_TAG;
9298 break;
9299 }
9300
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009301 if (name[0] == 'l')
9302 {
9303#ifndef FEAT_QUICKFIX
9304 ex_ni(eap);
9305 return;
9306#else
9307 cmd = DT_LTAG;
9308#endif
9309 }
9310
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9312 eap->forceit, TRUE);
9313}
9314
John Marriotted908f72024-04-18 22:46:56 +02009315enum {
9316 SPEC_PERC = 0,
9317 SPEC_HASH,
9318 SPEC_CWORD, // cursor word
9319 SPEC_CCWORD, // cursor WORD
9320 SPEC_CEXPR, // expr under cursor
9321 SPEC_CFILE, // cursor path name
9322 SPEC_SFILE, // ":so" file name
9323 SPEC_SLNUM, // ":so" file line number
9324 SPEC_STACK, // call stack
9325 SPEC_SCRIPT, // script file name
9326 SPEC_AFILE, // autocommand file name
9327 SPEC_ABUF, // autocommand buffer number
9328 SPEC_AMATCH, // autocommand match name
9329 SPEC_SFLNUM, // script file line number
9330 SPEC_SID // script ID: <SNR>123_
9331#ifdef FEAT_CLIENTSERVER
9332 , SPEC_CLIENT
9333#endif
9334};
9335
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009337 * Check "str" for starting with a special cmdline variable.
9338 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9339 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9340 */
9341 int
Mike Williams51024bb2024-05-30 07:46:30 +02009342find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009343{
John Marriotted908f72024-04-18 22:46:56 +02009344 // must be sorted by the 'value' field because it is used by bsearch()!
9345 static keyvalue_T spec_str_tab[] = {
9346 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9347 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9348 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9349 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9350 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9351 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9352 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009353#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009354 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009355#endif
John Marriotted908f72024-04-18 22:46:56 +02009356 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9357 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9358 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9359 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9360 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9361 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009362 };
John Marriotted908f72024-04-18 22:46:56 +02009363 keyvalue_T target;
9364 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009365
John Marriotted908f72024-04-18 22:46:56 +02009366 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009367 {
John Marriotted908f72024-04-18 22:46:56 +02009368 case '%':
9369 *usedlen = 1;
9370 return SPEC_PERC;
9371
9372 case '#':
9373 *usedlen = 1;
9374 return SPEC_HASH;
9375
9376 case '<':
9377 target.key = 0;
9378 target.value = (char *)src + 1; // skip over '<'
9379 target.length = 0; // not used, see cmp_keyvalue_value_n()
9380
9381 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab, ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]), cmp_keyvalue_value_n);
9382 if (entry == NULL)
9383 return -1;
9384
9385 *usedlen = entry->length + 1;
9386 return entry->key;
9387
9388 default:
9389 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009390 }
John Marriotted908f72024-04-18 22:46:56 +02009391
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009392 return -1;
9393}
9394
9395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 * Evaluate cmdline variables.
9397 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009398 * change "%" to curbuf->b_ffname
9399 * "#" to curwin->w_alt_fnum
9400 * "%%" to curwin->w_alt_fnum in Vim9 script
9401 * "<cword>" to word under the cursor
9402 * "<cWORD>" to WORD under the cursor
9403 * "<cexpr>" to C-expression under the cursor
9404 * "<cfile>" to path name under the cursor
9405 * "<sfile>" to sourced file name
9406 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009407 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009408 * "<slnum>" to sourced file line number
9409 * "<afile>" to file name for autocommand
9410 * "<abuf>" to buffer number for autocommand
9411 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 *
9413 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9414 * "" for error without a message) and NULL is returned.
9415 * Returns an allocated string if a valid match was found.
9416 * Returns NULL if no match was found. "usedlen" then still contains the
9417 * number of characters to skip.
9418 */
9419 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009420eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009421 char_u *src, // pointer into commandline
9422 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009423 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009424 linenr_T *lnump, // line number for :e command, or NULL
9425 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009426 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009427 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009428 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429{
9430 int i;
9431 char_u *s;
9432 char_u *result;
9433 char_u *resultbuf = NULL;
9434 int resultlen;
9435 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009436 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009438 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441
9442 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009443 if (escaped != NULL)
9444 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445
9446 /*
9447 * Check if there is something to do.
9448 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009449 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009450 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 {
9452 *usedlen = 1;
9453 return NULL;
9454 }
9455
9456 /*
9457 * Skip when preceded with a backslash "\%" and "\#".
9458 * Note: In "\\%" the % is also not recognized!
9459 */
9460 if (src > srcstart && src[-1] == '\\')
9461 {
9462 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009463 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 return NULL;
9465 }
9466
9467 /*
9468 * word or WORD under cursor
9469 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009470 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9471 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009473 resultlen = find_ident_under_cursor(&result,
9474 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9475 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9476 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 if (resultlen == 0)
9478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009479 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 return NULL;
9481 }
9482 }
9483
9484 /*
9485 * '#': Alternate file name
9486 * '%': Current file name
9487 * File name under the cursor
9488 * File name for autocommand
9489 * and following modifiers
9490 */
9491 else
9492 {
Mike Williams51024bb2024-05-30 07:46:30 +02009493 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009494
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 switch (spec_idx)
9496 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009497 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009498#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009499 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009502 // '%': current file
9503 if (curbuf->b_fname == NULL)
9504 {
9505 result = (char_u *)"";
9506 valid = 0; // Must have ":p:h" to be valid
9507 }
9508 else
9509 {
9510 result = curbuf->b_fname;
9511 tilde_file = STRCMP(result, "~") == 0;
9512 }
9513 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009515#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009516 // "%%" alternate file
9517 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009518#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009519 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009520 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009521 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009523 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 result = arg_all();
9525 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009526 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009527 if (escaped != NULL)
9528 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 break;
9531 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009532 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009533 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009534 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009536 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009537 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009538 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009539 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009541 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009543 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009544 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009545 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009546 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009547 return NULL;
9548 }
9549#ifdef FEAT_EVAL
9550 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9551 (long)i);
9552 if (result == NULL)
9553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009554 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009555 return NULL;
9556 }
9557#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009558 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 }
9562 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009563 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009564 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9565 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009566 buf = buflist_findnr(i);
9567 if (buf == NULL)
9568 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009569 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009570 return NULL;
9571 }
9572 if (lnump != NULL)
9573 *lnump = ECMD_LAST;
9574 if (buf->b_fname == NULL)
9575 {
9576 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009577 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009578 }
9579 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009580 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009581 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009582 tilde_file = STRCMP(result, "~") == 0;
9583 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 break;
9586
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009587 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009588 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 if (result == NULL)
9590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009591 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 return NULL;
9593 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009594 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009597 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009599 if (result != NULL && !autocmd_fname_full)
9600 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009601 // Still need to turn the fname into a full path. It is
9602 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009603 autocmd_fname_full = TRUE;
9604 result = FullName_save(autocmd_fname, FALSE);
9605 vim_free(autocmd_fname);
9606 autocmd_fname = result;
9607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 if (result == NULL)
9609 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009610 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 return NULL;
9612 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009613 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 break;
9615
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009616 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 if (autocmd_bufnr <= 0)
9618 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009619 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 return NULL;
9621 }
9622 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9623 result = strbuf;
9624 break;
9625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009626 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 result = autocmd_match;
9628 if (result == NULL)
9629 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009630 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 return NULL;
9632 }
9633 break;
9634
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009635 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009636 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 if (result == NULL)
9638 {
LemonBoy6013d002022-04-09 21:42:10 +01009639 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9640 return NULL;
9641 }
9642 resultbuf = result; // remember allocated string
9643 break;
9644 case SPEC_STACK: // call stack
9645 result = estack_sfile(ESTACK_STACK);
9646 if (result == NULL)
9647 {
9648 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9649 return NULL;
9650 }
9651 resultbuf = result; // remember allocated string
9652 break;
9653 case SPEC_SCRIPT: // script file name
9654 result = estack_sfile(ESTACK_SCRIPT);
9655 if (result == NULL)
9656 {
9657 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 return NULL;
9659 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009660 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009662
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009663 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009664 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009665 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009666 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009667 return NULL;
9668 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009669 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009670 result = strbuf;
9671 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009672
9673#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009674 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009675 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009676 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009677 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009678 return NULL;
9679 }
9680 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009681 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009682 result = strbuf;
9683 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009684
Bram Moolenaar90944302020-08-01 20:45:11 +02009685 case SPEC_SID:
9686 if (current_sctx.sc_sid <= 0)
9687 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009688 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009689 return NULL;
9690 }
9691 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9692 result = strbuf;
9693 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009694#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009695
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009696#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009697 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009698 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9699 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 result = strbuf;
9701 break;
9702#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009703
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009704 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009705 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009706 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 }
9708
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009709 resultlen = (int)STRLEN(result); // length of new string
9710 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 {
9712 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 resultlen = (int)(s - result);
9715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 else if (!skip_mod)
9717 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009718 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 &resultlen);
9720 if (result == NULL)
9721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009722 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 return NULL;
9724 }
9725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 }
9727
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009728 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009730 if (empty_is_error)
9731 {
9732 if (valid != VALID_HEAD + VALID_PATH)
9733 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9734 else
9735 *errormsg = _(e_evaluates_to_an_empty_string);
9736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 result = NULL;
9738 }
9739 else
9740 result = vim_strnsave(result, resultlen);
9741 vim_free(resultbuf);
9742 return result;
9743}
9744
9745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 * Expand the <sfile> string in "arg".
9747 *
9748 * Returns an allocated string, or NULL for any error.
9749 */
9750 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009751expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009753 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +02009755 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +02009757 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +02009759 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +02009760 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 char_u *p;
9762
John Marriotted908f72024-04-18 22:46:56 +02009763 resultlen = STRLEN(arg);
9764 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 if (result == NULL)
9766 return NULL;
9767
9768 for (p = result; *p; )
9769 {
9770 if (STRNCMP(p, "<sfile>", 7) != 0)
9771 ++p;
9772 else
9773 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009774 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009775 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 if (errormsg != NULL)
9777 {
9778 if (*errormsg)
9779 emsg(errormsg);
9780 vim_free(result);
9781 return NULL;
9782 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009783 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 {
9785 p += srclen;
9786 continue;
9787 }
John Marriotted908f72024-04-18 22:46:56 +02009788 repllen = STRLEN(repl);
9789 resultlen += repllen - srclen;
9790 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 if (newres == NULL)
9792 {
9793 vim_free(repl);
9794 vim_free(result);
9795 return NULL;
9796 }
John Marriotted908f72024-04-18 22:46:56 +02009797 len = p - result;
9798 mch_memmove(newres, result, len);
9799 STRCPY(newres + len, repl);
9800 len += repllen;
9801 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 vim_free(repl);
9803 vim_free(result);
9804 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009805 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 }
9807 }
9808
9809 return result;
9810}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009813/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009814 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009815 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009818dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 if (fname == NULL)
9821 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009822 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823}
9824#endif
9825
9826/*
9827 * ":behave {mswin,xterm}"
9828 */
9829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009830ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831{
9832 if (STRCMP(eap->arg, "mswin") == 0)
9833 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009834 set_option_value_give_err((char_u *)"selection",
9835 0L, (char_u *)"exclusive", 0);
9836 set_option_value_give_err((char_u *)"selectmode",
9837 0L, (char_u *)"mouse,key", 0);
9838 set_option_value_give_err((char_u *)"mousemodel",
9839 0L, (char_u *)"popup", 0);
9840 set_option_value_give_err((char_u *)"keymodel",
9841 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 }
9843 else if (STRCMP(eap->arg, "xterm") == 0)
9844 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009845 set_option_value_give_err((char_u *)"selection",
9846 0L, (char_u *)"inclusive", 0);
9847 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9848 set_option_value_give_err((char_u *)"mousemodel",
9849 0L, (char_u *)"extend", 0);
9850 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 }
9852 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009853 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854}
9855
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856static int filetype_detect = FALSE;
9857static int filetype_plugin = FALSE;
9858static int filetype_indent = FALSE;
9859
9860/*
9861 * ":filetype [plugin] [indent] {on,off,detect}"
9862 * on: Load the filetype.vim file to install autocommands for file types.
9863 * off: Load the ftoff.vim file to remove all autocommands for file types.
9864 * plugin on: load filetype.vim and ftplugin.vim
9865 * plugin off: load ftplugof.vim
9866 * indent on: load filetype.vim and indent.vim
9867 * indent off: load indoff.vim
9868 */
9869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009870ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871{
9872 char_u *arg = eap->arg;
9873 int plugin = FALSE;
9874 int indent = FALSE;
9875
9876 if (*eap->arg == NUL)
9877 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009878 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009879 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 filetype_detect ? "ON" : "OFF",
9881 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9882 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9883 return;
9884 }
9885
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009886 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 for (;;)
9888 {
9889 if (STRNCMP(arg, "plugin", 6) == 0)
9890 {
9891 plugin = TRUE;
9892 arg = skipwhite(arg + 6);
9893 continue;
9894 }
9895 if (STRNCMP(arg, "indent", 6) == 0)
9896 {
9897 indent = TRUE;
9898 arg = skipwhite(arg + 6);
9899 continue;
9900 }
9901 break;
9902 }
9903 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9904 {
9905 if (*arg == 'o' || !filetype_detect)
9906 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009907 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 filetype_detect = TRUE;
9909 if (plugin)
9910 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009911 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 filetype_plugin = TRUE;
9913 }
9914 if (indent)
9915 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009916 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 filetype_indent = TRUE;
9918 }
9919 }
9920 if (*arg == 'd')
9921 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009922 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009923 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 }
9925 }
9926 else if (STRCMP(arg, "off") == 0)
9927 {
9928 if (plugin || indent)
9929 {
9930 if (plugin)
9931 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009932 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 filetype_plugin = FALSE;
9934 }
9935 if (indent)
9936 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009937 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 filetype_indent = FALSE;
9939 }
9940 }
9941 else
9942 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009943 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 filetype_detect = FALSE;
9945 }
9946 }
9947 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009948 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949}
9950
9951/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009952 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 */
9954 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009955ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956{
zeertzjq5bf6c212024-03-31 18:41:27 +02009957 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009958 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009959
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009960 char_u *arg = eap->arg;
9961 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9962 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009963
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009964 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9965 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +02009966 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009970ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971{
9972#ifdef FEAT_DIGRAPHS
9973 if (*eap->arg != NUL)
9974 putdigraph(eap->arg);
9975 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009976 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977#else
mityu61065042021-07-19 20:07:21 +02009978 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979#endif
9980}
9981
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009982#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9983 void
9984set_no_hlsearch(int flag)
9985{
9986 no_hlsearch = flag;
9987# ifdef FEAT_EVAL
9988 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9989# endif
9990}
9991
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992/*
9993 * ":nohlsearch"
9994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009996ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009998 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009999 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001#endif
10002
10003#ifdef FEAT_CRYPT
10004/*
10005 * ":X": Get crypt key
10006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010008ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010010 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010011 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012}
10013#endif
10014
10015#ifdef FEAT_FOLDING
10016 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010017ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018{
10019 if (foldManualAllowed(TRUE))
10020 foldCreate(eap->line1, eap->line2);
10021}
10022
10023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010024ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025{
10026 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10027 eap->forceit, FALSE);
10028}
10029
10030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010031ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032{
10033 linenr_T lnum;
10034
Bram Moolenaar4facea32019-10-12 20:17:40 +020010035# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010036 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010037# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010038
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010039 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10041 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10042 ml_setmarked(lnum);
10043
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010044 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010046 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010047# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010048 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050}
10051#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010053#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010054/*
10055 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10056 * instead of a quickfix command.
10057 */
10058 int
10059is_loclist_cmd(int cmdidx)
10060{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010061 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010062 return FALSE;
10063 return cmdnames[cmdidx].cmd_name[0] == 'l';
10064}
10065#endif
10066
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010067 int
10068get_pressedreturn(void)
10069{
10070 return ex_pressedreturn;
10071}
10072
10073 void
10074set_pressedreturn(int val)
10075{
10076 ex_pressedreturn = val;
10077}