blob: 2a59301548268f7769cba77e8e037540da251da4 [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 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002919 if (*eap->cmd == NUL)
2920 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002921 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002922 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002923 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002924 if (vim9script && has_cmdmod(cmod, FALSE))
2925 *errormsg = _(e_command_modifier_without_command);
2926 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002927 return FAIL;
2928 }
2929
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002930 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002931
2932 // In Vim9 script a variable can shadow a command modifier:
2933 // verbose = 123
2934 // verbose += 123
2935 // silent! verbose = func()
2936 // verbose.member = 2
2937 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002938 // But not:
2939 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002940 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002941 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002942 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002943
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002944 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002945 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002946 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002947 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2948 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002949 break;
2950 }
2951
Bram Moolenaareffed932018-08-14 13:38:17 +02002952 switch (*p)
2953 {
John Marriotted908f72024-04-18 22:46:56 +02002954 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002955 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002956 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002957 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 continue;
2959
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002960 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002961 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002962 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 continue;
2964 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002965 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002966 {
2967#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002968 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002969#endif
2970 continue;
2971 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002972 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002973 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002974 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002975 continue;
2976
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002977 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002978 break;
2979#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002980 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002981#endif
2982 continue;
2983
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002984 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002985 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002986 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 continue;
2988 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002989 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002990 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002991 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002992 continue;
2993 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002994 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002995 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002996 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 continue;
2998 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002999 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003000 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003001 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 continue;
3003
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003004 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003005 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003006 char_u *reg_pat;
3007 char_u *nulp = NULL;
3008 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003009
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003010 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003011 || *p == NUL
3012 || (ends_excmd(*p)
3013#ifdef FEAT_EVAL
3014 // in ":filter #pat# cmd" # does not
3015 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003016 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003017#endif
3018 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003019 break;
3020 if (*p == '!')
3021 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003022 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003023 p = skipwhite(p + 1);
3024 if (*p == NUL || ends_excmd(*p))
3025 break;
3026 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003027#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003028 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003029 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003030 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003031#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003032 if (skip_only)
3033 p = skip_vimgrep_pat(p, NULL, NULL);
3034 else
3035 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003036 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3037 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003038 if (p == NULL || *p == NUL)
3039 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003040 if (!skip_only)
3041 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003042 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003043 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003044 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003045 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003046 // restore the character overwritten by NUL
3047 if (nulp != NULL)
3048 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003049 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003050 eap->cmd = p;
3051 continue;
3052 }
3053
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003054 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3055 {
3056 cmod->cmod_split |= WSP_HOR;
3057 continue;
3058 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003059 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003060 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003061 || *p == NUL || ends_excmd(*p))
3062 break;
3063 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003064 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003065 continue;
3066
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003067 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003068 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003069 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003070 continue;
3071 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003072 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3073 {
3074 if (ends_excmd2(p, eap->cmd))
3075 {
3076 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003077 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003078 return FAIL;
3079 }
3080 cmod->cmod_flags |= CMOD_LEGACY;
3081 continue;
3082 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003083
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003084 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003085 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003086 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003087 continue;
3088
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003089 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003090 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003091 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003092 continue;
3093 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003094 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003095 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003096 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003097 continue;
3098
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003099 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003100 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003101 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003102 continue;
3103
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003104 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003105 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003106 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003107 continue;
3108 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003109 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003110 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003111 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003112 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3113 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003114 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003115 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003116 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003117 }
3118 continue;
3119
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003120 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003121 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003122 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003123 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003124 long tabnr = get_address(eap, &eap->cmd,
3125 ADDR_TABS, eap->skip,
3126 skip_only, FALSE, 1);
3127 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003128 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003129 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003130 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003131 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3132 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003133 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003134 return FAIL;
3135 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003136 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003137 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003138 }
3139 eap->cmd = p;
3140 continue;
3141 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003142 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003143 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003144 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003145 continue;
3146
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003147 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003148 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003149 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003150 continue;
3151
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003152 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003153 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003154 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003155 continue;
3156 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003157 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003158 {
3159 if (ends_excmd2(p, eap->cmd))
3160 {
3161 *errormsg =
3162 _(e_vim9cmd_must_be_followed_by_command);
3163 return FAIL;
3164 }
3165 cmod->cmod_flags |= CMOD_VIM9CMD;
3166 continue;
3167 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003168 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003169 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003170 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003171 {
zeertzjqcd749632022-06-14 13:30:35 +01003172 // zero means not set, one is verbose == 0, etc.
3173 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003174 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003175 else
zeertzjqcd749632022-06-14 13:30:35 +01003176 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003177 eap->cmd = p;
3178 continue;
3179 }
3180 break;
3181 }
3182
Bram Moolenaar1501b632022-03-27 16:56:21 +01003183 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003184 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003185 if (eap->cmd > cmd_start)
3186 {
3187 // Move the '<,'> range to after the modifiers and insert a colon.
3188 // Since the modifiers have been parsed put the colon on top of the
3189 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3190 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003191 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003192 {
3193 size_t len = STRLEN(cmd_start);
3194
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003195 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003196 // "'<,'>mods" -> "mods *+
3197 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003198 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003199 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003200 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003201 }
3202 else
3203 {
3204 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3205 eap->cmd -= 5;
3206 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3207 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003208 }
3209 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003210 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003211 // Special case: change empty command to "+".
3212 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003213 eap->cmd = (char_u *)"'<,'>+";
3214 else
3215 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003216 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003217 else if (use_plus_cmd)
zeertzjq7d664bf2024-07-14 10:22:54 +02003218 eap->cmd = (char_u *)exmode_plus;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003219
Bram Moolenaareffed932018-08-14 13:38:17 +02003220 return OK;
3221}
3222
3223/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003224 * Return TRUE if "cmod" has anything set.
3225 */
3226 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003227has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003228{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003229 return (cmod->cmod_flags != 0 && (!ignore_silent
3230 || (cmod->cmod_flags
3231 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003232 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003233 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003234 || cmod->cmod_tab != 0
3235 || cmod->cmod_filter_regmatch.regprog != NULL;
3236}
3237
Bram Moolenaar8991be22022-02-14 21:51:46 +00003238#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003239/*
3240 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3241 */
3242 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003243cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003244{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003245 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003246 {
3247 emsg(_(e_misplaced_command_modifier));
3248 return TRUE;
3249 }
3250 return FALSE;
3251}
Dominique Pelle748b3082022-01-08 12:41:16 +00003252#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003253
3254/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003255 * Apply the command modifiers. Saves current state in "cmdmod", call
3256 * undo_cmdmod() later.
3257 */
3258 void
3259apply_cmdmod(cmdmod_T *cmod)
3260{
3261#ifdef HAVE_SANDBOX
3262 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3263 {
3264 ++sandbox;
3265 cmod->cmod_did_sandbox = TRUE;
3266 }
3267#endif
zeertzjqcd749632022-06-14 13:30:35 +01003268 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003269 {
3270 if (cmod->cmod_verbose_save == 0)
3271 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003272 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003273 }
3274
3275 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3276 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003277 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003278 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003279 cmod->cmod_save_msg_scroll = msg_scroll;
3280 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003281 if (cmod->cmod_flags & CMOD_SILENT)
3282 ++msg_silent;
3283 if (cmod->cmod_flags & CMOD_UNSILENT)
3284 msg_silent = 0;
3285
3286 if (cmod->cmod_flags & CMOD_ERRSILENT)
3287 {
3288 ++emsg_silent;
3289 ++cmod->cmod_did_esilent;
3290 }
3291
Bram Moolenaare1004402020-10-24 20:49:43 +02003292 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003293 {
3294 // Set 'eventignore' to "all".
3295 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003296 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003297 set_string_option_direct((char_u *)"ei", -1,
3298 (char_u *)"all", OPT_FREE, SID_NONE);
3299 }
3300}
3301
3302/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003303 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003304 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003305 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003306undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003307{
Bram Moolenaare1004402020-10-24 20:49:43 +02003308 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003309 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003310 p_verbose = cmod->cmod_verbose_save - 1;
3311 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003312 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003313
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003314#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003315 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003316 {
3317 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003318 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003319 }
3320#endif
3321
Bram Moolenaare1004402020-10-24 20:49:43 +02003322 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003323 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003324 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003325 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3326 OPT_FREE, SID_NONE);
3327 free_string_option(cmod->cmod_save_ei);
3328 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003329 }
3330
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003331 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003332
Bram Moolenaare1004402020-10-24 20:49:43 +02003333 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003334 {
3335 // messages could be enabled for a serious error, need to check if the
3336 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003337 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3338 msg_silent = cmod->cmod_save_msg_silent - 1;
3339 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003340 if (emsg_silent < 0)
3341 emsg_silent = 0;
3342 // Restore msg_scroll, it's set by file I/O commands, even when no
3343 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003344 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003345
3346 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3347 // somewhere in the line. Put it back in the first column.
3348 if (redirecting())
3349 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003350
Bram Moolenaare1004402020-10-24 20:49:43 +02003351 cmod->cmod_save_msg_silent = 0;
3352 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003353 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003354}
3355
3356/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003357 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003358 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003359 * Return FAIL and set "errormsg" or return OK.
3360 */
3361 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003362parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003363{
3364 int address_count = 1;
3365 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003366 int need_check_cursor = FALSE;
3367 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003368
3369 // Repeat for all ',' or ';' separated addresses.
3370 for (;;)
3371 {
3372 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003373 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003374 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003375 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003376 eap->addr_count == 0, address_count++);
3377 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003378 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003379 if (lnum == MAXLNUM)
3380 {
3381 if (*eap->cmd == '%') // '%' - all lines
3382 {
3383 ++eap->cmd;
3384 switch (eap->addr_type)
3385 {
3386 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003387 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003388 eap->line1 = 1;
3389 eap->line2 = curbuf->b_ml.ml_line_count;
3390 break;
3391 case ADDR_LOADED_BUFFERS:
3392 {
3393 buf_T *buf = firstbuf;
3394
3395 while (buf->b_next != NULL
3396 && buf->b_ml.ml_mfp == NULL)
3397 buf = buf->b_next;
3398 eap->line1 = buf->b_fnum;
3399 buf = lastbuf;
3400 while (buf->b_prev != NULL
3401 && buf->b_ml.ml_mfp == NULL)
3402 buf = buf->b_prev;
3403 eap->line2 = buf->b_fnum;
3404 break;
3405 }
3406 case ADDR_BUFFERS:
3407 eap->line1 = firstbuf->b_fnum;
3408 eap->line2 = lastbuf->b_fnum;
3409 break;
3410 case ADDR_WINDOWS:
3411 case ADDR_TABS:
3412 if (IS_USER_CMDIDX(eap->cmdidx))
3413 {
3414 eap->line1 = 1;
3415 eap->line2 = eap->addr_type == ADDR_WINDOWS
3416 ? LAST_WIN_NR : LAST_TAB_NR;
3417 }
3418 else
3419 {
3420 // there is no Vim command which uses '%' and
3421 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003422 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003423 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003424 }
3425 break;
3426 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003427 case ADDR_UNSIGNED:
3428 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003429 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003430 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003431 case ADDR_ARGUMENTS:
3432 if (ARGCOUNT == 0)
3433 eap->line1 = eap->line2 = 0;
3434 else
3435 {
3436 eap->line1 = 1;
3437 eap->line2 = ARGCOUNT;
3438 }
3439 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003440 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003441#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003442 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003443 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003444 if (eap->line2 == 0)
3445 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003446#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003447 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003448 case ADDR_NONE:
3449 // Will give an error later if a range is found.
3450 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003451 }
3452 ++eap->addr_count;
3453 }
3454 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3455 {
3456 pos_T *fp;
3457
3458 // '*' - visual area
3459 if (eap->addr_type != ADDR_LINES)
3460 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003461 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003462 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003463 }
3464
3465 ++eap->cmd;
3466 if (!eap->skip)
3467 {
3468 fp = getmark('<', FALSE);
3469 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003470 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003471 eap->line1 = fp->lnum;
3472 fp = getmark('>', FALSE);
3473 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003474 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003475 eap->line2 = fp->lnum;
3476 ++eap->addr_count;
3477 }
3478 }
3479 }
3480 else
3481 eap->line2 = lnum;
3482 eap->addr_count++;
3483
3484 if (*eap->cmd == ';')
3485 {
3486 if (!eap->skip)
3487 {
3488 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003489
Bram Moolenaar0e717042020-04-27 19:29:01 +02003490 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003491 // accept zero as address, so 0;/PATTERN/ works correctly
3492 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003493 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003494 if (eap->line2 > 0)
3495 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003496 else
3497 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003498 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003499 }
3500 }
3501 else if (*eap->cmd != ',')
3502 break;
3503 ++eap->cmd;
3504 }
3505
3506 // One address given: set start and end lines.
3507 if (eap->addr_count == 1)
3508 {
3509 eap->line1 = eap->line2;
3510 // ... but only implicit: really no address given
3511 if (lnum == MAXLNUM)
3512 eap->addr_count = 0;
3513 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003514 ret = OK;
3515
3516theend:
3517 if (need_check_cursor)
3518 check_cursor();
3519 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003520}
3521
3522/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003523 * Append "cmd" to the error message in IObuff.
3524 * Takes care of limiting the length and handling 0xa0, which would be
3525 * invisible otherwise.
3526 */
3527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003528append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003529{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003530 size_t len = STRLEN(IObuff);
3531 char_u *s = cmd;
3532 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003533
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003534 if (len > IOSIZE - 100)
3535 {
3536 // Not enough space, truncate and put in "...".
3537 d = IObuff + IOSIZE - 100;
3538 d -= mb_head_off(IObuff, d);
3539 STRCPY(d, "...");
3540 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003541 STRCAT(IObuff, ": ");
3542 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003543 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003544 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003545 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003546 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003547 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003548 STRCPY(d, "<a0>");
3549 d += 4;
3550 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003551 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3552 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003553 else
3554 MB_COPY_CHAR(s, d);
3555 }
3556 *d = NUL;
3557}
3558
Dominique Pelle748b3082022-01-08 12:41:16 +00003559#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003560/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003561 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3562 * the name. Otherwise just return "start".
3563 */
3564 char_u *
3565skip_option_env_lead(char_u *start)
3566{
3567 char_u *name = start;
3568
3569 if (*start == '&')
3570 {
3571 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3572 name += 3;
3573 else
3574 name += 1;
3575 }
3576 else if (*start == '$')
3577 name += 1;
3578 return name;
3579}
Dominique Pelle748b3082022-01-08 12:41:16 +00003580#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003581
3582/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003583 * Return TRUE and set "*idx" if "p" points to a one letter command.
3584 * If not in Vim9 script:
3585 * - The 'k' command can directly be followed by any character.
3586 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3587 * but :sre[wind] is another command, as are :scr[iptnames],
3588 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3589 */
3590 static int
3591one_letter_cmd(char_u *p, cmdidx_T *idx)
3592{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003593 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003594 return FALSE;
3595 if (*p == 'k')
3596 {
3597 *idx = CMD_k;
3598 return TRUE;
3599 }
3600 if (p[0] == 's'
3601 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3602 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3603 || p[1] == 'g'
3604 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3605 || p[1] == 'I'
3606 || (p[1] == 'r' && p[2] != 'e')))
3607 {
3608 *idx = CMD_substitute;
3609 return TRUE;
3610 }
3611 return FALSE;
3612}
3613
Dominique Pellee764d1b2023-03-12 21:20:59 +00003614#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003615/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003616 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3617 * call.
3618 */
3619 int
3620number_method(char_u *cmd)
3621{
3622 char_u *p = skipdigits(cmd);
3623
3624 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3625}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003626#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003627
3628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003630 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003631 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003632 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 *
3634 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3635 * assignment without "let". Sets eap->cmdidx to the command while returning
3636 * "eap->cmd".
3637 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 * Returns NULL for an ambiguous user command.
3639 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 char_u *
3641find_ex_command(
3642 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003643 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003644 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003645 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 int len;
3648 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003649 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003650#ifndef FEAT_EVAL
3651 int vim9 = FALSE;
3652#else
3653 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 /*
3656 * Recognize a Vim9 script function/method call and assignment:
3657 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3658 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003659 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003660 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003662 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003664 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003665 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3666 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003668 int oplen;
3669 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003670 char_u *swp;
3671
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003672 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003673 || (eap->cmd[0] == '$'
3674 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003675 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003676 && (valid_yank_reg(eap->cmd[1], FALSE)
3677 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003678 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003679 if (*eap->cmd == '&')
3680 {
3681 p = eap->cmd + 1;
3682 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3683 p += 2;
3684 p = to_name_end(p, FALSE);
3685 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003686 else if (*eap->cmd == '$')
3687 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003688 else
3689 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003690 if (ends_excmd(*skipwhite(p)))
3691 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003692 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3693 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003694 eap->cmdidx = CMD_eval;
3695 return eap->cmd;
3696 }
3697 // "&option" can be followed by "->" or "=", check below
3698 }
3699
3700 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003702 if (
3703 // "(..." is an expression.
3704 // "funcname(" is always a function call.
3705 *p == '('
3706 || (p == eap->cmd
3707 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003708 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003709 *eap->cmd == '{'
3710 // "'string'->func()" is an expression.
3711 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003712 // '"string"->func()' is an expression.
3713 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003714 // '$"string"->func()' is an expression.
3715 // "$'string'->func()" is an expression.
3716 || (eap->cmd[0] == '$'
3717 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3718 // '0z1234->func()' is an expression.
3719 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003720 // "g:varname" is an expression.
3721 || eap->cmd[1] == ':'
3722 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003723 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003724 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003725 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003726 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3727 {
3728 // "{" by itself is the start of a block.
3729 eap->cmdidx = CMD_block;
3730 return eap->cmd + 1;
3731 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003732 eap->cmdidx = CMD_eval;
3733 return eap->cmd;
3734 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003735
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003736 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003737 // "varname[]" is an expression.
3738 *p == '['
3739 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003740 || (*p == '.'
3741 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3742 // g:[key] is an expression
3743 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003744 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003745 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003746
3747 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003748 // Skip over the whole thing, it can be:
3749 // name.member = val
3750 // name[a : b] = val
3751 // name[idx] = val
3752 // name[idx].member = val
3753 // etc.
3754 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003755 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003756 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003757 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003758 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003759 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003760 || (after[0] == '.' && after[1] == '.'
3761 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003762 eap->cmdidx = CMD_var;
3763 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003764 --emsg_silent;
3765 return eap->cmd;
3766 }
3767
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003768 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3769 // an assignment.
3770 // If there is no line break inside the "[...]" then "p" is
3771 // advanced to after the "]" by to_name_const_end(): check if a "="
3772 // follows.
3773 // If "[...]" has a line break "p" still points at the "[" and it
3774 // can't be an assignment.
3775 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003776 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003777 char_u *eq;
3778
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003779 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003780 if (p == eap->cmd && *p == '[')
3781 {
3782 int count = 0;
3783 int semicolon = FALSE;
3784
3785 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3786 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003787 eq = p;
3788 if (eq != NULL)
3789 {
3790 eq = skipwhite(eq);
3791 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3792 ++eq;
3793 }
3794 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003795 {
3796 eap->cmdidx = CMD_eval;
3797 return eap->cmd;
3798 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003799 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003800 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003801 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003802 return eap->cmd;
3803 }
3804 }
3805
3806 // Recognize an assignment if we recognize the variable name:
3807 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003808 // "@r = expr"
3809 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003810 // "var = expr" where "var" is a variable name or we are skipping
3811 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003812 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003813 oplen = assignment_len(skipwhite(p), &heredoc);
3814 if (oplen > 0)
3815 {
3816 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3817 || *eap->cmd == '&'
3818 || *eap->cmd == '$'
3819 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003820 || (eap->skip && IS_WHITE_OR_NUL(
3821 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003822 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003823 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003824 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003825 return eap->cmd;
3826 }
3827 }
3828
Bram Moolenaar9510d222022-09-11 15:14:05 +01003829 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003830 // "w:varname: number = 123".
3831 if (eap->cmd[1] == ':' && *p == ':')
3832 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003833 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003834 return eap->cmd;
3835 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003836 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003837
Bram Moolenaar31d99482022-05-26 22:24:43 +01003838 // 1234->func() is a method call
3839 if (number_method(eap->cmd))
3840 {
3841 eap->cmdidx = CMD_eval;
3842 return eap->cmd;
3843 }
3844
Bram Moolenaar7b829262021-10-13 15:04:34 +01003845 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3846 // an expression. A global/substitute/list command needs to use a
3847 // longer name.
3848 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3849 {
3850 eap->cmdidx = CMD_eval;
3851 return eap->cmd;
3852 }
3853
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003854 // If it is an ID it might be a variable with an operator on the next
3855 // line, if the variable exists it can't be an Ex command.
3856 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003857 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003858 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3859 {
3860 eap->cmdidx = CMD_eval;
3861 return eap->cmd;
3862 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003863
3864 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003865 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3866 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003867 {
3868 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3869 return eap->cmd + 2;
3870 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 }
3872#endif
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 /*
3875 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 */
3877 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003878 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 ++p;
3881 }
3882 else
3883 {
3884 while (ASCII_ISALPHA(*p))
3885 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003886 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003887 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003889 while (ASCII_ISALNUM(*p))
3890 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 }
3892 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3893 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003894 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 ++p;
3896 while (ASCII_ISALPHA(*p))
3897 ++p;
3898 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003899
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003900 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003901 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 ++p;
3903 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003904 // The "d" command can directly be followed by 'l' or 'p' flag, when
3905 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003906 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003907 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003908 // Check for ":dl", ":dell", etc. to ":deletel": that's
3909 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003910 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003911 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003912 break;
3913 if (i == len - 1)
3914 {
3915 --len;
3916 if (p[-1] == 'l')
3917 eap->flags |= EXFLAG_LIST;
3918 else
3919 eap->flags |= EXFLAG_PRINT;
3920 }
3921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003923 if (ASCII_ISLOWER(eap->cmd[0]))
3924 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003925 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003926 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003927
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003928 if (command_count != (int)CMD_SIZE)
3929 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003930 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003931 getout(1);
3932 }
3933
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003934 // Use a precomputed index for fast look-up in cmdnames[]
3935 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003936 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3937 if (ASCII_ISLOWER(c2))
3938 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3939 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003940 else if (ASCII_ISUPPER(eap->cmd[0]))
3941 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003943 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
3945 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3946 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3947 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3948 (size_t)len) == 0)
3949 {
3950#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003951 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 *full = TRUE;
3953#endif
3954 break;
3955 }
3956
Bram Moolenaar204852a2022-03-05 12:56:44 +00003957 // :Print and :mode are not supported in Vim9 script.
3958 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003959 if (vim9 && eap->cmdidx != CMD_SIZE)
3960 {
3961 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3962 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003963 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003964 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003965 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003966 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003967 eap->cmdidx = CMD_SIZE;
3968 }
3969 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003970
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003971 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003972 // 'cpoptions'.
3973 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3974 p = eap->cmd;
3975
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003976 // Look for a user defined command as a last resort. Let ":Print" be
3977 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003978 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3979 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003981 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 while (ASCII_ISALNUM(*p))
3983 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003984 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003986 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 eap->cmdidx = CMD_SIZE;
3988 }
3989
Bram Moolenaar204852a2022-03-05 12:56:44 +00003990 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3991 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003992 eap->cmdidx = CMD_finally;
3993
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003994#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003995 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003996 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02003997 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003998 && (eap->cmdidx < 0 ||
3999 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004000 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004001 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4002
4003 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004004 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004005 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004006 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004007#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004008
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 return p;
4010}
4011
4012#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004013typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004014{
4015 char *name;
4016 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004017 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004018} cmdmod_info_T;
4019
4020static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004021 {"aboveleft", 3, FALSE},
4022 {"belowright", 3, FALSE},
4023 {"botright", 2, FALSE},
4024 {"browse", 3, FALSE},
4025 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004026 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004027 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004028 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004029 {"keepalt", 5, FALSE},
4030 {"keepjumps", 5, FALSE},
4031 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004032 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004033 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004034 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004035 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004036 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004037 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004038 {"rightbelow", 6, FALSE},
4039 {"sandbox", 3, FALSE},
4040 {"silent", 3, FALSE},
4041 {"tab", 3, TRUE},
4042 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004043 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004044 {"verbose", 4, TRUE},
4045 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004046 {"vim9cmd", 4, FALSE}
4047}; // cmdmod_info_tab
4048
Bram Moolenaared53fb92007-11-24 20:50:24 +00004049/*
4050 * Return length of a command modifier (including optional count).
4051 * Return zero when it's not a modifier.
4052 */
4053 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004054modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004055{
Christian Brabandt70a11a62024-07-26 19:13:55 +02004056 int i, j;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004057 char_u *p = cmd;
4058
4059 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004060 p = skipwhite(skipdigits(cmd + 1));
Christian Brabandt70a11a62024-07-26 19:13:55 +02004061 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004062 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004063 for (j = 0; p[j] != NUL; ++j)
4064 if (p[j] != cmdmod_info_tab[i].name[j])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004065 break;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004066 if (!ASCII_ISALPHA(p[j]) && j >= cmdmod_info_tab[i].minlen
4067 && (p == cmd || cmdmod_info_tab[i].has_count))
4068 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004069 }
4070 return 0;
4071}
4072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073/*
4074 * Return > 0 if an Ex command "name" exists.
4075 * Return 2 if there is an exact match.
4076 * Return 3 if there is an ambiguous match.
4077 */
4078 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004079cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080{
4081 exarg_T ea;
4082 int full = FALSE;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004083 int i;
4084 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004085 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
Christian Brabandt70a11a62024-07-26 19:13:55 +02004087 // Check command modifiers.
4088 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004090 for (j = 0; name[j] != NUL; ++j)
4091 if (name[j] != cmdmod_info_tab[i].name[j])
4092 break;
4093 if (name[j] == NUL && j >= cmdmod_info_tab[i].minlen)
4094 return (cmdmod_info_tab[i].name[j] == NUL ? 2 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004097 // Check built-in commands and user defined commands.
4098 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004099 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004101 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004103 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004105 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4106 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004107 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004108 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4110}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004111
4112/*
4113 * "fullcommand" function
4114 */
4115 void
4116f_fullcommand(typval_T *argvars, typval_T *rettv)
4117{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004118 exarg_T ea;
4119 char_u *name;
4120 char_u *p;
4121 int vim9script = in_vim9script();
4122 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004123
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004124 rettv->v_type = VAR_STRING;
4125 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004126
Bram Moolenaaraa534142022-09-15 21:46:02 +01004127 if (in_vim9script()
4128 && (check_for_string_arg(argvars, 0) == FAIL
4129 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004130 return;
4131
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004132 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004133 if (name == NULL)
4134 return;
4135
Bram Moolenaaraa534142022-09-15 21:46:02 +01004136 if (argvars[1].v_type != VAR_UNKNOWN)
4137 {
4138 vim9script = tv_get_bool(&argvars[1]);
4139 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4140 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4141 }
4142
zeertzjqc024ed92022-01-04 16:22:52 +00004143 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004144 name++;
4145 name = skip_range(name, TRUE, NULL);
4146
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004147 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4148 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004149 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004150 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004151 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004152 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004153 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004154 goto theend;
4155
4156 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004157 {
4158 int res;
4159
4160 ++emsg_silent;
4161 res = not_in_vim9(&ea);
4162 --emsg_silent;
4163
4164 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004165 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004166 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004167
4168 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004169 ? get_user_command_name(ea.useridx, ea.cmdidx)
4170 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004171theend:
4172 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004173}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174#endif
4175
Bram Moolenaard0190392019-08-23 21:17:35 +02004176 cmdidx_T
4177excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
Bram Moolenaard0190392019-08-23 21:17:35 +02004179 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004181 if (!one_letter_cmd(cmd, &idx))
4182 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4183 idx = (cmdidx_T)((int)idx + 1))
4184 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4185 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186
Bram Moolenaard0190392019-08-23 21:17:35 +02004187 return idx;
4188}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189
Bram Moolenaard0190392019-08-23 21:17:35 +02004190 long
4191excmd_get_argt(cmdidx_T idx)
4192{
4193 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194}
4195
4196/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004197 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 *
4199 * Backslashed delimiters after / or ? will be skipped, and commands will
4200 * not be expanded between /'s and ?'s or after "'".
4201 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004202 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 * Returns the "cmd" pointer advanced to beyond the range.
4204 */
4205 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004206skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004207 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004208 int skip_star, // skip "*" used for Visual range
4209 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004211 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004212 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004214 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004216 if (*cmd == '\\')
4217 {
4218 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4219 ++cmd;
4220 else
4221 break;
4222 }
4223 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004225 char_u *p = cmd;
4226
4227 // a quote is only valid at the start or after a separator
4228 while (p > cmd_start)
4229 {
4230 --p;
4231 if (!VIM_ISWHITE(*p))
4232 break;
4233 }
4234 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4235 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 if (*++cmd == NUL && ctx != NULL)
4237 *ctx = EXPAND_NOTHING;
4238 }
4239 else if (*cmd == '/' || *cmd == '?')
4240 {
4241 delim = *cmd++;
4242 while (*cmd != NUL && *cmd != delim)
4243 if (*cmd++ == '\\' && *cmd != NUL)
4244 ++cmd;
4245 if (*cmd == NUL && ctx != NULL)
4246 *ctx = EXPAND_NOTHING;
4247 }
4248 if (*cmd != NUL)
4249 ++cmd;
4250 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004251
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004252 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004253 while (*cmd == ':')
4254 cmd = skipwhite(cmd + 1);
4255
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004256 // Skip "*" used for Visual range.
4257 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4258 cmd = skipwhite(cmd + 1);
4259
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 return cmd;
4261}
4262
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004263 static void
4264addr_error(cmd_addr_T addr_type)
4265{
4266 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004267 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004268 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004269 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004270}
4271
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004273 * Return the default address for an address type.
4274 */
4275 static linenr_T
4276default_address(exarg_T *eap)
4277{
4278 linenr_T lnum = 0;
4279
4280 switch (eap->addr_type)
4281 {
4282 case ADDR_LINES:
4283 case ADDR_OTHER:
4284 // Default is the cursor line number. Avoid using an invalid
4285 // line number though.
4286 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4287 lnum = curbuf->b_ml.ml_line_count;
4288 else
4289 lnum = curwin->w_cursor.lnum;
4290 break;
4291 case ADDR_WINDOWS:
4292 lnum = CURRENT_WIN_NR;
4293 break;
4294 case ADDR_ARGUMENTS:
4295 lnum = curwin->w_arg_idx + 1;
4296 if (lnum > ARGCOUNT)
4297 lnum = ARGCOUNT;
4298 break;
4299 case ADDR_LOADED_BUFFERS:
4300 case ADDR_BUFFERS:
4301 lnum = curbuf->b_fnum;
4302 break;
4303 case ADDR_TABS:
4304 lnum = CURRENT_TAB_NR;
4305 break;
4306 case ADDR_TABS_RELATIVE:
4307 case ADDR_UNSIGNED:
4308 lnum = 1;
4309 break;
4310 case ADDR_QUICKFIX:
4311#ifdef FEAT_QUICKFIX
4312 lnum = qf_get_cur_idx(eap);
4313#endif
4314 break;
4315 case ADDR_QUICKFIX_VALID:
4316#ifdef FEAT_QUICKFIX
4317 lnum = qf_get_cur_valid_idx(eap);
4318#endif
4319 break;
4320 case ADDR_NONE:
4321 // Will give an error later if a range is found.
4322 break;
4323 }
4324 return lnum;
4325}
4326
4327/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004328 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 *
4330 * Set ptr to the next character after the part that was interpreted.
4331 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004332 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 *
4334 * Return MAXLNUM when no Ex address was found.
4335 */
4336 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004337get_address(
4338 exarg_T *eap UNUSED,
4339 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004340 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004341 int skip, // only skip the address, don't use it
4342 int silent, // no errors or side effects
4343 int to_other_file, // flag: may jump to other file
4344 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345{
4346 int c;
4347 int i;
4348 long n;
4349 char_u *cmd;
4350 pos_T pos;
4351 pos_T *fp;
4352 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004353 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
4355 cmd = skipwhite(*ptr);
4356 lnum = MAXLNUM;
4357 do
4358 {
4359 switch (*cmd)
4360 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004361 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004362 ++cmd;
4363 switch (addr_type)
4364 {
4365 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004366 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 lnum = curwin->w_cursor.lnum;
4368 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004369 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004370 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004371 break;
4372 case ADDR_ARGUMENTS:
4373 lnum = curwin->w_arg_idx + 1;
4374 break;
4375 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004376 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004377 lnum = curbuf->b_fnum;
4378 break;
4379 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004380 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004381 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004382 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004383 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004384 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004385 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004386 cmd = NULL;
4387 goto error;
4388 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004389 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004390#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004391 lnum = qf_get_cur_idx(eap);
4392#endif
4393 break;
4394 case ADDR_QUICKFIX_VALID:
4395#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004396 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004397#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004398 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004399 }
4400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004402 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004403 ++cmd;
4404 switch (addr_type)
4405 {
4406 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004407 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 lnum = curbuf->b_ml.ml_line_count;
4409 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004410 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004411 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004412 break;
4413 case ADDR_ARGUMENTS:
4414 lnum = ARGCOUNT;
4415 break;
4416 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004417 buf = lastbuf;
4418 while (buf->b_ml.ml_mfp == NULL)
4419 {
4420 if (buf->b_prev == NULL)
4421 break;
4422 buf = buf->b_prev;
4423 }
4424 lnum = buf->b_fnum;
4425 break;
4426 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004427 lnum = lastbuf->b_fnum;
4428 break;
4429 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004430 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004431 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004432 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004433 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004434 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004435 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004436 cmd = NULL;
4437 goto error;
4438 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004439 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004440#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004441 lnum = qf_get_size(eap);
4442 if (lnum == 0)
4443 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004444#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004445 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004446 case ADDR_QUICKFIX_VALID:
4447#ifdef FEAT_QUICKFIX
4448 lnum = qf_get_valid_size(eap);
4449 if (lnum == 0)
4450 lnum = 1;
4451#endif
4452 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 }
4454 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004456 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 if (*++cmd == NUL)
4458 {
4459 cmd = NULL;
4460 goto error;
4461 }
4462 if (addr_type != ADDR_LINES)
4463 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004464 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004465 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 goto error;
4467 }
4468 if (skip)
4469 ++cmd;
4470 else
4471 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004472 // Only accept a mark in another file when it is
4473 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004474 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4475 ++cmd;
4476 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004477 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004478 lnum = curwin->w_cursor.lnum;
4479 else
4480 {
4481 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
4483 cmd = NULL;
4484 goto error;
4485 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 lnum = fp->lnum;
4487 }
4488 }
4489 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
4491 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004492 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 c = *cmd++;
4494 if (addr_type != ADDR_LINES)
4495 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004496 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004497 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004498 goto error;
4499 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004500 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004501 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004502 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 if (*cmd == c)
4504 ++cmd;
4505 }
4506 else
4507 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004508 int flags;
4509
4510 pos = curwin->w_cursor; // save curwin->w_cursor
4511
4512 // When '/' or '?' follows another address, start from
4513 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004514 if (lnum > 0 && lnum != MAXLNUM)
4515 curwin->w_cursor.lnum =
4516 lnum > curbuf->b_ml.ml_line_count
4517 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004518
4519 // Start a forward search at the end of the line (unless
4520 // before the first line).
4521 // Start a backward search at the start of the line.
4522 // This makes sure we never match in the current
4523 // line, and can match anywhere in the
4524 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004525 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004526 curwin->w_cursor.col = MAXCOL;
4527 else
4528 curwin->w_cursor.col = 0;
4529 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004530 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004531 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 {
4533 curwin->w_cursor = pos;
4534 cmd = NULL;
4535 goto error;
4536 }
4537 lnum = curwin->w_cursor.lnum;
4538 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004539 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004540 cmd += searchcmdlen;
4541 }
4542 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004544 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 ++cmd;
4546 if (addr_type != ADDR_LINES)
4547 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004548 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004549 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004550 goto error;
4551 }
4552 if (*cmd == '&')
4553 i = RE_SUBST;
4554 else if (*cmd == '?' || *cmd == '/')
4555 i = RE_SEARCH;
4556 else
4557 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004558 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004559 cmd = NULL;
4560 goto error;
4561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 if (!skip)
4564 {
4565 /*
4566 * When search follows another address, start from
4567 * there.
4568 */
4569 if (lnum != MAXLNUM)
4570 pos.lnum = lnum;
4571 else
4572 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004574 /*
4575 * Start the search just like for the above
4576 * do_search().
4577 */
4578 if (*cmd != '?')
4579 pos.col = MAXCOL;
4580 else
4581 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004582 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004583 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004584 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004585 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004586 lnum = pos.lnum;
4587 else
4588 {
4589 cmd = NULL;
4590 goto error;
4591 }
4592 }
4593 ++cmd;
4594 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595
4596 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004597 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004598 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
4600
4601 for (;;)
4602 {
4603 cmd = skipwhite(cmd);
4604 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4605 break;
4606
4607 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004608 {
4609 switch (addr_type)
4610 {
4611 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004612 case ADDR_OTHER:
4613 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004614 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004615 break;
4616 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004617 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004618 break;
4619 case ADDR_ARGUMENTS:
4620 lnum = curwin->w_arg_idx + 1;
4621 break;
4622 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004623 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004624 lnum = curbuf->b_fnum;
4625 break;
4626 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004627 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004628 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004629 case ADDR_TABS_RELATIVE:
4630 lnum = 1;
4631 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004632 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004633#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004634 lnum = qf_get_cur_idx(eap);
4635#endif
4636 break;
4637 case ADDR_QUICKFIX_VALID:
4638#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004639 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004640#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004641 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004642 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004643 case ADDR_UNSIGNED:
4644 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004645 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004646 }
4647 }
4648
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004650 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 else
4652 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004653 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 n = 1;
4655 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004656 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004657 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004659 if (n == MAXLNUM)
4660 {
4661 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004662 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004663 goto error;
4664 }
4665 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004666
4667 if (addr_type == ADDR_TABS_RELATIVE)
4668 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004669 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004670 cmd = NULL;
4671 goto error;
4672 }
4673 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004674 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004675 lnum = compute_buffer_local_count(
4676 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004678 {
4679#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004680 // Relative line addressing: need to adjust for lines in a
4681 // closed fold after the first address.
4682 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4683 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004684 (void)hasFolding(lnum, NULL, &lnum);
4685#endif
4686 if (i == '-')
4687 lnum -= n;
4688 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004689 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004690 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004691 {
4692 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004693 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004694 goto error;
4695 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004696 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004697 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
4700 } while (*cmd == '/' || *cmd == '?');
4701
4702error:
4703 *ptr = cmd;
4704 return lnum;
4705}
4706
4707/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004708 * Set eap->line1 and eap->line2 to the whole range.
4709 * Used for commands with the EX_DFLALL flag and no range given.
4710 */
4711 static void
4712address_default_all(exarg_T *eap)
4713{
4714 eap->line1 = 1;
4715 switch (eap->addr_type)
4716 {
4717 case ADDR_LINES:
4718 case ADDR_OTHER:
4719 eap->line2 = curbuf->b_ml.ml_line_count;
4720 break;
4721 case ADDR_LOADED_BUFFERS:
4722 {
4723 buf_T *buf = firstbuf;
4724
4725 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4726 buf = buf->b_next;
4727 eap->line1 = buf->b_fnum;
4728 buf = lastbuf;
4729 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4730 buf = buf->b_prev;
4731 eap->line2 = buf->b_fnum;
4732 }
4733 break;
4734 case ADDR_BUFFERS:
4735 eap->line1 = firstbuf->b_fnum;
4736 eap->line2 = lastbuf->b_fnum;
4737 break;
4738 case ADDR_WINDOWS:
4739 eap->line2 = LAST_WIN_NR;
4740 break;
4741 case ADDR_TABS:
4742 eap->line2 = LAST_TAB_NR;
4743 break;
4744 case ADDR_TABS_RELATIVE:
4745 eap->line2 = 1;
4746 break;
4747 case ADDR_ARGUMENTS:
4748 if (ARGCOUNT == 0)
4749 eap->line1 = eap->line2 = 0;
4750 else
4751 eap->line2 = ARGCOUNT;
4752 break;
4753 case ADDR_QUICKFIX_VALID:
4754#ifdef FEAT_QUICKFIX
4755 eap->line2 = qf_get_valid_size(eap);
4756 if (eap->line2 == 0)
4757 eap->line2 = 1;
4758#endif
4759 break;
4760 case ADDR_NONE:
4761 case ADDR_UNSIGNED:
4762 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004763 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004764 break;
4765 }
4766}
4767
4768
4769/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004770 * Get flags from an Ex command argument.
4771 */
4772 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004773get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004774{
4775 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4776 {
4777 if (*eap->arg == 'l')
4778 eap->flags |= EXFLAG_LIST;
4779 else if (*eap->arg == 'p')
4780 eap->flags |= EXFLAG_PRINT;
4781 else
4782 eap->flags |= EXFLAG_NR;
4783 eap->arg = skipwhite(eap->arg + 1);
4784 }
4785}
4786
4787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 * Function called for command which is Not Implemented. NI!
4789 */
4790 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004791ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
4793 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004794 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004795 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796}
4797
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004798#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799/*
4800 * Function called for script command which is Not Implemented. NI!
4801 * Skips over ":perl <<EOF" constructs.
4802 */
4803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004804ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805{
4806 if (!eap->skip)
4807 ex_ni(eap);
4808 else
4809 vim_free(script_get(eap, eap->arg));
4810}
4811#endif
4812
4813/*
4814 * Check range in Ex command for validity.
4815 * Return NULL when valid, error message when invalid.
4816 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004817 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004818invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004820 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 if ( eap->line1 < 0
4823 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004824 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004825 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004826
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004827 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004828 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004829 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004830 {
4831 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004832 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004833#ifdef FEAT_DIFF
4834 + (eap->cmdidx == CMD_diffget)
4835#endif
4836 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004837 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004838 break;
4839 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004840 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004841 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004842 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004843 break;
4844 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004845 // Only a boundary check, not whether the buffers actually
4846 // exist.
4847 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004848 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004849 break;
4850 case ADDR_LOADED_BUFFERS:
4851 buf = firstbuf;
4852 while (buf->b_ml.ml_mfp == NULL)
4853 {
4854 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004855 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004856 buf = buf->b_next;
4857 }
4858 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004859 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004860 buf = lastbuf;
4861 while (buf->b_ml.ml_mfp == NULL)
4862 {
4863 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004864 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004865 buf = buf->b_prev;
4866 }
4867 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004868 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004869 break;
4870 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004871 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004872 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004873 break;
4874 case ADDR_TABS:
4875 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004876 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004877 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004878 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004879 case ADDR_OTHER:
4880 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004881 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004882 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004883#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004884 // No error for value that is too big, will use the last entry.
4885 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004886 {
4887 if (eap->addr_count == 0)
4888 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004889 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004890 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004891#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004892 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004893 case ADDR_QUICKFIX_VALID:
4894#ifdef FEAT_QUICKFIX
4895 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4896 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004897 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004898#endif
4899 break;
4900 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004901 case ADDR_NONE:
4902 // Will give an error elsewhere.
4903 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004904 }
4905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 return NULL;
4907}
4908
4909/*
4910 * Correct the range for zero line number, if required.
4911 */
4912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004913correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004915 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
4917 if (eap->line1 == 0)
4918 eap->line1 = 1;
4919 if (eap->line2 == 0)
4920 eap->line2 = 1;
4921 }
4922}
4923
Bram Moolenaar748bf032005-02-02 23:04:36 +00004924#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004925/*
4926 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4927 * pattern. Otherwise return eap->arg.
4928 */
4929 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004930skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004931{
4932 char_u *p = eap->arg;
4933
Bram Moolenaara37420f2006-02-04 22:37:47 +00004934 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4935 || eap->cmdidx == CMD_vimgrepadd
4936 || eap->cmdidx == CMD_lvimgrepadd
4937 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004938 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004939 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004940 if (p == NULL)
4941 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004942 }
4943 return p;
4944}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004945
4946/*
4947 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4948 * in the command line, so that things like % get expanded.
4949 */
4950 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004951replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004952{
4953 char_u *new_cmdline;
4954 char_u *program;
4955 char_u *pos;
4956 char_u *ptr;
4957 int len;
4958 int i;
4959
4960 /*
4961 * Don't do it when ":vimgrep" is used for ":grep".
4962 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004963 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4964 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4965 || eap->cmdidx == CMD_grepadd
4966 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004967 && !grep_internal(eap->cmdidx))
4968 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004969 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4970 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004971 {
4972 if (*curbuf->b_p_gp == NUL)
4973 program = p_gp;
4974 else
4975 program = curbuf->b_p_gp;
4976 }
4977 else
4978 {
4979 if (*curbuf->b_p_mp == NUL)
4980 program = p_mp;
4981 else
4982 program = curbuf->b_p_mp;
4983 }
4984
4985 p = skipwhite(p);
4986
4987 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4988 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004989 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004990 i = 1;
4991 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4992 ++i;
4993 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004994 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004995 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004996 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004997 ptr = new_cmdline;
4998 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4999 {
5000 i = (int)(pos - program);
5001 STRNCPY(ptr, program, i);
5002 STRCPY(ptr += i, p);
5003 ptr += len;
5004 program = pos + 2;
5005 }
5006 STRCPY(ptr, program);
5007 }
5008 else
5009 {
John Marriotted908f72024-04-18 22:46:56 +02005010 size_t program_len = STRLEN(program);
5011
5012 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005013 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005014 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005015 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005016 STRCPY(new_cmdline + program_len, " ");
5017 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005018 }
5019 msg_make(p);
5020
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005021 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005022 vim_free(*cmdlinep);
5023 *cmdlinep = new_cmdline;
5024 p = new_cmdline;
5025 }
5026 return p;
5027}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005028#endif
5029
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030/*
5031 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005032 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 * Return FAIL for failure, OK otherwise.
5034 */
5035 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005036expand_filename(
5037 exarg_T *eap,
5038 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005039 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005041 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005043 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 char_u *p;
5045 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005046 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
Bram Moolenaar748bf032005-02-02 23:04:36 +00005048#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005049 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005050 p = skip_grep_pat(eap);
5051#else
5052 p = eap->arg;
5053#endif
5054
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 /*
5056 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5057 * the file name contains a wildcard it should not cause expanding.
5058 * (it will be expanded anyway if there is a wildcard before replacing).
5059 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005060 has_wildcards = mch_has_wildcard(p);
5061 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005063#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005064 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005065 if (p[0] == '`' && p[1] == '=')
5066 {
5067 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005068 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005069 if (*p == '`')
5070 ++p;
5071 continue;
5072 }
5073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 /*
5075 * Quick check if this cannot be the start of a special string.
5076 * Also removes backslash before '%', '#' and '<'.
5077 */
5078 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5079 {
5080 ++p;
5081 continue;
5082 }
5083
5084 /*
5085 * Try to find a match at this position.
5086 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005087 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005088 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005089 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005091 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
5093 p += srclen;
5094 continue;
5095 }
5096
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005097 // Wildcards won't be expanded below, the replacement is taken
5098 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005099 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5100 {
5101 char_u *l = repl;
5102
5103 repl = expand_env_save(repl);
5104 vim_free(l);
5105 }
5106
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005107 // Need to escape white space et al. with a backslash.
5108 // Don't do this for:
5109 // - replacement that already has been escaped: "##"
5110 // - shell commands (may have to use quotes instead).
5111 // - non-unix systems when there is a single argument (spaces don't
5112 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005114 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 && eap->cmdidx != CMD_grep
5117 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005118 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005119 && eap->cmdidx != CMD_lgrep
5120 && eap->cmdidx != CMD_lgrepadd
5121 && eap->cmdidx != CMD_lmake
5122 && eap->cmdidx != CMD_make
5123 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005125 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126#endif
5127 )
5128 {
5129 char_u *l;
5130#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005131 // Don't escape a backslash here, because rem_backslash() doesn't
5132 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 static char_u *nobslash = (char_u *)" \t\"|";
5134# define ESCAPE_CHARS nobslash
5135#else
5136# define ESCAPE_CHARS escape_chars
5137#endif
5138
5139 for (l = repl; *l; ++l)
5140 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5141 {
5142 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5143 if (l != NULL)
5144 {
5145 vim_free(repl);
5146 repl = l;
5147 }
5148 break;
5149 }
5150 }
5151
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005152 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005153 if ((eap->usefilter || eap->cmdidx == CMD_bang
5154 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005155 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
5157 char_u *l;
5158
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005159 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 if (l != NULL)
5161 {
5162 vim_free(repl);
5163 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 }
5166
Mike Williams51024bb2024-05-30 07:46:30 +02005167 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 vim_free(repl);
5169 if (p == NULL)
5170 return FAIL;
5171 }
5172
5173 /*
5174 * One file argument: Expand wildcards.
5175 * Don't do this with ":r !command" or ":w !command".
5176 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005177 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
5179 /*
5180 * May do this twice:
5181 * 1. Replace environment variables.
5182 * 2. Replace any other wildcards, remove backslashes.
5183 */
5184 for (n = 1; n <= 2; ++n)
5185 {
5186 if (n == 2)
5187 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 /*
5189 * Halve the number of backslashes (this is Vi compatible).
5190 * For Unix and OS/2, when wildcards are expanded, this is
5191 * done by ExpandOne() below.
5192 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005193#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 if (!has_wildcards)
5195#endif
5196 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198
5199 if (has_wildcards)
5200 {
5201 if (n == 1)
5202 {
5203 /*
5204 * First loop: May expand environment variables. This
5205 * can be done much faster with expand_env() than with
5206 * something else (e.g., calling a shell).
5207 * After expanding environment variables, check again
5208 * if there are still wildcards present.
5209 */
5210 if (vim_strchr(eap->arg, '$') != NULL
5211 || vim_strchr(eap->arg, '~') != NULL)
5212 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005213 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005214 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 has_wildcards = mch_has_wildcard(NameBuff);
5216 p = NameBuff;
5217 }
5218 else
5219 p = NULL;
5220 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005221 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
5223 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005224 int options = WILD_LIST_NOTFOUND
5225 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
5227 ExpandInit(&xpc);
5228 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005229 if (p_wic)
5230 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005232 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 if (p == NULL)
5234 return FAIL;
5235 }
5236 if (p != NULL)
5237 {
John Marriotted908f72024-04-18 22:46:56 +02005238 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005240 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 vim_free(p);
5242 }
5243 }
5244 }
5245 }
5246 return OK;
5247}
5248
5249/*
5250 * Replace part of the command line, keeping eap->cmd, eap->arg and
5251 * eap->nextcmd correct.
5252 * "src" points to the part that is to be replaced, of length "srclen".
5253 * "repl" is the replacement string.
5254 * Returns a pointer to the character after the replaced string.
5255 * Returns NULL for failure.
5256 */
5257 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005258repl_cmdline(
5259 exarg_T *eap,
5260 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005261 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005262 char_u *repl,
5263 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264{
John Marriotted908f72024-04-18 22:46:56 +02005265 size_t repllen;
5266 size_t taillen;
5267 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005269 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271 /*
5272 * The new command line is build in new_cmdline[].
5273 * First allocate it.
5274 * Careful: a "+cmd" argument may have been NUL terminated.
5275 */
John Marriotted908f72024-04-18 22:46:56 +02005276 repllen = STRLEN(repl);
5277 taillen = STRLEN(src + srclen);
5278 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005279 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005280 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005281 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005282 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283
5284 /*
5285 * Copy the stuff before the expanded part.
5286 * Copy the expanded stuff.
5287 * Copy what came after the expanded part.
5288 * Copy the next commands, if there are any.
5289 */
John Marriotted908f72024-04-18 22:46:56 +02005290 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5291 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005292
John Marriotted908f72024-04-18 22:46:56 +02005293 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5294 new_cmdlinelen += repllen; // remember the end of the string
5295 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5296 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005298 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
John Marriotted908f72024-04-18 22:46:56 +02005300 new_cmdlinelen += taillen + 1;
5301 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5302 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 }
5304 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5305 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5306 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5307 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5308 vim_free(*cmdlinep);
5309 *cmdlinep = new_cmdline;
5310
5311 return src;
5312}
5313
5314/*
5315 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005316 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 */
5318 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005319separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320{
5321 char_u *p;
5322
Bram Moolenaar86b68352004-12-27 21:59:20 +00005323#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005324 p = skip_grep_pat(eap);
5325#else
5326 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005327#endif
5328
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005329 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
5331 if (*p == Ctrl_V)
5332 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005333 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005334 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005336 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005337 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005338 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 break;
5340 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005341
5342#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005343 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005344 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005345 {
5346 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005347 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005348 if (*p == NUL) // stop at NUL after CTRL-V
5349 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005350 }
5351#endif
5352
Bram Moolenaareda29c92022-10-02 12:59:00 +01005353 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005354 // :@" and :*" do not start a comment!
5355 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005356 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005357 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005358 && !(eap->argt & EX_NOTRLCOM)
5359 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5360 || p != eap->arg)
5361 && (eap->cmdidx != CMD_redir
5362 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005363 || (*p == '#'
5364 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005365 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005366 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005367 || (*p == '|'
5368 && eap->cmdidx != CMD_append
5369 && eap->cmdidx != CMD_change
5370 && eap->cmdidx != CMD_insert)
5371 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
5373 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005374 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 * AND 'b' is present in 'cpoptions'.
5376 */
5377 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005378 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005380 if (!keep_backslash)
5381 {
5382 STRMOVE(p - 1, p); // remove the '\'
5383 --p;
5384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 }
5386 else
5387 {
5388 eap->nextcmd = check_nextcmd(p);
5389 *p = NUL;
5390 break;
5391 }
5392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005394
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005395 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 del_trailing_spaces(eap->arg);
5397}
5398
5399/*
5400 * get + command from ex argument
5401 */
5402 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005403getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
5405 char_u *arg = *argp;
5406 char_u *command = NULL;
5407
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005408 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {
5410 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005411 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 command = dollar_command;
5413 else
5414 {
5415 command = arg;
5416 arg = skip_cmd_arg(command, TRUE);
5417 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005418 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 }
5420
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005421 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 *argp = arg;
5423 }
5424 return command;
5425}
5426
5427/*
5428 * Find end of "+command" argument. Skip over "\ " and "\\".
5429 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005430 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005431skip_cmd_arg(
5432 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005433 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434{
5435 while (*p && !vim_isspace(*p))
5436 {
5437 if (*p == '\\' && p[1] != NUL)
5438 {
5439 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005440 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 else
5442 ++p;
5443 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005444 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
5446 return p;
5447}
5448
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005449 int
5450get_bad_opt(char_u *p, exarg_T *eap)
5451{
5452 if (STRICMP(p, "keep") == 0)
5453 eap->bad_char = BAD_KEEP;
5454 else if (STRICMP(p, "drop") == 0)
5455 eap->bad_char = BAD_DROP;
5456 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5457 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005458 else
5459 return FAIL;
5460 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005461}
5462
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005464 * Function given to ExpandGeneric() to obtain the list of bad= names.
5465 */
5466 static char_u *
5467get_bad_name(expand_T *xp UNUSED, int idx)
5468{
5469 // Note: Keep this in sync with getargopt.
5470 static char *(p_bad_values[]) =
5471 {
5472 "?",
5473 "keep",
5474 "drop",
5475 };
5476
John Marriotted908f72024-04-18 22:46:56 +02005477 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5478 return NULL;
5479
5480 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005481}
5482
5483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 * Get "++opt=arg" argument.
5485 * Return FAIL or OK.
5486 */
5487 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005488getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489{
5490 char_u *arg = eap->arg + 2;
5491 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005492 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005495 // Note: Keep this in sync with get_argopt_name.
5496
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005497 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5499 {
5500 if (*arg == 'n')
5501 {
5502 arg += 2;
5503 eap->force_bin = FORCE_NOBIN;
5504 }
5505 else
5506 eap->force_bin = FORCE_BIN;
5507 if (!checkforcmd(&arg, "binary", 3))
5508 return FAIL;
5509 eap->arg = skipwhite(arg);
5510 return OK;
5511 }
5512
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005513 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005514 if (STRNCMP(arg, "edit", 4) == 0)
5515 {
5516 eap->read_edit = TRUE;
5517 eap->arg = skipwhite(arg + 4);
5518 return OK;
5519 }
5520
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 if (STRNCMP(arg, "ff", 2) == 0)
5522 {
5523 arg += 2;
5524 pp = &eap->force_ff;
5525 }
5526 else if (STRNCMP(arg, "fileformat", 10) == 0)
5527 {
5528 arg += 10;
5529 pp = &eap->force_ff;
5530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 else if (STRNCMP(arg, "enc", 3) == 0)
5532 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005533 if (STRNCMP(arg, "encoding", 8) == 0)
5534 arg += 8;
5535 else
5536 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 pp = &eap->force_enc;
5538 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005539 else if (STRNCMP(arg, "bad", 3) == 0)
5540 {
5541 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005542 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
5545 if (pp == NULL || *arg != '=')
5546 return FAIL;
5547
5548 ++arg;
5549 *pp = (int)(arg - eap->cmd);
5550 arg = skip_cmd_arg(arg, FALSE);
5551 eap->arg = skipwhite(arg);
5552 *arg = NUL;
5553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 if (pp == &eap->force_ff)
5555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5557 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005558 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005560 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005562 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5564 *p = TOLOWER_ASC(*p);
5565 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005566 else
5567 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005568 // Check ++bad= argument. Must be a single-byte character, "keep" or
5569 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005570 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005571 return FAIL;
5572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
5574 return OK;
5575}
5576
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005577/*
5578 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5579 */
5580 static char_u *
5581get_argopt_name(expand_T *xp UNUSED, int idx)
5582{
5583 // Note: Keep this in sync with getargopt.
5584 static char *(p_opt_values[]) =
5585 {
5586 "fileformat=",
5587 "encoding=",
5588 "binary",
5589 "nobinary",
5590 "bad=",
5591 "edit",
5592 };
5593
John Marriotted908f72024-04-18 22:46:56 +02005594 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5595 return NULL;
5596
5597 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005598}
5599
5600/*
5601 * Command-line expansion for ++opt=name.
5602 */
5603 int
5604expand_argopt(
5605 char_u *pat,
5606 expand_T *xp,
5607 regmatch_T *rmp,
5608 char_u ***matches,
5609 int *numMatches)
5610{
5611 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5612 {
5613 char_u *(*cb)(expand_T *, int) = NULL;
5614
5615 char_u *name_end = xp->xp_pattern - 1;
5616 if (name_end - xp->xp_line >= 2
5617 && STRNCMP(name_end - 2, "ff", 2) == 0)
5618 cb = get_fileformat_name;
5619 else if (name_end - xp->xp_line >= 10
5620 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5621 cb = get_fileformat_name;
5622 else if (name_end - xp->xp_line >= 3
5623 && STRNCMP(name_end - 3, "enc", 3) == 0)
5624 cb = get_encoding_name;
5625 else if (name_end - xp->xp_line >= 8
5626 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5627 cb = get_encoding_name;
5628 else if (name_end - xp->xp_line >= 3
5629 && STRNCMP(name_end - 3, "bad", 3) == 0)
5630 cb = get_bad_name;
5631
5632 if (cb != NULL)
5633 {
5634 return ExpandGeneric(
5635 pat,
5636 xp,
5637 rmp,
5638 matches,
5639 numMatches,
5640 cb,
5641 FALSE);
5642 }
5643 return FAIL;
5644 }
5645
5646 // Special handling of "ff" which acts as a short form of
5647 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005648 if (xp->xp_pattern_len == 2
5649 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005650 {
5651 *matches = ALLOC_MULT(char_u *, 1);
5652 if (*matches == NULL)
5653 return FAIL;
5654 *numMatches = 1;
5655 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5656 return OK;
5657 }
5658
5659 return ExpandGeneric(
5660 pat,
5661 xp,
5662 rmp,
5663 matches,
5664 numMatches,
5665 get_argopt_name,
5666 FALSE);
5667}
5668
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005670ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671{
5672 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005673 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 * directory for security reasons.
5675 */
5676 if (secure)
5677 {
5678 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005679 eap->errmsg =
5680 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005683 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 else
5685 do_augroup(eap->arg, eap->forceit);
5686}
5687
5688/*
5689 * ":doautocmd": Apply the automatic commands to the current buffer.
5690 */
5691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005692ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005694 char_u *arg = eap->arg;
5695 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005696 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005697
Bram Moolenaar1610d052016-06-09 22:53:01 +02005698 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005699 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005700 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005701 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704/*
5705 * :[N]bunload[!] [N] [bufname] unload buffer
5706 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5707 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5708 */
5709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005710ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005712 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005713 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 eap->errmsg = do_bufdel(
5715 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5716 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5717 : DOBUF_UNLOAD, eap->arg,
5718 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5719}
5720
5721/*
5722 * :[N]buffer [N] to buffer N
5723 * :[N]sbuffer [N] to buffer N
5724 */
5725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005726ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005728 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005729 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005731 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 else
5733 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005734 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5736 else
5737 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005738 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005739 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 }
5741}
5742
5743/*
5744 * :[N]bmodified [N] to next mod. buffer
5745 * :[N]sbmodified [N] to next mod. buffer
5746 */
5747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005748ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005751 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005752 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753}
5754
5755/*
5756 * :[N]bnext [N] to next buffer
5757 * :[N]sbnext [N] split and to next buffer
5758 */
5759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005760ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005762 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005763 return;
5764
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005766 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005767 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768}
5769
5770/*
5771 * :[N]bNext [N] to previous buffer
5772 * :[N]bprevious [N] to previous buffer
5773 * :[N]sbNext [N] split and to previous buffer
5774 * :[N]sbprevious [N] split and to previous buffer
5775 */
5776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005777ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005779 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005780 return;
5781
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005783 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005784 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785}
5786
5787/*
5788 * :brewind to first buffer
5789 * :bfirst to first buffer
5790 * :sbrewind split and to first buffer
5791 * :sbfirst split and to first buffer
5792 */
5793 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005794ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005796 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005797 return;
5798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005800 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005801 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802}
5803
5804/*
5805 * :blast to last buffer
5806 * :sblast split and to last buffer
5807 */
5808 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005809ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005811 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005812 return;
5813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005815 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005816 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
Bram Moolenaar7a092242020-04-16 22:10:49 +02005819/*
5820 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005821 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005824ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005826 int comment_char = '"';
5827
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005828 if (in_vim9script())
5829 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005830 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005831}
5832
5833/*
5834 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5835 * to "cmd_start" or has a white space character before it.
5836 */
5837 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005838ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005839{
5840 int c = *cmd;
5841
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005842 if (c == NUL || c == '|' || c == '\n')
5843 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005844 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005845 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5846 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005847 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005848 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849}
5850
5851#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5852 || defined(PROTO)
5853/*
5854 * Return the next command, after the first '|' or '\n'.
5855 * Return NULL if not found.
5856 */
5857 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005858find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859{
5860 while (*p != '|' && *p != '\n')
5861 {
5862 if (*p == NUL)
5863 return NULL;
5864 ++p;
5865 }
5866 return (p + 1);
5867}
5868#endif
5869
5870/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005871 * Check if *p is a separator between Ex commands, skipping over white space.
5872 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 */
5874 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005875check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005877 char_u *s = skipwhite(p);
5878
5879 if (*s == '|' || *s == '\n')
5880 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 else
5882 return NULL;
5883}
5884
5885/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005886 * If "eap->nextcmd" is not set, check for a next command at "p".
5887 */
5888 void
5889set_nextcmd(exarg_T *eap, char_u *arg)
5890{
5891 char_u *p = check_nextcmd(arg);
5892
5893 if (eap->nextcmd == NULL)
5894 eap->nextcmd = p;
5895 else if (p != NULL)
5896 // cannot use "| command" inside a {} block
5897 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5898}
5899
5900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 * - if there are more files to edit
5902 * - and this is the last window
5903 * - and forceit not used
5904 * - and not repeated twice on a row
5905 * return FAIL and give error message if 'message' TRUE
5906 * return OK otherwise
5907 */
5908 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005909check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005910 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005911 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912{
5913 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5914
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005915 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005916 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 {
5918 if (message)
5919 {
5920#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005921 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5922 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005924 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005926 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5927 NGETTEXT("%d more file to edit. Quit anyway?",
5928 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5930 return OK;
5931 return FAIL;
5932 }
5933#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005934 semsg(NGETTEXT(e_nr_more_file_to_edit,
5935 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005936 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 }
5938 return FAIL;
5939 }
5940 return OK;
5941}
5942
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943/*
5944 * Function given to ExpandGeneric() to obtain the list of command names.
5945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005947get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948{
5949 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005950 return expand_user_command_name(idx);
Christian Brabandt70a11a62024-07-26 19:13:55 +02005951 // the following are no real commands
5952 if (STRNCMP(cmdnames[idx].cmd_name, "{", 1) == 0 ||
5953 STRNCMP(cmdnames[idx].cmd_name, "}", 1) == 0)
5954 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 return cmdnames[idx].cmd_name;
5956}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005959ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960{
Bram Moolenaare6850792010-05-14 15:28:44 +02005961 if (*eap->arg == NUL)
5962 {
5963#ifdef FEAT_EVAL
5964 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5965 char_u *p = NULL;
5966
5967 if (expr != NULL)
5968 {
5969 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005970 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005971 --emsg_off;
5972 vim_free(expr);
5973 }
5974 if (p != NULL)
5975 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005976 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005977 vim_free(p);
5978 }
5979 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005980 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005981#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005982 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005983#endif
5984 }
5985 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005986 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005987
5988#ifdef FEAT_VTP
5989 else if (has_vtp_working())
5990 {
5991 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005992 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005993 redrawcmd();
5994 }
5995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996}
5997
5998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005999ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000{
6001 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006002 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 do_highlight(eap->arg, eap->forceit, FALSE);
6004}
6005
6006
6007/*
6008 * Call this function if we thought we were going to exit, but we won't
6009 * (because of an error). May need to restore the terminal mode.
6010 */
6011 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006012not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013{
6014 exiting = FALSE;
6015 settmode(TMODE_RAW);
6016}
6017
Bram Moolenaar3552e742021-05-29 12:21:58 +02006018 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006019before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6020{
6021 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6022
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006023 // Bail out when autocommands closed the window.
6024 // Refuse to quit when the buffer in the last window is being closed (can
6025 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006026 if (!win_valid(wp)
6027 || curbuf_locked()
6028 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6029 return TRUE;
6030
6031 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6032 {
6033 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006034 // Refuse to quit when locked or when the window was closed or the
6035 // buffer in the last window is being closed (can only happen in
6036 // autocommands).
6037 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006038 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6039 return TRUE;
6040 }
6041
6042 return FALSE;
6043}
6044
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006046 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006047 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006048 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006050 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006051ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006053 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 if (cmdwin_type != 0)
6056 {
6057 cmdwin_result = Ctrl_C;
6058 return;
6059 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006060 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006061 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006062 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006063 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006064 return;
6065 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006066 if (eap->addr_count > 0)
6067 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006068 int wnr = eap->line2;
6069
6070 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6071 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006072 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006073 }
6074 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006075 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006076
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006077 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006078 if (curbuf_locked())
6079 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006080
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006081 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006082 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006083 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084
6085#ifdef FEAT_NETBEANS_INTG
6086 netbeansForcedQuit = eap->forceit;
6087#endif
6088
6089 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006090 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 */
6092 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6093 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006094 if ((!buf_hide(wp->w_buffer)
6095 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006096 | (eap->forceit ? CCGD_FORCEIT : 0)
6097 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006099 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
6101 not_exiting();
6102 }
6103 else
6104 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006105 // quit last window
6106 // Note: only_one_window() returns true, even so a help window is
6107 // still open. In that case only quit, if no address has been
6108 // specified. Example:
6109 // :h|wincmd w|1q - don't quit
6110 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006111 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006113 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006114#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006116#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006117 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006118 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 }
6120}
6121
6122/*
6123 * ":cquit".
6124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006126ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006128 // this does not always pass on the exit code to the Manx compiler. why?
6129 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130}
6131
6132/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006133 * Do preparations for "qall" and "wqall".
6134 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006136 int
6137before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 if (cmdwin_type != 0)
6140 {
6141 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006142 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 else
6144 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006145 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006147
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006148 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006149 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006150 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006151 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006152 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006153 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006154
6155 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006156 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006157
Bram Moolenaar411da642023-05-10 16:53:27 +01006158 return OK;
6159}
6160
6161/*
6162 * ":qall": try to quit all windows
6163 */
6164 static void
6165ex_quit_all(exarg_T *eap)
6166{
6167 if (before_quit_all(eap) == FAIL)
6168 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006170 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 getout(0);
6172 not_exiting();
6173}
6174
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175/*
6176 * ":close": close current window, unless it is the last one
6177 */
6178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006179ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006181 win_T *win;
6182 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006184 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006186 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006187 {
6188 if (eap->addr_count == 0)
6189 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006190 else
6191 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006192 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006193 {
6194 winnr++;
6195 if (winnr == eap->line2)
6196 break;
6197 }
6198 if (win == NULL)
6199 win = lastwin;
6200 ex_win_close(eap->forceit, win, NULL);
6201 }
6202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203}
6204
Bram Moolenaar4033c552017-09-16 20:54:51 +02006205#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206/*
6207 * ":pclose": Close any preview window.
6208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006210ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006211{
6212 win_T *win;
6213
Bram Moolenaar79648732019-07-18 21:43:07 +02006214 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006215 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006216 if (win->w_p_pvw)
6217 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006218 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006219 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006220 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006221# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006222 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006223 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006224# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006225}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006226#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006227
Bram Moolenaarf740b292006-02-16 22:11:02 +00006228/*
6229 * Close window "win" and take care of handling closing the last window for a
6230 * modified buffer.
6231 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006233ex_win_close(
6234 int forceit,
6235 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006236 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
6238 int need_hide;
6239 buf_T *buf = win->w_buffer;
6240
Bram Moolenaarcf844172020-06-26 19:44:06 +02006241 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006242 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006243 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006244 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006245 return;
6246 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006247 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006248 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006249
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006251 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006253#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006254 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006256# ifdef FEAT_TERMINAL
6257 if (term_job_running(buf->b_term))
6258 {
6259 if (term_confirm_stop(buf) == FAIL)
6260 return;
6261 // Manually kill the terminal here because this command will
6262 // hide it otherwise.
6263 free_terminal(buf);
6264 need_hide = FALSE;
6265 }
6266 else
6267# endif
6268 {
6269 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006270
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006271 set_bufref(&bufref, buf);
6272 dialog_changed(buf, FALSE);
6273 if (bufref_valid(&bufref) && bufIsChanged(buf))
6274 return;
6275 need_hide = FALSE;
6276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 }
6278 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006281 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 return;
6283 }
6284 }
6285
Bram Moolenaar4033c552017-09-16 20:54:51 +02006286#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006288#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006289
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006290 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006291 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006292 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006293 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006294 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295}
6296
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006298 * Handle the argument for a tabpage related ex command.
6299 * Returns a tabpage number.
6300 * When an error is encountered then eap->errmsg is set.
6301 */
6302 static int
6303get_tabpage_arg(exarg_T *eap)
6304{
6305 int tab_number;
6306 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6307
6308 if (eap->arg && *eap->arg != NUL)
6309 {
6310 char_u *p = eap->arg;
6311 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006312 int relative = 0; // argument +N/-N means: go to N places to the
6313 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006314
6315 if (*p == '-')
6316 {
6317 relative = -1;
6318 p++;
6319 }
6320 else if (*p == '+')
6321 {
6322 relative = 1;
6323 p++;
6324 }
6325
6326 p_save = p;
6327 tab_number = getdigits(&p);
6328
6329 if (relative == 0)
6330 {
6331 if (STRCMP(p, "$") == 0)
6332 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006333 else if (STRCMP(p, "#") == 0)
6334 if (valid_tabpage(lastused_tabpage))
6335 tab_number = tabpage_index(lastused_tabpage);
6336 else
6337 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006338 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6339 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006340 tab_number = 0;
6341 goto theend;
6342 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006343 else if (p == p_save || *p_save == '-' || *p != NUL
6344 || tab_number > LAST_TAB_NR)
6345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006346 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006347 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006348 goto theend;
6349 }
6350 }
6351 else
6352 {
6353 if (*p_save == NUL)
6354 tab_number = 1;
6355 else if (p == p_save || *p_save == '-' || *p != NUL
6356 || tab_number == 0)
6357 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006358 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006359 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006360 goto theend;
6361 }
6362 tab_number = tab_number * relative + tabpage_index(curtab);
6363 if (!unaccept_arg0 && relative == -1)
6364 --tab_number;
6365 }
6366 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006367 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006368 }
6369 else if (eap->addr_count > 0)
6370 {
6371 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006372 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006373 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006374 tab_number = 0;
6375 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006376 else
6377 {
6378 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006379 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006380 {
zeertzjq076faac2024-03-25 16:41:06 +01006381 char_u *cmdp = eap->cmd;
6382
6383 while (--cmdp > *eap->cmdlinep
6384 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6385 ;
6386 if (*cmdp == '-')
6387 {
6388 --tab_number;
6389 if (tab_number < unaccept_arg0)
6390 eap->errmsg = _(e_invalid_range);
6391 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006392 }
6393 }
6394 }
6395 else
6396 {
6397 switch (eap->cmdidx)
6398 {
6399 case CMD_tabnext:
6400 tab_number = tabpage_index(curtab) + 1;
6401 if (tab_number > LAST_TAB_NR)
6402 tab_number = 1;
6403 break;
6404 case CMD_tabmove:
6405 tab_number = LAST_TAB_NR;
6406 break;
6407 default:
6408 tab_number = tabpage_index(curtab);
6409 }
6410 }
6411
6412theend:
6413 return tab_number;
6414}
6415
6416/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006417 * ":tabclose": close current tab page, unless it is the last one.
6418 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 */
6420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006421ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006423 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006424 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006425
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006426 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006427 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006428 cmdwin_result = K_IGNORE;
6429 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006430 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006431
6432 if (first_tabpage->tp_next == NULL)
6433 {
6434 emsg(_(e_cannot_close_last_tab_page));
6435 return;
6436 }
6437
6438 if (window_layout_locked(CMD_tabclose))
6439 return;
6440
6441 tab_number = get_tabpage_arg(eap);
6442 if (eap->errmsg != NULL)
6443 return;
6444
6445 tp = find_tabpage(tab_number);
6446 if (tp == NULL)
6447 {
6448 beep_flush();
6449 return;
6450 }
6451 if (tp != curtab)
6452 {
6453 tabpage_close_other(tp, eap->forceit);
6454 return;
6455 }
6456 else if (!text_locked() && !curbuf_locked())
6457 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006458}
6459
6460/*
6461 * ":tabonly": close all tab pages except the current one
6462 */
6463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006464ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006465{
6466 tabpage_T *tp;
6467 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006468 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006469
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006470 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006471 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006472 cmdwin_result = K_IGNORE;
6473 return;
6474 }
6475
6476 if (first_tabpage->tp_next == NULL)
6477 {
6478 msg(_("Already only one tab page"));
6479 return;
6480 }
6481
6482 if (window_layout_locked(CMD_tabonly))
6483 return;
6484
6485 tab_number = get_tabpage_arg(eap);
6486 if (eap->errmsg != NULL)
6487 return;
6488
6489 goto_tabpage(tab_number);
6490 // Repeat this up to a 1000 times, because autocommands may
6491 // mess up the lists.
6492 for (done = 0; done < 1000; ++done)
6493 {
6494 FOR_ALL_TABPAGES(tp)
6495 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006496 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006497 tabpage_close_other(tp, eap->forceit);
6498 // if we failed to close it quit
6499 if (valid_tabpage(tp))
6500 done = 1000;
6501 // start over, "tp" is now invalid
6502 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006503 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006504 if (first_tabpage->tp_next == NULL)
6505 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
6509/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006510 * Close the current tab page.
6511 */
6512 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006513tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006514{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006515 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006516 return;
6517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006518 // First close all the windows but the current one. If that worked then
6519 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006520 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006521 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006522 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006523 ex_win_close(forceit, curwin, NULL);
6524# ifdef FEAT_GUI
6525 need_mouse_correct = TRUE;
6526# endif
6527}
6528
6529/*
6530 * Close tab page "tp", which is not the current tab page.
6531 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006532 * Also takes care of the tab pages line disappearing when closing the
6533 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006534 */
6535 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006536tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006537{
6538 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006539 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006541 // Limit to 1000 windows, autocommands may add a window while we close
6542 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006543 while (++done < 1000)
6544 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006545 wp = tp->tp_firstwin;
6546 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006547
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006548 // Autocommands may delete the tab page under our fingers and we may
6549 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006550 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006551 break;
6552 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006553
Bram Moolenaar29323592016-07-24 22:04:11 +02006554 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006555}
6556
6557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 * ":only".
6559 */
6560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006561ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006563 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006564 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565# ifdef FEAT_GUI
6566 need_mouse_correct = TRUE;
6567# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006568 if (eap->addr_count > 0)
6569 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006570 win_T *wp;
6571 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006572 for (wp = firstwin; --wnr > 0; )
6573 {
6574 if (wp->w_next == NULL)
6575 break;
6576 else
6577 wp = wp->w_next;
6578 }
6579 win_goto(wp);
6580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 close_others(TRUE, eap->forceit);
6582}
6583
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006585ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006587 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006588 if (eap->skip)
6589 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006590
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006591 if (window_layout_locked(CMD_hide))
6592 return;
6593#ifdef FEAT_GUI
6594 need_mouse_correct = TRUE;
6595#endif
6596 if (eap->addr_count == 0)
6597 win_close(curwin, FALSE); // don't free buffer
6598 else
6599 {
6600 int winnr = 0;
6601 win_T *win;
6602
6603 FOR_ALL_WINDOWS(win)
6604 {
6605 winnr++;
6606 if (winnr == eap->line2)
6607 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006609 if (win == NULL)
6610 win = lastwin;
6611 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 }
6613}
6614
6615/*
6616 * ":stop" and ":suspend": Suspend Vim.
6617 */
dbivolaruab16ad32021-12-29 19:41:47 +00006618 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006619ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620{
6621 /*
6622 * Disallow suspending for "rvim".
6623 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006624 if (check_restricted())
6625 return;
6626
6627 if (!eap->forceit)
6628 autowrite_all();
6629 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6630 windgoto((int)Rows - 1, 0);
6631 out_char('\n');
6632 out_flush();
6633 stoptermcap();
6634 out_flush(); // needed for SUN to restore xterm buffer
6635 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6636 ui_suspend(); // call machine specific function
6637 maketitle();
6638 resettitle(); // force updating the title
6639 starttermcap();
6640 scroll_start(); // scroll screen before redrawing
6641 redraw_later_clear();
6642 shell_resized(); // may have resized window
6643 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644}
6645
6646/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006647 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 */
6649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006650ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006652#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006653 if (not_in_vim9(eap) == FAIL)
6654 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 if (cmdwin_type != 0)
6657 {
6658 cmdwin_result = Ctrl_C;
6659 return;
6660 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006661 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006662 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006663 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006664 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006665 return;
6666 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006667
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006669 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 */
6671 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6672 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006673
6674 // Write the buffer for ":wq" or when it was changed.
6675 // Trigger QuitPre and ExitPre.
6676 // Check if we can exit now, after autocommands have changed things.
6677 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6678 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006680 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 {
6682 not_exiting();
6683 }
6684 else
6685 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006686 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006688 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689# ifdef FEAT_GUI
6690 need_mouse_correct = TRUE;
6691# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006692 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006693 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 }
6695}
6696
6697/*
6698 * ":print", ":list", ":number".
6699 */
6700 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006701ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006703 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006704 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006705 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006707 for ( ;!got_int; ui_breakcheck())
6708 {
6709 print_line(eap->line1,
6710 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6711 || (eap->flags & EXFLAG_NR)),
6712 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6713 if (++eap->line1 > eap->line2)
6714 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006715 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006716 }
6717 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006718 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006719 curwin->w_cursor.lnum = eap->line2;
6720 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 }
6722
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724}
6725
6726#ifdef FEAT_BYTEOFF
6727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006728ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
6730 goto_byte(eap->line2);
6731}
6732#endif
6733
6734/*
6735 * ":shell".
6736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006738ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740 do_shell(NULL, 0);
6741}
6742
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006743#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006745static int drop_busy = FALSE;
6746static int drop_filec;
6747static char_u **drop_filev = NULL;
6748static int drop_split;
6749static void (*drop_callback)(void *);
6750static void *drop_cookie;
6751
6752 static void
6753handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 exarg_T ea;
6756 int save_msg_scroll = msg_scroll;
6757
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006758 // Setting the argument list may cause screen updates and being called
6759 // recursively. Avoid that by setting drop_busy.
6760 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006762 // Check whether the current buffer is changed. If so, we will need
6763 // to split the current window or data could be lost.
6764 // We don't need to check if the 'hidden' option is set, as in this
6765 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006766 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 {
6768 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006769 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 --emsg_off;
6771 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006772 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 if (win_split(0, 0) == FAIL)
6775 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006776 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006778 // When splitting the window, create a new alist. Otherwise the
6779 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 alist_unlink(curwin->w_alist);
6781 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 }
6783
6784 /*
6785 * Set up the new argument list.
6786 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006787 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788
6789 /*
6790 * Move to the first file.
6791 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006792 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006793 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 ea.cmd = (char_u *)"next";
6795 do_argfile(&ea, 0);
6796
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006797 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6798 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 need_start_insertmode = FALSE;
6800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006801 // Restore msg_scroll, otherwise a following command may cause scrolling
6802 // unexpectedly. The screen will be redrawn by the caller, thus
6803 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006805
6806 if (drop_callback != NULL)
6807 drop_callback(drop_cookie);
6808
6809 drop_filev = NULL;
6810 drop_busy = FALSE;
6811}
6812
6813/*
6814 * Handle a file drop. The code is here because a drop is *nearly* like an
6815 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006816 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006817 * code is very similar to :args and hence needs access to a lot of the static
6818 * functions in this file.
6819 *
6820 * The "filev" list must have been allocated using alloc(), as should each item
6821 * in the list. This function takes over responsibility for freeing the "filev"
6822 * list.
6823 */
6824 void
6825handle_drop(
6826 int filec, // the number of files dropped
6827 char_u **filev, // the list of files dropped
6828 int split, // force splitting the window
6829 void (*callback)(void *), // to be called after setting the argument
6830 // list
6831 void *cookie) // argument for "callback" (allocated)
6832{
6833 // Cannot handle recursive drops, finish the pending one.
6834 if (drop_busy)
6835 {
6836 FreeWild(filec, filev);
6837 vim_free(cookie);
6838 return;
6839 }
6840
6841 // When calling handle_drop() more than once in a row we only use the last
6842 // one.
6843 if (drop_filev != NULL)
6844 {
6845 FreeWild(drop_filec, drop_filev);
6846 vim_free(drop_cookie);
6847 }
6848
6849 drop_filec = filec;
6850 drop_filev = filev;
6851 drop_split = split;
6852 drop_callback = callback;
6853 drop_cookie = cookie;
6854
6855 // Postpone this when:
6856 // - editing the command line
6857 // - not possible to change the current buffer
6858 // - updating the screen
6859 // As it may change buffers and window structures that are in use and cause
6860 // freed memory to be used.
6861 if (text_locked() || curbuf_locked() || updating_screen)
6862 return;
6863
6864 handle_drop_internal();
6865}
6866
6867/*
6868 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6869 * reset: Handle a postponed drop.
6870 */
6871 void
6872handle_any_postponed_drop(void)
6873{
6874 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006875 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006876 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877}
6878#endif
6879
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 * ":preserve".
6882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006884ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006886 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 ml_preserve(curbuf, TRUE);
6888}
6889
6890/*
6891 * ":recover".
6892 */
6893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006894ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006896 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006898 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6899 | CCGD_MULTWIN
6900 | (eap->forceit ? CCGD_FORCEIT : 0)
6901 | CCGD_EXCMD)
6902
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 && (*eap->arg == NUL
6904 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006905 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 recoverymode = FALSE;
6907}
6908
6909/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006910 * Command modifier used in a wrong way. Also for other commands that can't
6911 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 */
6913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006914ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006916 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917}
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919/*
6920 * :sview [+command] file split window with new file, read-only
6921 * :split [[+command] file] split window with current or new file
6922 * :vsplit [[+command] file] split window vertically with current or new file
6923 * :new [[+command] file] split window with no or new file
6924 * :vnew [[+command] file] split vertically window with no or new file
6925 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006926 *
6927 * :tabedit open new Tab page with empty window
6928 * :tabedit [+command] file open new Tab page and edit "file"
6929 * :tabnew [[+command] file] just like :tabedit
6930 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 */
6932 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006933ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006935 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006937#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006938 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006939 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006940#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006941 int use_tab = eap->cmdidx == CMD_tabedit
6942 || eap->cmdidx == CMD_tabfind
6943 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006945 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006946 return;
6947
Bram Moolenaar4033c552017-09-16 20:54:51 +02006948#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
Bram Moolenaar4033c552017-09-16 20:54:51 +02006952#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006953 // A ":split" in the quickfix window works like ":new". Don't want two
6954 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006955 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {
6957 if (eap->cmdidx == CMD_split)
6958 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 if (eap->cmdidx == CMD_vsplit)
6960 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006964 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006966 char_u *file_to_find = NULL;
6967 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006969 FNAME_MESS, TRUE, curbuf->b_ffname,
6970 &file_to_find, &search_ctx);
6971 vim_free(file_to_find);
6972 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 if (fname == NULL)
6974 goto theend;
6975 eap->arg = fname;
6976 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006977# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006978 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 && eap->cmdidx != CMD_new)
6981 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006982 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006983# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006984 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006985# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006986 au_has_group((char_u *)"FileExplorer"))
6987 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006988 // No browsing supported but we do have the file explorer:
6989 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006990 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006991 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006992 }
6993 else
6994 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006995 fname = do_browse(0, (char_u *)(use_tab
6996 ? _("Edit File in new tab page")
6997 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006999 if (fname == NULL)
7000 goto theend;
7001 eap->arg = fname;
7002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007004 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007007 /*
7008 * Either open new tab page or split the window.
7009 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007010 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007011 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007012 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007013 : eap->addr_count == 0 ? 0
7014 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007015 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007016 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007017
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007018 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007019 if (curwin != old_curwin
7020 && win_valid(old_curwin)
7021 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007022 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007023 old_curwin->w_alt_fnum = curbuf->b_fnum;
7024 }
7025 }
7026 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7028 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007029 // Reset 'scrollbind' when editing another file, but keep it when
7030 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007031 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007032 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 else
7034 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 do_exedit(eap, old_curwin);
7036 }
7037
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007038# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007039 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042theend:
7043 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007045
7046/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007047 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007048 */
7049 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007050tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007051{
7052 exarg_T ea;
7053
Bram Moolenaara80faa82020-04-12 19:37:17 +02007054 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007055 ea.cmdidx = CMD_tabnew;
7056 ea.cmd = (char_u *)"tabn";
7057 ea.arg = (char_u *)"";
7058 ex_splitview(&ea);
7059}
7060
7061/*
7062 * :tabnext command
7063 */
7064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007065ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007066{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007067 int tab_number;
7068
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007069 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007070 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007071 switch (eap->cmdidx)
7072 {
7073 case CMD_tabfirst:
7074 case CMD_tabrewind:
7075 goto_tabpage(1);
7076 break;
7077 case CMD_tablast:
7078 goto_tabpage(9999);
7079 break;
7080 case CMD_tabprevious:
7081 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007082 if (eap->arg && *eap->arg != NUL)
7083 {
7084 char_u *p = eap->arg;
7085 char_u *p_save = p;
7086
7087 tab_number = getdigits(&p);
7088 if (p == p_save || *p_save == '-' || *p != NUL
7089 || tab_number == 0)
7090 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007091 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007092 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007093 return;
7094 }
7095 }
7096 else
7097 {
7098 if (eap->addr_count == 0)
7099 tab_number = 1;
7100 else
7101 {
7102 tab_number = eap->line2;
7103 if (tab_number < 1)
7104 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007105 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007106 return;
7107 }
7108 }
7109 }
7110 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007111 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007112 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007113 tab_number = get_tabpage_arg(eap);
7114 if (eap->errmsg == NULL)
7115 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007116 break;
7117 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007118}
7119
7120/*
7121 * :tabmove command
7122 */
7123 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007124ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007125{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007126 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007127
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007128 tab_number = get_tabpage_arg(eap);
7129 if (eap->errmsg == NULL)
7130 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007131}
7132
7133/*
7134 * :tabs command: List tabs and their contents.
7135 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007137ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007138{
7139 tabpage_T *tp;
7140 win_T *wp;
7141 int tabcount = 1;
7142
7143 msg_start();
7144 msg_scroll = TRUE;
7145 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7146 {
7147 msg_putchar('\n');
7148 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007149 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007150 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007151 ui_breakcheck();
7152
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007153 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007154 wp = firstwin;
7155 else
7156 wp = tp->tp_firstwin;
7157 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7158 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007159 msg_putchar('\n');
7160 msg_putchar(wp == curwin ? '>' : ' ');
7161 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007162 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7163 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007164 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007165 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007166 else
7167 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7168 IObuff, IOSIZE, TRUE);
7169 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007170 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007171 ui_breakcheck();
7172 }
7173 }
7174}
7175
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176/*
7177 * ":mode": Set screen mode.
7178 * If no argument given, just get the screen size and redraw.
7179 */
7180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007181ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182{
7183 if (*eap->arg == NUL)
7184 shell_resized();
7185 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007186 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187}
7188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189/*
7190 * ":resize".
7191 * set, increment or decrement current window height
7192 */
7193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007194ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 int n;
7197 win_T *wp = curwin;
7198
7199 if (eap->addr_count > 0)
7200 {
7201 n = eap->line2;
7202 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7203 ;
7204 }
7205
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007206# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007210 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 {
7212 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007213 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007214 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007216 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 }
7218 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
7220 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007221 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007222 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007224 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 }
7226}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227
7228/*
7229 * ":find [+command] <file>" command.
7230 */
7231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007232ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233{
Colin Kennedy21570352024-03-03 16:16:47 +01007234 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007235 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007236
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 char_u *fname;
7238 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007239 char_u *file_to_find = NULL;
7240 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
7242 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007243 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 if (eap->addr_count > 0)
7245 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007246 // Repeat finding the file "count" times. This matters when it appears
7247 // several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 count = eap->line2;
7249 while (fname != NULL && --count > 0)
7250 {
7251 vim_free(fname);
7252 fname = find_file_in_path(NULL, 0, FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007253 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 }
7255 }
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007256 VIM_CLEAR(file_to_find);
7257 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007259 if (fname == NULL)
7260 return;
7261
7262 eap->arg = fname;
7263 do_exedit(eap, NULL);
7264 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265}
7266
7267/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007268 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007269 */
7270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007271ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007272{
7273 regmatch_T regmatch;
7274 char_u *p;
7275
Bram Moolenaar65088802021-03-13 21:07:21 +01007276#ifdef FEAT_EVAL
7277 if (not_in_vim9(eap) == FAIL)
7278 return;
7279#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007280 curwin->w_cursor.lnum = eap->line2;
7281 beginline(BL_SOL | BL_FIX);
7282 if (*eap->arg == '/')
7283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007284 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007285 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007286 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007287 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007288 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007289 if (regmatch.regprog != NULL)
7290 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007291 // make a copy of the line, when searching for a mark it might be
7292 // flushed
7293 char_u *line = vim_strsave(ml_get_curline());
7294
Bram Moolenaardf177f62005-02-22 08:39:57 +00007295 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007296 if (vim_regexec(&regmatch, line, (colnr_T)0))
7297 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007298 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007299 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007300 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007301 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007302 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007303 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007304 eap->arg += STRLEN(eap->arg);
7305 }
7306 check_cursor();
7307
7308 eap->cmdidx = CMD_visual;
7309 do_exedit(eap, NULL);
7310}
7311
7312/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007313 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 */
7315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007316ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007318 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7319
Colin Kennedy21570352024-03-03 16:16:47 +01007320 // Exclude commands which keep the window's current buffer
7321 if (
7322 eap->cmdidx != CMD_badd
7323 && eap->cmdidx != CMD_balt
7324 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007325 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7326 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007327 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007328
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 do_exedit(eap, NULL);
7330}
7331
7332/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007333 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007336do_exedit(
7337 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007338 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339{
7340 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007342 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007344 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7345 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007346 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 /*
7348 * ":vi" command ends Ex mode.
7349 */
7350 if (exmode_active && (eap->cmdidx == CMD_visual
7351 || eap->cmdidx == CMD_view))
7352 {
7353 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007354 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007356 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007357 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007358 if (global_busy)
7359 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007360 if (eap->nextcmd != NULL)
7361 {
7362 stuffReadbuff(eap->nextcmd);
7363 eap->nextcmd = NULL;
7364 }
7365
7366 if (exmode_was != EXMODE_VIM)
7367 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007368 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007369 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007370 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007371 no_wait_return = 0;
7372 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007373 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007374 msg_scroll = 0;
7375#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007376 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007377 hold_gui_events = 0;
7378#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007379 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007380 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007381
7382 main_loop(FALSE, TRUE);
7383
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007384 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007385 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007386 no_wait_return = save_nwr;
7387 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007388#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007389 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007390#endif
7391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 }
7395
7396 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007397 || eap->cmdidx == CMD_tabnew
7398 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007399 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007401 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 setpcmark();
7403 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007404 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7405 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007407 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 || *eap->arg != NUL
7409#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007410 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411#endif
7412 )
7413 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007414 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7415 // Only ":edit" or ":script" can bring us here, others are stopped
7416 // earlier.
7417 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007418 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007419
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 n = readonlymode;
7421 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7422 readonlymode = TRUE;
7423 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007424 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7425 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007426 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7427 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7429 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007430 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7432 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7433 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007434 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007436 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007437 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007438 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7439 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007440 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007442 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 if (old_curwin != NULL)
7444 {
7445 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007446 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007448#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007449 cleanup_T cs;
7450
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007451 // Reset the error/interrupt/exception state here so that
7452 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007453 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007454#endif
7455#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007457#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007458 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007459
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007460#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007461 // Restore the error/interrupt/exception state if not
7462 // discarded by a new aborting error, interrupt, or
7463 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007464 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 }
7467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 }
7469 else if (readonlymode && curbuf->b_nwindows == 1)
7470 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007471 // When editing an already visited buffer, 'readonly' won't be set
7472 // but the previous value is kept. With ":view" and ":sview" we
7473 // want the file to be readonly, except when another window is
7474 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 curbuf->b_p_ro = TRUE;
7476 }
7477 readonlymode = n;
7478 }
7479 else
7480 {
7481 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007482 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 if (n != curwin->w_arg_idx_invalid)
7486 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 }
7488
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 /*
7490 * if ":split file" worked, set alternate file name in old window to new
7491 * file
7492 */
7493 if (old_curwin != NULL
7494 && *eap->arg != NUL
7495 && curwin != old_curwin
7496 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007497 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007498 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
7501 ex_no_reprint = TRUE;
7502}
7503
7504#ifndef FEAT_GUI
7505/*
7506 * ":gui" and ":gvim" when there is no GUI.
7507 */
7508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007509ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007511 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512}
7513#endif
7514
Bram Moolenaar4f974752019-02-17 17:44:42 +01007515#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007517ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518{
7519 gui_make_tearoff(eap->arg);
7520}
7521#endif
7522
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007523#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7524 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007526ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007528# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7529 if (gui.in_use)
7530 gui_make_popup(eap->arg, eap->forceit);
7531# ifdef FEAT_TERM_POPUP_MENU
7532 else
7533# endif
7534# endif
7535# ifdef FEAT_TERM_POPUP_MENU
7536 pum_make_popup(eap->arg, eap->forceit);
7537# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538}
7539#endif
7540
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007542ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543{
7544 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007545 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007547 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548}
7549
7550/*
7551 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7552 * offset.
7553 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007556ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007559 win_T *save_curwin = curwin;
7560 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 long topline;
7562 long y;
7563 linenr_T old_linenr = curwin->w_cursor.lnum;
7564
7565 setpcmark();
7566
7567 /*
7568 * determine max topline
7569 */
7570 if (curwin->w_p_scb)
7571 {
7572 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007573 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 {
7575 if (wp->w_p_scb && wp->w_buffer)
7576 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007577 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 if (topline > y)
7579 topline = y;
7580 }
7581 }
7582 if (topline < 1)
7583 topline = 1;
7584 }
7585 else
7586 {
7587 topline = 1;
7588 }
7589
7590
7591 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007592 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007594 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 {
7596 if (curwin->w_p_scb)
7597 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007598 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 y = topline - curwin->w_topline;
7600 if (y > 0)
7601 scrollup(y, TRUE);
7602 else
7603 scrolldown(-y, TRUE);
7604 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007605 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 }
7609 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007610 curwin = save_curwin;
7611 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 if (curwin->w_p_scb)
7613 {
7614 did_syncbind = TRUE;
7615 checkpcmark();
7616 if (old_linenr != curwin->w_cursor.lnum)
7617 {
7618 char_u ctrl_o[2];
7619
7620 ctrl_o[0] = Ctrl_O;
7621 ctrl_o[1] = 0;
7622 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7623 }
7624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625}
7626
7627
7628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007629ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007631 int i;
7632 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7633 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007635 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007637 do_bang(1, eap, FALSE, FALSE, TRUE);
7638 return;
7639 }
7640
7641 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7642 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643
7644#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007645 if (cmdmod.cmod_flags & CMOD_BROWSE)
7646 {
7647 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007649 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7650 NULL, NULL, NULL, curbuf);
7651 if (browseFile != NULL)
7652 {
7653 i = readfile(browseFile, NULL,
7654 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7655 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 }
7657 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007658 i = OK;
7659 }
7660 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007662 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007664 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 return;
7666 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007667 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 }
7669 else
7670 {
7671 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7672 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7673 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007674 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
7676 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007677 if (i != OK)
7678 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007679#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007680 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007682 semsg(_(e_cant_open_file_str), eap->arg);
7683 }
7684 else
7685 {
7686 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007687 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007688 // Delete the empty line that remains. Historically ex does
7689 // this but vi doesn't.
7690 if (eap->line2 == 0)
7691 lnum = curbuf->b_ml.ml_line_count;
7692 else
7693 lnum = 1;
7694 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007695 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007696 ml_delete(lnum);
7697 if (curwin->w_cursor.lnum > 1
7698 && curwin->w_cursor.lnum >= lnum)
7699 --curwin->w_cursor.lnum;
7700 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007701 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007702 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007703 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 }
7705}
7706
Bram Moolenaarea408852005-06-25 22:49:46 +00007707static char_u *prev_dir = NULL;
7708
7709#if defined(EXITFREE) || defined(PROTO)
7710 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007711free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007712{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007713 VIM_CLEAR(prev_dir);
7714 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007715}
7716#endif
7717
Bram Moolenaarf4258302013-06-02 18:20:17 +02007718/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007719 * Get the previous directory for the given chdir scope.
7720 */
7721 static char_u *
7722get_prevdir(cdscope_T scope)
7723{
7724 if (scope == CDSCOPE_WINDOW)
7725 return curwin->w_prevdir;
7726 else if (scope == CDSCOPE_TABPAGE)
7727 return curtab->tp_prevdir;
7728 return prev_dir;
7729}
7730
7731/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007732 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007733 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7734 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007735 */
7736 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007737post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007738{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007739 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007740 // Clear tab local directory for both :cd and :tcd
7741 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007742 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007743 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007744 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007745 char_u *pdir = get_prevdir(scope);
7746
7747 // If still in the global directory, need to remember current
7748 // directory as the global directory.
7749 if (globaldir == NULL && pdir != NULL)
7750 globaldir = vim_strsave(pdir);
7751
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007752 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007753 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007754 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007755 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007756 curtab->tp_localdir = vim_strsave(NameBuff);
7757 else
7758 curwin->w_localdir = vim_strsave(NameBuff);
7759 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007760 }
7761 else
7762 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007763 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007764 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007765 }
7766
zeertzjq64be6aa2021-11-19 11:59:08 +00007767 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007768 shorten_fnames(TRUE);
7769}
7770
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007771/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007772 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7773 */
7774 void
7775trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7776{
7777#ifdef FEAT_EVAL
7778 dict_T *v_event;
7779 save_v_event_T save_v_event;
7780
7781 v_event = get_v_event(&save_v_event);
7782 (void)dict_add_string(v_event, "directory", new_dir);
7783 dict_set_items_ro(v_event);
7784#endif
7785 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7786#ifdef FEAT_EVAL
7787 restore_v_event(v_event, &save_v_event);
7788#endif
7789}
7790
7791/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007792 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007793 * chdir() function.
7794 * scope == CDSCOPE_WINDOW: changes the window-local directory
7795 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7796 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007797 * Returns TRUE if the directory is successfully changed.
7798 */
7799 int
7800changedir_func(
7801 char_u *new_dir,
7802 int forceit,
7803 cdscope_T scope)
7804{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007805 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007806 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007807 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007808 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007809 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007810
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007811 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007812 return FALSE;
7813
7814 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7815 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007816 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007817 return FALSE;
7818 }
7819
7820 // ":cd -": Change to previous directory
7821 if (STRCMP(new_dir, "-") == 0)
7822 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007823 pdir = get_prevdir(scope);
7824 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007825 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007826 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007827 return FALSE;
7828 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007829 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007830 }
7831
7832 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007833 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007834 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007835 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007836 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007837
Bakudankun29f3a452021-12-11 12:28:08 +00007838 // For UNIX ":cd" means: go to home directory.
7839 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007840#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007841 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007842#else
7843 if (*new_dir == NUL && p_cdh)
7844#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007845 {
7846 // use NameBuff for home directory name
7847# ifdef VMS
7848 char_u *p;
7849
7850 p = mch_getenv((char_u *)"SYS$LOGIN");
7851 if (p == NULL || *p == NUL) // empty is the same as not set
7852 NameBuff[0] = NUL;
7853 else
7854 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7855# else
7856 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7857# endif
7858 new_dir = NameBuff;
7859 }
zeertzjqf38aad82021-12-30 13:45:57 +00007860 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007861 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7862 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007863 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007864 if (scope == CDSCOPE_WINDOW)
7865 acmd_fname = (char_u *)"window";
7866 else if (scope == CDSCOPE_TABPAGE)
7867 acmd_fname = (char_u *)"tabpage";
7868 else
7869 acmd_fname = (char_u *)"global";
7870 trigger_DirChangedPre(acmd_fname, new_dir);
7871
7872 if (vim_chdir(new_dir))
7873 {
7874 emsg(_(e_command_failed));
7875 vim_free(pdir);
7876 return FALSE;
7877 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007878 }
zeertzjq73257142022-02-02 13:16:37 +00007879
7880 if (scope == CDSCOPE_WINDOW)
7881 pp = &curwin->w_prevdir;
7882 else if (scope == CDSCOPE_TABPAGE)
7883 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007884 else
zeertzjq73257142022-02-02 13:16:37 +00007885 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007886 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007887 *pp = pdir;
7888
7889 post_chdir(scope);
7890
7891 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007892 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007893 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007894 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007895}
Bram Moolenaarea408852005-06-25 22:49:46 +00007896
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007898 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007900 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007901ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007903 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904
7905 new_dir = eap->arg;
7906#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007907 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7908 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007910 ex_pwd(NULL);
7911 return;
7912 }
7913#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007914
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007915 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007916
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007917 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7918 scope = CDSCOPE_WINDOW;
7919 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7920 scope = CDSCOPE_TABPAGE;
7921
7922 if (changedir_func(new_dir, eap->forceit, scope))
7923 {
7924 // Echo the new current directory if the command was typed.
7925 if (KeyTyped || p_verbose >= 5)
7926 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 }
7928}
7929
7930/*
7931 * ":pwd".
7932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007934ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
7936 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7937 {
7938#ifdef BACKSLASH_IN_FILENAME
7939 slash_adjust(NameBuff);
7940#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007941 if (p_verbose > 0)
7942 {
7943 char *context = "global";
7944
Bram Moolenaar05268152021-11-18 18:53:45 +00007945 if (last_chdir_reason != NULL)
7946 context = last_chdir_reason;
7947 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007948 context = "window";
7949 else if (curtab->tp_localdir != NULL)
7950 context = "tabpage";
7951 smsg("[%s] %s", context, (char *)NameBuff);
7952 }
7953 else
7954 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 }
7956 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007957 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958}
7959
7960/*
7961 * ":=".
7962 */
7963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007964ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007966 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007967 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968}
7969
7970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007971ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007973 int n;
7974 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975
7976 if (cursor_valid())
7977 {
7978 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7979 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007980 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007982
7983 len = eap->line2;
7984 switch (*eap->arg)
7985 {
7986 case 'm': break;
7987 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007988 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007989 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007990
7991 // Hide the cursor if invoked with !
7992 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993}
7994
7995/*
7996 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007997 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 */
7999 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008000do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001{
Bram Moolenaar52953192019-08-16 10:27:13 +02008002 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008003 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008004# ifdef ELAPSED_FUNC
8005 elapsed_T start_tv;
8006
8007 // Remember at what time we started, so that we know how much longer we
8008 // should wait after waiting for a bit.
8009 ELAPSED_INIT(start_tv);
8010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008012 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008013 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008014 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008015 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008016
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008017 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008018 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008020 wait_now = msec - done > 1000L ? 1000L : msec - done;
8021#ifdef FEAT_TIMERS
8022 {
8023 long due_time = check_due_timer();
8024
8025 if (due_time > 0 && due_time < wait_now)
8026 wait_now = due_time;
8027 }
8028#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008029#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008030 if (has_any_channel() && wait_now > 20L)
8031 wait_now = 20L;
8032#endif
8033#ifdef FEAT_SOUND
8034 if (has_any_sound_callback() && wait_now > 20L)
8035 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008036#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008037 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008038
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008039#ifdef FEAT_JOB_CHANNEL
8040 if (has_any_channel())
8041 ui_breakcheck_force(TRUE);
8042 else
8043#endif
8044 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008045#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008046 // Process the netbeans and clientserver messages that may have been
8047 // received in the call to ui_breakcheck() when the GUI is in use. This
8048 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008049 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008050#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008051
8052# ifdef ELAPSED_FUNC
8053 // actual time passed
8054 done = ELAPSED_FUNC(start_tv);
8055# else
8056 // guestimate time passed (will actually be more)
8057 done += wait_now;
8058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008060
8061 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8062 // input buffer, otherwise a following call to input() fails.
8063 if (got_int)
8064 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008065
8066 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008067 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068}
8069
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070/*
8071 * ":winsize" command (obsolete).
8072 */
8073 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008074ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076 int w, h;
8077 char_u *arg = eap->arg;
8078 char_u *p;
8079
Keith Thompson184f71c2024-01-04 21:19:04 +01008080 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008081 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008082 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008083 return;
8084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 w = getdigits(&arg);
8086 arg = skipwhite(arg);
8087 p = arg;
8088 h = getdigits(&arg);
8089 if (*p != NUL && *arg == NUL)
8090 set_shellsize(w, h, TRUE);
8091 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008092 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093}
8094
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008096ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097{
8098 int xchar = NUL;
8099 char_u *p;
8100
8101 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8102 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008103 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 if (eap->arg[1] == NUL)
8105 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008106 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 return;
8108 }
8109 xchar = eap->arg[1];
8110 p = eap->arg + 2;
8111 }
8112 else
8113 p = eap->arg + 1;
8114
Bram Moolenaar63b91732021-08-05 20:40:03 +02008115 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008117 if (*p != NUL && *p != (
8118#ifdef FEAT_EVAL
8119 in_vim9script() ? '#' :
8120#endif
8121 '"')
8122 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008123 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008124 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008126 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008127 postponed_split_flags = cmdmod.cmod_split;
8128 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8130 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008131 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 }
8133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134
Bram Moolenaar843ee412004-06-30 16:16:41 +00008135#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136/*
8137 * ":winpos".
8138 */
8139 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008140ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141{
8142 int x, y;
8143 char_u *arg = eap->arg;
8144 char_u *p;
8145
8146 if (*arg == NUL)
8147 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008148# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008149# ifdef VIMDLL
8150 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8151 mch_get_winpos(&x, &y) != FAIL)
8152# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008154# else
8155 if (mch_get_winpos(&x, &y) != FAIL)
8156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {
8158 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008159 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 }
8161 else
8162# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008163 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 }
8165 else
8166 {
8167 x = getdigits(&arg);
8168 arg = skipwhite(arg);
8169 p = arg;
8170 y = getdigits(&arg);
8171 if (*p == NUL || *arg != NUL)
8172 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008173 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 return;
8175 }
8176# ifdef FEAT_GUI
8177 if (gui.in_use)
8178 gui_mch_set_winpos(x, y);
8179 else if (gui.starting)
8180 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008181 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 gui_win_x = x;
8183 gui_win_y = y;
8184 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008185# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 else
8187# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008188# endif
8189# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008190 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191# endif
8192# ifdef HAVE_TGETENT
8193 if (*T_CWP)
8194 term_set_winpos(x, y);
8195# endif
8196 }
8197}
8198#endif
8199
8200/*
8201 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8202 */
8203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008204ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205{
8206 oparg_T oa;
8207
8208 clear_oparg(&oa);
8209 oa.regname = eap->regname;
8210 oa.start.lnum = eap->line1;
8211 oa.end.lnum = eap->line2;
8212 oa.line_count = eap->line2 - eap->line1 + 1;
8213 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008215 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {
8217 setpcmark();
8218 curwin->w_cursor.lnum = eap->line1;
8219 beginline(BL_SOL | BL_FIX);
8220 }
8221
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008222 if (VIsual_active)
8223 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008224
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 switch (eap->cmdidx)
8226 {
8227 case CMD_delete:
8228 oa.op_type = OP_DELETE;
8229 op_delete(&oa);
8230 break;
8231
8232 case CMD_yank:
8233 oa.op_type = OP_YANK;
8234 (void)op_yank(&oa, FALSE, TRUE);
8235 break;
8236
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008237 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008238 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008240 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8241#else
8242 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008244 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 oa.op_type = OP_RSHIFT;
8246 else
8247 oa.op_type = OP_LSHIFT;
8248 op_shift(&oa, FALSE, eap->amount);
8249 break;
8250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008252 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253}
8254
8255/*
8256 * ":put".
8257 */
8258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008259ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008261 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 if (eap->line2 == 0)
8263 {
8264 eap->line2 = 1;
8265 eap->forceit = TRUE;
8266 }
8267 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008268 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008269 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008270 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271}
8272
8273/*
8274 * Handle ":copy" and ":move".
8275 */
8276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008277ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278{
8279 long n;
8280
Bram Moolenaar491799b2020-08-01 19:23:43 +02008281#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008282 if (not_in_vim9(eap) == FAIL)
8283 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008284#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008285 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008286 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {
8288 eap->nextcmd = NULL;
8289 return;
8290 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008291 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292
8293 /*
8294 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8295 */
8296 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8297 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008298 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 return;
8300 }
8301
8302 if (eap->cmdidx == CMD_move)
8303 {
8304 if (do_move(eap->line1, eap->line2, n) == FAIL)
8305 return;
8306 }
8307 else
8308 ex_copy(eap->line1, eap->line2, n);
8309 u_clearline();
8310 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008311 ex_may_print(eap);
8312}
8313
8314/*
8315 * Print the current line if flags were given to the Ex command.
8316 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008317 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008318ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008319{
8320 if (eap->flags != 0)
8321 {
8322 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8323 (eap->flags & EXFLAG_LIST));
8324 ex_no_reprint = TRUE;
8325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326}
8327
8328/*
8329 * ":smagic" and ":snomagic".
8330 */
8331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008332ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008334 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008336 magic_overruled = eap->cmdidx == CMD_smagic
8337 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008338 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008339 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340}
8341
8342/*
8343 * ":join".
8344 */
8345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008346ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347{
8348 curwin->w_cursor.lnum = eap->line1;
8349 if (eap->line1 == eap->line2)
8350 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008351 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 return;
8353 if (eap->line2 == curbuf->b_ml.ml_line_count)
8354 {
8355 beep_flush();
8356 return;
8357 }
8358 ++eap->line2;
8359 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008360 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008362 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363}
8364
8365/*
8366 * ":[addr]@r" or ":[addr]*r": execute register
8367 */
8368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008369ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
8371 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008372 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
8374 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008375 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376
8377#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008378 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379#endif
8380
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008381 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 c = *eap->arg;
8383 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8384 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008385 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008386 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8387 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008390 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008393 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008395 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008397 /*
8398 * Execute from the typeahead buffer.
8399 * Continue until the stuff buffer is empty and all added characters
8400 * have been consumed.
8401 */
8402 while (!stuff_empty() || typebuf.tb_len > prev_len)
8403 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8404
8405 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406}
8407
8408/*
8409 * ":!".
8410 */
8411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008412ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413{
8414 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8415}
8416
8417/*
8418 * ":undo".
8419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008421ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008423 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008424 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008425 else
8426 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427}
8428
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008429#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008431ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008432{
8433 char_u hash[UNDO_HASH_SIZE];
8434
8435 u_compute_hash(hash);
8436 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8437}
8438
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008440ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008441{
8442 char_u hash[UNDO_HASH_SIZE];
8443
8444 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008445 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008446}
8447#endif
8448
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449/*
8450 * ":redo".
8451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008453ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 u_redo(1);
8456}
8457
8458/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008459 * ":earlier" and ":later".
8460 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008462ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008463{
8464 long count = 0;
8465 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008466 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008467 char_u *p = eap->arg;
8468
8469 if (*p == NUL)
8470 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008471 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008472 {
8473 count = getdigits(&p);
8474 switch (*p)
8475 {
8476 case 's': ++p; sec = TRUE; break;
8477 case 'm': ++p; sec = TRUE; count *= 60; break;
8478 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008479 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8480 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008481 }
8482 }
8483
8484 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008485 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008486 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008487 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8488 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008489}
8490
8491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 * ":redir": start/stop redirection.
8493 */
8494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008495ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496{
8497 char *mode;
8498 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008499 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500
Bram Moolenaarba768492016-07-08 15:32:54 +02008501#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008502 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008503 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008504 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008505 return;
8506 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008507#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008508
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 if (STRICMP(eap->arg, "END") == 0)
8510 close_redir();
8511 else
8512 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008513 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008515 ++arg;
8516 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008518 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 mode = "a";
8520 }
8521 else
8522 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008523 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524
8525 close_redir();
8526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008527 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008528 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 if (fname == NULL)
8530 return;
8531#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008532 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 {
8534 char_u *browseFile;
8535
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008536 browseFile = do_browse(BROWSE_SAVE,
8537 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008538 fname, NULL, NULL,
8539 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008541 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 vim_free(fname);
8543 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008544 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 }
8546#endif
8547
8548 redir_fd = open_exfile(fname, eap->forceit, mode);
8549 vim_free(fname);
8550 }
8551#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008552 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008554 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008556 ++arg;
8557 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008559 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008560 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008562 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008564 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008565 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008566 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008567 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008569 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008570 if (*arg == '>')
8571 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008572 // Make register empty when not using @A-@Z and the
8573 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008574 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008575 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008577 }
8578 if (*arg != NUL)
8579 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008580 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008581 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008582 }
8583 }
8584 else if (*arg == '=' && arg[1] == '>')
8585 {
8586 int append;
8587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008588 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008589 close_redir();
8590 arg += 2;
8591
8592 if (*arg == '>')
8593 {
8594 ++arg;
8595 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 }
8597 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008598 append = FALSE;
8599
8600 if (var_redir_start(skipwhite(arg), append) == OK)
8601 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 }
8603#endif
8604
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008605 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008608 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008610
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008611 // Make sure redirection is not off. Can happen for cmdline completion
8612 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008613 if (redir_fd != NULL
8614#ifdef FEAT_EVAL
8615 || redir_reg || redir_vname
8616#endif
8617 )
8618 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619}
8620
8621/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008622 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008624 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008625ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008627 redraw_cmd(eap->forceit);
8628}
8629
8630/*
8631 * ":redraw": force redraw, with clear if "clear" is TRUE.
8632 */
8633 void
8634redraw_cmd(int clear)
8635{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008636 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008638
8639 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008641
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008642 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008644 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 if (need_maketitle)
8646 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008647#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8648# ifdef VIMDLL
8649 if (!gui.in_use)
8650# endif
8651 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008652#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008653 RedrawingDisabled = save_RedrawingDisabled;
8654 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655
Bram Moolenaar5017c662022-04-07 18:06:08 +01008656 // After drawing the statusline screen_attr may still be set.
8657 screen_stop_highlight();
8658
Bram Moolenaara2a89732022-08-31 14:46:18 +01008659 // Reset msg_didout, so that a message that's there is overwritten.
8660 msg_didout = FALSE;
8661 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008663 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008664 need_wait_return = FALSE;
8665
Bram Moolenaara653e532022-04-19 11:38:24 +01008666 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008667 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008668 redrawcmdline();
8669
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 out_flush();
8671}
8672
8673/*
8674 * ":redrawstatus": force redraw of status line(s)
8675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008677ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 if (eap->forceit)
8680 status_redraw_all();
8681 else
8682 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008683 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008684 return; // redraw later
8685
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008686 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008687 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008688
8689 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008690 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008691
zeertzjq320d9102022-09-20 17:12:13 +01008692 if (State & MODE_CMDLINE)
8693 redraw_statuslines();
8694 else
8695 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008696 RedrawingDisabled = save_RedrawingDisabled;
8697 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699}
8700
Bram Moolenaare12bab32019-01-08 22:02:56 +01008701/*
8702 * ":redrawtabline": force redraw of the tabline
8703 */
8704 static void
8705ex_redrawtabline(exarg_T *eap UNUSED)
8706{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008707 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008708 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008709
8710 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008711 p_lz = FALSE;
8712
8713 draw_tabline();
8714
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008715 RedrawingDisabled = save_RedrawingDisabled;
8716 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008717 out_flush();
8718}
8719
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008721close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722{
8723 if (redir_fd != NULL)
8724 {
8725 fclose(redir_fd);
8726 redir_fd = NULL;
8727 }
8728#ifdef FEAT_EVAL
8729 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008730 if (redir_vname)
8731 {
8732 var_redir_stop();
8733 redir_vname = 0;
8734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735#endif
8736}
8737
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008738#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008739 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008740vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008741{
8742 if (vim_mkdir(name, prot) != 0)
8743 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008744 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008745 return FAIL;
8746 }
8747 return OK;
8748}
8749#endif
8750
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751/*
8752 * Open a file for writing for an Ex command, with some checks.
8753 * Return file descriptor, or NULL on failure.
8754 */
8755 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008756open_exfile(
8757 char_u *fname,
8758 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008759 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
8761 FILE *fd;
8762
8763#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008764 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 if (mch_isdir(fname))
8766 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008767 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 return NULL;
8769 }
8770#endif
8771 if (!forceit && *mode != 'a' && vim_fexists(fname))
8772 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008773 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 return NULL;
8775 }
8776
8777 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008778 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779
8780 return fd;
8781}
8782
8783/*
8784 * ":mark" and ":k".
8785 */
8786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008787ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
8789 pos_T pos;
8790
Bram Moolenaar10b94212021-02-19 21:42:57 +01008791#ifdef FEAT_EVAL
8792 if (not_in_vim9(eap) == FAIL)
8793 return;
8794#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008795 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008797 emsg(_(e_argument_required));
8798 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008800
8801 if (eap->arg[1] != NUL) // more than one character?
8802 {
8803 semsg(_(e_trailing_characters_str), eap->arg);
8804 return;
8805 }
8806
8807 pos = curwin->w_cursor; // save curwin->w_cursor
8808 curwin->w_cursor.lnum = eap->line2;
8809 beginline(BL_WHITE | BL_FIX);
8810 if (setmark(*eap->arg) == FAIL) // set mark
8811 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
8812 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813}
8814
8815/*
8816 * Update w_topline, w_leftcol and the cursor position.
8817 */
8818 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008819update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008821 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 update_topline();
8823 if (!curwin->w_p_wrap)
8824 validate_cursor();
8825 update_curswant();
8826}
8827
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008829 * Save the current State and go to Normal mode.
8830 * Return TRUE if the typeahead could be saved.
8831 */
8832 int
8833save_current_state(save_state_T *sst)
8834{
8835 sst->save_msg_scroll = msg_scroll;
8836 sst->save_restart_edit = restart_edit;
8837 sst->save_msg_didout = msg_didout;
8838 sst->save_State = State;
8839 sst->save_insertmode = p_im;
8840 sst->save_finish_op = finish_op;
8841 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008842 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008843 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008844
Bram Moolenaar4324d872020-12-01 20:12:24 +01008845 msg_scroll = FALSE; // no msg scrolling in Normal mode
8846 restart_edit = 0; // don't go to Insert mode
8847 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008848
Bram Moolenaara452b802020-12-01 21:47:59 +01008849 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008850 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008851
8852 /*
8853 * Save the current typeahead. This is required to allow using ":normal"
8854 * from an event handler and makes sure we don't hang when the argument
8855 * ends with half a command.
8856 */
8857 save_typeahead(&sst->tabuf);
8858 return sst->tabuf.typebuf_valid;
8859}
8860
8861 void
8862restore_current_state(save_state_T *sst)
8863{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008864 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008865 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008866
8867 msg_scroll = sst->save_msg_scroll;
8868 restart_edit = sst->save_restart_edit;
8869 p_im = sst->save_insertmode;
8870 finish_op = sst->save_finish_op;
8871 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008872 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008873 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008874 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008875 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008876
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008877 // Restore the state (needed when called from a function executed for
8878 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008879 State = sst->save_State;
8880#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008881 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008882#endif
8883}
8884
8885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 * ":normal[!] {commands}": Execute normal mode commands.
8887 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008888 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008889ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008891 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 char_u *arg = NULL;
8893 int l;
8894 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008896 if (ex_normal_lock > 0)
8897 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008898 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008899 return;
8900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 if (ex_normal_busy >= p_mmd)
8902 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008903 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 return;
8905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 /*
8908 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8909 * this for the K_SPECIAL leading byte, otherwise special keys will not
8910 * work.
8911 */
8912 if (has_mbyte)
8913 {
8914 int len = 0;
8915
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008916 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 for (p = eap->arg; *p != NUL; ++p)
8918 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008919#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008920 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008922#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008923 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008924 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008925#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 )
8929 len += 2;
8930 }
8931 if (len > 0)
8932 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008933 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 if (arg != NULL)
8935 {
8936 len = 0;
8937 for (p = eap->arg; *p != NUL; ++p)
8938 {
8939 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008940#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 if (*p == CSI)
8942 {
8943 arg[len++] = KS_EXTRA;
8944 arg[len++] = (int)KE_CSI;
8945 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008946#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008947 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 {
8949 arg[len++] = *++p;
8950 if (*p == K_SPECIAL)
8951 {
8952 arg[len++] = KS_SPECIAL;
8953 arg[len++] = KE_FILLER;
8954 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008955#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 else if (*p == CSI)
8957 {
8958 arg[len++] = KS_EXTRA;
8959 arg[len++] = (int)KE_CSI;
8960 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 }
8963 arg[len] = NUL;
8964 }
8965 }
8966 }
8967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008969 ++ex_normal_busy;
8970 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 {
8972 /*
8973 * Repeat the :normal command for each line in the range. When no
8974 * range given, execute it just once, without positioning the cursor
8975 * first.
8976 */
8977 do
8978 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 if (eap->addr_count != 0)
8980 {
8981 curwin->w_cursor.lnum = eap->line1++;
8982 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008983 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 }
8985
Bram Moolenaar13505972019-01-24 15:04:48 +01008986 exec_normal_cmd(arg != NULL
8987 ? arg
8988 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 }
8990 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8991 }
8992
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008993 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 update_topline_cursor();
8995
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008996 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008998 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008999#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009000 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009001#endif
9002
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004}
9005
9006/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009007 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 */
9009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009010ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009012 if (eap->forceit)
9013 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009014 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009015 if (!curwin->w_cursor.lnum)
9016 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009017 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009018 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009019#ifdef FEAT_TERMINAL
9020 // Ignore this when running in an active terminal.
9021 if (term_job_running(curbuf->b_term))
9022 return;
9023#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009024
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009025 // Ignore the command when already in Insert mode. Inserting an
9026 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009027 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009028 return;
9029
Bram Moolenaar64969662005-12-14 21:59:55 +00009030 if (eap->cmdidx == CMD_startinsert)
9031 restart_edit = 'a';
9032 else if (eap->cmdidx == CMD_startreplace)
9033 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009035 restart_edit = 'V';
9036
9037 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009039 if (eap->cmdidx == CMD_startinsert)
9040 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009041 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009043
9044 if (VIsual_active)
9045 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046}
9047
9048/*
9049 * ":stopinsert"
9050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009052ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054 restart_edit = 0;
9055 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009056 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009059/*
9060 * Execute normal mode command "cmd".
9061 * "remap" can be REMAP_NONE or REMAP_YES.
9062 */
9063 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009064exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009065{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009066 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009067 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009068 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009069}
Bram Moolenaar25281632016-01-21 23:32:32 +01009070
Bram Moolenaar25281632016-01-21 23:32:32 +01009071/*
9072 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009073 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009074 */
9075 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009076exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009077{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009078 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009079 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009080
Bram Moolenaar905dd902019-04-07 14:21:47 +02009081 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9082 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009083 clear_oparg(&oa);
9084 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009085 while ((!stuff_empty()
9086 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009087 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009088 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009089 {
9090 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009091#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009092 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009093 && oa.op_type == OP_NOP && oa.regname == NUL
9094 && !VIsual_active)
9095 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009096 // If terminal_loop() returns OK we got a key that is handled
9097 // in Normal model. With FAIL we first need to position the
9098 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009099 if (terminal_loop(TRUE) == OK)
9100 normal_cmd(&oa, TRUE);
9101 }
9102 else
9103#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009104 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009105 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009106 }
9107}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009108
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109#ifdef FEAT_FIND_ID
9110 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009111ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112{
9113 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9114 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009115 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116}
9117
Bram Moolenaar4033c552017-09-16 20:54:51 +02009118#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119/*
9120 * ":psearch"
9121 */
9122 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009123ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124{
9125 g_do_tagpreview = p_pvh;
9126 ex_findpat(eap);
9127 g_do_tagpreview = 0;
9128}
9129#endif
9130
9131 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009132ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133{
9134 int whole = TRUE;
9135 long n;
9136 char_u *p;
9137 int action;
9138
9139 switch (cmdnames[eap->cmdidx].cmd_name[2])
9140 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009141 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9143 action = ACTION_GOTO;
9144 else
9145 action = ACTION_SHOW;
9146 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009147 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 action = ACTION_SHOW_ALL;
9149 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009150 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 action = ACTION_GOTO;
9152 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009153 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 action = ACTION_SPLIT;
9155 break;
9156 }
9157
9158 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009159 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 {
9161 n = getdigits(&eap->arg);
9162 eap->arg = skipwhite(eap->arg);
9163 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009164 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 {
9166 whole = FALSE;
9167 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009168 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 if (*p)
9170 {
9171 *p++ = NUL;
9172 p = skipwhite(p);
9173
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009174 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009175 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009176 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009178 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 }
9180 }
9181 if (!eap->skip)
9182 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9183 whole, !eap->forceit,
9184 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009185 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186}
9187#endif
9188
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189
Bram Moolenaar4033c552017-09-16 20:54:51 +02009190#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191/*
9192 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9193 */
9194 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009195ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009197 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9199}
9200
9201/*
9202 * ":pedit"
9203 */
9204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009205ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206{
9207 win_T *curwin_save = curwin;
9208
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009209 if (ERROR_IF_ANY_POPUP_WINDOW)
9210 return;
9211
Bram Moolenaar026587b2019-08-17 15:08:00 +02009212 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009214 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009215
Bram Moolenaar026587b2019-08-17 15:08:00 +02009216 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009218
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 if (curwin != curwin_save && win_valid(curwin_save))
9220 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009221 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009223 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 win_enter(curwin_save, TRUE);
9225 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009226# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009227 else if (WIN_IS_POPUP(curwin))
9228 {
9229 // can't keep focus in popup window
9230 win_enter(firstwin, TRUE);
9231 }
9232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 g_do_tagpreview = 0;
9234}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
9237/*
9238 * ":stag", ":stselect" and ":stjump".
9239 */
9240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009241ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242{
9243 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009244 postponed_split_flags = cmdmod.cmod_split;
9245 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9247 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009248 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250
9251/*
9252 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9253 */
9254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009255ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
9257 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9258}
9259
9260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009261ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262{
9263 int cmd;
9264
9265 switch (name[1])
9266 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009267 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009269 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009271 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009273 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009275 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009277 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009279 case 'f': // ":tfirst"
9280 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009282 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009284 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009286 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009288 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 return;
9290 }
9291#endif
9292 cmd = DT_TAG;
9293 break;
9294 }
9295
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009296 if (name[0] == 'l')
9297 {
9298#ifndef FEAT_QUICKFIX
9299 ex_ni(eap);
9300 return;
9301#else
9302 cmd = DT_LTAG;
9303#endif
9304 }
9305
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9307 eap->forceit, TRUE);
9308}
9309
John Marriotted908f72024-04-18 22:46:56 +02009310enum {
9311 SPEC_PERC = 0,
9312 SPEC_HASH,
9313 SPEC_CWORD, // cursor word
9314 SPEC_CCWORD, // cursor WORD
9315 SPEC_CEXPR, // expr under cursor
9316 SPEC_CFILE, // cursor path name
9317 SPEC_SFILE, // ":so" file name
9318 SPEC_SLNUM, // ":so" file line number
9319 SPEC_STACK, // call stack
9320 SPEC_SCRIPT, // script file name
9321 SPEC_AFILE, // autocommand file name
9322 SPEC_ABUF, // autocommand buffer number
9323 SPEC_AMATCH, // autocommand match name
9324 SPEC_SFLNUM, // script file line number
9325 SPEC_SID // script ID: <SNR>123_
9326#ifdef FEAT_CLIENTSERVER
9327 , SPEC_CLIENT
9328#endif
9329};
9330
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009332 * Check "str" for starting with a special cmdline variable.
9333 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9334 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9335 */
9336 int
Mike Williams51024bb2024-05-30 07:46:30 +02009337find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009338{
John Marriotted908f72024-04-18 22:46:56 +02009339 // must be sorted by the 'value' field because it is used by bsearch()!
9340 static keyvalue_T spec_str_tab[] = {
9341 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9342 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9343 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9344 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9345 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9346 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9347 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009348#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009349 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009350#endif
John Marriotted908f72024-04-18 22:46:56 +02009351 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9352 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9353 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9354 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9355 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9356 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009357 };
John Marriotted908f72024-04-18 22:46:56 +02009358 keyvalue_T target;
9359 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009360
John Marriotted908f72024-04-18 22:46:56 +02009361 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009362 {
John Marriotted908f72024-04-18 22:46:56 +02009363 case '%':
9364 *usedlen = 1;
9365 return SPEC_PERC;
9366
9367 case '#':
9368 *usedlen = 1;
9369 return SPEC_HASH;
9370
9371 case '<':
9372 target.key = 0;
9373 target.value = (char *)src + 1; // skip over '<'
9374 target.length = 0; // not used, see cmp_keyvalue_value_n()
9375
9376 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab, ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]), cmp_keyvalue_value_n);
9377 if (entry == NULL)
9378 return -1;
9379
9380 *usedlen = entry->length + 1;
9381 return entry->key;
9382
9383 default:
9384 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009385 }
John Marriotted908f72024-04-18 22:46:56 +02009386
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009387 return -1;
9388}
9389
9390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 * Evaluate cmdline variables.
9392 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009393 * change "%" to curbuf->b_ffname
9394 * "#" to curwin->w_alt_fnum
9395 * "%%" to curwin->w_alt_fnum in Vim9 script
9396 * "<cword>" to word under the cursor
9397 * "<cWORD>" to WORD under the cursor
9398 * "<cexpr>" to C-expression under the cursor
9399 * "<cfile>" to path name under the cursor
9400 * "<sfile>" to sourced file name
9401 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009402 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009403 * "<slnum>" to sourced file line number
9404 * "<afile>" to file name for autocommand
9405 * "<abuf>" to buffer number for autocommand
9406 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 *
9408 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9409 * "" for error without a message) and NULL is returned.
9410 * Returns an allocated string if a valid match was found.
9411 * Returns NULL if no match was found. "usedlen" then still contains the
9412 * number of characters to skip.
9413 */
9414 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009415eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009416 char_u *src, // pointer into commandline
9417 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009418 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009419 linenr_T *lnump, // line number for :e command, or NULL
9420 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009421 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009422 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009423 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424{
9425 int i;
9426 char_u *s;
9427 char_u *result;
9428 char_u *resultbuf = NULL;
9429 int resultlen;
9430 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009431 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009433 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436
9437 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009438 if (escaped != NULL)
9439 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440
9441 /*
9442 * Check if there is something to do.
9443 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009444 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009445 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
9447 *usedlen = 1;
9448 return NULL;
9449 }
9450
9451 /*
9452 * Skip when preceded with a backslash "\%" and "\#".
9453 * Note: In "\\%" the % is also not recognized!
9454 */
9455 if (src > srcstart && src[-1] == '\\')
9456 {
9457 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009458 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 return NULL;
9460 }
9461
9462 /*
9463 * word or WORD under cursor
9464 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009465 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9466 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009468 resultlen = find_ident_under_cursor(&result,
9469 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9470 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9471 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 if (resultlen == 0)
9473 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009474 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 return NULL;
9476 }
9477 }
9478
9479 /*
9480 * '#': Alternate file name
9481 * '%': Current file name
9482 * File name under the cursor
9483 * File name for autocommand
9484 * and following modifiers
9485 */
9486 else
9487 {
Mike Williams51024bb2024-05-30 07:46:30 +02009488 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009489
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 switch (spec_idx)
9491 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009492 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009493#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009494 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009497 // '%': current file
9498 if (curbuf->b_fname == NULL)
9499 {
9500 result = (char_u *)"";
9501 valid = 0; // Must have ":p:h" to be valid
9502 }
9503 else
9504 {
9505 result = curbuf->b_fname;
9506 tilde_file = STRCMP(result, "~") == 0;
9507 }
9508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009510#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009511 // "%%" alternate file
9512 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009513#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009514 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009515 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009516 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009518 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 result = arg_all();
9520 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009521 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009522 if (escaped != NULL)
9523 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 break;
9526 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009527 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009528 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009529 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009531 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009532 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009533 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009534 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009536 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009538 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009539 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009540 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009541 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009542 return NULL;
9543 }
9544#ifdef FEAT_EVAL
9545 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9546 (long)i);
9547 if (result == NULL)
9548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009549 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009550 return NULL;
9551 }
9552#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009553 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 }
9557 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009558 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009559 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9560 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009561 buf = buflist_findnr(i);
9562 if (buf == NULL)
9563 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009564 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009565 return NULL;
9566 }
9567 if (lnump != NULL)
9568 *lnump = ECMD_LAST;
9569 if (buf->b_fname == NULL)
9570 {
9571 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009572 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009573 }
9574 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009575 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009576 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009577 tilde_file = STRCMP(result, "~") == 0;
9578 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 break;
9581
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009582 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009583 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 if (result == NULL)
9585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009586 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 return NULL;
9588 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009589 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009592 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009594 if (result != NULL && !autocmd_fname_full)
9595 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009596 // Still need to turn the fname into a full path. It is
9597 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009598 autocmd_fname_full = TRUE;
9599 result = FullName_save(autocmd_fname, FALSE);
9600 vim_free(autocmd_fname);
9601 autocmd_fname = result;
9602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 if (result == NULL)
9604 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009605 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 return NULL;
9607 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009608 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 break;
9610
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009611 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 if (autocmd_bufnr <= 0)
9613 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009614 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 return NULL;
9616 }
9617 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9618 result = strbuf;
9619 break;
9620
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009621 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 result = autocmd_match;
9623 if (result == NULL)
9624 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009625 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 return NULL;
9627 }
9628 break;
9629
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009630 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009631 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 if (result == NULL)
9633 {
LemonBoy6013d002022-04-09 21:42:10 +01009634 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9635 return NULL;
9636 }
9637 resultbuf = result; // remember allocated string
9638 break;
9639 case SPEC_STACK: // call stack
9640 result = estack_sfile(ESTACK_STACK);
9641 if (result == NULL)
9642 {
9643 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9644 return NULL;
9645 }
9646 resultbuf = result; // remember allocated string
9647 break;
9648 case SPEC_SCRIPT: // script file name
9649 result = estack_sfile(ESTACK_SCRIPT);
9650 if (result == NULL)
9651 {
9652 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 return NULL;
9654 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009655 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009657
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009658 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009659 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009660 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009661 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009662 return NULL;
9663 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009664 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009665 result = strbuf;
9666 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009667
9668#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009669 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009670 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009671 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009672 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009673 return NULL;
9674 }
9675 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009676 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009677 result = strbuf;
9678 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009679
Bram Moolenaar90944302020-08-01 20:45:11 +02009680 case SPEC_SID:
9681 if (current_sctx.sc_sid <= 0)
9682 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009683 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009684 return NULL;
9685 }
9686 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9687 result = strbuf;
9688 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009689#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009690
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009691#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009692 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009693 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9694 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 result = strbuf;
9696 break;
9697#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009698
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009699 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009700 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009701 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 }
9703
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009704 resultlen = (int)STRLEN(result); // length of new string
9705 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 {
9707 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 resultlen = (int)(s - result);
9710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 else if (!skip_mod)
9712 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009713 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 &resultlen);
9715 if (result == NULL)
9716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009717 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 return NULL;
9719 }
9720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 }
9722
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009723 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009725 if (empty_is_error)
9726 {
9727 if (valid != VALID_HEAD + VALID_PATH)
9728 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9729 else
9730 *errormsg = _(e_evaluates_to_an_empty_string);
9731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 result = NULL;
9733 }
9734 else
9735 result = vim_strnsave(result, resultlen);
9736 vim_free(resultbuf);
9737 return result;
9738}
9739
9740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 * Expand the <sfile> string in "arg".
9742 *
9743 * Returns an allocated string, or NULL for any error.
9744 */
9745 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009746expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009748 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +02009750 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +02009752 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +02009754 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +02009755 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 char_u *p;
9757
John Marriotted908f72024-04-18 22:46:56 +02009758 resultlen = STRLEN(arg);
9759 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 if (result == NULL)
9761 return NULL;
9762
9763 for (p = result; *p; )
9764 {
9765 if (STRNCMP(p, "<sfile>", 7) != 0)
9766 ++p;
9767 else
9768 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009769 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009770 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 if (errormsg != NULL)
9772 {
9773 if (*errormsg)
9774 emsg(errormsg);
9775 vim_free(result);
9776 return NULL;
9777 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009778 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 {
9780 p += srclen;
9781 continue;
9782 }
John Marriotted908f72024-04-18 22:46:56 +02009783 repllen = STRLEN(repl);
9784 resultlen += repllen - srclen;
9785 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 if (newres == NULL)
9787 {
9788 vim_free(repl);
9789 vim_free(result);
9790 return NULL;
9791 }
John Marriotted908f72024-04-18 22:46:56 +02009792 len = p - result;
9793 mch_memmove(newres, result, len);
9794 STRCPY(newres + len, repl);
9795 len += repllen;
9796 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 vim_free(repl);
9798 vim_free(result);
9799 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009800 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 }
9802 }
9803
9804 return result;
9805}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009808/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009809 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009810 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009813dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 if (fname == NULL)
9816 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009817 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818}
9819#endif
9820
9821/*
9822 * ":behave {mswin,xterm}"
9823 */
9824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009825ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826{
9827 if (STRCMP(eap->arg, "mswin") == 0)
9828 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009829 set_option_value_give_err((char_u *)"selection",
9830 0L, (char_u *)"exclusive", 0);
9831 set_option_value_give_err((char_u *)"selectmode",
9832 0L, (char_u *)"mouse,key", 0);
9833 set_option_value_give_err((char_u *)"mousemodel",
9834 0L, (char_u *)"popup", 0);
9835 set_option_value_give_err((char_u *)"keymodel",
9836 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 }
9838 else if (STRCMP(eap->arg, "xterm") == 0)
9839 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009840 set_option_value_give_err((char_u *)"selection",
9841 0L, (char_u *)"inclusive", 0);
9842 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9843 set_option_value_give_err((char_u *)"mousemodel",
9844 0L, (char_u *)"extend", 0);
9845 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 }
9847 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009848 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849}
9850
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851static int filetype_detect = FALSE;
9852static int filetype_plugin = FALSE;
9853static int filetype_indent = FALSE;
9854
9855/*
9856 * ":filetype [plugin] [indent] {on,off,detect}"
9857 * on: Load the filetype.vim file to install autocommands for file types.
9858 * off: Load the ftoff.vim file to remove all autocommands for file types.
9859 * plugin on: load filetype.vim and ftplugin.vim
9860 * plugin off: load ftplugof.vim
9861 * indent on: load filetype.vim and indent.vim
9862 * indent off: load indoff.vim
9863 */
9864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009865ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
9867 char_u *arg = eap->arg;
9868 int plugin = FALSE;
9869 int indent = FALSE;
9870
9871 if (*eap->arg == NUL)
9872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009873 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009874 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 filetype_detect ? "ON" : "OFF",
9876 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9877 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9878 return;
9879 }
9880
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009881 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 for (;;)
9883 {
9884 if (STRNCMP(arg, "plugin", 6) == 0)
9885 {
9886 plugin = TRUE;
9887 arg = skipwhite(arg + 6);
9888 continue;
9889 }
9890 if (STRNCMP(arg, "indent", 6) == 0)
9891 {
9892 indent = TRUE;
9893 arg = skipwhite(arg + 6);
9894 continue;
9895 }
9896 break;
9897 }
9898 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9899 {
9900 if (*arg == 'o' || !filetype_detect)
9901 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009902 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 filetype_detect = TRUE;
9904 if (plugin)
9905 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009906 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 filetype_plugin = TRUE;
9908 }
9909 if (indent)
9910 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009911 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 filetype_indent = TRUE;
9913 }
9914 }
9915 if (*arg == 'd')
9916 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009917 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009918 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 }
9920 }
9921 else if (STRCMP(arg, "off") == 0)
9922 {
9923 if (plugin || indent)
9924 {
9925 if (plugin)
9926 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009927 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 filetype_plugin = FALSE;
9929 }
9930 if (indent)
9931 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009932 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 filetype_indent = FALSE;
9934 }
9935 }
9936 else
9937 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009938 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 filetype_detect = FALSE;
9940 }
9941 }
9942 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009943 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944}
9945
9946/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009947 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 */
9949 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009950ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951{
zeertzjq5bf6c212024-03-31 18:41:27 +02009952 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009953 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009954
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009955 char_u *arg = eap->arg;
9956 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9957 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009958
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009959 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9960 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +02009961 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009965ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966{
9967#ifdef FEAT_DIGRAPHS
9968 if (*eap->arg != NUL)
9969 putdigraph(eap->arg);
9970 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009971 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972#else
mityu61065042021-07-19 20:07:21 +02009973 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974#endif
9975}
9976
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009977#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9978 void
9979set_no_hlsearch(int flag)
9980{
9981 no_hlsearch = flag;
9982# ifdef FEAT_EVAL
9983 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9984# endif
9985}
9986
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987/*
9988 * ":nohlsearch"
9989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009991ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009993 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009994 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996#endif
9997
9998#ifdef FEAT_CRYPT
9999/*
10000 * ":X": Get crypt key
10001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010003ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010005 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010006 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007}
10008#endif
10009
10010#ifdef FEAT_FOLDING
10011 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010012ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013{
10014 if (foldManualAllowed(TRUE))
10015 foldCreate(eap->line1, eap->line2);
10016}
10017
10018 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010019ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020{
10021 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10022 eap->forceit, FALSE);
10023}
10024
10025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010026ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027{
10028 linenr_T lnum;
10029
Bram Moolenaar4facea32019-10-12 20:17:40 +020010030# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010031 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010032# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010033
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010034 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10036 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10037 ml_setmarked(lnum);
10038
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010039 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010041 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010042# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010043 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045}
10046#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010048#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010049/*
10050 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10051 * instead of a quickfix command.
10052 */
10053 int
10054is_loclist_cmd(int cmdidx)
10055{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010056 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010057 return FALSE;
10058 return cmdnames[cmdidx].cmd_name[0] == 'l';
10059}
10060#endif
10061
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010062 int
10063get_pressedreturn(void)
10064{
10065 return ex_pressedreturn;
10066}
10067
10068 void
10069set_pressedreturn(int val)
10070{
10071 ex_pressedreturn = val;
10072}