blob: 19b1d85c6868d0e20430c11ad7f706de20aa62b1 [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
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
86static 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/*
464 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
465 * command is given.
466 */
467 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100468do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100469 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 int save_msg_scroll;
472 int prev_msg_row;
473 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100474 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000475
476 if (improved)
477 exmode_active = EXMODE_VIM;
478 else
479 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100480 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100481 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100483 // When using ":global /pat/ visual" and then "Q" we return to continue
484 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000485 if (global_busy)
486 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
488 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100489 ++RedrawingDisabled; // don't redisplay the window
490 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100492 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 ++hold_gui_events;
494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495
Bram Moolenaar32526b32019-01-19 17:43:09 +0100496 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 while (exmode_active)
498 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100499 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000500 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
501 {
502 exmode_active = FALSE;
503 break;
504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 msg_scroll = TRUE;
506 need_wait_return = FALSE;
507 ex_pressedreturn = FALSE;
508 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100509 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 prev_msg_row = msg_row;
511 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 if (improved)
513 {
514 cmdline_row = msg_row;
515 do_cmdline(NULL, getexline, NULL, 0);
516 }
517 else
518 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
519 lines_left = Rows - 1;
520
Bram Moolenaardf177f62005-02-22 08:39:57 +0000521 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100522 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000524 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000525 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if (ex_pressedreturn)
529 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100530 // go up one line, to overwrite the ":<CR>" line, so the
531 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 msg_row = prev_msg_row;
533 if (prev_msg_row == Rows - 1)
534 msg_row--;
535 }
536 msg_col = 0;
537 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
538 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100541 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000542 {
543 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000544 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000545 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000546 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
549
550#ifdef FEAT_GUI
551 --hold_gui_events;
552#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100553 if (RedrawingDisabled > 0)
554 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100556 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 need_wait_return = FALSE;
558 msg_scroll = save_msg_scroll;
559}
560
561/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200562 * Print the executed command for when 'verbose' is set.
563 * When "lnum" is 0 only print the command.
564 */
565 static void
566msg_verbose_cmd(linenr_T lnum, char_u *cmd)
567{
568 ++no_wait_return;
569 verbose_enter_scroll();
570
571 if (lnum == 0)
572 smsg(_("Executing: %s"), cmd);
573 else
574 smsg(_("line %ld: %s"), (long)lnum, cmd);
575 if (msg_silent == 0)
576 msg_puts("\n"); // don't overwrite this
577
578 verbose_leave_scroll();
579 --no_wait_return;
580}
581
582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 * Execute a simple command line. Used for translated commands like "*".
584 */
585 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100586do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 return do_cmdline(cmd, NULL, NULL,
589 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
590}
591
592/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100593 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
594 * This allows for using a range without ":" in Vim9 script.
595 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200596 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100597do_cmd_argument(char_u *cmd)
598{
599 return do_cmdline(cmd, NULL, NULL,
600 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
601}
602
603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * do_cmdline(): execute one Ex command line
605 *
606 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100607 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * 2. Split up in parts separated with '|'.
609 *
610 * This function can be called recursively!
611 *
612 * flags:
613 * DOCMD_VERBOSE - The command will be included in the error message.
614 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100615 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 * DOCMD_KEYTYPED - Don't reset KeyTyped.
617 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
618 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
619 *
620 * return FAIL if cmdline could not be executed, OK otherwise
621 */
622 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100623do_cmdline(
624 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200625 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100626 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100627 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 char_u *next_cmdline; // next cmd to execute
630 char_u *cmdline_copy = NULL; // copy of cmd line
631 int used_getline = FALSE; // used "fgetline" to obtain command
632 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 int count = 0; // line number count
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100635 int did_inc_RedrawingDisabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 int retval = OK;
637#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100638 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100639 garray_T lines_ga; // keep lines for ":while"/":for"
640 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200641 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100642 char_u *fname = NULL; // function or script name
643 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
644 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
645 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200647 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200648 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100650 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200651 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000653 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000655 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100657# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658# define cmd_cookie cookie
659#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100660 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200661#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100662 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
663 // location for storing error messages to be converted to an exception.
664 // This ensures that the do_errthrow() call in do_one_cmd() does not
665 // combine the messages stored by an earlier invocation of do_one_cmd()
666 // with the command name of the later one. This would happen when
667 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 saved_msg_list = msg_list;
669 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670#endif
671
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100672 // It's possible to create an endless loop with ":execute", catch that
673 // here. The value of 200 allows nested function calls, ":source", etc.
674 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100675 if (call_depth >= 200
676#ifdef FEAT_EVAL
677 && call_depth >= p_mfd
678#endif
679 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000681 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100683 // When converting to an exception, we do not include the command name
684 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100685 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 msg_list = saved_msg_list;
687#endif
688 return FAIL;
689 }
690 ++call_depth;
691
692#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100693 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000695 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100699 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100700 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 ++ex_nesting_level;
703
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100704 // Get the function or script name and the address where the next breakpoint
705 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000706 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
708 fname = func_name(real_cookie);
709 breakpoint = func_breakpoint(real_cookie);
710 dbg_tick = func_dbg_tick(real_cookie);
711 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100712 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100714 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 breakpoint = source_breakpoint(real_cookie);
716 dbg_tick = source_dbg_tick(real_cookie);
717 }
718
719 /*
720 * Initialize "force_abort" and "suppress_errthrow" at the top level.
721 */
722 if (!recursive)
723 {
724 force_abort = FALSE;
725 suppress_errthrow = FALSE;
726 }
727
728 /*
729 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000730 * exception handling (used when debugging). Otherwise clear it to avoid
731 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000733 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000734 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000735 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200736 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 initial_trylevel = trylevel;
739
740 /*
741 * "did_throw" will be set to TRUE when an exception is being thrown.
742 */
743 did_throw = FALSE;
744#endif
745 /*
746 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000747 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 * If force_abort is set, we cancel everything.
749 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100750#ifdef FEAT_EVAL
751 did_emsg_cumul += did_emsg;
752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 did_emsg = FALSE;
754
755 /*
756 * KeyTyped is only set when calling vgetc(). Reset it here when not
757 * calling vgetc() (sourced command lines).
758 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100759 if (!(flags & DOCMD_KEYTYPED)
760 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 KeyTyped = FALSE;
762
763 /*
764 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000765 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 * - for multiple commands on one line, separated with '|'
767 * - when repeating until there are no more lines (for ":source")
768 */
769 next_cmdline = cmdline;
770 do
771 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100773 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774#endif
775
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100776 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (next_cmdline == NULL
778#ifdef FEAT_EVAL
779 && !force_abort
780 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000781 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100784 {
785#ifdef FEAT_EVAL
786 did_emsg_cumul += did_emsg;
787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000792 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100793 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * 3. If a line is given: Make a copy, so we can mess with it.
795 */
796
797#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100798 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000799 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100801 // Each '|' separated command is stored separately in lines_ga, to
802 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100803 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100805 // Check if a function has returned or, unless it has an unclosed
806 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000810 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811 func_line_end(real_cookie);
812# endif
813 if (func_has_ended(real_cookie))
814 {
815 retval = FAIL;
816 break;
817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000820 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100821 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822 script_line_end();
823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100825 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
828 retval = FAIL;
829 break;
830 }
831
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (breakpoint != NULL && dbg_tick != NULL
834 && *dbg_tick != debug_tick)
835 {
836 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100838 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 *dbg_tick = debug_tick;
840 }
841
842 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100843 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100845 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100847 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100849 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100850 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100852 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100853 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 *dbg_tick = debug_tick;
855 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000857 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000858 {
859 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100860 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862 script_line_start();
863 }
864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#endif
867
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100868 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 if (next_cmdline == NULL)
870 {
871 /*
872 * Need to set msg_didout for the first line after an ":if",
873 * otherwise the ":if" will be overwritten.
874 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100875 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100877 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#ifdef FEAT_EVAL
879 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
880#else
881 0
882#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100883 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
884 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100886 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100887 // returned for the end of a sourced file or executed function
888 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (KeyTyped && !(flags & DOCMD_REPEAT))
890 need_wait_return = FALSE;
891 retval = FAIL;
892 break;
893 }
894 used_getline = TRUE;
895
896 /*
897 * Keep the first typed line. Clear it when more lines are typed.
898 */
899 if (flags & DOCMD_KEEPLINE)
900 {
901 vim_free(repeat_cmdline);
902 if (count == 0)
903 repeat_cmdline = vim_strsave(next_cmdline);
904 else
905 repeat_cmdline = NULL;
906 }
907 }
908
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100909 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 else if (cmdline_copy == NULL)
911 {
912 next_cmdline = vim_strsave(next_cmdline);
913 if (next_cmdline == NULL)
914 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200915 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 retval = FAIL;
917 break;
918 }
919 }
920 cmdline_copy = next_cmdline;
921
922#ifdef FEAT_EVAL
923 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200924 * Inside a while/for loop, and when the command looks like a ":while"
925 * or ":for", the line is stored, because we may need it later when
926 * looping.
927 *
928 * When there is a '|' and another command, it is stored separately,
929 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000930 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200931 *
932 * Pass a different "fgetline" function to do_one_cmd() below,
933 * that it stores lines in or reads them from "lines_ga". Makes it
934 * possible to define a function inside a while/for loop and handles
935 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200937 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200939 cmd_getline = get_loop_line;
940 cmd_cookie = (void *)&cmd_loop_cookie;
941 cmd_loop_cookie.lines_gap = &lines_ga;
942 cmd_loop_cookie.current_line = current_line;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100943 cmd_loop_cookie.lc_getline = fgetline;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200944 cmd_loop_cookie.cookie = cookie;
945 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
946
947 // Save the current line when encountering it the first time.
948 if (current_line == lines_ga.ga_len
949 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 {
951 retval = FAIL;
952 break;
953 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200954 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200956 else
957 {
958 cmd_getline = fgetline;
959 cmd_cookie = cookie;
960 }
961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 did_endif = FALSE;
963#endif
964
965 if (count++ == 0)
966 {
967 /*
968 * All output from the commands is put below each other, without
969 * waiting for a return. Don't do this when executing commands
970 * from a script or when being called recursive (e.g. for ":e
971 * +command file").
972 */
973 if (!(flags & DOCMD_NOWAIT) && !recursive)
974 {
975 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100976 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100978 msg_scroll = TRUE; // put messages below each other
979 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100981 did_inc_RedrawingDisabled = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 }
983 }
984
Bram Moolenaar823654b2020-05-29 23:03:09 +0200985 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100986 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 /*
989 * 2. Execute one '|' separated command.
990 * do_one_cmd() will return NULL if there is no trailing '|'.
991 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
992 */
993 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100994 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#ifdef FEAT_EVAL
996 &cstack,
997#endif
998 cmd_getline, cmd_cookie);
999 --recursive;
1000
1001#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001002 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001003 // Use "current_line" from "cmd_loop_cookie", it may have been
1004 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001005 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006#endif
1007
1008 if (next_cmdline == NULL)
1009 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001010 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 /*
1013 * If the command was typed, remember it for the ':' register.
1014 * Do this AFTER executing the command to make :@: work.
1015 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 && new_last_cmdline != NULL)
1018 {
1019 vim_free(last_cmdline);
1020 last_cmdline = new_last_cmdline;
1021 new_last_cmdline = NULL;
1022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024 else
1025 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001026 // need to copy the command after the '|' to cmdline_copy, for the
1027 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001028 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 next_cmdline = cmdline_copy;
1030 }
1031
1032
1033#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001034 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001036 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001038 {
1039 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
1045 ++current_line;
1046
1047 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 * An ":endwhile", ":endfor" and ":continue" is handled here.
1049 * If we were executing commands, jump back to the ":while" or
1050 * ":for".
1051 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001055 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // Jump back to the matching ":while" or ":for". Be careful
1058 // not to use a cs_line[] from an entry that isn't a ":while"
1059 // or ":for": It would make "current_line" invalid and can
1060 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if (!did_emsg && !got_int && !did_throw
1062 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001063 && (cstack.cs_flags[cstack.cs_idx]
1064 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 && cstack.cs_line[cstack.cs_idx] >= 0
1066 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1067 {
1068 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001069 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001071 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001073 // Check for the next breakpoint at or after the ":while"
1074 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001075 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
1077 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001078 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 fname,
1080 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1081 *dbg_tick = debug_tick;
1082 }
1083 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001086 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001088 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1089 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 }
1091 }
1092
1093 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001098 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001099 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 }
1101 }
1102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001103 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001104 if (breakpoint != NULL && has_watchexpr())
1105 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001106 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001107 *dbg_tick = debug_tick;
1108 }
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 /*
1111 * When not inside any ":while" loop, clear remembered lines.
1112 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001113 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
1115 if (lines_ga.ga_len > 0)
1116 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001117 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1119 free_cmdlines(&lines_ga);
1120 }
1121 current_line = 0;
1122 }
1123
1124 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001125 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1126 * being restored at the ":endtry". Reset them here and set the
1127 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1128 * This includes the case where a missing ":endif", ":endwhile" or
1129 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 cstack.cs_lflags &= ~CSL_HAD_FINA;
1134 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1135 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 did_throw ? (void *)current_exception : NULL);
1137 did_emsg = got_int = did_throw = FALSE;
1138 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1139 }
1140
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141 // Update global "trylevel" for recursive calls to do_cmdline() from
1142 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 trylevel = initial_trylevel + cstack.cs_trylevel;
1144
1145 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001146 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 * files) is aborted because of an error, an interrupt, or an uncaught
1148 * exception, cancel everything. If it is left normally, reset
1149 * force_abort to get the non-EH compatible abortion behavior for
1150 * the rest of the script.
1151 */
1152 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1153 force_abort = FALSE;
1154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001155 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001157#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
1159 }
1160 /*
1161 * Continue executing command lines when:
1162 * - no CTRL-C typed, no aborting error, no exception thrown or try
1163 * conditionals need to be checked for executing finally clauses or
1164 * catching an interrupt exception
1165 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001166 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 * looping for ":source" command or function call.
1168 */
1169 while (!((got_int
1170#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001171 || (did_emsg && (force_abort || in_vim9script()))
1172 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#endif
1174 )
1175#ifdef FEAT_EVAL
1176 && cstack.cs_trylevel == 0
1177#endif
1178 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001179 && !(did_emsg
1180#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001181 // Keep going when inside try/catch, so that the error can be
1182 // deal with, except when it is a syntax error, it may cause
1183 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001184 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1185#endif
1186 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001187 && (getline_equal(fgetline, cookie, getexmodeline)
1188 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 && (next_cmdline != NULL
1190#ifdef FEAT_EVAL
1191 || cstack.cs_idx >= 0
1192#endif
1193 || (flags & DOCMD_REPEAT)));
1194
1195 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001196 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#ifdef FEAT_EVAL
1198 free_cmdlines(&lines_ga);
1199 ga_clear(&lines_ga);
1200
1201 if (cstack.cs_idx >= 0)
1202 {
1203 /*
1204 * If a sourced file or executed function ran to its end, report the
1205 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001206 * In Vim9 script do not give a second error, executing aborts after
1207 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001209 if (!got_int && !did_throw && !aborting()
1210 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001211 && ((getline_equal(fgetline, cookie, getsourceline)
1212 && !source_finished(fgetline, cookie))
1213 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 && !func_has_ended(real_cookie))))
1215 {
1216 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001217 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001219 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001221 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001223 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225
1226 /*
1227 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1228 * ":endtry" in a sourced file or executed function. If the try
1229 * conditional is in its finally clause, ignore anything pending.
1230 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001231 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 */
1233 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001234 {
1235 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1236
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001237 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001238 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001239 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1240 &cstack.cs_looplevel);
1241 }
1242 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 trylevel = initial_trylevel;
1244 }
1245
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001246 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1247 // lack was reported above and the error message is to be converted to an
1248 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001249 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 ? (char_u *)"endfunction" : (char_u *)NULL);
1251
1252 if (trylevel == 0)
1253 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001254 // Just in case did_throw got set but current_exception wasn't.
1255 if (current_exception == NULL)
1256 did_throw = FALSE;
1257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 /*
1259 * When an exception is being thrown out of the outermost try
1260 * conditional, discard the uncaught exception, disable the conversion
1261 * of interrupts or errors to exceptions, and ensure that no more
1262 * commands are executed.
1263 */
1264 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001265 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
1267 /*
1268 * On an interrupt or an aborting error not converted to an exception,
1269 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001270 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 * when force_abort is set and did_emsg unset in case of an interrupt
1272 * from a finally clause after an error.
1273 */
1274 else if (got_int || (did_emsg && force_abort))
1275 suppress_errthrow = TRUE;
1276 }
1277
1278 /*
1279 * The current cstack will be freed when do_cmdline() returns. An uncaught
1280 * exception will have to be rethrown in the previous cstack. If a function
1281 * has just returned or a script file was just finished and the previous
1282 * cstack belongs to the same function or, respectively, script file, it
1283 * will have to be checked for finally clauses to be executed due to the
1284 * ":return" or ":finish". This is done in do_one_cmd().
1285 */
1286 if (did_throw)
1287 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001288 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001290 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 && ex_nesting_level > func_level(real_cookie) + 1))
1292 {
1293 if (!did_throw)
1294 check_cstack = TRUE;
1295 }
1296 else
1297 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001298 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001299 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 --ex_nesting_level;
1301 /*
1302 * Go to debug mode when returning from a function in which we are
1303 * single-stepping.
1304 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001305 if ((getline_equal(fgetline, cookie, getsourceline)
1306 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001308 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 ? (char_u *)_("End of sourced file")
1310 : (char_u *)_("End of function"));
1311 }
1312
1313 /*
1314 * Restore the exception environment (done after returning from the
1315 * debugger).
1316 */
1317 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001318 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
1320 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001321
1322 // Cleanup if "cs_emsg_silent_list" remains.
1323 if (cstack.cs_emsg_silent_list != NULL)
1324 {
1325 eslist_T *elem, *temp;
1326
1327 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1328 {
1329 temp = elem->next;
1330 vim_free(elem);
1331 }
1332 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001333#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334
1335 /*
1336 * If there was too much output to fit on the command line, ask the user to
1337 * hit return before redrawing the screen. With the ":global" command we do
1338 * this only once after the command is finished.
1339 */
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001340 if (did_inc_RedrawingDisabled)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001342 if (RedrawingDisabled > 0)
1343 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 --no_wait_return;
1345 msg_scroll = FALSE;
1346
1347 /*
1348 * When just finished an ":if"-":else" which was typed, no need to
1349 * wait for hit-return. Also for an error situation.
1350 */
1351 if (retval == FAIL
1352#ifdef FEAT_EVAL
1353 || (did_endif && KeyTyped && !did_emsg)
1354#endif
1355 )
1356 {
1357 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001358 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360 else if (need_wait_return)
1361 {
1362 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001363 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 * here should not overwrite the command that may be shown before
1365 * doing that.
1366 */
1367 msg_didout |= msg_didout_before_start;
1368 wait_return(FALSE);
1369 }
1370 }
1371
Bram Moolenaar2a942252012-11-28 23:03:07 +01001372#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001373 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001374#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 /*
1376 * Reset if_level, in case a sourced script file contains more ":if" than
1377 * ":endif" (could be ":if x | foo | endif").
1378 */
1379 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001380#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 --call_depth;
1383 return retval;
1384}
1385
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001386#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001387/*
1388 * Handle when "did_throw" is set after executing commands.
1389 */
1390 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001391handle_did_throw(void)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001392{
1393 char *p = NULL;
1394 msglist_T *messages = NULL;
ichizok7e5fe382023-04-15 13:17:50 +01001395 ESTACK_CHECK_DECLARATION;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001396
1397 /*
1398 * If the uncaught exception is a user exception, report it as an
1399 * error. If it is an error exception, display the saved error
1400 * message now. For an interrupt exception, do nothing; the
1401 * interrupt message is given elsewhere.
1402 */
1403 switch (current_exception->type)
1404 {
1405 case ET_USER:
1406 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001407 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001408 current_exception->value);
1409 p = (char *)vim_strsave(IObuff);
1410 break;
1411 case ET_ERROR:
1412 messages = current_exception->messages;
1413 current_exception->messages = NULL;
1414 break;
1415 case ET_INTERRUPT:
1416 break;
1417 }
1418
1419 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1420 current_exception->throw_lnum);
ichizok7e5fe382023-04-15 13:17:50 +01001421 ESTACK_CHECK_SETUP;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001422 current_exception->throw_name = NULL;
1423
1424 discard_current_exception(); // uses IObuff if 'verbose'
1425 suppress_errthrow = TRUE;
1426 force_abort = TRUE;
1427
1428 if (messages != NULL)
1429 {
1430 do
1431 {
1432 msglist_T *next = messages->next;
1433 int save_compiling = estack_compiling;
1434
1435 estack_compiling = messages->msg_compiling;
1436 emsg(messages->msg);
1437 vim_free(messages->msg);
1438 vim_free(messages->sfile);
1439 vim_free(messages);
1440 messages = next;
1441 estack_compiling = save_compiling;
1442 }
1443 while (messages != NULL);
1444 }
1445 else if (p != NULL)
1446 {
1447 emsg(p);
1448 vim_free(p);
1449 }
1450 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01001451 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001452 estack_pop();
1453}
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001456 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 */
1458 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001459get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001461 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 wcmd_T *wp;
1463 char_u *line;
1464
1465 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1466 {
1467 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001468 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001470 // First time inside the ":while"/":for": get line normally.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001471 if (cp->lc_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001472 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001474 line = cp->lc_getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001475 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 ++cp->current_line;
1477
1478 return line;
1479 }
1480
1481 KeyTyped = FALSE;
1482 ++cp->current_line;
1483 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001484 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 return vim_strsave(wp->line);
1486}
1487
1488/*
1489 * Store a line in "gap" so that a ":while" loop can execute it again.
1490 */
1491 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001492store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493{
1494 if (ga_grow(gap, 1) == FAIL)
1495 return FAIL;
1496 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001497 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 return OK;
1500}
1501
1502/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001503 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 */
1505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001506free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
1508 while (gap->ga_len > 0)
1509 {
1510 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1511 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513}
1514#endif
1515
1516/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001517 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1518 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001521getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001522 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001523 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001524 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001527 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001528 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001530 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1531 // function that's originally used to obtain the lines. This may be
1532 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001533 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001534 cp = (struct loop_cookie *)cookie;
1535 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001537 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 cp = cp->cookie;
1539 }
1540 return gp == func;
1541#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001542 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#endif
1544}
1545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001547 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 * getline function. Otherwise return "cookie".
1549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001551getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001552 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001553 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
Bram Moolenaar13505972019-01-24 15:04:48 +01001555#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001556 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001557 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001559 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1560 // cookie that's originally used to obtain the lines. This may be nested
1561 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001562 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001563 cp = (struct loop_cookie *)cookie;
1564 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001566 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 cp = cp->cookie;
1568 }
1569 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001570#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001573}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001575#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001576/*
1577 * Get the next line source line without advancing.
1578 */
1579 char_u *
1580getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001581 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001582 void *cookie) // argument for fgetline()
1583{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001584 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001585 struct loop_cookie *cp;
1586 wcmd_T *wp;
1587
1588 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1589 // cookie that's originally used to obtain the lines. This may be nested
1590 // several levels.
1591 gp = fgetline;
1592 cp = (struct loop_cookie *)cookie;
1593 while (gp == get_loop_line)
1594 {
1595 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1596 {
1597 // executing lines a second time, use the stored copy
1598 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1599 return wp->line;
1600 }
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001601 gp = cp->lc_getline;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001602 cp = cp->cookie;
1603 }
1604 if (gp == getsourceline)
1605 return source_nextline(cp);
1606 return NULL;
1607}
1608#endif
1609
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001610
1611/*
1612 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1613 * ":bwipeout", etc.
1614 * Returns the buffer number.
1615 */
1616 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001617compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001618{
1619 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001620 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001621 int count = offset;
1622
1623 buf = firstbuf;
1624 while (buf->b_next != NULL && buf->b_fnum < lnum)
1625 buf = buf->b_next;
1626 while (count != 0)
1627 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001628 count += (offset < 0) ? 1 : -1;
1629 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1630 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001631 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001632 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001633 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001634 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001635 while (buf->b_ml.ml_mfp == NULL)
1636 {
1637 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1638 if (nextbuf == NULL)
1639 break;
1640 buf = nextbuf;
1641 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001642 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001643 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001644 if (addr_type == ADDR_LOADED_BUFFERS)
1645 while (buf->b_ml.ml_mfp == NULL)
1646 {
1647 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1648 if (nextbuf == NULL)
1649 break;
1650 buf = nextbuf;
1651 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001652 return buf->b_fnum;
1653}
1654
Bram Moolenaarb7316892019-05-01 18:08:42 +02001655/*
1656 * Return the window number of "win".
1657 * When "win" is NULL return the number of windows.
1658 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001659 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001660current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001661{
1662 win_T *wp;
1663 int nr = 0;
1664
Bram Moolenaar29323592016-07-24 22:04:11 +02001665 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001666 {
1667 ++nr;
1668 if (wp == win)
1669 break;
1670 }
1671 return nr;
1672}
1673
1674 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001675current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001676{
1677 tabpage_T *tp;
1678 int nr = 0;
1679
Bram Moolenaar29323592016-07-24 22:04:11 +02001680 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001681 {
1682 ++nr;
1683 if (tp == tab)
1684 break;
1685 }
1686 return nr;
1687}
1688
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001689 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001690comment_start(char_u *p, int starts_with_colon UNUSED)
1691{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001692 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001693 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001694 return *p == '"';
1695}
1696
Bram Moolenaarf240e182014-11-27 18:33:02 +01001697# define CURRENT_WIN_NR current_win_nr(curwin)
1698# define LAST_WIN_NR current_win_nr(NULL)
1699# define CURRENT_TAB_NR current_tab_nr(curtab)
1700# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702/*
1703 * Execute one Ex command.
1704 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001705 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1706 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 *
1708 * 1. skip comment lines and leading space
1709 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001710 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001711 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001712 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001713 * 6. parse arguments
1714 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001716 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 *
1718 * This function may be called recursively!
1719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001721do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001722 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001723 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001725 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001727 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001728 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001730 char_u *p;
1731 linenr_T lnum;
1732 long n;
1733 char *errormsg = NULL; // error message
1734 char_u *after_modifier = NULL;
1735 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001736 cmdmod_T save_cmdmod;
1737 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001738 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001739 int ni; // set when Not Implemented
1740 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001741 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001742 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001743#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001744 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001745#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001746 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001747 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaara80faa82020-04-12 19:37:17 +02001749 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 ea.line1 = 1;
1751 ea.line2 = 1;
1752#ifdef FEAT_EVAL
1753 ++ex_nesting_level;
1754#endif
1755
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001756 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 if (quitmore
1758#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001759 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001760 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001761#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001762 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001763 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 --quitmore;
1765
1766 /*
1767 * Reset browse, confirm, etc.. They are restored when returning, for
1768 * recursive calls.
1769 */
1770 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001772 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001773 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1774 goto doend;
1775
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001776/*
1777 * 1. Skip comment lines and leading white space and colons.
1778 * 2. Handle command modifiers.
1779 */
1780 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001782 ea.cmdlinep = cmdlinep;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001783 ea.ea_getline = fgetline;
Bram Moolenaareffed932018-08-14 13:38:17 +02001784 ea.cookie = cookie;
1785#ifdef FEAT_EVAL
1786 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001787 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001789 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001790 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001791 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001792 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794#ifdef FEAT_EVAL
1795 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1796 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1797#else
1798 ea.skip = (if_level > 0);
1799#endif
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001802 * 3. Skip over the range to find the command. Let "p" point to after it.
1803 *
1804 * We need the command to know what kind of range it uses.
1805 */
1806 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001807
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001808 // In Vim9 script a colon is required before the range. This may also be
1809 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001810 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001811 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001812 {
1813 may_have_range = FALSE;
1814 for (p = ea.cmd; p >= *cmdlinep; --p)
1815 {
1816 if (*p == ':')
1817 may_have_range = TRUE;
1818 if (p < ea.cmd && !VIM_ISWHITE(*p))
1819 break;
1820 }
1821 }
1822 else
1823 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001824 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001825 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001827#ifdef FEAT_EVAL
1828 // Handle ":export" - it functions almost like a command modifier.
1829 // ":export var Name: type"
1830 // ":export def Name(..."
1831 // etc.
1832 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1833 is_export = TRUE;
1834#endif
1835
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001836 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001837 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001838 if (ea.cmd == cmd + 1 && *cmd == '$')
1839 // should be "$VAR = val"
1840 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001841#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001842 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001843#else
1844 p = find_ex_command(&ea, NULL, NULL, NULL);
1845#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001846 if (ea.cmdidx == CMD_SIZE)
1847 {
1848 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1849
1850 // If a ':' before the range is missing, give a clearer error
1851 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001852 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001853 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001854 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001855 goto doend;
1856 }
1857 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001858 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001861
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001862#ifdef FEAT_EVAL
1863# ifdef FEAT_PROFILE
1864 // Count this line for profiling if skip is TRUE.
1865 if (do_profiling == PROF_YES
1866 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1867 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1868 {
1869 int skip = did_emsg || got_int || did_throw;
1870
1871 if (ea.cmdidx == CMD_catch)
1872 skip = !skip && !(cstack->cs_idx >= 0
1873 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1874 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1875 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1876 skip = skip || !(cstack->cs_idx >= 0
1877 && !(cstack->cs_flags[cstack->cs_idx]
1878 & (CSF_ACTIVE | CSF_TRUE)));
1879 else if (ea.cmdidx == CMD_finally)
1880 skip = FALSE;
1881 else if (ea.cmdidx != CMD_endif
1882 && ea.cmdidx != CMD_endfor
1883 && ea.cmdidx != CMD_endtry
1884 && ea.cmdidx != CMD_endwhile)
1885 skip = ea.skip;
1886
1887 if (!skip)
1888 {
1889 if (getline_equal(fgetline, cookie, get_func_line))
1890 func_line_exec(getline_cookie(fgetline, cookie));
1891 else if (getline_equal(fgetline, cookie, getsourceline))
1892 script_line_exec();
1893 }
1894 }
1895# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001896#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001897
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001898 ea.cmd = cmd;
1899
1900#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001901 // May go to debug mode. If this happens and the ">quit" debug command is
1902 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001903 dbg_check_breakpoint(&ea);
1904 if (!ea.skip && got_int)
1905 {
1906 ea.skip = TRUE;
1907 (void)do_intthrow(cstack);
1908 }
1909#endif
1910
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001911/*
1912 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 *
1914 * where 'addr' is:
1915 *
1916 * % (entire file)
1917 * $ [+-NUM]
1918 * 'x [+-NUM] (where x denotes a currently defined mark)
1919 * . [+-NUM]
1920 * [+-NUM]..
1921 * NUM
1922 *
1923 * The ea.cmd pointer is updated to point to the first character following the
1924 * range spec. If an initial address is found, but no second, the upper bound
1925 * is equal to the lower.
1926 */
1927
Bram Moolenaar25190db2019-05-04 15:05:28 +02001928 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001929 if (!IS_USER_CMDIDX(ea.cmdidx))
1930 {
1931 if (ea.cmdidx != CMD_SIZE)
1932 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1933 else
1934 ea.addr_type = ADDR_LINES;
1935
Bram Moolenaar25190db2019-05-04 15:05:28 +02001936 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001937 if (ea.cmdidx == CMD_wincmd && p != NULL)
1938 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001939#ifdef FEAT_QUICKFIX
1940 // :.cc in quickfix window uses line number
1941 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1942 ea.addr_type = ADDR_OTHER;
1943#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001944 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001945
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001946 if (!may_have_range)
1947 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001948 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1949 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001952 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 */
1954
1955 /*
1956 * Skip ':' and any white space
1957 */
1958 ea.cmd = skipwhite(ea.cmd);
1959 while (*ea.cmd == ':')
1960 ea.cmd = skipwhite(ea.cmd + 1);
1961
1962 /*
1963 * If we got a line, but no command, then go to the line.
1964 * If we find a '|' or '\n' we set ea.nextcmd.
1965 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001966 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1967 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
1969 /*
1970 * strange vi behaviour:
1971 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001972 * ":3|..." prints line 3 (not in Vim9 script)
1973 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001975 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001977 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 goto doend;
1979 }
1980
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001981 // If this looks like an undefined user command and there are CmdUndefined
1982 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001983 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1984 && ASCII_ISUPPER(*ea.cmd)
1985 && has_cmdundefined())
1986 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001987 int ret;
1988
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001989 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001990 while (ASCII_ISALNUM(*p))
1991 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001992 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001993 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1994 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001995 // If the autocommands did something and didn't cause an error, try
1996 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001997 p = (ret
1998#ifdef FEAT_EVAL
1999 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002000#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002002 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002003
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (p == NULL)
2005 {
2006 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002007 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 goto doend;
2009 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002010 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002011 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2012 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 {
2014 errormsg = uc_fun_cmd();
2015 goto doend;
2016 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002017
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 if (ea.cmdidx == CMD_SIZE)
2019 {
2020 if (!ea.skip)
2021 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002022 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002024 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002025 // If the modifier was parsed OK the error must be in the
2026 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002027 if (after_modifier != NULL)
2028 append_command(after_modifier);
2029 else
2030 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002031 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002032 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002033 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002034 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 }
2036 goto doend;
2037 }
2038
Bram Moolenaar958636c2014-10-21 20:01:58 +02002039 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2040 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002041#ifdef HAVE_EX_SCRIPT_NI
2042 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2043#endif
2044 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
2046#ifndef FEAT_EVAL
2047 /*
2048 * When the expression evaluation is disabled, recognize the ":if" and
2049 * ":endif" commands and ignore everything in between it.
2050 */
2051 if (ea.cmdidx == CMD_if)
2052 ++if_level;
2053 if (if_level)
2054 {
2055 if (ea.cmdidx == CMD_endif)
2056 --if_level;
2057 goto doend;
2058 }
2059
2060#endif
2061
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002062 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002063 if (*p == '!' && ea.cmdidx != CMD_substitute
2064 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
2066 ++p;
2067 ea.forceit = TRUE;
2068 }
2069 else
2070 ea.forceit = FALSE;
2071
2072/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002073 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002075 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002076 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 if (!ea.skip)
2079 {
2080#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002081 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002083 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002084 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 goto doend;
2086 }
2087#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002088 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002089 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002090 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002091 goto doend;
2092 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002093 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002095 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002096 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 goto doend;
2098 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002099
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002100 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002102 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2103 {
2104 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002105 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002106 goto doend;
2107 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002108 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2109 {
2110 // Command not allowed when text is locked
2111 errormsg = _(get_text_locked_msg());
2112 goto doend;
2113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002115
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002116 // Disallow editing another buffer when "curbuf_lock" is set.
2117 // Do allow ":checktime" (it is postponed).
2118 // Do allow ":edit" (check for an argument later).
2119 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002120 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002121 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002122 && ea.cmdidx != CMD_edit
2123 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002124 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002125 && curbuf_locked())
2126 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002128 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002130 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 goto doend;
2132 }
2133 }
2134
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002135 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002137 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 goto doend;
2139 }
2140
2141 /*
2142 * Don't complain about the range if it is not used
2143 * (could happen if line_count is accidentally set to 0).
2144 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002145 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
2147 /*
2148 * If the range is backwards, ask for confirmation and, if given, swap
2149 * ea.line1 & ea.line2 so it's forwards again.
2150 * When global command is busy, don't ask, will fail below.
2151 */
2152 if (!global_busy && ea.line1 > ea.line2)
2153 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002154 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002156 if (sourcing || exmode_active)
2157 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002158 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002159 goto doend;
2160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 if (ask_yesno((char_u *)
2162 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002163 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
2165 lnum = ea.line1;
2166 ea.line1 = ea.line2;
2167 ea.line2 = lnum;
2168 }
2169 if ((errormsg = invalid_range(&ea)) != NULL)
2170 goto doend;
2171 }
2172
Bram Moolenaarb7316892019-05-01 18:08:42 +02002173 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2174 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 ea.line2 = 1;
2176
2177 correct_range(&ea);
2178
2179#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002180 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002181 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002183 // Put the first line at the start of a closed fold, put the last line
2184 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 (void)hasFolding(ea.line1, &ea.line1, NULL);
2186 (void)hasFolding(ea.line2, NULL, &ea.line2);
2187 }
2188#endif
2189
2190#ifdef FEAT_QUICKFIX
2191 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002192 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 * option here, so things like % get expanded.
2194 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002195 p = replace_makeprg(&ea, p, cmdlinep);
2196 if (p == NULL)
2197 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#endif
2199
2200 /*
2201 * Skip to start of argument.
2202 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2203 */
2204 if (ea.cmdidx == CMD_bang)
2205 ea.arg = p;
2206 else
2207 ea.arg = skipwhite(p);
2208
Bram Moolenaar379fb762018-08-30 15:58:28 +02002209 // ":file" cannot be run with an argument when "curbuf_lock" is set
2210 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2211 goto doend;
2212
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 /*
2214 * Check for "++opt=val" argument.
2215 * Must be first, allow ":w ++enc=utf8 !cmd"
2216 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002217 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2219 if (getargopt(&ea) == FAIL && !ni)
2220 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002221 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 goto doend;
2223 }
2224
2225 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002227 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002229 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002231 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 goto doend;
2233 }
2234 ea.arg = skipwhite(ea.arg + 1);
2235 ea.append = TRUE;
2236 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002237 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
2239 ++ea.arg;
2240 ea.usefilter = TRUE;
2241 }
2242 }
2243
2244 if (ea.cmdidx == CMD_read)
2245 {
2246 if (ea.forceit)
2247 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002248 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 ea.forceit = FALSE;
2250 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002251 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
2253 ++ea.arg;
2254 ea.usefilter = TRUE;
2255 }
2256 }
2257
2258 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2259 {
2260 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002261 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
2263 ++ea.arg;
2264 ++ea.amount;
2265 }
2266 ea.arg = skipwhite(ea.arg);
2267 }
2268
2269 /*
2270 * Check for "+command" argument, before checking for next command.
2271 * Don't do this for ":read !cmd" and ":write !cmd".
2272 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002273 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2275
2276 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002277 * For commands that do not use '|' inside their argument: Check for '|' to
2278 * separate commands and '"' or '#' to start comments.
2279 *
2280 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002281 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002282 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002283 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002285 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2286 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002287 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002288 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002289 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002290 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002291 || ea.cmdidx == CMD_global
2292 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002293 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002294#ifdef FEAT_EVAL
2295 || inside_block(&ea)
2296#endif
2297 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
2299 for (p = ea.arg; *p; ++p)
2300 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002301 // Remove one backslash before a newline, so that it's possible to
2302 // pass a newline to the shell and also a newline that is preceded
2303 // with a backslash. This makes it impossible to end a shell
2304 // command in a backslash, but that doesn't appear useful.
2305 // Halving the number of backslashes is incompatible with previous
2306 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002308 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002309 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
2311 ea.nextcmd = p + 1;
2312 *p = NUL;
2313 break;
2314 }
2315 }
2316 }
2317
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002318 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002319 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002321 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002322 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002324 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002325 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002326 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002328#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002329 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002330 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002332 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002333 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002336 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2337 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002338 {
2339 ea.regname = *ea.arg++;
2340#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002341 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002342 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2343 {
kuuote08d7b1c2021-10-04 22:17:36 +01002344 if (!ea.skip)
2345 {
2346 set_expr_line(vim_strsave(ea.arg), &ea);
2347 did_set_expr_line = TRUE;
2348 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002349 ea.arg += STRLEN(ea.arg);
2350 }
2351#endif
2352 ea.arg = skipwhite(ea.arg);
2353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
2355
2356 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002357 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 * count, it's a buffer name.
2359 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002361 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002362 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002364 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002366 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002368 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 goto doend;
2370 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002371 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
2373 ea.line2 = n;
2374 if (ea.addr_count == 0)
2375 ea.addr_count = 1;
2376 }
2377 else
2378 {
2379 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002380 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002381 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002382 else
2383 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 ++ea.addr_count;
2385 /*
2386 * Be vi compatible: no error message for out of range.
2387 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002388 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 ea.line2 = curbuf->b_ml.ml_line_count;
2390 }
2391 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002392
2393 /*
2394 * Check for flags: 'l', 'p' and '#'.
2395 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002396 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002397 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002398 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2399 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002401 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002402 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 goto doend;
2404 }
2405
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002406 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002408 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 goto doend;
2410 }
2411
2412#ifdef FEAT_EVAL
2413 /*
2414 * Skip the command when it's not going to be executed.
2415 * The commands like :if, :endif, etc. always need to be executed.
2416 * Also make an exception for commands that handle a trailing command
2417 * themselves.
2418 */
2419 if (ea.skip)
2420 {
2421 switch (ea.cmdidx)
2422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002423 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_while:
2425 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002426 case CMD_for:
2427 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 case CMD_if:
2429 case CMD_elseif:
2430 case CMD_else:
2431 case CMD_endif:
2432 case CMD_try:
2433 case CMD_catch:
2434 case CMD_finally:
2435 case CMD_endtry:
2436 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002437 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 break;
2439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002440 // Commands that handle '|' themselves. Check: A command should
2441 // either have the EX_TRLBAR flag, appear in this list or appear in
2442 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_aboveleft:
2444 case CMD_and:
2445 case CMD_belowright:
2446 case CMD_botright:
2447 case CMD_browse:
2448 case CMD_call:
2449 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002450 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 case CMD_delfunction:
2452 case CMD_djump:
2453 case CMD_dlist:
2454 case CMD_dsearch:
2455 case CMD_dsplit:
2456 case CMD_echo:
2457 case CMD_echoerr:
2458 case CMD_echomsg:
2459 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002460 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002462 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002463 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 case CMD_help:
2465 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002466 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 case CMD_ijump:
2468 case CMD_ilist:
2469 case CMD_isearch:
2470 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002471 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 case CMD_keepjumps:
2473 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002474 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 case CMD_leftabove:
2476 case CMD_let:
2477 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002478 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002479 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002481 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002482 case CMD_noautocmd:
2483 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 case CMD_perl:
2485 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002486 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002487 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002488 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 case CMD_return:
2490 case CMD_rightbelow:
2491 case CMD_ruby:
2492 case CMD_silent:
2493 case CMD_smagic:
2494 case CMD_snomagic:
2495 case CMD_substitute:
2496 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002497 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 case CMD_tcl:
2499 case CMD_throw:
2500 case CMD_tilde:
2501 case CMD_topleft:
2502 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002503 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002504 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 case CMD_verbose:
2506 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002507 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 break;
2509
2510 default: goto doend;
2511 }
2512 }
2513#endif
2514
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002515 if ((ea.argt & EX_XFILE)
2516 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2517 goto doend;
2518
2519#ifdef FEAT_EVAL
2520 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002522 emsg(_(e_invalid_command_after_export));
2523 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 /*
2528 * Accept buffer name. Cannot be used at the same time with a buffer
2529 * number. Don't do this for a user command.
2530 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002531 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002532 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 {
2534 /*
2535 * :bdelete, :bwipeout and :bunload take several arguments, separated
2536 * by spaces: find next space (skipping over escaped characters).
2537 * The others take one argument: ignore trailing spaces.
2538 */
2539 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2540 || ea.cmdidx == CMD_bunload)
2541 p = skiptowhite_esc(ea.arg);
2542 else
2543 {
2544 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002545 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 --p;
2547 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002548 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002549 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002550 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 goto doend;
2552 ea.addr_count = 1;
2553 ea.arg = skipwhite(p);
2554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002556 // The :try command saves the emsg_silent flag, reset it here when
2557 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002558 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002559 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002560 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002561 if (emsg_silent < 0)
2562 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002563 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002564 }
2565
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002567 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
Bram Moolenaar958636c2014-10-21 20:01:58 +02002570 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 {
2572 /*
2573 * Execute a user-defined command.
2574 */
2575 do_ucmd(&ea);
2576 }
2577 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
2579 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002580 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2583 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002584 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586
2587#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002588 // A command will reset "is_export" when exporting an item. If it is still
LemonBoy90c27b22023-08-27 19:28:15 +02002589 // set something went wrong or the command was never executed.
2590 if (!ea.skip && is_export)
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002591 {
2592 if (errormsg == NULL)
2593 errormsg = _(e_export_with_invalid_argument);
2594 is_export = FALSE;
2595 }
2596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002598 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002599 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002600 && current_sctx.sc_sid > 0
2601 && ea.cmdidx != CMD_endif
2602 && (cstack->cs_idx < 0
2603 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002604 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 /*
2607 * If the command just executed called do_cmdline(), any throw or ":return"
2608 * or ":finish" encountered there must also check the cstack of the still
2609 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2610 * exception, or reanimate a returned function or finished script file and
2611 * return or finish it again.
2612 */
2613 if (need_rethrow)
2614 do_throw(cstack);
2615 else if (check_cstack)
2616 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002617 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002619 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 && current_func_returned())
2621 do_return(&ea, TRUE, FALSE, NULL);
2622 }
2623 need_rethrow = check_cstack = FALSE;
2624#endif
2625
2626doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002627 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002630 curwin->w_cursor.col = 0;
2631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
2633 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2634 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002635 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002637 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
2639 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002640 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002642 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
2644 emsg(errormsg);
2645 }
2646#ifdef FEAT_EVAL
2647 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002648 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2649 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002650
2651 if (did_set_expr_line)
2652 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002653 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654#endif
2655
Bram Moolenaare1004402020-10-24 20:49:43 +02002656 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002658 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002659 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002661 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 ea.nextcmd = NULL;
2663
2664#ifdef FEAT_EVAL
2665 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002666 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667#endif
2668
2669 return ea.nextcmd;
2670}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002672static char ex_error_buf[MSG_BUF_LEN];
2673
2674/*
2675 * Return an error message with argument included.
2676 * Uses a static buffer, only the last error will be kept.
2677 * "msg" will be translated, caller should use N_().
2678 */
2679 char *
2680ex_errmsg(char *msg, char_u *arg)
2681{
2682 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2683 return ex_error_buf;
2684}
2685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002687 * Handle a range without a command.
2688 * Returns an error message on failure.
2689 */
2690 char *
2691ex_range_without_command(exarg_T *eap)
2692{
2693 char *errormsg = NULL;
2694
2695 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2696#ifdef FEAT_EVAL
2697 && !in_vim9script()
2698#endif
2699 )
2700 {
2701 eap->cmdidx = CMD_print;
2702 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2703 if ((errormsg = invalid_range(eap)) == NULL)
2704 {
2705 correct_range(eap);
2706 ex_print(eap);
2707 }
2708 }
2709 else if (eap->addr_count != 0)
2710 {
2711 if (eap->line2 > curbuf->b_ml.ml_line_count)
2712 {
2713 // With '-' in 'cpoptions' a line number past the file is an
2714 // error, otherwise put it at the end of the file.
2715 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2716 eap->line2 = -1;
2717 else
2718 eap->line2 = curbuf->b_ml.ml_line_count;
2719 }
2720
2721 if (eap->line2 < 0)
2722 errormsg = _(e_invalid_range);
2723 else
2724 {
2725 if (eap->line2 == 0)
2726 curwin->w_cursor.lnum = 1;
2727 else
2728 curwin->w_cursor.lnum = eap->line2;
2729 beginline(BL_SOL | BL_FIX);
2730 }
2731 }
2732 return errormsg;
2733}
2734
2735/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002736 * Check for an Ex command with optional tail.
2737 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002738 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002739 */
2740 static int
2741checkforcmd_opt(
2742 char_u **pp, // start of command
2743 char *cmd, // name of command
2744 int len, // required length
2745 int noparen)
2746{
2747 int i;
2748
2749 for (i = 0; cmd[i] != NUL; ++i)
2750 if (((char_u *)cmd)[i] != (*pp)[i])
2751 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002752 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002753 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002754 {
2755 *pp = skipwhite(*pp + i);
2756 return TRUE;
2757 }
2758 return FALSE;
2759}
2760
2761/*
2762 * Check for an Ex command with optional tail.
2763 * If there is a match advance "pp" to the argument and return TRUE.
2764 */
2765 int
2766checkforcmd(
2767 char_u **pp, // start of command
2768 char *cmd, // name of command
2769 int len) // required length
2770{
2771 return checkforcmd_opt(pp, cmd, len, FALSE);
2772}
2773
2774/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002775 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002776 * If there is a match advance "pp" to the argument and return TRUE.
2777 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002778 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002779checkforcmd_noparen(
2780 char_u **pp, // start of command
2781 char *cmd, // name of command
2782 int len) // required length
2783{
2784 return checkforcmd_opt(pp, cmd, len, TRUE);
2785}
2786
2787/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002788 * Parse and skip over command modifiers:
2789 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002790 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002792 * When "skip_only" is TRUE the global variables are not changed, except for
2793 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002794 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2795 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002796 * Call apply_cmdmod() to get the side effects of the modifiers:
2797 * - Increment "sandbox" for ":sandbox"
2798 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002799 * - set msg_silent for ":silent"
2800 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002801 * Return FAIL when the command is not to be executed.
2802 * May set "errormsg" to an error message.
2803 */
2804 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002805parse_command_modifiers(
2806 exarg_T *eap,
2807 char **errormsg,
2808 cmdmod_T *cmod,
2809 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002810{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002811 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002812 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002813 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002814 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002815 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002816 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002817
Bram Moolenaare1004402020-10-24 20:49:43 +02002818 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002819 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002820
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002821 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2822 {
2823 // The automatically inserted Visual area range is skipped, so that
2824 // typing ":cmdmod cmd" in Visual mode works without having to move the
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002825 // range to after the modififiers. The command will be
2826 // "'<,'>cmdmod cmd", parse "cmdmod cmd" and then put back "'<,'>"
2827 // before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002828 eap->cmd += 5;
2829 cmd_start = eap->cmd;
2830 has_visual_range = TRUE;
2831 }
2832
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002833 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002834 for (;;)
2835 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002836 char_u *p;
2837
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002839 {
2840 if (*eap->cmd == ':')
2841 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002843 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002844
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002845 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002847 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2848 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002849 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2850 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002851 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002852 if (!skip_only)
2853 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002854 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002855 }
2856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002857 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002858 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002859 {
2860 // a comment ends at a NL
2861 if (eap->nextcmd == NULL)
2862 {
2863 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2864 if (eap->nextcmd != NULL)
2865 ++eap->nextcmd;
2866 }
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002867 if (vim9script)
2868 {
2869 if (has_cmdmod(cmod, FALSE))
2870 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002871#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002872 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2873 && eap->cmd[2] != '{')
2874 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002875#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002876 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002878 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002879 if (*eap->cmd == NUL)
2880 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002881 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002882 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002883 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002884 if (vim9script && has_cmdmod(cmod, FALSE))
2885 *errormsg = _(e_command_modifier_without_command);
2886 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002887 return FAIL;
2888 }
2889
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002890 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002891
2892 // In Vim9 script a variable can shadow a command modifier:
2893 // verbose = 123
2894 // verbose += 123
2895 // silent! verbose = func()
2896 // verbose.member = 2
2897 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002898 // But not:
2899 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002900 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002901 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002902 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002903
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002904 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002905 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002906 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002907 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2908 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002909 break;
2910 }
2911
Bram Moolenaareffed932018-08-14 13:38:17 +02002912 switch (*p)
2913 {
Bram Moolenaar91324262022-09-09 13:27:59 +01002914 // When adding an entry, also modify cmdmods[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002915 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002916 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002917 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002918 continue;
2919
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002920 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002922 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002923 continue;
2924 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002925 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002926 {
2927#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002928 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002929#endif
2930 continue;
2931 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002932 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002933 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002934 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002935 continue;
2936
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002937 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002938 break;
2939#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002940 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002941#endif
2942 continue;
2943
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002944 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002945 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002946 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002947 continue;
2948 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002949 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002950 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002951 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002952 continue;
2953 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002954 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002955 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002956 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002957 continue;
2958 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002959 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002960 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002961 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002962 continue;
2963
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002964 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002965 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002966 char_u *reg_pat;
2967 char_u *nulp = NULL;
2968 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002969
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002970 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002971 || *p == NUL
2972 || (ends_excmd(*p)
2973#ifdef FEAT_EVAL
2974 // in ":filter #pat# cmd" # does not
2975 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002976 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002977#endif
2978 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002979 break;
2980 if (*p == '!')
2981 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002982 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002983 p = skipwhite(p + 1);
2984 if (*p == NUL || ends_excmd(*p))
2985 break;
2986 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002987#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002988 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002989 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002990 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002991#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002992 if (skip_only)
2993 p = skip_vimgrep_pat(p, NULL, NULL);
2994 else
2995 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002996 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2997 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002998 if (p == NULL || *p == NUL)
2999 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003000 if (!skip_only)
3001 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003002 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003003 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003004 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003005 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003006 // restore the character overwritten by NUL
3007 if (nulp != NULL)
3008 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003009 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003010 eap->cmd = p;
3011 continue;
3012 }
3013
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003014 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3015 {
3016 cmod->cmod_split |= WSP_HOR;
3017 continue;
3018 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003019 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003020 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 || *p == NUL || ends_excmd(*p))
3022 break;
3023 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003024 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003025 continue;
3026
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003027 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003028 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003029 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003030 continue;
3031 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003032 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3033 {
3034 if (ends_excmd2(p, eap->cmd))
3035 {
3036 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003037 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003038 return FAIL;
3039 }
3040 cmod->cmod_flags |= CMOD_LEGACY;
3041 continue;
3042 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003043
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003044 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003045 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003046 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003047 continue;
3048
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003049 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003050 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003051 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003052 continue;
3053 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003054 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003055 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003056 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003057 continue;
3058
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003059 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003060 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003061 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003062 continue;
3063
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003064 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003065 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003066 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003067 continue;
3068 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003069 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003070 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003071 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003072 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003074 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003075 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003076 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003077 }
3078 continue;
3079
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003080 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003081 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003082 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003083 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003084 long tabnr = get_address(eap, &eap->cmd,
3085 ADDR_TABS, eap->skip,
3086 skip_only, FALSE, 1);
3087 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003088 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003089 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003090 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003091 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3092 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003093 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003094 return FAIL;
3095 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003096 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003097 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003098 }
3099 eap->cmd = p;
3100 continue;
3101 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003102 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003103 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003104 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003105 continue;
3106
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003107 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003108 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003109 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003110 continue;
3111
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003112 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003113 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003114 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003115 continue;
3116 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003117 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003118 {
3119 if (ends_excmd2(p, eap->cmd))
3120 {
3121 *errormsg =
3122 _(e_vim9cmd_must_be_followed_by_command);
3123 return FAIL;
3124 }
3125 cmod->cmod_flags |= CMOD_VIM9CMD;
3126 continue;
3127 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003128 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003129 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003130 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003131 {
zeertzjqcd749632022-06-14 13:30:35 +01003132 // zero means not set, one is verbose == 0, etc.
3133 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003134 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003135 else
zeertzjqcd749632022-06-14 13:30:35 +01003136 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003137 eap->cmd = p;
3138 continue;
3139 }
3140 break;
3141 }
3142
Bram Moolenaar1501b632022-03-27 16:56:21 +01003143 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003144 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003145 if (eap->cmd > cmd_start)
3146 {
3147 // Move the '<,'> range to after the modifiers and insert a colon.
3148 // Since the modifiers have been parsed put the colon on top of the
3149 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3150 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003151 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003152 {
3153 size_t len = STRLEN(cmd_start);
3154
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003155 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003156 // "'<,'>mods" -> "mods *+
3157 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003158 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003159 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003160 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003161 }
3162 else
3163 {
3164 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3165 eap->cmd -= 5;
3166 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3167 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003168 }
3169 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003170 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003171 // Special case: change empty command to "+".
3172 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003173 eap->cmd = (char_u *)"'<,'>+";
3174 else
3175 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003176 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003177 else if (use_plus_cmd)
3178 eap->cmd = (char_u *)"+";
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003179
Bram Moolenaareffed932018-08-14 13:38:17 +02003180 return OK;
3181}
3182
3183/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003184 * Return TRUE if "cmod" has anything set.
3185 */
3186 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003187has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003188{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003189 return (cmod->cmod_flags != 0 && (!ignore_silent
3190 || (cmod->cmod_flags
3191 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003192 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003193 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003194 || cmod->cmod_tab != 0
3195 || cmod->cmod_filter_regmatch.regprog != NULL;
3196}
3197
Bram Moolenaar8991be22022-02-14 21:51:46 +00003198#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003199/*
3200 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3201 */
3202 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003203cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003204{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003205 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003206 {
3207 emsg(_(e_misplaced_command_modifier));
3208 return TRUE;
3209 }
3210 return FALSE;
3211}
Dominique Pelle748b3082022-01-08 12:41:16 +00003212#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003213
3214/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003215 * Apply the command modifiers. Saves current state in "cmdmod", call
3216 * undo_cmdmod() later.
3217 */
3218 void
3219apply_cmdmod(cmdmod_T *cmod)
3220{
3221#ifdef HAVE_SANDBOX
3222 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3223 {
3224 ++sandbox;
3225 cmod->cmod_did_sandbox = TRUE;
3226 }
3227#endif
zeertzjqcd749632022-06-14 13:30:35 +01003228 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003229 {
3230 if (cmod->cmod_verbose_save == 0)
3231 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003232 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003233 }
3234
3235 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3236 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003237 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003238 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003239 cmod->cmod_save_msg_scroll = msg_scroll;
3240 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003241 if (cmod->cmod_flags & CMOD_SILENT)
3242 ++msg_silent;
3243 if (cmod->cmod_flags & CMOD_UNSILENT)
3244 msg_silent = 0;
3245
3246 if (cmod->cmod_flags & CMOD_ERRSILENT)
3247 {
3248 ++emsg_silent;
3249 ++cmod->cmod_did_esilent;
3250 }
3251
Bram Moolenaare1004402020-10-24 20:49:43 +02003252 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003253 {
3254 // Set 'eventignore' to "all".
3255 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003256 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003257 set_string_option_direct((char_u *)"ei", -1,
3258 (char_u *)"all", OPT_FREE, SID_NONE);
3259 }
3260}
3261
3262/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003263 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003264 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003265 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003266undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003267{
Bram Moolenaare1004402020-10-24 20:49:43 +02003268 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003269 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003270 p_verbose = cmod->cmod_verbose_save - 1;
3271 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003272 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003273
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003274#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003275 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003276 {
3277 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003278 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003279 }
3280#endif
3281
Bram Moolenaare1004402020-10-24 20:49:43 +02003282 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003284 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003285 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3286 OPT_FREE, SID_NONE);
3287 free_string_option(cmod->cmod_save_ei);
3288 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003289 }
3290
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003291 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003292
Bram Moolenaare1004402020-10-24 20:49:43 +02003293 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003294 {
3295 // messages could be enabled for a serious error, need to check if the
3296 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003297 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3298 msg_silent = cmod->cmod_save_msg_silent - 1;
3299 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003300 if (emsg_silent < 0)
3301 emsg_silent = 0;
3302 // Restore msg_scroll, it's set by file I/O commands, even when no
3303 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003304 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003305
3306 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3307 // somewhere in the line. Put it back in the first column.
3308 if (redirecting())
3309 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003310
Bram Moolenaare1004402020-10-24 20:49:43 +02003311 cmod->cmod_save_msg_silent = 0;
3312 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003313 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003314}
3315
3316/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003317 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003318 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003319 * Return FAIL and set "errormsg" or return OK.
3320 */
3321 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003322parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003323{
3324 int address_count = 1;
3325 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003326 int need_check_cursor = FALSE;
3327 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003328
3329 // Repeat for all ',' or ';' separated addresses.
3330 for (;;)
3331 {
3332 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003333 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003334 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003335 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003336 eap->addr_count == 0, address_count++);
3337 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003338 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003339 if (lnum == MAXLNUM)
3340 {
3341 if (*eap->cmd == '%') // '%' - all lines
3342 {
3343 ++eap->cmd;
3344 switch (eap->addr_type)
3345 {
3346 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003347 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003348 eap->line1 = 1;
3349 eap->line2 = curbuf->b_ml.ml_line_count;
3350 break;
3351 case ADDR_LOADED_BUFFERS:
3352 {
3353 buf_T *buf = firstbuf;
3354
3355 while (buf->b_next != NULL
3356 && buf->b_ml.ml_mfp == NULL)
3357 buf = buf->b_next;
3358 eap->line1 = buf->b_fnum;
3359 buf = lastbuf;
3360 while (buf->b_prev != NULL
3361 && buf->b_ml.ml_mfp == NULL)
3362 buf = buf->b_prev;
3363 eap->line2 = buf->b_fnum;
3364 break;
3365 }
3366 case ADDR_BUFFERS:
3367 eap->line1 = firstbuf->b_fnum;
3368 eap->line2 = lastbuf->b_fnum;
3369 break;
3370 case ADDR_WINDOWS:
3371 case ADDR_TABS:
3372 if (IS_USER_CMDIDX(eap->cmdidx))
3373 {
3374 eap->line1 = 1;
3375 eap->line2 = eap->addr_type == ADDR_WINDOWS
3376 ? LAST_WIN_NR : LAST_TAB_NR;
3377 }
3378 else
3379 {
3380 // there is no Vim command which uses '%' and
3381 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003382 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003383 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003384 }
3385 break;
3386 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003387 case ADDR_UNSIGNED:
3388 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003389 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003390 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003391 case ADDR_ARGUMENTS:
3392 if (ARGCOUNT == 0)
3393 eap->line1 = eap->line2 = 0;
3394 else
3395 {
3396 eap->line1 = 1;
3397 eap->line2 = ARGCOUNT;
3398 }
3399 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003400 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003401#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003402 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003403 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003404 if (eap->line2 == 0)
3405 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003406#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003407 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003408 case ADDR_NONE:
3409 // Will give an error later if a range is found.
3410 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003411 }
3412 ++eap->addr_count;
3413 }
3414 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3415 {
3416 pos_T *fp;
3417
3418 // '*' - visual area
3419 if (eap->addr_type != ADDR_LINES)
3420 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003421 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003422 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003423 }
3424
3425 ++eap->cmd;
3426 if (!eap->skip)
3427 {
3428 fp = getmark('<', FALSE);
3429 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003430 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003431 eap->line1 = fp->lnum;
3432 fp = getmark('>', FALSE);
3433 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003434 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003435 eap->line2 = fp->lnum;
3436 ++eap->addr_count;
3437 }
3438 }
3439 }
3440 else
3441 eap->line2 = lnum;
3442 eap->addr_count++;
3443
3444 if (*eap->cmd == ';')
3445 {
3446 if (!eap->skip)
3447 {
3448 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003449
Bram Moolenaar0e717042020-04-27 19:29:01 +02003450 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003451 // accept zero as address, so 0;/PATTERN/ works correctly
3452 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003453 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003454 if (eap->line2 > 0)
3455 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003456 else
3457 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003458 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003459 }
3460 }
3461 else if (*eap->cmd != ',')
3462 break;
3463 ++eap->cmd;
3464 }
3465
3466 // One address given: set start and end lines.
3467 if (eap->addr_count == 1)
3468 {
3469 eap->line1 = eap->line2;
3470 // ... but only implicit: really no address given
3471 if (lnum == MAXLNUM)
3472 eap->addr_count = 0;
3473 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003474 ret = OK;
3475
3476theend:
3477 if (need_check_cursor)
3478 check_cursor();
3479 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003480}
3481
3482/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003483 * Append "cmd" to the error message in IObuff.
3484 * Takes care of limiting the length and handling 0xa0, which would be
3485 * invisible otherwise.
3486 */
3487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003488append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003489{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003490 size_t len = STRLEN(IObuff);
3491 char_u *s = cmd;
3492 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003493
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003494 if (len > IOSIZE - 100)
3495 {
3496 // Not enough space, truncate and put in "...".
3497 d = IObuff + IOSIZE - 100;
3498 d -= mb_head_off(IObuff, d);
3499 STRCPY(d, "...");
3500 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003501 STRCAT(IObuff, ": ");
3502 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003503 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003504 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003505 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003506 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003507 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003508 STRCPY(d, "<a0>");
3509 d += 4;
3510 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003511 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3512 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003513 else
3514 MB_COPY_CHAR(s, d);
3515 }
3516 *d = NUL;
3517}
3518
Dominique Pelle748b3082022-01-08 12:41:16 +00003519#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003520/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003521 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3522 * the name. Otherwise just return "start".
3523 */
3524 char_u *
3525skip_option_env_lead(char_u *start)
3526{
3527 char_u *name = start;
3528
3529 if (*start == '&')
3530 {
3531 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3532 name += 3;
3533 else
3534 name += 1;
3535 }
3536 else if (*start == '$')
3537 name += 1;
3538 return name;
3539}
Dominique Pelle748b3082022-01-08 12:41:16 +00003540#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003541
3542/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003543 * Return TRUE and set "*idx" if "p" points to a one letter command.
3544 * If not in Vim9 script:
3545 * - The 'k' command can directly be followed by any character.
3546 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3547 * but :sre[wind] is another command, as are :scr[iptnames],
3548 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3549 */
3550 static int
3551one_letter_cmd(char_u *p, cmdidx_T *idx)
3552{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003553 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003554 return FALSE;
3555 if (*p == 'k')
3556 {
3557 *idx = CMD_k;
3558 return TRUE;
3559 }
3560 if (p[0] == 's'
3561 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3562 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3563 || p[1] == 'g'
3564 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3565 || p[1] == 'I'
3566 || (p[1] == 'r' && p[2] != 'e')))
3567 {
3568 *idx = CMD_substitute;
3569 return TRUE;
3570 }
3571 return FALSE;
3572}
3573
Dominique Pellee764d1b2023-03-12 21:20:59 +00003574#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003575/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003576 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3577 * call.
3578 */
3579 int
3580number_method(char_u *cmd)
3581{
3582 char_u *p = skipdigits(cmd);
3583
3584 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3585}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003586#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003587
3588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003590 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003591 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003592 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 *
3594 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3595 * assignment without "let". Sets eap->cmdidx to the command while returning
3596 * "eap->cmd".
3597 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 * Returns NULL for an ambiguous user command.
3599 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 char_u *
3601find_ex_command(
3602 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003603 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003604 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003605 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606{
3607 int len;
3608 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003609 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003610#ifndef FEAT_EVAL
3611 int vim9 = FALSE;
3612#else
3613 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 /*
3616 * Recognize a Vim9 script function/method call and assignment:
3617 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3618 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003619 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003620 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003622 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003624 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003625 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3626 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003628 int oplen;
3629 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003630 char_u *swp;
3631
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003632 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003633 || (eap->cmd[0] == '$'
3634 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003635 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003636 && (valid_yank_reg(eap->cmd[1], FALSE)
3637 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003638 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003639 if (*eap->cmd == '&')
3640 {
3641 p = eap->cmd + 1;
3642 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3643 p += 2;
3644 p = to_name_end(p, FALSE);
3645 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003646 else if (*eap->cmd == '$')
3647 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003648 else
3649 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003650 if (ends_excmd(*skipwhite(p)))
3651 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003652 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3653 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003654 eap->cmdidx = CMD_eval;
3655 return eap->cmd;
3656 }
3657 // "&option" can be followed by "->" or "=", check below
3658 }
3659
3660 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003662 if (
3663 // "(..." is an expression.
3664 // "funcname(" is always a function call.
3665 *p == '('
3666 || (p == eap->cmd
3667 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003668 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003669 *eap->cmd == '{'
3670 // "'string'->func()" is an expression.
3671 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003672 // '"string"->func()' is an expression.
3673 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003674 // '$"string"->func()' is an expression.
3675 // "$'string'->func()" is an expression.
3676 || (eap->cmd[0] == '$'
3677 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3678 // '0z1234->func()' is an expression.
3679 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003680 // "g:varname" is an expression.
3681 || eap->cmd[1] == ':'
3682 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003683 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003684 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003685 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003686 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3687 {
3688 // "{" by itself is the start of a block.
3689 eap->cmdidx = CMD_block;
3690 return eap->cmd + 1;
3691 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003692 eap->cmdidx = CMD_eval;
3693 return eap->cmd;
3694 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003695
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003696 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003697 // "varname[]" is an expression.
3698 *p == '['
3699 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003700 || (*p == '.'
3701 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3702 // g:[key] is an expression
3703 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003704 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003705 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003706
3707 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003708 // Skip over the whole thing, it can be:
3709 // name.member = val
3710 // name[a : b] = val
3711 // name[idx] = val
3712 // name[idx].member = val
3713 // etc.
3714 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003715 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003716 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003717 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003718 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003719 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003720 || (after[0] == '.' && after[1] == '.'
3721 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003722 eap->cmdidx = CMD_var;
3723 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003724 --emsg_silent;
3725 return eap->cmd;
3726 }
3727
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003728 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3729 // an assignment.
3730 // If there is no line break inside the "[...]" then "p" is
3731 // advanced to after the "]" by to_name_const_end(): check if a "="
3732 // follows.
3733 // If "[...]" has a line break "p" still points at the "[" and it
3734 // can't be an assignment.
3735 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003736 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003737 char_u *eq;
3738
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003739 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003740 if (p == eap->cmd && *p == '[')
3741 {
3742 int count = 0;
3743 int semicolon = FALSE;
3744
3745 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3746 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003747 eq = p;
3748 if (eq != NULL)
3749 {
3750 eq = skipwhite(eq);
3751 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3752 ++eq;
3753 }
3754 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003755 {
3756 eap->cmdidx = CMD_eval;
3757 return eap->cmd;
3758 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003759 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003760 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003761 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003762 return eap->cmd;
3763 }
3764 }
3765
3766 // Recognize an assignment if we recognize the variable name:
3767 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003768 // "@r = expr"
3769 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003770 // "var = expr" where "var" is a variable name or we are skipping
3771 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003772 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003773 oplen = assignment_len(skipwhite(p), &heredoc);
3774 if (oplen > 0)
3775 {
3776 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3777 || *eap->cmd == '&'
3778 || *eap->cmd == '$'
3779 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003780 || (eap->skip && IS_WHITE_OR_NUL(
3781 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003782 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003783 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003784 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003785 return eap->cmd;
3786 }
3787 }
3788
Bram Moolenaar9510d222022-09-11 15:14:05 +01003789 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003790 // "w:varname: number = 123".
3791 if (eap->cmd[1] == ':' && *p == ':')
3792 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003793 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003794 return eap->cmd;
3795 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003796 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003797
Bram Moolenaar31d99482022-05-26 22:24:43 +01003798 // 1234->func() is a method call
3799 if (number_method(eap->cmd))
3800 {
3801 eap->cmdidx = CMD_eval;
3802 return eap->cmd;
3803 }
3804
Bram Moolenaar7b829262021-10-13 15:04:34 +01003805 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3806 // an expression. A global/substitute/list command needs to use a
3807 // longer name.
3808 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3809 {
3810 eap->cmdidx = CMD_eval;
3811 return eap->cmd;
3812 }
3813
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003814 // If it is an ID it might be a variable with an operator on the next
3815 // line, if the variable exists it can't be an Ex command.
3816 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003817 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003818 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3819 {
3820 eap->cmdidx = CMD_eval;
3821 return eap->cmd;
3822 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003823
3824 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003825 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3826 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003827 {
3828 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3829 return eap->cmd + 2;
3830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 }
3832#endif
3833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 /*
3835 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 */
3837 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003838 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 ++p;
3841 }
3842 else
3843 {
3844 while (ASCII_ISALPHA(*p))
3845 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003846 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003847 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003849 while (ASCII_ISALNUM(*p))
3850 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 }
3852 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3853 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003854 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 ++p;
3856 while (ASCII_ISALPHA(*p))
3857 ++p;
3858 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003859
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003860 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003861 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 ++p;
3863 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003864 // The "d" command can directly be followed by 'l' or 'p' flag, when
3865 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003866 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003867 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003868 // Check for ":dl", ":dell", etc. to ":deletel": that's
3869 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003870 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003871 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003872 break;
3873 if (i == len - 1)
3874 {
3875 --len;
3876 if (p[-1] == 'l')
3877 eap->flags |= EXFLAG_LIST;
3878 else
3879 eap->flags |= EXFLAG_PRINT;
3880 }
3881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003883 if (ASCII_ISLOWER(eap->cmd[0]))
3884 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003885 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003886 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003887
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003888 if (command_count != (int)CMD_SIZE)
3889 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003890 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003891 getout(1);
3892 }
3893
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003894 // Use a precomputed index for fast look-up in cmdnames[]
3895 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003896 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3897 if (ASCII_ISLOWER(c2))
3898 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3899 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003900 else if (ASCII_ISUPPER(eap->cmd[0]))
3901 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003903 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
3905 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3906 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3907 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3908 (size_t)len) == 0)
3909 {
3910#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003911 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 *full = TRUE;
3913#endif
3914 break;
3915 }
3916
Bram Moolenaar204852a2022-03-05 12:56:44 +00003917 // :Print and :mode are not supported in Vim9 script.
3918 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003919 if (vim9 && eap->cmdidx != CMD_SIZE)
3920 {
3921 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3922 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003923 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003924 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003925 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003926 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003927 eap->cmdidx = CMD_SIZE;
3928 }
3929 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003930
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003931 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003932 // 'cpoptions'.
3933 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3934 p = eap->cmd;
3935
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003936 // Look for a user defined command as a last resort. Let ":Print" be
3937 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003938 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3939 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003941 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 while (ASCII_ISALNUM(*p))
3943 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003944 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003946 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 eap->cmdidx = CMD_SIZE;
3948 }
3949
Bram Moolenaar204852a2022-03-05 12:56:44 +00003950 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3951 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003952 eap->cmdidx = CMD_finally;
3953
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003954#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003955 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003956 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02003957 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003958 && (eap->cmdidx < 0 ||
3959 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003960 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003961 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3962
3963 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003964 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003965 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003966 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003967#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003968
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 return p;
3970}
3971
3972#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003973static struct cmdmod
3974{
3975 char *name;
3976 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003977 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003978} cmdmods[] = {
3979 {"aboveleft", 3, FALSE},
3980 {"belowright", 3, FALSE},
3981 {"botright", 2, FALSE},
3982 {"browse", 3, FALSE},
3983 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003984 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003985 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003986 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003987 {"keepalt", 5, FALSE},
3988 {"keepjumps", 5, FALSE},
3989 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003990 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003991 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003992 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003993 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003994 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003995 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003996 {"rightbelow", 6, FALSE},
3997 {"sandbox", 3, FALSE},
3998 {"silent", 3, FALSE},
3999 {"tab", 3, TRUE},
4000 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004001 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004002 {"verbose", 4, TRUE},
4003 {"vertical", 4, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004004 {"vim9cmd", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004005};
4006
4007/*
4008 * Return length of a command modifier (including optional count).
4009 * Return zero when it's not a modifier.
4010 */
4011 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004012modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004013{
4014 int i, j;
4015 char_u *p = cmd;
4016
4017 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004018 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02004019 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004020 {
4021 for (j = 0; p[j] != NUL; ++j)
4022 if (p[j] != cmdmods[i].name[j])
4023 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02004024 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00004025 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00004026 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004027 }
4028 return 0;
4029}
4030
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031/*
4032 * Return > 0 if an Ex command "name" exists.
4033 * Return 2 if there is an exact match.
4034 * Return 3 if there is an ambiguous match.
4035 */
4036 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004037cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038{
4039 exarg_T ea;
4040 int full = FALSE;
4041 int i;
4042 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004043 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004045 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02004046 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
4048 for (j = 0; name[j] != NUL; ++j)
4049 if (name[j] != cmdmods[i].name[j])
4050 break;
4051 if (name[j] == NUL && j >= cmdmods[i].minlen)
4052 return (cmdmods[i].name[j] == NUL ? 2 : 1);
4053 }
4054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004055 // Check built-in commands and user defined commands.
4056 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004057 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004059 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004061 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004063 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4064 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004065 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004066 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4068}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004069
4070/*
4071 * "fullcommand" function
4072 */
4073 void
4074f_fullcommand(typval_T *argvars, typval_T *rettv)
4075{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004076 exarg_T ea;
4077 char_u *name;
4078 char_u *p;
4079 int vim9script = in_vim9script();
4080 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004081
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004082 rettv->v_type = VAR_STRING;
4083 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004084
Bram Moolenaaraa534142022-09-15 21:46:02 +01004085 if (in_vim9script()
4086 && (check_for_string_arg(argvars, 0) == FAIL
4087 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004088 return;
4089
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004090 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004091 if (name == NULL)
4092 return;
4093
Bram Moolenaaraa534142022-09-15 21:46:02 +01004094 if (argvars[1].v_type != VAR_UNKNOWN)
4095 {
4096 vim9script = tv_get_bool(&argvars[1]);
4097 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4098 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4099 }
4100
zeertzjqc024ed92022-01-04 16:22:52 +00004101 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004102 name++;
4103 name = skip_range(name, TRUE, NULL);
4104
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004105 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4106 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004107 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004108 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004109 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004110 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004111 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004112 goto theend;
4113
4114 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004115 {
4116 int res;
4117
4118 ++emsg_silent;
4119 res = not_in_vim9(&ea);
4120 --emsg_silent;
4121
4122 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004123 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004124 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004125
4126 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004127 ? get_user_command_name(ea.useridx, ea.cmdidx)
4128 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004129theend:
4130 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004131}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132#endif
4133
Bram Moolenaard0190392019-08-23 21:17:35 +02004134 cmdidx_T
4135excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136{
Bram Moolenaard0190392019-08-23 21:17:35 +02004137 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004139 if (!one_letter_cmd(cmd, &idx))
4140 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4141 idx = (cmdidx_T)((int)idx + 1))
4142 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4143 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
Bram Moolenaard0190392019-08-23 21:17:35 +02004145 return idx;
4146}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
Bram Moolenaard0190392019-08-23 21:17:35 +02004148 long
4149excmd_get_argt(cmdidx_T idx)
4150{
4151 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152}
4153
4154/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004155 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 *
4157 * Backslashed delimiters after / or ? will be skipped, and commands will
4158 * not be expanded between /'s and ?'s or after "'".
4159 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004160 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 * Returns the "cmd" pointer advanced to beyond the range.
4162 */
4163 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004164skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004165 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004166 int skip_star, // skip "*" used for Visual range
4167 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004169 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004170 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004172 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004174 if (*cmd == '\\')
4175 {
4176 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4177 ++cmd;
4178 else
4179 break;
4180 }
4181 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004183 char_u *p = cmd;
4184
4185 // a quote is only valid at the start or after a separator
4186 while (p > cmd_start)
4187 {
4188 --p;
4189 if (!VIM_ISWHITE(*p))
4190 break;
4191 }
4192 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4193 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 if (*++cmd == NUL && ctx != NULL)
4195 *ctx = EXPAND_NOTHING;
4196 }
4197 else if (*cmd == '/' || *cmd == '?')
4198 {
4199 delim = *cmd++;
4200 while (*cmd != NUL && *cmd != delim)
4201 if (*cmd++ == '\\' && *cmd != NUL)
4202 ++cmd;
4203 if (*cmd == NUL && ctx != NULL)
4204 *ctx = EXPAND_NOTHING;
4205 }
4206 if (*cmd != NUL)
4207 ++cmd;
4208 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004209
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004210 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004211 while (*cmd == ':')
4212 cmd = skipwhite(cmd + 1);
4213
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004214 // Skip "*" used for Visual range.
4215 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4216 cmd = skipwhite(cmd + 1);
4217
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 return cmd;
4219}
4220
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004221 static void
4222addr_error(cmd_addr_T addr_type)
4223{
4224 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004225 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004226 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004227 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004228}
4229
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004231 * Return the default address for an address type.
4232 */
4233 static linenr_T
4234default_address(exarg_T *eap)
4235{
4236 linenr_T lnum = 0;
4237
4238 switch (eap->addr_type)
4239 {
4240 case ADDR_LINES:
4241 case ADDR_OTHER:
4242 // Default is the cursor line number. Avoid using an invalid
4243 // line number though.
4244 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4245 lnum = curbuf->b_ml.ml_line_count;
4246 else
4247 lnum = curwin->w_cursor.lnum;
4248 break;
4249 case ADDR_WINDOWS:
4250 lnum = CURRENT_WIN_NR;
4251 break;
4252 case ADDR_ARGUMENTS:
4253 lnum = curwin->w_arg_idx + 1;
4254 if (lnum > ARGCOUNT)
4255 lnum = ARGCOUNT;
4256 break;
4257 case ADDR_LOADED_BUFFERS:
4258 case ADDR_BUFFERS:
4259 lnum = curbuf->b_fnum;
4260 break;
4261 case ADDR_TABS:
4262 lnum = CURRENT_TAB_NR;
4263 break;
4264 case ADDR_TABS_RELATIVE:
4265 case ADDR_UNSIGNED:
4266 lnum = 1;
4267 break;
4268 case ADDR_QUICKFIX:
4269#ifdef FEAT_QUICKFIX
4270 lnum = qf_get_cur_idx(eap);
4271#endif
4272 break;
4273 case ADDR_QUICKFIX_VALID:
4274#ifdef FEAT_QUICKFIX
4275 lnum = qf_get_cur_valid_idx(eap);
4276#endif
4277 break;
4278 case ADDR_NONE:
4279 // Will give an error later if a range is found.
4280 break;
4281 }
4282 return lnum;
4283}
4284
4285/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004286 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 *
4288 * Set ptr to the next character after the part that was interpreted.
4289 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004290 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 *
4292 * Return MAXLNUM when no Ex address was found.
4293 */
4294 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004295get_address(
4296 exarg_T *eap UNUSED,
4297 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004298 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004299 int skip, // only skip the address, don't use it
4300 int silent, // no errors or side effects
4301 int to_other_file, // flag: may jump to other file
4302 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303{
4304 int c;
4305 int i;
4306 long n;
4307 char_u *cmd;
4308 pos_T pos;
4309 pos_T *fp;
4310 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004311 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
4313 cmd = skipwhite(*ptr);
4314 lnum = MAXLNUM;
4315 do
4316 {
4317 switch (*cmd)
4318 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004319 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004320 ++cmd;
4321 switch (addr_type)
4322 {
4323 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004324 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 lnum = curwin->w_cursor.lnum;
4326 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004327 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004328 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004329 break;
4330 case ADDR_ARGUMENTS:
4331 lnum = curwin->w_arg_idx + 1;
4332 break;
4333 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004334 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004335 lnum = curbuf->b_fnum;
4336 break;
4337 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004338 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004339 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004340 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004341 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004342 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004343 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004344 cmd = NULL;
4345 goto error;
4346 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004347 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004348#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004349 lnum = qf_get_cur_idx(eap);
4350#endif
4351 break;
4352 case ADDR_QUICKFIX_VALID:
4353#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004354 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004355#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004356 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004357 }
4358 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004360 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004361 ++cmd;
4362 switch (addr_type)
4363 {
4364 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004365 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 lnum = curbuf->b_ml.ml_line_count;
4367 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004368 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004369 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004370 break;
4371 case ADDR_ARGUMENTS:
4372 lnum = ARGCOUNT;
4373 break;
4374 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004375 buf = lastbuf;
4376 while (buf->b_ml.ml_mfp == NULL)
4377 {
4378 if (buf->b_prev == NULL)
4379 break;
4380 buf = buf->b_prev;
4381 }
4382 lnum = buf->b_fnum;
4383 break;
4384 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004385 lnum = lastbuf->b_fnum;
4386 break;
4387 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004388 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004389 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004390 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004391 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004392 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004393 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004394 cmd = NULL;
4395 goto error;
4396 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004397 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004398#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004399 lnum = qf_get_size(eap);
4400 if (lnum == 0)
4401 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004402#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004403 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004404 case ADDR_QUICKFIX_VALID:
4405#ifdef FEAT_QUICKFIX
4406 lnum = qf_get_valid_size(eap);
4407 if (lnum == 0)
4408 lnum = 1;
4409#endif
4410 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004411 }
4412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004414 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 if (*++cmd == NUL)
4416 {
4417 cmd = NULL;
4418 goto error;
4419 }
4420 if (addr_type != ADDR_LINES)
4421 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004422 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004423 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004424 goto error;
4425 }
4426 if (skip)
4427 ++cmd;
4428 else
4429 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004430 // Only accept a mark in another file when it is
4431 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004432 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4433 ++cmd;
4434 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004435 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004436 lnum = curwin->w_cursor.lnum;
4437 else
4438 {
4439 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 {
4441 cmd = NULL;
4442 goto error;
4443 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004444 lnum = fp->lnum;
4445 }
4446 }
4447 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004450 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004451 c = *cmd++;
4452 if (addr_type != ADDR_LINES)
4453 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004454 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004455 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004456 goto error;
4457 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004458 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004460 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004461 if (*cmd == c)
4462 ++cmd;
4463 }
4464 else
4465 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004466 int flags;
4467
4468 pos = curwin->w_cursor; // save curwin->w_cursor
4469
4470 // When '/' or '?' follows another address, start from
4471 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004472 if (lnum > 0 && lnum != MAXLNUM)
4473 curwin->w_cursor.lnum =
4474 lnum > curbuf->b_ml.ml_line_count
4475 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004476
4477 // Start a forward search at the end of the line (unless
4478 // before the first line).
4479 // Start a backward search at the start of the line.
4480 // This makes sure we never match in the current
4481 // line, and can match anywhere in the
4482 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004483 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004484 curwin->w_cursor.col = MAXCOL;
4485 else
4486 curwin->w_cursor.col = 0;
4487 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004488 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004489 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 {
4491 curwin->w_cursor = pos;
4492 cmd = NULL;
4493 goto error;
4494 }
4495 lnum = curwin->w_cursor.lnum;
4496 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004497 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004498 cmd += searchcmdlen;
4499 }
4500 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004502 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 ++cmd;
4504 if (addr_type != ADDR_LINES)
4505 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004506 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004507 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004508 goto error;
4509 }
4510 if (*cmd == '&')
4511 i = RE_SUBST;
4512 else if (*cmd == '?' || *cmd == '/')
4513 i = RE_SEARCH;
4514 else
4515 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004516 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004517 cmd = NULL;
4518 goto error;
4519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004521 if (!skip)
4522 {
4523 /*
4524 * When search follows another address, start from
4525 * there.
4526 */
4527 if (lnum != MAXLNUM)
4528 pos.lnum = lnum;
4529 else
4530 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 /*
4533 * Start the search just like for the above
4534 * do_search().
4535 */
4536 if (*cmd != '?')
4537 pos.col = MAXCOL;
4538 else
4539 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004540 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004541 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004542 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004543 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004544 lnum = pos.lnum;
4545 else
4546 {
4547 cmd = NULL;
4548 goto error;
4549 }
4550 }
4551 ++cmd;
4552 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
4554 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004555 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004556 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
4558
4559 for (;;)
4560 {
4561 cmd = skipwhite(cmd);
4562 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4563 break;
4564
4565 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004566 {
4567 switch (addr_type)
4568 {
4569 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004570 case ADDR_OTHER:
4571 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004572 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004573 break;
4574 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004575 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 break;
4577 case ADDR_ARGUMENTS:
4578 lnum = curwin->w_arg_idx + 1;
4579 break;
4580 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004581 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004582 lnum = curbuf->b_fnum;
4583 break;
4584 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004585 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004586 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004587 case ADDR_TABS_RELATIVE:
4588 lnum = 1;
4589 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004590 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004591#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004592 lnum = qf_get_cur_idx(eap);
4593#endif
4594 break;
4595 case ADDR_QUICKFIX_VALID:
4596#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004597 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004598#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004599 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004600 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004601 case ADDR_UNSIGNED:
4602 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004603 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004604 }
4605 }
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004608 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 else
4610 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004611 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 n = 1;
4613 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004614 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004615 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004617 if (n == MAXLNUM)
4618 {
4619 emsg(_(e_line_number_out_of_range));
4620 goto error;
4621 }
4622 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004623
4624 if (addr_type == ADDR_TABS_RELATIVE)
4625 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004626 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004627 cmd = NULL;
4628 goto error;
4629 }
4630 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004631 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004632 lnum = compute_buffer_local_count(
4633 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004635 {
4636#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004637 // Relative line addressing: need to adjust for lines in a
4638 // closed fold after the first address.
4639 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4640 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004641 (void)hasFolding(lnum, NULL, &lnum);
4642#endif
4643 if (i == '-')
4644 lnum -= n;
4645 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004646 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004647 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004648 {
4649 emsg(_(e_line_number_out_of_range));
4650 goto error;
4651 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004652 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004653 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 } while (*cmd == '/' || *cmd == '?');
4657
4658error:
4659 *ptr = cmd;
4660 return lnum;
4661}
4662
4663/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004664 * Set eap->line1 and eap->line2 to the whole range.
4665 * Used for commands with the EX_DFLALL flag and no range given.
4666 */
4667 static void
4668address_default_all(exarg_T *eap)
4669{
4670 eap->line1 = 1;
4671 switch (eap->addr_type)
4672 {
4673 case ADDR_LINES:
4674 case ADDR_OTHER:
4675 eap->line2 = curbuf->b_ml.ml_line_count;
4676 break;
4677 case ADDR_LOADED_BUFFERS:
4678 {
4679 buf_T *buf = firstbuf;
4680
4681 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4682 buf = buf->b_next;
4683 eap->line1 = buf->b_fnum;
4684 buf = lastbuf;
4685 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4686 buf = buf->b_prev;
4687 eap->line2 = buf->b_fnum;
4688 }
4689 break;
4690 case ADDR_BUFFERS:
4691 eap->line1 = firstbuf->b_fnum;
4692 eap->line2 = lastbuf->b_fnum;
4693 break;
4694 case ADDR_WINDOWS:
4695 eap->line2 = LAST_WIN_NR;
4696 break;
4697 case ADDR_TABS:
4698 eap->line2 = LAST_TAB_NR;
4699 break;
4700 case ADDR_TABS_RELATIVE:
4701 eap->line2 = 1;
4702 break;
4703 case ADDR_ARGUMENTS:
4704 if (ARGCOUNT == 0)
4705 eap->line1 = eap->line2 = 0;
4706 else
4707 eap->line2 = ARGCOUNT;
4708 break;
4709 case ADDR_QUICKFIX_VALID:
4710#ifdef FEAT_QUICKFIX
4711 eap->line2 = qf_get_valid_size(eap);
4712 if (eap->line2 == 0)
4713 eap->line2 = 1;
4714#endif
4715 break;
4716 case ADDR_NONE:
4717 case ADDR_UNSIGNED:
4718 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004719 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004720 break;
4721 }
4722}
4723
4724
4725/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004726 * Get flags from an Ex command argument.
4727 */
4728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004729get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004730{
4731 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4732 {
4733 if (*eap->arg == 'l')
4734 eap->flags |= EXFLAG_LIST;
4735 else if (*eap->arg == 'p')
4736 eap->flags |= EXFLAG_PRINT;
4737 else
4738 eap->flags |= EXFLAG_NR;
4739 eap->arg = skipwhite(eap->arg + 1);
4740 }
4741}
4742
4743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 * Function called for command which is Not Implemented. NI!
4745 */
4746 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004747ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
4749 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004750 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004751 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752}
4753
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004754#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755/*
4756 * Function called for script command which is Not Implemented. NI!
4757 * Skips over ":perl <<EOF" constructs.
4758 */
4759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004760ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761{
4762 if (!eap->skip)
4763 ex_ni(eap);
4764 else
4765 vim_free(script_get(eap, eap->arg));
4766}
4767#endif
4768
4769/*
4770 * Check range in Ex command for validity.
4771 * Return NULL when valid, error message when invalid.
4772 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004773 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004774invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004776 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004777
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 if ( eap->line1 < 0
4779 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004780 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004781 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004782
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004783 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004784 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004785 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004786 {
4787 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004788 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004789#ifdef FEAT_DIFF
4790 + (eap->cmdidx == CMD_diffget)
4791#endif
4792 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004793 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004794 break;
4795 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004796 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004797 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004798 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004799 break;
4800 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004801 // Only a boundary check, not whether the buffers actually
4802 // exist.
4803 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004804 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004805 break;
4806 case ADDR_LOADED_BUFFERS:
4807 buf = firstbuf;
4808 while (buf->b_ml.ml_mfp == NULL)
4809 {
4810 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004811 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004812 buf = buf->b_next;
4813 }
4814 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004815 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004816 buf = lastbuf;
4817 while (buf->b_ml.ml_mfp == NULL)
4818 {
4819 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004820 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004821 buf = buf->b_prev;
4822 }
4823 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004824 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004825 break;
4826 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004827 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004828 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004829 break;
4830 case ADDR_TABS:
4831 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004832 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004833 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004834 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004835 case ADDR_OTHER:
4836 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004837 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004838 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004839#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004840 // No error for value that is too big, will use the last entry.
4841 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004842 {
4843 if (eap->addr_count == 0)
4844 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004845 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004846 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004847#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004848 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004849 case ADDR_QUICKFIX_VALID:
4850#ifdef FEAT_QUICKFIX
4851 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4852 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004853 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004854#endif
4855 break;
4856 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004857 case ADDR_NONE:
4858 // Will give an error elsewhere.
4859 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004860 }
4861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 return NULL;
4863}
4864
4865/*
4866 * Correct the range for zero line number, if required.
4867 */
4868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004869correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004871 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
4873 if (eap->line1 == 0)
4874 eap->line1 = 1;
4875 if (eap->line2 == 0)
4876 eap->line2 = 1;
4877 }
4878}
4879
Bram Moolenaar748bf032005-02-02 23:04:36 +00004880#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004881/*
4882 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4883 * pattern. Otherwise return eap->arg.
4884 */
4885 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004886skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004887{
4888 char_u *p = eap->arg;
4889
Bram Moolenaara37420f2006-02-04 22:37:47 +00004890 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4891 || eap->cmdidx == CMD_vimgrepadd
4892 || eap->cmdidx == CMD_lvimgrepadd
4893 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004894 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004895 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004896 if (p == NULL)
4897 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004898 }
4899 return p;
4900}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004901
4902/*
4903 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4904 * in the command line, so that things like % get expanded.
4905 */
4906 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004907replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004908{
4909 char_u *new_cmdline;
4910 char_u *program;
4911 char_u *pos;
4912 char_u *ptr;
4913 int len;
4914 int i;
4915
4916 /*
4917 * Don't do it when ":vimgrep" is used for ":grep".
4918 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004919 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4920 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4921 || eap->cmdidx == CMD_grepadd
4922 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004923 && !grep_internal(eap->cmdidx))
4924 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004925 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4926 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004927 {
4928 if (*curbuf->b_p_gp == NUL)
4929 program = p_gp;
4930 else
4931 program = curbuf->b_p_gp;
4932 }
4933 else
4934 {
4935 if (*curbuf->b_p_mp == NUL)
4936 program = p_mp;
4937 else
4938 program = curbuf->b_p_mp;
4939 }
4940
4941 p = skipwhite(p);
4942
4943 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4944 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004945 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004946 i = 1;
4947 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4948 ++i;
4949 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004950 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004951 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004952 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004953 ptr = new_cmdline;
4954 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4955 {
4956 i = (int)(pos - program);
4957 STRNCPY(ptr, program, i);
4958 STRCPY(ptr += i, p);
4959 ptr += len;
4960 program = pos + 2;
4961 }
4962 STRCPY(ptr, program);
4963 }
4964 else
4965 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004966 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004967 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004968 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004969 STRCPY(new_cmdline, program);
4970 STRCAT(new_cmdline, " ");
4971 STRCAT(new_cmdline, p);
4972 }
4973 msg_make(p);
4974
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004975 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004976 vim_free(*cmdlinep);
4977 *cmdlinep = new_cmdline;
4978 p = new_cmdline;
4979 }
4980 return p;
4981}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004982#endif
4983
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984/*
4985 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004986 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 * Return FAIL for failure, OK otherwise.
4988 */
4989 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004990expand_filename(
4991 exarg_T *eap,
4992 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004993 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004995 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 char_u *repl;
4997 int srclen;
4998 char_u *p;
4999 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005000 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
Bram Moolenaar748bf032005-02-02 23:04:36 +00005002#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005003 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005004 p = skip_grep_pat(eap);
5005#else
5006 p = eap->arg;
5007#endif
5008
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 /*
5010 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5011 * the file name contains a wildcard it should not cause expanding.
5012 * (it will be expanded anyway if there is a wildcard before replacing).
5013 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005014 has_wildcards = mch_has_wildcard(p);
5015 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005017#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005018 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005019 if (p[0] == '`' && p[1] == '=')
5020 {
5021 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005022 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005023 if (*p == '`')
5024 ++p;
5025 continue;
5026 }
5027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 /*
5029 * Quick check if this cannot be the start of a special string.
5030 * Also removes backslash before '%', '#' and '<'.
5031 */
5032 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5033 {
5034 ++p;
5035 continue;
5036 }
5037
5038 /*
5039 * Try to find a match at this position.
5040 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005041 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005042 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005043 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005045 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
5047 p += srclen;
5048 continue;
5049 }
5050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005051 // Wildcards won't be expanded below, the replacement is taken
5052 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005053 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5054 {
5055 char_u *l = repl;
5056
5057 repl = expand_env_save(repl);
5058 vim_free(l);
5059 }
5060
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005061 // Need to escape white space et al. with a backslash.
5062 // Don't do this for:
5063 // - replacement that already has been escaped: "##"
5064 // - shell commands (may have to use quotes instead).
5065 // - non-unix systems when there is a single argument (spaces don't
5066 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005068 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 && eap->cmdidx != CMD_grep
5071 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005072 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005073 && eap->cmdidx != CMD_lgrep
5074 && eap->cmdidx != CMD_lgrepadd
5075 && eap->cmdidx != CMD_lmake
5076 && eap->cmdidx != CMD_make
5077 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005079 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080#endif
5081 )
5082 {
5083 char_u *l;
5084#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005085 // Don't escape a backslash here, because rem_backslash() doesn't
5086 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 static char_u *nobslash = (char_u *)" \t\"|";
5088# define ESCAPE_CHARS nobslash
5089#else
5090# define ESCAPE_CHARS escape_chars
5091#endif
5092
5093 for (l = repl; *l; ++l)
5094 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5095 {
5096 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5097 if (l != NULL)
5098 {
5099 vim_free(repl);
5100 repl = l;
5101 }
5102 break;
5103 }
5104 }
5105
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005106 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005107 if ((eap->usefilter || eap->cmdidx == CMD_bang
5108 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005109 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 char_u *l;
5112
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005113 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (l != NULL)
5115 {
5116 vim_free(repl);
5117 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 }
5120
5121 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5122 vim_free(repl);
5123 if (p == NULL)
5124 return FAIL;
5125 }
5126
5127 /*
5128 * One file argument: Expand wildcards.
5129 * Don't do this with ":r !command" or ":w !command".
5130 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005131 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
5133 /*
5134 * May do this twice:
5135 * 1. Replace environment variables.
5136 * 2. Replace any other wildcards, remove backslashes.
5137 */
5138 for (n = 1; n <= 2; ++n)
5139 {
5140 if (n == 2)
5141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 /*
5143 * Halve the number of backslashes (this is Vi compatible).
5144 * For Unix and OS/2, when wildcards are expanded, this is
5145 * done by ExpandOne() below.
5146 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005147#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (!has_wildcards)
5149#endif
5150 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
5152
5153 if (has_wildcards)
5154 {
5155 if (n == 1)
5156 {
5157 /*
5158 * First loop: May expand environment variables. This
5159 * can be done much faster with expand_env() than with
5160 * something else (e.g., calling a shell).
5161 * After expanding environment variables, check again
5162 * if there are still wildcards present.
5163 */
5164 if (vim_strchr(eap->arg, '$') != NULL
5165 || vim_strchr(eap->arg, '~') != NULL)
5166 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005167 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005168 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 has_wildcards = mch_has_wildcard(NameBuff);
5170 p = NameBuff;
5171 }
5172 else
5173 p = NULL;
5174 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005175 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
5177 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005178 int options = WILD_LIST_NOTFOUND
5179 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
5181 ExpandInit(&xpc);
5182 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005183 if (p_wic)
5184 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005186 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (p == NULL)
5188 return FAIL;
5189 }
5190 if (p != NULL)
5191 {
5192 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5193 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005194 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 vim_free(p);
5196 }
5197 }
5198 }
5199 }
5200 return OK;
5201}
5202
5203/*
5204 * Replace part of the command line, keeping eap->cmd, eap->arg and
5205 * eap->nextcmd correct.
5206 * "src" points to the part that is to be replaced, of length "srclen".
5207 * "repl" is the replacement string.
5208 * Returns a pointer to the character after the replaced string.
5209 * Returns NULL for failure.
5210 */
5211 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005212repl_cmdline(
5213 exarg_T *eap,
5214 char_u *src,
5215 int srclen,
5216 char_u *repl,
5217 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218{
5219 int len;
5220 int i;
5221 char_u *new_cmdline;
5222
5223 /*
5224 * The new command line is build in new_cmdline[].
5225 * First allocate it.
5226 * Careful: a "+cmd" argument may have been NUL terminated.
5227 */
5228 len = (int)STRLEN(repl);
5229 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005230 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005231 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005232 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005233 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
5235 /*
5236 * Copy the stuff before the expanded part.
5237 * Copy the expanded stuff.
5238 * Copy what came after the expanded part.
5239 * Copy the next commands, if there are any.
5240 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005241 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005245 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005247 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005249 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
5251 i = (int)STRLEN(new_cmdline) + 1;
5252 STRCPY(new_cmdline + i, eap->nextcmd);
5253 eap->nextcmd = new_cmdline + i;
5254 }
5255 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5256 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5257 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5258 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5259 vim_free(*cmdlinep);
5260 *cmdlinep = new_cmdline;
5261
5262 return src;
5263}
5264
5265/*
5266 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005267 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 */
5269 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005270separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
5272 char_u *p;
5273
Bram Moolenaar86b68352004-12-27 21:59:20 +00005274#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005275 p = skip_grep_pat(eap);
5276#else
5277 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005278#endif
5279
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005280 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {
5282 if (*p == Ctrl_V)
5283 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005284 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005285 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005287 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005288 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005289 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 break;
5291 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005292
5293#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005294 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005295 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005296 {
5297 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005298 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005299 if (*p == NUL) // stop at NUL after CTRL-V
5300 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005301 }
5302#endif
5303
Bram Moolenaareda29c92022-10-02 12:59:00 +01005304 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005305 // :@" and :*" do not start a comment!
5306 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005307 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005308 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005309 && !(eap->argt & EX_NOTRLCOM)
5310 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5311 || p != eap->arg)
5312 && (eap->cmdidx != CMD_redir
5313 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005314 || (*p == '#'
5315 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005316 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005317 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 || *p == '|' || *p == '\n')
5319 {
5320 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005321 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 * AND 'b' is present in 'cpoptions'.
5323 */
5324 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005325 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005327 if (!keep_backslash)
5328 {
5329 STRMOVE(p - 1, p); // remove the '\'
5330 --p;
5331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333 else
5334 {
5335 eap->nextcmd = check_nextcmd(p);
5336 *p = NUL;
5337 break;
5338 }
5339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005341
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005342 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 del_trailing_spaces(eap->arg);
5344}
5345
5346/*
5347 * get + command from ex argument
5348 */
5349 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005350getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351{
5352 char_u *arg = *argp;
5353 char_u *command = NULL;
5354
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005355 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {
5357 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005358 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 command = dollar_command;
5360 else
5361 {
5362 command = arg;
5363 arg = skip_cmd_arg(command, TRUE);
5364 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005365 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005368 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 *argp = arg;
5370 }
5371 return command;
5372}
5373
5374/*
5375 * Find end of "+command" argument. Skip over "\ " and "\\".
5376 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005377 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005378skip_cmd_arg(
5379 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005380 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381{
5382 while (*p && !vim_isspace(*p))
5383 {
5384 if (*p == '\\' && p[1] != NUL)
5385 {
5386 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005387 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 else
5389 ++p;
5390 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005391 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 }
5393 return p;
5394}
5395
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005396 int
5397get_bad_opt(char_u *p, exarg_T *eap)
5398{
5399 if (STRICMP(p, "keep") == 0)
5400 eap->bad_char = BAD_KEEP;
5401 else if (STRICMP(p, "drop") == 0)
5402 eap->bad_char = BAD_DROP;
5403 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5404 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005405 else
5406 return FAIL;
5407 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005408}
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005411 * Function given to ExpandGeneric() to obtain the list of bad= names.
5412 */
5413 static char_u *
5414get_bad_name(expand_T *xp UNUSED, int idx)
5415{
5416 // Note: Keep this in sync with getargopt.
5417 static char *(p_bad_values[]) =
5418 {
5419 "?",
5420 "keep",
5421 "drop",
5422 };
5423
5424 if (idx < (int)ARRAY_LENGTH(p_bad_values))
5425 return (char_u*)p_bad_values[idx];
5426 return NULL;
5427}
5428
5429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 * Get "++opt=arg" argument.
5431 * Return FAIL or OK.
5432 */
5433 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005434getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
5436 char_u *arg = eap->arg + 2;
5437 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005438 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005441 // Note: Keep this in sync with get_argopt_name.
5442
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005443 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5445 {
5446 if (*arg == 'n')
5447 {
5448 arg += 2;
5449 eap->force_bin = FORCE_NOBIN;
5450 }
5451 else
5452 eap->force_bin = FORCE_BIN;
5453 if (!checkforcmd(&arg, "binary", 3))
5454 return FAIL;
5455 eap->arg = skipwhite(arg);
5456 return OK;
5457 }
5458
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005459 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005460 if (STRNCMP(arg, "edit", 4) == 0)
5461 {
5462 eap->read_edit = TRUE;
5463 eap->arg = skipwhite(arg + 4);
5464 return OK;
5465 }
5466
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 if (STRNCMP(arg, "ff", 2) == 0)
5468 {
5469 arg += 2;
5470 pp = &eap->force_ff;
5471 }
5472 else if (STRNCMP(arg, "fileformat", 10) == 0)
5473 {
5474 arg += 10;
5475 pp = &eap->force_ff;
5476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 else if (STRNCMP(arg, "enc", 3) == 0)
5478 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005479 if (STRNCMP(arg, "encoding", 8) == 0)
5480 arg += 8;
5481 else
5482 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 pp = &eap->force_enc;
5484 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005485 else if (STRNCMP(arg, "bad", 3) == 0)
5486 {
5487 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005488 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
5491 if (pp == NULL || *arg != '=')
5492 return FAIL;
5493
5494 ++arg;
5495 *pp = (int)(arg - eap->cmd);
5496 arg = skip_cmd_arg(arg, FALSE);
5497 eap->arg = skipwhite(arg);
5498 *arg = NUL;
5499
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 if (pp == &eap->force_ff)
5501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5503 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005504 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005506 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005508 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5510 *p = TOLOWER_ASC(*p);
5511 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005512 else
5513 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005514 // Check ++bad= argument. Must be a single-byte character, "keep" or
5515 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005516 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005517 return FAIL;
5518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
5520 return OK;
5521}
5522
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005523/*
5524 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5525 */
5526 static char_u *
5527get_argopt_name(expand_T *xp UNUSED, int idx)
5528{
5529 // Note: Keep this in sync with getargopt.
5530 static char *(p_opt_values[]) =
5531 {
5532 "fileformat=",
5533 "encoding=",
5534 "binary",
5535 "nobinary",
5536 "bad=",
5537 "edit",
5538 };
5539
5540 if (idx < (int)ARRAY_LENGTH(p_opt_values))
5541 return (char_u*)p_opt_values[idx];
5542 return NULL;
5543}
5544
5545/*
5546 * Command-line expansion for ++opt=name.
5547 */
5548 int
5549expand_argopt(
5550 char_u *pat,
5551 expand_T *xp,
5552 regmatch_T *rmp,
5553 char_u ***matches,
5554 int *numMatches)
5555{
5556 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5557 {
5558 char_u *(*cb)(expand_T *, int) = NULL;
5559
5560 char_u *name_end = xp->xp_pattern - 1;
5561 if (name_end - xp->xp_line >= 2
5562 && STRNCMP(name_end - 2, "ff", 2) == 0)
5563 cb = get_fileformat_name;
5564 else if (name_end - xp->xp_line >= 10
5565 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5566 cb = get_fileformat_name;
5567 else if (name_end - xp->xp_line >= 3
5568 && STRNCMP(name_end - 3, "enc", 3) == 0)
5569 cb = get_encoding_name;
5570 else if (name_end - xp->xp_line >= 8
5571 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5572 cb = get_encoding_name;
5573 else if (name_end - xp->xp_line >= 3
5574 && STRNCMP(name_end - 3, "bad", 3) == 0)
5575 cb = get_bad_name;
5576
5577 if (cb != NULL)
5578 {
5579 return ExpandGeneric(
5580 pat,
5581 xp,
5582 rmp,
5583 matches,
5584 numMatches,
5585 cb,
5586 FALSE);
5587 }
5588 return FAIL;
5589 }
5590
5591 // Special handling of "ff" which acts as a short form of
5592 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005593 if (xp->xp_pattern_len == 2
5594 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005595 {
5596 *matches = ALLOC_MULT(char_u *, 1);
5597 if (*matches == NULL)
5598 return FAIL;
5599 *numMatches = 1;
5600 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5601 return OK;
5602 }
5603
5604 return ExpandGeneric(
5605 pat,
5606 xp,
5607 rmp,
5608 matches,
5609 numMatches,
5610 get_argopt_name,
5611 FALSE);
5612}
5613
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005615ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
5617 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005618 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 * directory for security reasons.
5620 */
5621 if (secure)
5622 {
5623 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005624 eap->errmsg =
5625 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005628 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 else
5630 do_augroup(eap->arg, eap->forceit);
5631}
5632
5633/*
5634 * ":doautocmd": Apply the automatic commands to the current buffer.
5635 */
5636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005637ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005639 char_u *arg = eap->arg;
5640 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005641 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005642
Bram Moolenaar1610d052016-06-09 22:53:01 +02005643 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005644 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005645 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005646 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649/*
5650 * :[N]bunload[!] [N] [bufname] unload buffer
5651 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5652 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5653 */
5654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005657 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005658 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 eap->errmsg = do_bufdel(
5660 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5661 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5662 : DOBUF_UNLOAD, eap->arg,
5663 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5664}
5665
5666/*
5667 * :[N]buffer [N] to buffer N
5668 * :[N]sbuffer [N] to buffer N
5669 */
5670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005671ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005673 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005674 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005676 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 else
5678 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005679 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5681 else
5682 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005683 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005684 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686}
5687
5688/*
5689 * :[N]bmodified [N] to next mod. buffer
5690 * :[N]sbmodified [N] to next mod. buffer
5691 */
5692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005693ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
5695 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005696 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005697 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698}
5699
5700/*
5701 * :[N]bnext [N] to next buffer
5702 * :[N]sbnext [N] split and to next buffer
5703 */
5704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005705ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005707 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005708 return;
5709
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005711 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005712 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713}
5714
5715/*
5716 * :[N]bNext [N] to previous buffer
5717 * :[N]bprevious [N] to previous buffer
5718 * :[N]sbNext [N] split and to previous buffer
5719 * :[N]sbprevious [N] split and to previous buffer
5720 */
5721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005722ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005724 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005725 return;
5726
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005728 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005729 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730}
5731
5732/*
5733 * :brewind to first buffer
5734 * :bfirst to first buffer
5735 * :sbrewind split and to first buffer
5736 * :sbfirst split and to first buffer
5737 */
5738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005739ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005741 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005742 return;
5743
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005745 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005746 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747}
5748
5749/*
5750 * :blast to last buffer
5751 * :sblast split and to last buffer
5752 */
5753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005754ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005756 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005757 return;
5758
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005760 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005761 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
Bram Moolenaar7a092242020-04-16 22:10:49 +02005764/*
5765 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005766 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005769ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005771 int comment_char = '"';
5772
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005773 if (in_vim9script())
5774 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005775 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005776}
5777
5778/*
5779 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5780 * to "cmd_start" or has a white space character before it.
5781 */
5782 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005783ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005784{
5785 int c = *cmd;
5786
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005787 if (c == NUL || c == '|' || c == '\n')
5788 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005789 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005790 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5791 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005792 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005793 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794}
5795
5796#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5797 || defined(PROTO)
5798/*
5799 * Return the next command, after the first '|' or '\n'.
5800 * Return NULL if not found.
5801 */
5802 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005803find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
5805 while (*p != '|' && *p != '\n')
5806 {
5807 if (*p == NUL)
5808 return NULL;
5809 ++p;
5810 }
5811 return (p + 1);
5812}
5813#endif
5814
5815/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005816 * Check if *p is a separator between Ex commands, skipping over white space.
5817 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 */
5819 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005820check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005822 char_u *s = skipwhite(p);
5823
5824 if (*s == '|' || *s == '\n')
5825 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 else
5827 return NULL;
5828}
5829
5830/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005831 * If "eap->nextcmd" is not set, check for a next command at "p".
5832 */
5833 void
5834set_nextcmd(exarg_T *eap, char_u *arg)
5835{
5836 char_u *p = check_nextcmd(arg);
5837
5838 if (eap->nextcmd == NULL)
5839 eap->nextcmd = p;
5840 else if (p != NULL)
5841 // cannot use "| command" inside a {} block
5842 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5843}
5844
5845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 * - if there are more files to edit
5847 * - and this is the last window
5848 * - and forceit not used
5849 * - and not repeated twice on a row
5850 * return FAIL and give error message if 'message' TRUE
5851 * return OK otherwise
5852 */
5853 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005854check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005855 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005856 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5859
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005860 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005861 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {
5863 if (message)
5864 {
5865#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005866 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5867 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005869 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005871 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5872 NGETTEXT("%d more file to edit. Quit anyway?",
5873 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5875 return OK;
5876 return FAIL;
5877 }
5878#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005879 semsg(NGETTEXT(e_nr_more_file_to_edit,
5880 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005881 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
5883 return FAIL;
5884 }
5885 return OK;
5886}
5887
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888/*
5889 * Function given to ExpandGeneric() to obtain the list of command names.
5890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005892get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
5894 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005895 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 return cmdnames[idx].cmd_name;
5897}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005900ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901{
Bram Moolenaare6850792010-05-14 15:28:44 +02005902 if (*eap->arg == NUL)
5903 {
5904#ifdef FEAT_EVAL
5905 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5906 char_u *p = NULL;
5907
5908 if (expr != NULL)
5909 {
5910 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005911 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005912 --emsg_off;
5913 vim_free(expr);
5914 }
5915 if (p != NULL)
5916 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005917 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005918 vim_free(p);
5919 }
5920 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005921 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005922#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005923 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005924#endif
5925 }
5926 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005927 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005928
5929#ifdef FEAT_VTP
5930 else if (has_vtp_working())
5931 {
5932 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005933 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005934 redrawcmd();
5935 }
5936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937}
5938
5939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005940ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941{
5942 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005943 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 do_highlight(eap->arg, eap->forceit, FALSE);
5945}
5946
5947
5948/*
5949 * Call this function if we thought we were going to exit, but we won't
5950 * (because of an error). May need to restore the terminal mode.
5951 */
5952 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005953not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
5955 exiting = FALSE;
5956 settmode(TMODE_RAW);
5957}
5958
Bram Moolenaar3552e742021-05-29 12:21:58 +02005959 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005960before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5961{
5962 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5963
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005964 // Bail out when autocommands closed the window.
5965 // Refuse to quit when the buffer in the last window is being closed (can
5966 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005967 if (!win_valid(wp)
5968 || curbuf_locked()
5969 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5970 return TRUE;
5971
5972 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5973 {
5974 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005975 // Refuse to quit when locked or when the window was closed or the
5976 // buffer in the last window is being closed (can only happen in
5977 // autocommands).
5978 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005979 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5980 return TRUE;
5981 }
5982
5983 return FALSE;
5984}
5985
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005987 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005988 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005989 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005991 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005992ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005994 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005995
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 if (cmdwin_type != 0)
5997 {
5998 cmdwin_result = Ctrl_C;
5999 return;
6000 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006001 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006002 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006003 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006004 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006005 return;
6006 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006007 if (eap->addr_count > 0)
6008 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006009 int wnr = eap->line2;
6010
6011 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6012 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006013 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006014 }
6015 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006016 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006017
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006018 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006019 if (curbuf_locked())
6020 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006021
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006022 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006023 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006024 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025
6026#ifdef FEAT_NETBEANS_INTG
6027 netbeansForcedQuit = eap->forceit;
6028#endif
6029
6030 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006031 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 */
6033 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6034 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006035 if ((!buf_hide(wp->w_buffer)
6036 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006037 | (eap->forceit ? CCGD_FORCEIT : 0)
6038 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006040 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 {
6042 not_exiting();
6043 }
6044 else
6045 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006046 // quit last window
6047 // Note: only_one_window() returns true, even so a help window is
6048 // still open. In that case only quit, if no address has been
6049 // specified. Example:
6050 // :h|wincmd w|1q - don't quit
6051 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006052 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006054 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006055#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006057#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006058 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006059 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 }
6061}
6062
6063/*
6064 * ":cquit".
6065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006067ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006069 // this does not always pass on the exit code to the Manx compiler. why?
6070 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071}
6072
6073/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006074 * Do preparations for "qall" and "wqall".
6075 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006077 int
6078before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 if (cmdwin_type != 0)
6081 {
6082 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006083 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 else
6085 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006086 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006088
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006089 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006090 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006091 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006092 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006093 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006094 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006095
6096 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006097 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006098
Bram Moolenaar411da642023-05-10 16:53:27 +01006099 return OK;
6100}
6101
6102/*
6103 * ":qall": try to quit all windows
6104 */
6105 static void
6106ex_quit_all(exarg_T *eap)
6107{
6108 if (before_quit_all(eap) == FAIL)
6109 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006111 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 getout(0);
6113 not_exiting();
6114}
6115
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116/*
6117 * ":close": close current window, unless it is the last one
6118 */
6119 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006120ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006122 win_T *win;
6123 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006125 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006127 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006128 {
6129 if (eap->addr_count == 0)
6130 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006131 else
6132 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006133 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006134 {
6135 winnr++;
6136 if (winnr == eap->line2)
6137 break;
6138 }
6139 if (win == NULL)
6140 win = lastwin;
6141 ex_win_close(eap->forceit, win, NULL);
6142 }
6143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144}
6145
Bram Moolenaar4033c552017-09-16 20:54:51 +02006146#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006147/*
6148 * ":pclose": Close any preview window.
6149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006151ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006152{
6153 win_T *win;
6154
Bram Moolenaar79648732019-07-18 21:43:07 +02006155 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006156 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006157 if (win->w_p_pvw)
6158 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006159 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006160 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006161 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006162# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006163 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006164 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006165# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006166}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006167#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006168
Bram Moolenaarf740b292006-02-16 22:11:02 +00006169/*
6170 * Close window "win" and take care of handling closing the last window for a
6171 * modified buffer.
6172 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006174ex_win_close(
6175 int forceit,
6176 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006177 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
6179 int need_hide;
6180 buf_T *buf = win->w_buffer;
6181
Bram Moolenaarcf844172020-06-26 19:44:06 +02006182 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006183 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006184 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006185 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006186 return;
6187 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006188 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006189 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006190
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006192 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006194#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006195 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006197# ifdef FEAT_TERMINAL
6198 if (term_job_running(buf->b_term))
6199 {
6200 if (term_confirm_stop(buf) == FAIL)
6201 return;
6202 // Manually kill the terminal here because this command will
6203 // hide it otherwise.
6204 free_terminal(buf);
6205 need_hide = FALSE;
6206 }
6207 else
6208# endif
6209 {
6210 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006211
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006212 set_bufref(&bufref, buf);
6213 dialog_changed(buf, FALSE);
6214 if (bufref_valid(&bufref) && bufIsChanged(buf))
6215 return;
6216 need_hide = FALSE;
6217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 }
6219 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006222 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 return;
6224 }
6225 }
6226
Bram Moolenaar4033c552017-09-16 20:54:51 +02006227#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006229#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006230
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006231 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006232 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006233 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006234 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006235 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236}
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006239 * Handle the argument for a tabpage related ex command.
6240 * Returns a tabpage number.
6241 * When an error is encountered then eap->errmsg is set.
6242 */
6243 static int
6244get_tabpage_arg(exarg_T *eap)
6245{
6246 int tab_number;
6247 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6248
6249 if (eap->arg && *eap->arg != NUL)
6250 {
6251 char_u *p = eap->arg;
6252 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006253 int relative = 0; // argument +N/-N means: go to N places to the
6254 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006255
6256 if (*p == '-')
6257 {
6258 relative = -1;
6259 p++;
6260 }
6261 else if (*p == '+')
6262 {
6263 relative = 1;
6264 p++;
6265 }
6266
6267 p_save = p;
6268 tab_number = getdigits(&p);
6269
6270 if (relative == 0)
6271 {
6272 if (STRCMP(p, "$") == 0)
6273 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006274 else if (STRCMP(p, "#") == 0)
6275 if (valid_tabpage(lastused_tabpage))
6276 tab_number = tabpage_index(lastused_tabpage);
6277 else
6278 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006279 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6280 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006281 tab_number = 0;
6282 goto theend;
6283 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006284 else if (p == p_save || *p_save == '-' || *p != NUL
6285 || tab_number > LAST_TAB_NR)
6286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006287 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006288 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006289 goto theend;
6290 }
6291 }
6292 else
6293 {
6294 if (*p_save == NUL)
6295 tab_number = 1;
6296 else if (p == p_save || *p_save == '-' || *p != NUL
6297 || tab_number == 0)
6298 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006299 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006300 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006301 goto theend;
6302 }
6303 tab_number = tab_number * relative + tabpage_index(curtab);
6304 if (!unaccept_arg0 && relative == -1)
6305 --tab_number;
6306 }
6307 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006308 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006309 }
6310 else if (eap->addr_count > 0)
6311 {
6312 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006313 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006314 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006315 tab_number = 0;
6316 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006317 else
6318 {
6319 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006320 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006321 {
6322 --tab_number;
6323 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006324 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006325 }
6326 }
6327 }
6328 else
6329 {
6330 switch (eap->cmdidx)
6331 {
6332 case CMD_tabnext:
6333 tab_number = tabpage_index(curtab) + 1;
6334 if (tab_number > LAST_TAB_NR)
6335 tab_number = 1;
6336 break;
6337 case CMD_tabmove:
6338 tab_number = LAST_TAB_NR;
6339 break;
6340 default:
6341 tab_number = tabpage_index(curtab);
6342 }
6343 }
6344
6345theend:
6346 return tab_number;
6347}
6348
6349/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006350 * ":tabclose": close current tab page, unless it is the last one.
6351 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 */
6353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006354ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006356 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006357 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006358
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006359 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006360 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006361 cmdwin_result = K_IGNORE;
6362 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006363 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006364
6365 if (first_tabpage->tp_next == NULL)
6366 {
6367 emsg(_(e_cannot_close_last_tab_page));
6368 return;
6369 }
6370
6371 if (window_layout_locked(CMD_tabclose))
6372 return;
6373
6374 tab_number = get_tabpage_arg(eap);
6375 if (eap->errmsg != NULL)
6376 return;
6377
6378 tp = find_tabpage(tab_number);
6379 if (tp == NULL)
6380 {
6381 beep_flush();
6382 return;
6383 }
6384 if (tp != curtab)
6385 {
6386 tabpage_close_other(tp, eap->forceit);
6387 return;
6388 }
6389 else if (!text_locked() && !curbuf_locked())
6390 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006391}
6392
6393/*
6394 * ":tabonly": close all tab pages except the current one
6395 */
6396 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006397ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006398{
6399 tabpage_T *tp;
6400 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006401 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006402
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006403 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006404 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006405 cmdwin_result = K_IGNORE;
6406 return;
6407 }
6408
6409 if (first_tabpage->tp_next == NULL)
6410 {
6411 msg(_("Already only one tab page"));
6412 return;
6413 }
6414
6415 if (window_layout_locked(CMD_tabonly))
6416 return;
6417
6418 tab_number = get_tabpage_arg(eap);
6419 if (eap->errmsg != NULL)
6420 return;
6421
6422 goto_tabpage(tab_number);
6423 // Repeat this up to a 1000 times, because autocommands may
6424 // mess up the lists.
6425 for (done = 0; done < 1000; ++done)
6426 {
6427 FOR_ALL_TABPAGES(tp)
6428 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006429 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006430 tabpage_close_other(tp, eap->forceit);
6431 // if we failed to close it quit
6432 if (valid_tabpage(tp))
6433 done = 1000;
6434 // start over, "tp" is now invalid
6435 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006436 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006437 if (first_tabpage->tp_next == NULL)
6438 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441
6442/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006443 * Close the current tab page.
6444 */
6445 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006446tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006447{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006448 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006449 return;
6450
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006451 // First close all the windows but the current one. If that worked then
6452 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006453 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006454 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006455 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006456 ex_win_close(forceit, curwin, NULL);
6457# ifdef FEAT_GUI
6458 need_mouse_correct = TRUE;
6459# endif
6460}
6461
6462/*
6463 * Close tab page "tp", which is not the current tab page.
6464 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006465 * Also takes care of the tab pages line disappearing when closing the
6466 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006467 */
6468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006469tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006470{
6471 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006472 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006474 // Limit to 1000 windows, autocommands may add a window while we close
6475 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006476 while (++done < 1000)
6477 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006478 wp = tp->tp_firstwin;
6479 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006480
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006481 // Autocommands may delete the tab page under our fingers and we may
6482 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006483 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006484 break;
6485 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006486
Bram Moolenaar29323592016-07-24 22:04:11 +02006487 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006488}
6489
6490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 * ":only".
6492 */
6493 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006494ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006496 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006497 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498# ifdef FEAT_GUI
6499 need_mouse_correct = TRUE;
6500# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006501 if (eap->addr_count > 0)
6502 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006503 win_T *wp;
6504 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006505 for (wp = firstwin; --wnr > 0; )
6506 {
6507 if (wp->w_next == NULL)
6508 break;
6509 else
6510 wp = wp->w_next;
6511 }
6512 win_goto(wp);
6513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 close_others(TRUE, eap->forceit);
6515}
6516
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006518ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006520 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006521 if (eap->skip)
6522 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006523
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006524 if (window_layout_locked(CMD_hide))
6525 return;
6526#ifdef FEAT_GUI
6527 need_mouse_correct = TRUE;
6528#endif
6529 if (eap->addr_count == 0)
6530 win_close(curwin, FALSE); // don't free buffer
6531 else
6532 {
6533 int winnr = 0;
6534 win_T *win;
6535
6536 FOR_ALL_WINDOWS(win)
6537 {
6538 winnr++;
6539 if (winnr == eap->line2)
6540 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006542 if (win == NULL)
6543 win = lastwin;
6544 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
6546}
6547
6548/*
6549 * ":stop" and ":suspend": Suspend Vim.
6550 */
dbivolaruab16ad32021-12-29 19:41:47 +00006551 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006552ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553{
6554 /*
6555 * Disallow suspending for "rvim".
6556 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006557 if (check_restricted())
6558 return;
6559
6560 if (!eap->forceit)
6561 autowrite_all();
6562 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6563 windgoto((int)Rows - 1, 0);
6564 out_char('\n');
6565 out_flush();
6566 stoptermcap();
6567 out_flush(); // needed for SUN to restore xterm buffer
6568 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6569 ui_suspend(); // call machine specific function
6570 maketitle();
6571 resettitle(); // force updating the title
6572 starttermcap();
6573 scroll_start(); // scroll screen before redrawing
6574 redraw_later_clear();
6575 shell_resized(); // may have resized window
6576 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577}
6578
6579/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006580 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 */
6582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006583ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006585#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006586 if (not_in_vim9(eap) == FAIL)
6587 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 if (cmdwin_type != 0)
6590 {
6591 cmdwin_result = Ctrl_C;
6592 return;
6593 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006594 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006595 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006596 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006597 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006598 return;
6599 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006600
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006602 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 */
6604 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6605 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006606
6607 // Write the buffer for ":wq" or when it was changed.
6608 // Trigger QuitPre and ExitPre.
6609 // Check if we can exit now, after autocommands have changed things.
6610 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6611 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006613 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 {
6615 not_exiting();
6616 }
6617 else
6618 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006619 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006621 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622# ifdef FEAT_GUI
6623 need_mouse_correct = TRUE;
6624# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006625 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006626 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 }
6628}
6629
6630/*
6631 * ":print", ":list", ":number".
6632 */
6633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006634ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006636 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006637 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006638 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006640 for ( ;!got_int; ui_breakcheck())
6641 {
6642 print_line(eap->line1,
6643 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6644 || (eap->flags & EXFLAG_NR)),
6645 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6646 if (++eap->line1 > eap->line2)
6647 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006648 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006649 }
6650 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006651 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006652 curwin->w_cursor.lnum = eap->line2;
6653 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 }
6655
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657}
6658
6659#ifdef FEAT_BYTEOFF
6660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006661ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662{
6663 goto_byte(eap->line2);
6664}
6665#endif
6666
6667/*
6668 * ":shell".
6669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006671ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672{
6673 do_shell(NULL, 0);
6674}
6675
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006676#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006678static int drop_busy = FALSE;
6679static int drop_filec;
6680static char_u **drop_filev = NULL;
6681static int drop_split;
6682static void (*drop_callback)(void *);
6683static void *drop_cookie;
6684
6685 static void
6686handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
6688 exarg_T ea;
6689 int save_msg_scroll = msg_scroll;
6690
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006691 // Setting the argument list may cause screen updates and being called
6692 // recursively. Avoid that by setting drop_busy.
6693 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006695 // Check whether the current buffer is changed. If so, we will need
6696 // to split the current window or data could be lost.
6697 // We don't need to check if the 'hidden' option is set, as in this
6698 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006699 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 {
6701 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006702 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 --emsg_off;
6704 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006705 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 if (win_split(0, 0) == FAIL)
6708 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006709 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006711 // When splitting the window, create a new alist. Otherwise the
6712 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 alist_unlink(curwin->w_alist);
6714 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 }
6716
6717 /*
6718 * Set up the new argument list.
6719 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006720 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
6722 /*
6723 * Move to the first file.
6724 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006725 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006726 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 ea.cmd = (char_u *)"next";
6728 do_argfile(&ea, 0);
6729
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006730 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6731 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 need_start_insertmode = FALSE;
6733
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006734 // Restore msg_scroll, otherwise a following command may cause scrolling
6735 // unexpectedly. The screen will be redrawn by the caller, thus
6736 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006738
6739 if (drop_callback != NULL)
6740 drop_callback(drop_cookie);
6741
6742 drop_filev = NULL;
6743 drop_busy = FALSE;
6744}
6745
6746/*
6747 * Handle a file drop. The code is here because a drop is *nearly* like an
6748 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006749 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006750 * code is very similar to :args and hence needs access to a lot of the static
6751 * functions in this file.
6752 *
6753 * The "filev" list must have been allocated using alloc(), as should each item
6754 * in the list. This function takes over responsibility for freeing the "filev"
6755 * list.
6756 */
6757 void
6758handle_drop(
6759 int filec, // the number of files dropped
6760 char_u **filev, // the list of files dropped
6761 int split, // force splitting the window
6762 void (*callback)(void *), // to be called after setting the argument
6763 // list
6764 void *cookie) // argument for "callback" (allocated)
6765{
6766 // Cannot handle recursive drops, finish the pending one.
6767 if (drop_busy)
6768 {
6769 FreeWild(filec, filev);
6770 vim_free(cookie);
6771 return;
6772 }
6773
6774 // When calling handle_drop() more than once in a row we only use the last
6775 // one.
6776 if (drop_filev != NULL)
6777 {
6778 FreeWild(drop_filec, drop_filev);
6779 vim_free(drop_cookie);
6780 }
6781
6782 drop_filec = filec;
6783 drop_filev = filev;
6784 drop_split = split;
6785 drop_callback = callback;
6786 drop_cookie = cookie;
6787
6788 // Postpone this when:
6789 // - editing the command line
6790 // - not possible to change the current buffer
6791 // - updating the screen
6792 // As it may change buffers and window structures that are in use and cause
6793 // freed memory to be used.
6794 if (text_locked() || curbuf_locked() || updating_screen)
6795 return;
6796
6797 handle_drop_internal();
6798}
6799
6800/*
6801 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6802 * reset: Handle a postponed drop.
6803 */
6804 void
6805handle_any_postponed_drop(void)
6806{
6807 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006808 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006809 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810}
6811#endif
6812
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 * ":preserve".
6815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006817ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006819 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 ml_preserve(curbuf, TRUE);
6821}
6822
6823/*
6824 * ":recover".
6825 */
6826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006827ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006829 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006831 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6832 | CCGD_MULTWIN
6833 | (eap->forceit ? CCGD_FORCEIT : 0)
6834 | CCGD_EXCMD)
6835
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 && (*eap->arg == NUL
6837 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006838 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 recoverymode = FALSE;
6840}
6841
6842/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006843 * Command modifier used in a wrong way. Also for other commands that can't
6844 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 */
6846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006847ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006849 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850}
6851
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852/*
6853 * :sview [+command] file split window with new file, read-only
6854 * :split [[+command] file] split window with current or new file
6855 * :vsplit [[+command] file] split window vertically with current or new file
6856 * :new [[+command] file] split window with no or new file
6857 * :vnew [[+command] file] split vertically window with no or new file
6858 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006859 *
6860 * :tabedit open new Tab page with empty window
6861 * :tabedit [+command] file open new Tab page and edit "file"
6862 * :tabnew [[+command] file] just like :tabedit
6863 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 */
6865 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006866ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006868 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006870#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006871 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006872 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006873#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006874 int use_tab = eap->cmdidx == CMD_tabedit
6875 || eap->cmdidx == CMD_tabfind
6876 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006878 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006879 return;
6880
Bram Moolenaar4033c552017-09-16 20:54:51 +02006881#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884
Bram Moolenaar4033c552017-09-16 20:54:51 +02006885#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006886 // A ":split" in the quickfix window works like ":new". Don't want two
6887 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006888 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
6890 if (eap->cmdidx == CMD_split)
6891 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 if (eap->cmdidx == CMD_vsplit)
6893 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006897 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006899 char_u *file_to_find = NULL;
6900 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006902 FNAME_MESS, TRUE, curbuf->b_ffname,
6903 &file_to_find, &search_ctx);
6904 vim_free(file_to_find);
6905 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 if (fname == NULL)
6907 goto theend;
6908 eap->arg = fname;
6909 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006910# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006911 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 && eap->cmdidx != CMD_new)
6914 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006915 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006916# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006917 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006918# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006919 au_has_group((char_u *)"FileExplorer"))
6920 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006921 // No browsing supported but we do have the file explorer:
6922 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006923 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006924 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006925 }
6926 else
6927 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006928 fname = do_browse(0, (char_u *)(use_tab
6929 ? _("Edit File in new tab page")
6930 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006932 if (fname == NULL)
6933 goto theend;
6934 eap->arg = fname;
6935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006937 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006940 /*
6941 * Either open new tab page or split the window.
6942 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006943 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006944 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006945 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006946 : eap->addr_count == 0 ? 0
6947 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006948 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006949 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006950
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006951 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006952 if (curwin != old_curwin
6953 && win_valid(old_curwin)
6954 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006955 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006956 old_curwin->w_alt_fnum = curbuf->b_fnum;
6957 }
6958 }
6959 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6961 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006962 // Reset 'scrollbind' when editing another file, but keep it when
6963 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006964 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006965 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 else
6967 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 do_exedit(eap, old_curwin);
6969 }
6970
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006971# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006972 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975theend:
6976 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006978
6979/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006980 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006981 */
6982 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006983tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006984{
6985 exarg_T ea;
6986
Bram Moolenaara80faa82020-04-12 19:37:17 +02006987 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006988 ea.cmdidx = CMD_tabnew;
6989 ea.cmd = (char_u *)"tabn";
6990 ea.arg = (char_u *)"";
6991 ex_splitview(&ea);
6992}
6993
6994/*
6995 * :tabnext command
6996 */
6997 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006998ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006999{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007000 int tab_number;
7001
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007002 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007003 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007004 switch (eap->cmdidx)
7005 {
7006 case CMD_tabfirst:
7007 case CMD_tabrewind:
7008 goto_tabpage(1);
7009 break;
7010 case CMD_tablast:
7011 goto_tabpage(9999);
7012 break;
7013 case CMD_tabprevious:
7014 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007015 if (eap->arg && *eap->arg != NUL)
7016 {
7017 char_u *p = eap->arg;
7018 char_u *p_save = p;
7019
7020 tab_number = getdigits(&p);
7021 if (p == p_save || *p_save == '-' || *p != NUL
7022 || tab_number == 0)
7023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007024 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007025 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007026 return;
7027 }
7028 }
7029 else
7030 {
7031 if (eap->addr_count == 0)
7032 tab_number = 1;
7033 else
7034 {
7035 tab_number = eap->line2;
7036 if (tab_number < 1)
7037 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007038 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007039 return;
7040 }
7041 }
7042 }
7043 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007044 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007045 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007046 tab_number = get_tabpage_arg(eap);
7047 if (eap->errmsg == NULL)
7048 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007049 break;
7050 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007051}
7052
7053/*
7054 * :tabmove command
7055 */
7056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007057ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007058{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007059 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007060
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007061 tab_number = get_tabpage_arg(eap);
7062 if (eap->errmsg == NULL)
7063 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007064}
7065
7066/*
7067 * :tabs command: List tabs and their contents.
7068 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007070ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007071{
7072 tabpage_T *tp;
7073 win_T *wp;
7074 int tabcount = 1;
7075
7076 msg_start();
7077 msg_scroll = TRUE;
7078 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7079 {
7080 msg_putchar('\n');
7081 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007082 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007083 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007084 ui_breakcheck();
7085
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007086 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007087 wp = firstwin;
7088 else
7089 wp = tp->tp_firstwin;
7090 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7091 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007092 msg_putchar('\n');
7093 msg_putchar(wp == curwin ? '>' : ' ');
7094 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007095 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7096 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007097 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007098 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007099 else
7100 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7101 IObuff, IOSIZE, TRUE);
7102 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007103 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007104 ui_breakcheck();
7105 }
7106 }
7107}
7108
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109/*
7110 * ":mode": Set screen mode.
7111 * If no argument given, just get the screen size and redraw.
7112 */
7113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007114ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115{
7116 if (*eap->arg == NUL)
7117 shell_resized();
7118 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007119 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120}
7121
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122/*
7123 * ":resize".
7124 * set, increment or decrement current window height
7125 */
7126 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007127ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 int n;
7130 win_T *wp = curwin;
7131
7132 if (eap->addr_count > 0)
7133 {
7134 n = eap->line2;
7135 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7136 ;
7137 }
7138
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007139# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007143 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 {
7145 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007146 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007147 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007149 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 }
7151 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 {
7153 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007154 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007155 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007157 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
7159}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160
7161/*
7162 * ":find [+command] <file>" command.
7163 */
7164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007165ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166{
Colin Kennedy21570352024-03-03 16:16:47 +01007167 if (!check_can_set_curbuf_forceit(eap->forceit))
7168 return;
7169
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 char_u *fname;
7171 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007172 char_u *file_to_find = NULL;
7173 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
7175 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007176 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 if (eap->addr_count > 0)
7178 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007179 // Repeat finding the file "count" times. This matters when it appears
7180 // several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 count = eap->line2;
7182 while (fname != NULL && --count > 0)
7183 {
7184 vim_free(fname);
7185 fname = find_file_in_path(NULL, 0, FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007186 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 }
7188 }
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007189 VIM_CLEAR(file_to_find);
7190 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007192 if (fname == NULL)
7193 return;
7194
7195 eap->arg = fname;
7196 do_exedit(eap, NULL);
7197 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198}
7199
7200/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007201 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007202 */
7203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007204ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007205{
7206 regmatch_T regmatch;
7207 char_u *p;
7208
Bram Moolenaar65088802021-03-13 21:07:21 +01007209#ifdef FEAT_EVAL
7210 if (not_in_vim9(eap) == FAIL)
7211 return;
7212#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007213 curwin->w_cursor.lnum = eap->line2;
7214 beginline(BL_SOL | BL_FIX);
7215 if (*eap->arg == '/')
7216 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007217 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007218 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007219 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007220 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007221 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007222 if (regmatch.regprog != NULL)
7223 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007224 // make a copy of the line, when searching for a mark it might be
7225 // flushed
7226 char_u *line = vim_strsave(ml_get_curline());
7227
Bram Moolenaardf177f62005-02-22 08:39:57 +00007228 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007229 if (vim_regexec(&regmatch, line, (colnr_T)0))
7230 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007231 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007232 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007233 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007234 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007235 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007236 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007237 eap->arg += STRLEN(eap->arg);
7238 }
7239 check_cursor();
7240
7241 eap->cmdidx = CMD_visual;
7242 do_exedit(eap, NULL);
7243}
7244
7245/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007246 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 */
7248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007249ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250{
Colin Kennedy21570352024-03-03 16:16:47 +01007251 // Exclude commands which keep the window's current buffer
7252 if (
7253 eap->cmdidx != CMD_badd
7254 && eap->cmdidx != CMD_balt
7255 // All other commands must obey 'winfixbuf' / ! rules
7256 && !check_can_set_curbuf_forceit(eap->forceit))
7257 return;
7258
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 do_exedit(eap, NULL);
7260}
7261
7262/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007263 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007266do_exedit(
7267 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007268 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
7270 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007272 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007274 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7275 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007276 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 /*
7278 * ":vi" command ends Ex mode.
7279 */
7280 if (exmode_active && (eap->cmdidx == CMD_visual
7281 || eap->cmdidx == CMD_view))
7282 {
7283 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007284 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007287 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007288 if (global_busy)
7289 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007290 if (eap->nextcmd != NULL)
7291 {
7292 stuffReadbuff(eap->nextcmd);
7293 eap->nextcmd = NULL;
7294 }
7295
7296 if (exmode_was != EXMODE_VIM)
7297 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007298 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007299 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007300 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007301 no_wait_return = 0;
7302 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007303 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007304 msg_scroll = 0;
7305#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007306 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007307 hold_gui_events = 0;
7308#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007309 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007310 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007311
7312 main_loop(FALSE, TRUE);
7313
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007314 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007315 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007316 no_wait_return = save_nwr;
7317 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007318#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007319 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007320#endif
7321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 }
7325
7326 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007327 || eap->cmdidx == CMD_tabnew
7328 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007329 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007331 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 setpcmark();
7333 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007334 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7335 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007337 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 || *eap->arg != NUL
7339#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007340 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341#endif
7342 )
7343 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007344 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7345 // Only ":edit" or ":script" can bring us here, others are stopped
7346 // earlier.
7347 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007348 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007349
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 n = readonlymode;
7351 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7352 readonlymode = TRUE;
7353 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007354 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7355 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007356 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7357 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7359 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007360 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7362 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7363 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007364 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007366 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007367 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007368 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7369 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007370 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007372 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 if (old_curwin != NULL)
7374 {
7375 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007376 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007378#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007379 cleanup_T cs;
7380
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007381 // Reset the error/interrupt/exception state here so that
7382 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007383 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007384#endif
7385#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007387#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007388 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007389
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007390#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007391 // Restore the error/interrupt/exception state if not
7392 // discarded by a new aborting error, interrupt, or
7393 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007394 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 }
7397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 }
7399 else if (readonlymode && curbuf->b_nwindows == 1)
7400 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007401 // When editing an already visited buffer, 'readonly' won't be set
7402 // but the previous value is kept. With ":view" and ":sview" we
7403 // want the file to be readonly, except when another window is
7404 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 curbuf->b_p_ro = TRUE;
7406 }
7407 readonlymode = n;
7408 }
7409 else
7410 {
7411 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007412 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 if (n != curwin->w_arg_idx_invalid)
7416 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 }
7418
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 /*
7420 * if ":split file" worked, set alternate file name in old window to new
7421 * file
7422 */
7423 if (old_curwin != NULL
7424 && *eap->arg != NUL
7425 && curwin != old_curwin
7426 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007427 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007428 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
7431 ex_no_reprint = TRUE;
7432}
7433
7434#ifndef FEAT_GUI
7435/*
7436 * ":gui" and ":gvim" when there is no GUI.
7437 */
7438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007439ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007441 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442}
7443#endif
7444
Bram Moolenaar4f974752019-02-17 17:44:42 +01007445#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007447ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
7449 gui_make_tearoff(eap->arg);
7450}
7451#endif
7452
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007453#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7454 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007456ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007458# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7459 if (gui.in_use)
7460 gui_make_popup(eap->arg, eap->forceit);
7461# ifdef FEAT_TERM_POPUP_MENU
7462 else
7463# endif
7464# endif
7465# ifdef FEAT_TERM_POPUP_MENU
7466 pum_make_popup(eap->arg, eap->forceit);
7467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468}
7469#endif
7470
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007472ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473{
7474 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007475 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007477 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478}
7479
7480/*
7481 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7482 * offset.
7483 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007486ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007489 win_T *save_curwin = curwin;
7490 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 long topline;
7492 long y;
7493 linenr_T old_linenr = curwin->w_cursor.lnum;
7494
7495 setpcmark();
7496
7497 /*
7498 * determine max topline
7499 */
7500 if (curwin->w_p_scb)
7501 {
7502 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007503 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 {
7505 if (wp->w_p_scb && wp->w_buffer)
7506 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007507 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 if (topline > y)
7509 topline = y;
7510 }
7511 }
7512 if (topline < 1)
7513 topline = 1;
7514 }
7515 else
7516 {
7517 topline = 1;
7518 }
7519
7520
7521 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007522 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007524 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 {
7526 if (curwin->w_p_scb)
7527 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007528 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 y = topline - curwin->w_topline;
7530 if (y > 0)
7531 scrollup(y, TRUE);
7532 else
7533 scrolldown(-y, TRUE);
7534 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007535 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 }
7539 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007540 curwin = save_curwin;
7541 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 if (curwin->w_p_scb)
7543 {
7544 did_syncbind = TRUE;
7545 checkpcmark();
7546 if (old_linenr != curwin->w_cursor.lnum)
7547 {
7548 char_u ctrl_o[2];
7549
7550 ctrl_o[0] = Ctrl_O;
7551 ctrl_o[1] = 0;
7552 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7553 }
7554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555}
7556
7557
7558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007559ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007561 int i;
7562 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7563 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007565 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007567 do_bang(1, eap, FALSE, FALSE, TRUE);
7568 return;
7569 }
7570
7571 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7572 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573
7574#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007575 if (cmdmod.cmod_flags & CMOD_BROWSE)
7576 {
7577 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007579 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7580 NULL, NULL, NULL, curbuf);
7581 if (browseFile != NULL)
7582 {
7583 i = readfile(browseFile, NULL,
7584 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7585 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 }
7587 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007588 i = OK;
7589 }
7590 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007592 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007594 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 return;
7596 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007597 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 }
7599 else
7600 {
7601 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7602 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7603 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007604 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605
7606 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007607 if (i != OK)
7608 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007609#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007610 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007612 semsg(_(e_cant_open_file_str), eap->arg);
7613 }
7614 else
7615 {
7616 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007617 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007618 // Delete the empty line that remains. Historically ex does
7619 // this but vi doesn't.
7620 if (eap->line2 == 0)
7621 lnum = curbuf->b_ml.ml_line_count;
7622 else
7623 lnum = 1;
7624 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007625 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007626 ml_delete(lnum);
7627 if (curwin->w_cursor.lnum > 1
7628 && curwin->w_cursor.lnum >= lnum)
7629 --curwin->w_cursor.lnum;
7630 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007631 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007632 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007633 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 }
7635}
7636
Bram Moolenaarea408852005-06-25 22:49:46 +00007637static char_u *prev_dir = NULL;
7638
7639#if defined(EXITFREE) || defined(PROTO)
7640 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007641free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007642{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007643 VIM_CLEAR(prev_dir);
7644 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007645}
7646#endif
7647
Bram Moolenaarf4258302013-06-02 18:20:17 +02007648/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007649 * Get the previous directory for the given chdir scope.
7650 */
7651 static char_u *
7652get_prevdir(cdscope_T scope)
7653{
7654 if (scope == CDSCOPE_WINDOW)
7655 return curwin->w_prevdir;
7656 else if (scope == CDSCOPE_TABPAGE)
7657 return curtab->tp_prevdir;
7658 return prev_dir;
7659}
7660
7661/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007662 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007663 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7664 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007665 */
7666 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007667post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007668{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007669 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007670 // Clear tab local directory for both :cd and :tcd
7671 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007672 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007673 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007674 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007675 char_u *pdir = get_prevdir(scope);
7676
7677 // If still in the global directory, need to remember current
7678 // directory as the global directory.
7679 if (globaldir == NULL && pdir != NULL)
7680 globaldir = vim_strsave(pdir);
7681
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007682 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007683 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007684 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007685 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007686 curtab->tp_localdir = vim_strsave(NameBuff);
7687 else
7688 curwin->w_localdir = vim_strsave(NameBuff);
7689 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007690 }
7691 else
7692 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007693 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007694 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007695 }
7696
zeertzjq64be6aa2021-11-19 11:59:08 +00007697 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007698 shorten_fnames(TRUE);
7699}
7700
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007701/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007702 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7703 */
7704 void
7705trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7706{
7707#ifdef FEAT_EVAL
7708 dict_T *v_event;
7709 save_v_event_T save_v_event;
7710
7711 v_event = get_v_event(&save_v_event);
7712 (void)dict_add_string(v_event, "directory", new_dir);
7713 dict_set_items_ro(v_event);
7714#endif
7715 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7716#ifdef FEAT_EVAL
7717 restore_v_event(v_event, &save_v_event);
7718#endif
7719}
7720
7721/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007722 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007723 * chdir() function.
7724 * scope == CDSCOPE_WINDOW: changes the window-local directory
7725 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7726 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007727 * Returns TRUE if the directory is successfully changed.
7728 */
7729 int
7730changedir_func(
7731 char_u *new_dir,
7732 int forceit,
7733 cdscope_T scope)
7734{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007735 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007736 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007737 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007738 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007739 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007740
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007741 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007742 return FALSE;
7743
7744 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7745 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007746 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007747 return FALSE;
7748 }
7749
7750 // ":cd -": Change to previous directory
7751 if (STRCMP(new_dir, "-") == 0)
7752 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007753 pdir = get_prevdir(scope);
7754 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007755 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007756 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007757 return FALSE;
7758 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007759 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007760 }
7761
7762 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007763 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007764 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007765 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007766 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007767
Bakudankun29f3a452021-12-11 12:28:08 +00007768 // For UNIX ":cd" means: go to home directory.
7769 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007770#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007771 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007772#else
7773 if (*new_dir == NUL && p_cdh)
7774#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007775 {
7776 // use NameBuff for home directory name
7777# ifdef VMS
7778 char_u *p;
7779
7780 p = mch_getenv((char_u *)"SYS$LOGIN");
7781 if (p == NULL || *p == NUL) // empty is the same as not set
7782 NameBuff[0] = NUL;
7783 else
7784 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7785# else
7786 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7787# endif
7788 new_dir = NameBuff;
7789 }
zeertzjqf38aad82021-12-30 13:45:57 +00007790 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007791 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7792 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007793 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007794 if (scope == CDSCOPE_WINDOW)
7795 acmd_fname = (char_u *)"window";
7796 else if (scope == CDSCOPE_TABPAGE)
7797 acmd_fname = (char_u *)"tabpage";
7798 else
7799 acmd_fname = (char_u *)"global";
7800 trigger_DirChangedPre(acmd_fname, new_dir);
7801
7802 if (vim_chdir(new_dir))
7803 {
7804 emsg(_(e_command_failed));
7805 vim_free(pdir);
7806 return FALSE;
7807 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007808 }
zeertzjq73257142022-02-02 13:16:37 +00007809
7810 if (scope == CDSCOPE_WINDOW)
7811 pp = &curwin->w_prevdir;
7812 else if (scope == CDSCOPE_TABPAGE)
7813 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007814 else
zeertzjq73257142022-02-02 13:16:37 +00007815 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007816 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007817 *pp = pdir;
7818
7819 post_chdir(scope);
7820
7821 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007822 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007823 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007824 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007825}
Bram Moolenaarea408852005-06-25 22:49:46 +00007826
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007828 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007830 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007831ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007833 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834
7835 new_dir = eap->arg;
7836#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007837 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7838 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007840 ex_pwd(NULL);
7841 return;
7842 }
7843#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007844
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007845 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007846
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007847 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7848 scope = CDSCOPE_WINDOW;
7849 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7850 scope = CDSCOPE_TABPAGE;
7851
7852 if (changedir_func(new_dir, eap->forceit, scope))
7853 {
7854 // Echo the new current directory if the command was typed.
7855 if (KeyTyped || p_verbose >= 5)
7856 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 }
7858}
7859
7860/*
7861 * ":pwd".
7862 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007864ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
7866 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7867 {
7868#ifdef BACKSLASH_IN_FILENAME
7869 slash_adjust(NameBuff);
7870#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007871 if (p_verbose > 0)
7872 {
7873 char *context = "global";
7874
Bram Moolenaar05268152021-11-18 18:53:45 +00007875 if (last_chdir_reason != NULL)
7876 context = last_chdir_reason;
7877 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007878 context = "window";
7879 else if (curtab->tp_localdir != NULL)
7880 context = "tabpage";
7881 smsg("[%s] %s", context, (char *)NameBuff);
7882 }
7883 else
7884 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 }
7886 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007887 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888}
7889
7890/*
7891 * ":=".
7892 */
7893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007894ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007896 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007897 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898}
7899
7900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007901ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007903 int n;
7904 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905
7906 if (cursor_valid())
7907 {
7908 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7909 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007910 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007912
7913 len = eap->line2;
7914 switch (*eap->arg)
7915 {
7916 case 'm': break;
7917 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007918 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007919 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007920
7921 // Hide the cursor if invoked with !
7922 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923}
7924
7925/*
7926 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007927 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 */
7929 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007930do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
Bram Moolenaar52953192019-08-16 10:27:13 +02007932 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007933 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007934# ifdef ELAPSED_FUNC
7935 elapsed_T start_tv;
7936
7937 // Remember at what time we started, so that we know how much longer we
7938 // should wait after waiting for a bit.
7939 ELAPSED_INIT(start_tv);
7940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007942 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007943 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007944 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007945 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007946
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007947 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007948 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007950 wait_now = msec - done > 1000L ? 1000L : msec - done;
7951#ifdef FEAT_TIMERS
7952 {
7953 long due_time = check_due_timer();
7954
7955 if (due_time > 0 && due_time < wait_now)
7956 wait_now = due_time;
7957 }
7958#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007959#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007960 if (has_any_channel() && wait_now > 20L)
7961 wait_now = 20L;
7962#endif
7963#ifdef FEAT_SOUND
7964 if (has_any_sound_callback() && wait_now > 20L)
7965 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007966#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007967 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007968
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007969#ifdef FEAT_JOB_CHANNEL
7970 if (has_any_channel())
7971 ui_breakcheck_force(TRUE);
7972 else
7973#endif
7974 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007975#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007976 // Process the netbeans and clientserver messages that may have been
7977 // received in the call to ui_breakcheck() when the GUI is in use. This
7978 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007979 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007980#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007981
7982# ifdef ELAPSED_FUNC
7983 // actual time passed
7984 done = ELAPSED_FUNC(start_tv);
7985# else
7986 // guestimate time passed (will actually be more)
7987 done += wait_now;
7988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007990
7991 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7992 // input buffer, otherwise a following call to input() fails.
7993 if (got_int)
7994 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007995
7996 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007997 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998}
7999
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000/*
8001 * ":winsize" command (obsolete).
8002 */
8003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008004ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
8006 int w, h;
8007 char_u *arg = eap->arg;
8008 char_u *p;
8009
Keith Thompson184f71c2024-01-04 21:19:04 +01008010 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008011 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008012 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008013 return;
8014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 w = getdigits(&arg);
8016 arg = skipwhite(arg);
8017 p = arg;
8018 h = getdigits(&arg);
8019 if (*p != NUL && *arg == NUL)
8020 set_shellsize(w, h, TRUE);
8021 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008022 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023}
8024
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008026ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027{
8028 int xchar = NUL;
8029 char_u *p;
8030
8031 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008033 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 if (eap->arg[1] == NUL)
8035 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008036 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 return;
8038 }
8039 xchar = eap->arg[1];
8040 p = eap->arg + 2;
8041 }
8042 else
8043 p = eap->arg + 1;
8044
Bram Moolenaar63b91732021-08-05 20:40:03 +02008045 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008047 if (*p != NUL && *p != (
8048#ifdef FEAT_EVAL
8049 in_vim9script() ? '#' :
8050#endif
8051 '"')
8052 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008053 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008054 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008056 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008057 postponed_split_flags = cmdmod.cmod_split;
8058 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8060 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008061 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 }
8063}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064
Bram Moolenaar843ee412004-06-30 16:16:41 +00008065#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066/*
8067 * ":winpos".
8068 */
8069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008070ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071{
8072 int x, y;
8073 char_u *arg = eap->arg;
8074 char_u *p;
8075
8076 if (*arg == NUL)
8077 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008078# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008079# ifdef VIMDLL
8080 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8081 mch_get_winpos(&x, &y) != FAIL)
8082# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008084# else
8085 if (mch_get_winpos(&x, &y) != FAIL)
8086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {
8088 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008089 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 }
8091 else
8092# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008093 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 }
8095 else
8096 {
8097 x = getdigits(&arg);
8098 arg = skipwhite(arg);
8099 p = arg;
8100 y = getdigits(&arg);
8101 if (*p == NUL || *arg != NUL)
8102 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008103 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 return;
8105 }
8106# ifdef FEAT_GUI
8107 if (gui.in_use)
8108 gui_mch_set_winpos(x, y);
8109 else if (gui.starting)
8110 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008111 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 gui_win_x = x;
8113 gui_win_y = y;
8114 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008115# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 else
8117# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008118# endif
8119# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008120 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121# endif
8122# ifdef HAVE_TGETENT
8123 if (*T_CWP)
8124 term_set_winpos(x, y);
8125# endif
8126 }
8127}
8128#endif
8129
8130/*
8131 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8132 */
8133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008134ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135{
8136 oparg_T oa;
8137
8138 clear_oparg(&oa);
8139 oa.regname = eap->regname;
8140 oa.start.lnum = eap->line1;
8141 oa.end.lnum = eap->line2;
8142 oa.line_count = eap->line2 - eap->line1 + 1;
8143 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008145 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {
8147 setpcmark();
8148 curwin->w_cursor.lnum = eap->line1;
8149 beginline(BL_SOL | BL_FIX);
8150 }
8151
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008152 if (VIsual_active)
8153 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008154
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 switch (eap->cmdidx)
8156 {
8157 case CMD_delete:
8158 oa.op_type = OP_DELETE;
8159 op_delete(&oa);
8160 break;
8161
8162 case CMD_yank:
8163 oa.op_type = OP_YANK;
8164 (void)op_yank(&oa, FALSE, TRUE);
8165 break;
8166
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008167 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008168 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008170 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8171#else
8172 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008174 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 oa.op_type = OP_RSHIFT;
8176 else
8177 oa.op_type = OP_LSHIFT;
8178 op_shift(&oa, FALSE, eap->amount);
8179 break;
8180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008182 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183}
8184
8185/*
8186 * ":put".
8187 */
8188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008189ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008191 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 if (eap->line2 == 0)
8193 {
8194 eap->line2 = 1;
8195 eap->forceit = TRUE;
8196 }
8197 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008198 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008199 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008200 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201}
8202
8203/*
8204 * Handle ":copy" and ":move".
8205 */
8206 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008207ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208{
8209 long n;
8210
Bram Moolenaar491799b2020-08-01 19:23:43 +02008211#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008212 if (not_in_vim9(eap) == FAIL)
8213 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008214#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008215 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008216 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {
8218 eap->nextcmd = NULL;
8219 return;
8220 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008221 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222
8223 /*
8224 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8225 */
8226 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8227 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008228 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 return;
8230 }
8231
8232 if (eap->cmdidx == CMD_move)
8233 {
8234 if (do_move(eap->line1, eap->line2, n) == FAIL)
8235 return;
8236 }
8237 else
8238 ex_copy(eap->line1, eap->line2, n);
8239 u_clearline();
8240 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008241 ex_may_print(eap);
8242}
8243
8244/*
8245 * Print the current line if flags were given to the Ex command.
8246 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008247 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008248ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008249{
8250 if (eap->flags != 0)
8251 {
8252 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8253 (eap->flags & EXFLAG_LIST));
8254 ex_no_reprint = TRUE;
8255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256}
8257
8258/*
8259 * ":smagic" and ":snomagic".
8260 */
8261 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008262ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008264 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008266 magic_overruled = eap->cmdidx == CMD_smagic
8267 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008268 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008269 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270}
8271
8272/*
8273 * ":join".
8274 */
8275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008276ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277{
8278 curwin->w_cursor.lnum = eap->line1;
8279 if (eap->line1 == eap->line2)
8280 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008281 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 return;
8283 if (eap->line2 == curbuf->b_ml.ml_line_count)
8284 {
8285 beep_flush();
8286 return;
8287 }
8288 ++eap->line2;
8289 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008290 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008292 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293}
8294
8295/*
8296 * ":[addr]@r" or ":[addr]*r": execute register
8297 */
8298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008299ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008302 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303
8304 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008305 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306
8307#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008308 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309#endif
8310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008311 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 c = *eap->arg;
8313 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8314 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008315 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008316 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8317 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008320 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008323 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008325 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008327 /*
8328 * Execute from the typeahead buffer.
8329 * Continue until the stuff buffer is empty and all added characters
8330 * have been consumed.
8331 */
8332 while (!stuff_empty() || typebuf.tb_len > prev_len)
8333 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8334
8335 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336}
8337
8338/*
8339 * ":!".
8340 */
8341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008342ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343{
8344 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8345}
8346
8347/*
8348 * ":undo".
8349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008351ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008353 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008354 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008355 else
8356 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357}
8358
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008359#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008361ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008362{
8363 char_u hash[UNDO_HASH_SIZE];
8364
8365 u_compute_hash(hash);
8366 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8367}
8368
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008370ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008371{
8372 char_u hash[UNDO_HASH_SIZE];
8373
8374 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008375 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008376}
8377#endif
8378
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379/*
8380 * ":redo".
8381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008383ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384{
8385 u_redo(1);
8386}
8387
8388/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008389 * ":earlier" and ":later".
8390 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008392ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008393{
8394 long count = 0;
8395 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008396 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008397 char_u *p = eap->arg;
8398
8399 if (*p == NUL)
8400 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008401 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008402 {
8403 count = getdigits(&p);
8404 switch (*p)
8405 {
8406 case 's': ++p; sec = TRUE; break;
8407 case 'm': ++p; sec = TRUE; count *= 60; break;
8408 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008409 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8410 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008411 }
8412 }
8413
8414 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008415 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008416 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008417 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8418 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008419}
8420
8421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 * ":redir": start/stop redirection.
8423 */
8424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008425ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426{
8427 char *mode;
8428 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008429 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430
Bram Moolenaarba768492016-07-08 15:32:54 +02008431#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008432 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008433 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008434 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008435 return;
8436 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008437#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008438
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 if (STRICMP(eap->arg, "END") == 0)
8440 close_redir();
8441 else
8442 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008443 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008445 ++arg;
8446 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008448 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 mode = "a";
8450 }
8451 else
8452 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008453 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454
8455 close_redir();
8456
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008457 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008458 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 if (fname == NULL)
8460 return;
8461#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008462 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {
8464 char_u *browseFile;
8465
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008466 browseFile = do_browse(BROWSE_SAVE,
8467 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008468 fname, NULL, NULL,
8469 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008471 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 vim_free(fname);
8473 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008474 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 }
8476#endif
8477
8478 redir_fd = open_exfile(fname, eap->forceit, mode);
8479 vim_free(fname);
8480 }
8481#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008482 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008484 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008486 ++arg;
8487 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008489 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008490 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008492 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008494 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008495 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008496 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008497 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008499 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008500 if (*arg == '>')
8501 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008502 // Make register empty when not using @A-@Z and the
8503 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008504 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008505 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008507 }
8508 if (*arg != NUL)
8509 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008510 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008511 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008512 }
8513 }
8514 else if (*arg == '=' && arg[1] == '>')
8515 {
8516 int append;
8517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008518 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008519 close_redir();
8520 arg += 2;
8521
8522 if (*arg == '>')
8523 {
8524 ++arg;
8525 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 }
8527 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008528 append = FALSE;
8529
8530 if (var_redir_start(skipwhite(arg), append) == OK)
8531 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 }
8533#endif
8534
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008535 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008538 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008541 // Make sure redirection is not off. Can happen for cmdline completion
8542 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008543 if (redir_fd != NULL
8544#ifdef FEAT_EVAL
8545 || redir_reg || redir_vname
8546#endif
8547 )
8548 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549}
8550
8551/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008552 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008554 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008555ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008557 redraw_cmd(eap->forceit);
8558}
8559
8560/*
8561 * ":redraw": force redraw, with clear if "clear" is TRUE.
8562 */
8563 void
8564redraw_cmd(int clear)
8565{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008566 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008568
8569 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008571
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008572 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008574 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 if (need_maketitle)
8576 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008577#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8578# ifdef VIMDLL
8579 if (!gui.in_use)
8580# endif
8581 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008582#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008583 RedrawingDisabled = save_RedrawingDisabled;
8584 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585
Bram Moolenaar5017c662022-04-07 18:06:08 +01008586 // After drawing the statusline screen_attr may still be set.
8587 screen_stop_highlight();
8588
Bram Moolenaara2a89732022-08-31 14:46:18 +01008589 // Reset msg_didout, so that a message that's there is overwritten.
8590 msg_didout = FALSE;
8591 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008593 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008594 need_wait_return = FALSE;
8595
Bram Moolenaara653e532022-04-19 11:38:24 +01008596 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008597 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008598 redrawcmdline();
8599
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 out_flush();
8601}
8602
8603/*
8604 * ":redrawstatus": force redraw of status line(s)
8605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008607ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 if (eap->forceit)
8610 status_redraw_all();
8611 else
8612 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008613 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008614 return; // redraw later
8615
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008616 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008617 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008618
8619 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008620 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008621
zeertzjq320d9102022-09-20 17:12:13 +01008622 if (State & MODE_CMDLINE)
8623 redraw_statuslines();
8624 else
8625 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008626 RedrawingDisabled = save_RedrawingDisabled;
8627 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629}
8630
Bram Moolenaare12bab32019-01-08 22:02:56 +01008631/*
8632 * ":redrawtabline": force redraw of the tabline
8633 */
8634 static void
8635ex_redrawtabline(exarg_T *eap UNUSED)
8636{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008637 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008638 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008639
8640 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008641 p_lz = FALSE;
8642
8643 draw_tabline();
8644
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008645 RedrawingDisabled = save_RedrawingDisabled;
8646 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008647 out_flush();
8648}
8649
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008651close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652{
8653 if (redir_fd != NULL)
8654 {
8655 fclose(redir_fd);
8656 redir_fd = NULL;
8657 }
8658#ifdef FEAT_EVAL
8659 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008660 if (redir_vname)
8661 {
8662 var_redir_stop();
8663 redir_vname = 0;
8664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665#endif
8666}
8667
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008668#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008669 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008670vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008671{
8672 if (vim_mkdir(name, prot) != 0)
8673 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008674 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008675 return FAIL;
8676 }
8677 return OK;
8678}
8679#endif
8680
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681/*
8682 * Open a file for writing for an Ex command, with some checks.
8683 * Return file descriptor, or NULL on failure.
8684 */
8685 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008686open_exfile(
8687 char_u *fname,
8688 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008689 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690{
8691 FILE *fd;
8692
8693#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008694 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 if (mch_isdir(fname))
8696 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008697 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 return NULL;
8699 }
8700#endif
8701 if (!forceit && *mode != 'a' && vim_fexists(fname))
8702 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008703 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 return NULL;
8705 }
8706
8707 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008708 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709
8710 return fd;
8711}
8712
8713/*
8714 * ":mark" and ":k".
8715 */
8716 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008717ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718{
8719 pos_T pos;
8720
Bram Moolenaar10b94212021-02-19 21:42:57 +01008721#ifdef FEAT_EVAL
8722 if (not_in_vim9(eap) == FAIL)
8723 return;
8724#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008725 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008727 emsg(_(e_argument_required));
8728 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008730
8731 if (eap->arg[1] != NUL) // more than one character?
8732 {
8733 semsg(_(e_trailing_characters_str), eap->arg);
8734 return;
8735 }
8736
8737 pos = curwin->w_cursor; // save curwin->w_cursor
8738 curwin->w_cursor.lnum = eap->line2;
8739 beginline(BL_WHITE | BL_FIX);
8740 if (setmark(*eap->arg) == FAIL) // set mark
8741 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
8742 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743}
8744
8745/*
8746 * Update w_topline, w_leftcol and the cursor position.
8747 */
8748 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008749update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008751 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 update_topline();
8753 if (!curwin->w_p_wrap)
8754 validate_cursor();
8755 update_curswant();
8756}
8757
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008759 * Save the current State and go to Normal mode.
8760 * Return TRUE if the typeahead could be saved.
8761 */
8762 int
8763save_current_state(save_state_T *sst)
8764{
8765 sst->save_msg_scroll = msg_scroll;
8766 sst->save_restart_edit = restart_edit;
8767 sst->save_msg_didout = msg_didout;
8768 sst->save_State = State;
8769 sst->save_insertmode = p_im;
8770 sst->save_finish_op = finish_op;
8771 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008772 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008773 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008774
Bram Moolenaar4324d872020-12-01 20:12:24 +01008775 msg_scroll = FALSE; // no msg scrolling in Normal mode
8776 restart_edit = 0; // don't go to Insert mode
8777 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008778
Bram Moolenaara452b802020-12-01 21:47:59 +01008779 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008780 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008781
8782 /*
8783 * Save the current typeahead. This is required to allow using ":normal"
8784 * from an event handler and makes sure we don't hang when the argument
8785 * ends with half a command.
8786 */
8787 save_typeahead(&sst->tabuf);
8788 return sst->tabuf.typebuf_valid;
8789}
8790
8791 void
8792restore_current_state(save_state_T *sst)
8793{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008794 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008795 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008796
8797 msg_scroll = sst->save_msg_scroll;
8798 restart_edit = sst->save_restart_edit;
8799 p_im = sst->save_insertmode;
8800 finish_op = sst->save_finish_op;
8801 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008802 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008803 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008804 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008805 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008806
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008807 // Restore the state (needed when called from a function executed for
8808 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008809 State = sst->save_State;
8810#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008811 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008812#endif
8813}
8814
8815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 * ":normal[!] {commands}": Execute normal mode commands.
8817 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008818 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008819ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008821 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 char_u *arg = NULL;
8823 int l;
8824 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008826 if (ex_normal_lock > 0)
8827 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008828 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008829 return;
8830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 if (ex_normal_busy >= p_mmd)
8832 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008833 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 return;
8835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 /*
8838 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8839 * this for the K_SPECIAL leading byte, otherwise special keys will not
8840 * work.
8841 */
8842 if (has_mbyte)
8843 {
8844 int len = 0;
8845
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008846 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 for (p = eap->arg; *p != NUL; ++p)
8848 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008849#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008850 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008852#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008853 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008854 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008855#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 )
8859 len += 2;
8860 }
8861 if (len > 0)
8862 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008863 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 if (arg != NULL)
8865 {
8866 len = 0;
8867 for (p = eap->arg; *p != NUL; ++p)
8868 {
8869 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008870#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 if (*p == CSI)
8872 {
8873 arg[len++] = KS_EXTRA;
8874 arg[len++] = (int)KE_CSI;
8875 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008876#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008877 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 {
8879 arg[len++] = *++p;
8880 if (*p == K_SPECIAL)
8881 {
8882 arg[len++] = KS_SPECIAL;
8883 arg[len++] = KE_FILLER;
8884 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008885#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 else if (*p == CSI)
8887 {
8888 arg[len++] = KS_EXTRA;
8889 arg[len++] = (int)KE_CSI;
8890 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 }
8893 arg[len] = NUL;
8894 }
8895 }
8896 }
8897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008899 ++ex_normal_busy;
8900 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
8902 /*
8903 * Repeat the :normal command for each line in the range. When no
8904 * range given, execute it just once, without positioning the cursor
8905 * first.
8906 */
8907 do
8908 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 if (eap->addr_count != 0)
8910 {
8911 curwin->w_cursor.lnum = eap->line1++;
8912 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008913 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 }
8915
Bram Moolenaar13505972019-01-24 15:04:48 +01008916 exec_normal_cmd(arg != NULL
8917 ? arg
8918 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 }
8920 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8921 }
8922
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008923 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 update_topline_cursor();
8925
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008926 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008928 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008929#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008930 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008931#endif
8932
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934}
8935
8936/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008937 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 */
8939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008940ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008942 if (eap->forceit)
8943 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008944 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008945 if (!curwin->w_cursor.lnum)
8946 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008947 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008948 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008949#ifdef FEAT_TERMINAL
8950 // Ignore this when running in an active terminal.
8951 if (term_job_running(curbuf->b_term))
8952 return;
8953#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008954
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008955 // Ignore the command when already in Insert mode. Inserting an
8956 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01008957 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00008958 return;
8959
Bram Moolenaar64969662005-12-14 21:59:55 +00008960 if (eap->cmdidx == CMD_startinsert)
8961 restart_edit = 'a';
8962 else if (eap->cmdidx == CMD_startreplace)
8963 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008965 restart_edit = 'V';
8966
8967 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008969 if (eap->cmdidx == CMD_startinsert)
8970 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008971 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008973
8974 if (VIsual_active)
8975 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976}
8977
8978/*
8979 * ":stopinsert"
8980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008982ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983{
8984 restart_edit = 0;
8985 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008986 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008989/*
8990 * Execute normal mode command "cmd".
8991 * "remap" can be REMAP_NONE or REMAP_YES.
8992 */
8993 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008994exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008995{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008996 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008997 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008998 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008999}
Bram Moolenaar25281632016-01-21 23:32:32 +01009000
Bram Moolenaar25281632016-01-21 23:32:32 +01009001/*
9002 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009003 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009004 */
9005 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009006exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009007{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009008 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009009 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009010
Bram Moolenaar905dd902019-04-07 14:21:47 +02009011 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9012 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009013 clear_oparg(&oa);
9014 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009015 while ((!stuff_empty()
9016 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009017 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009018 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009019 {
9020 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009021#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009022 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009023 && oa.op_type == OP_NOP && oa.regname == NUL
9024 && !VIsual_active)
9025 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009026 // If terminal_loop() returns OK we got a key that is handled
9027 // in Normal model. With FAIL we first need to position the
9028 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009029 if (terminal_loop(TRUE) == OK)
9030 normal_cmd(&oa, TRUE);
9031 }
9032 else
9033#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009034 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009035 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009036 }
9037}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009038
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039#ifdef FEAT_FIND_ID
9040 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009041ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042{
9043 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9044 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009045 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046}
9047
Bram Moolenaar4033c552017-09-16 20:54:51 +02009048#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049/*
9050 * ":psearch"
9051 */
9052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009053ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054{
9055 g_do_tagpreview = p_pvh;
9056 ex_findpat(eap);
9057 g_do_tagpreview = 0;
9058}
9059#endif
9060
9061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009062ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
9064 int whole = TRUE;
9065 long n;
9066 char_u *p;
9067 int action;
9068
9069 switch (cmdnames[eap->cmdidx].cmd_name[2])
9070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009071 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9073 action = ACTION_GOTO;
9074 else
9075 action = ACTION_SHOW;
9076 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009077 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 action = ACTION_SHOW_ALL;
9079 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009080 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 action = ACTION_GOTO;
9082 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009083 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 action = ACTION_SPLIT;
9085 break;
9086 }
9087
9088 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009089 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 {
9091 n = getdigits(&eap->arg);
9092 eap->arg = skipwhite(eap->arg);
9093 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009094 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 {
9096 whole = FALSE;
9097 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009098 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 if (*p)
9100 {
9101 *p++ = NUL;
9102 p = skipwhite(p);
9103
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009104 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009105 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009106 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009108 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 }
9110 }
9111 if (!eap->skip)
9112 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9113 whole, !eap->forceit,
9114 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009115 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116}
9117#endif
9118
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119
Bram Moolenaar4033c552017-09-16 20:54:51 +02009120#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121/*
9122 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9123 */
9124 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009125ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009127 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9129}
9130
9131/*
9132 * ":pedit"
9133 */
9134 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009135ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
9137 win_T *curwin_save = curwin;
9138
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009139 if (ERROR_IF_ANY_POPUP_WINDOW)
9140 return;
9141
Bram Moolenaar026587b2019-08-17 15:08:00 +02009142 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009144 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009145
Bram Moolenaar026587b2019-08-17 15:08:00 +02009146 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009148
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 if (curwin != curwin_save && win_valid(curwin_save))
9150 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009151 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009153 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 win_enter(curwin_save, TRUE);
9155 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009156# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009157 else if (WIN_IS_POPUP(curwin))
9158 {
9159 // can't keep focus in popup window
9160 win_enter(firstwin, TRUE);
9161 }
9162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 g_do_tagpreview = 0;
9164}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166
9167/*
9168 * ":stag", ":stselect" and ":stjump".
9169 */
9170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009171ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172{
9173 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009174 postponed_split_flags = cmdmod.cmod_split;
9175 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9177 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009178 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180
9181/*
9182 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9183 */
9184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009185ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186{
9187 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9188}
9189
9190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009191ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192{
9193 int cmd;
9194
9195 switch (name[1])
9196 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009197 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009199 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009201 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009203 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009205 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009207 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009209 case 'f': // ":tfirst"
9210 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009212 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009214 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009216 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009218 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 return;
9220 }
9221#endif
9222 cmd = DT_TAG;
9223 break;
9224 }
9225
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009226 if (name[0] == 'l')
9227 {
9228#ifndef FEAT_QUICKFIX
9229 ex_ni(eap);
9230 return;
9231#else
9232 cmd = DT_LTAG;
9233#endif
9234 }
9235
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9237 eap->forceit, TRUE);
9238}
9239
9240/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009241 * Check "str" for starting with a special cmdline variable.
9242 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9243 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9244 */
9245 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009246find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009247{
9248 int len;
9249 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009250 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009251 "%",
9252#define SPEC_PERC 0
9253 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009254#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009255 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02009256#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009257 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02009258#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009259 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02009260#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009261 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009262#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009263 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009264#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009265 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02009266#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02009267 "<stack>", // call stack
9268#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01009269 "<script>", // script file name
9270#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009271 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01009272#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009273 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009274#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009275 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009276#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009277 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009278#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02009279 "<SID>", // script ID: <SNR>123_
9280#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009281#ifdef FEAT_CLIENTSERVER
9282 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02009283# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009284#endif
9285 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009286
K.Takataeeec2542021-06-02 13:28:16 +02009287 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009288 {
9289 len = (int)STRLEN(spec_str[i]);
9290 if (STRNCMP(src, spec_str[i], len) == 0)
9291 {
9292 *usedlen = len;
9293 return i;
9294 }
9295 }
9296 return -1;
9297}
9298
9299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 * Evaluate cmdline variables.
9301 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009302 * change "%" to curbuf->b_ffname
9303 * "#" to curwin->w_alt_fnum
9304 * "%%" to curwin->w_alt_fnum in Vim9 script
9305 * "<cword>" to word under the cursor
9306 * "<cWORD>" to WORD under the cursor
9307 * "<cexpr>" to C-expression under the cursor
9308 * "<cfile>" to path name under the cursor
9309 * "<sfile>" to sourced file name
9310 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009311 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009312 * "<slnum>" to sourced file line number
9313 * "<afile>" to file name for autocommand
9314 * "<abuf>" to buffer number for autocommand
9315 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 *
9317 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9318 * "" for error without a message) and NULL is returned.
9319 * Returns an allocated string if a valid match was found.
9320 * Returns NULL if no match was found. "usedlen" then still contains the
9321 * number of characters to skip.
9322 */
9323 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009324eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009325 char_u *src, // pointer into commandline
9326 char_u *srcstart, // beginning of valid memory for src
9327 int *usedlen, // characters after src that are used
9328 linenr_T *lnump, // line number for :e command, or NULL
9329 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009330 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009331 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009332 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333{
9334 int i;
9335 char_u *s;
9336 char_u *result;
9337 char_u *resultbuf = NULL;
9338 int resultlen;
9339 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009340 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009342 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345
9346 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009347 if (escaped != NULL)
9348 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349
9350 /*
9351 * Check if there is something to do.
9352 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009353 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009354 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 {
9356 *usedlen = 1;
9357 return NULL;
9358 }
9359
9360 /*
9361 * Skip when preceded with a backslash "\%" and "\#".
9362 * Note: In "\\%" the % is also not recognized!
9363 */
9364 if (src > srcstart && src[-1] == '\\')
9365 {
9366 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009367 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 return NULL;
9369 }
9370
9371 /*
9372 * word or WORD under cursor
9373 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009374 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9375 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009377 resultlen = find_ident_under_cursor(&result,
9378 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9379 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9380 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 if (resultlen == 0)
9382 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009383 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 return NULL;
9385 }
9386 }
9387
9388 /*
9389 * '#': Alternate file name
9390 * '%': Current file name
9391 * File name under the cursor
9392 * File name for autocommand
9393 * and following modifiers
9394 */
9395 else
9396 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009397 int off = 0;
9398
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 switch (spec_idx)
9400 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009401 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009402#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009403 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009406 // '%': current file
9407 if (curbuf->b_fname == NULL)
9408 {
9409 result = (char_u *)"";
9410 valid = 0; // Must have ":p:h" to be valid
9411 }
9412 else
9413 {
9414 result = curbuf->b_fname;
9415 tilde_file = STRCMP(result, "~") == 0;
9416 }
9417 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009419#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009420 // "%%" alternate file
9421 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009422#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009423 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009424 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009425 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009427 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 result = arg_all();
9429 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009430 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009431 if (escaped != NULL)
9432 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 break;
9435 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009436 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009437 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009438 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009440 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009441 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009442 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009443 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009445 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009447 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009448 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009449 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009450 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009451 return NULL;
9452 }
9453#ifdef FEAT_EVAL
9454 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9455 (long)i);
9456 if (result == NULL)
9457 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009458 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009459 return NULL;
9460 }
9461#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009462 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 }
9466 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009467 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009468 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9469 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009470 buf = buflist_findnr(i);
9471 if (buf == NULL)
9472 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009473 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009474 return NULL;
9475 }
9476 if (lnump != NULL)
9477 *lnump = ECMD_LAST;
9478 if (buf->b_fname == NULL)
9479 {
9480 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009481 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009482 }
9483 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009484 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009485 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009486 tilde_file = STRCMP(result, "~") == 0;
9487 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 break;
9490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009491 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009492 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 if (result == NULL)
9494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009495 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 return NULL;
9497 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009498 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009501 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009503 if (result != NULL && !autocmd_fname_full)
9504 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009505 // Still need to turn the fname into a full path. It is
9506 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009507 autocmd_fname_full = TRUE;
9508 result = FullName_save(autocmd_fname, FALSE);
9509 vim_free(autocmd_fname);
9510 autocmd_fname = result;
9511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 if (result == NULL)
9513 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009514 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 return NULL;
9516 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009517 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 break;
9519
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009520 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 if (autocmd_bufnr <= 0)
9522 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009523 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 return NULL;
9525 }
9526 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9527 result = strbuf;
9528 break;
9529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009530 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 result = autocmd_match;
9532 if (result == NULL)
9533 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009534 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 return NULL;
9536 }
9537 break;
9538
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009539 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009540 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 if (result == NULL)
9542 {
LemonBoy6013d002022-04-09 21:42:10 +01009543 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9544 return NULL;
9545 }
9546 resultbuf = result; // remember allocated string
9547 break;
9548 case SPEC_STACK: // call stack
9549 result = estack_sfile(ESTACK_STACK);
9550 if (result == NULL)
9551 {
9552 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9553 return NULL;
9554 }
9555 resultbuf = result; // remember allocated string
9556 break;
9557 case SPEC_SCRIPT: // script file name
9558 result = estack_sfile(ESTACK_SCRIPT);
9559 if (result == NULL)
9560 {
9561 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 return NULL;
9563 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009564 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009566
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009567 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009568 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009569 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009570 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009571 return NULL;
9572 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009573 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009574 result = strbuf;
9575 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009576
9577#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009578 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009579 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009580 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009581 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009582 return NULL;
9583 }
9584 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009585 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009586 result = strbuf;
9587 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009588
Bram Moolenaar90944302020-08-01 20:45:11 +02009589 case SPEC_SID:
9590 if (current_sctx.sc_sid <= 0)
9591 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009592 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009593 return NULL;
9594 }
9595 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9596 result = strbuf;
9597 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009598#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009599
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009600#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009601 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009602 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9603 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 result = strbuf;
9605 break;
9606#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009607
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009608 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009609 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009610 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 }
9612
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009613 resultlen = (int)STRLEN(result); // length of new string
9614 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 {
9616 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 resultlen = (int)(s - result);
9619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 else if (!skip_mod)
9621 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009622 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 &resultlen);
9624 if (result == NULL)
9625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009626 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 return NULL;
9628 }
9629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 }
9631
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009632 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009634 if (empty_is_error)
9635 {
9636 if (valid != VALID_HEAD + VALID_PATH)
9637 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9638 else
9639 *errormsg = _(e_evaluates_to_an_empty_string);
9640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 result = NULL;
9642 }
9643 else
9644 result = vim_strnsave(result, resultlen);
9645 vim_free(resultbuf);
9646 return result;
9647}
9648
9649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 * Expand the <sfile> string in "arg".
9651 *
9652 * Returns an allocated string, or NULL for any error.
9653 */
9654 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009655expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009657 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 int len;
9659 char_u *result;
9660 char_u *newres;
9661 char_u *repl;
9662 int srclen;
9663 char_u *p;
9664
9665 result = vim_strsave(arg);
9666 if (result == NULL)
9667 return NULL;
9668
9669 for (p = result; *p; )
9670 {
9671 if (STRNCMP(p, "<sfile>", 7) != 0)
9672 ++p;
9673 else
9674 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009675 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009676 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 if (errormsg != NULL)
9678 {
9679 if (*errormsg)
9680 emsg(errormsg);
9681 vim_free(result);
9682 return NULL;
9683 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009684 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 {
9686 p += srclen;
9687 continue;
9688 }
9689 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9690 newres = alloc(len);
9691 if (newres == NULL)
9692 {
9693 vim_free(repl);
9694 vim_free(result);
9695 return NULL;
9696 }
9697 mch_memmove(newres, result, (size_t)(p - result));
9698 STRCPY(newres + (p - result), repl);
9699 len = (int)STRLEN(newres);
9700 STRCAT(newres, p + srclen);
9701 vim_free(repl);
9702 vim_free(result);
9703 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009704 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 }
9706 }
9707
9708 return result;
9709}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009712/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009713 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009714 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009717dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 if (fname == NULL)
9720 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009721 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722}
9723#endif
9724
9725/*
9726 * ":behave {mswin,xterm}"
9727 */
9728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009729ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731 if (STRCMP(eap->arg, "mswin") == 0)
9732 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009733 set_option_value_give_err((char_u *)"selection",
9734 0L, (char_u *)"exclusive", 0);
9735 set_option_value_give_err((char_u *)"selectmode",
9736 0L, (char_u *)"mouse,key", 0);
9737 set_option_value_give_err((char_u *)"mousemodel",
9738 0L, (char_u *)"popup", 0);
9739 set_option_value_give_err((char_u *)"keymodel",
9740 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 }
9742 else if (STRCMP(eap->arg, "xterm") == 0)
9743 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009744 set_option_value_give_err((char_u *)"selection",
9745 0L, (char_u *)"inclusive", 0);
9746 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9747 set_option_value_give_err((char_u *)"mousemodel",
9748 0L, (char_u *)"extend", 0);
9749 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 }
9751 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009752 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753}
9754
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755static int filetype_detect = FALSE;
9756static int filetype_plugin = FALSE;
9757static int filetype_indent = FALSE;
9758
9759/*
9760 * ":filetype [plugin] [indent] {on,off,detect}"
9761 * on: Load the filetype.vim file to install autocommands for file types.
9762 * off: Load the ftoff.vim file to remove all autocommands for file types.
9763 * plugin on: load filetype.vim and ftplugin.vim
9764 * plugin off: load ftplugof.vim
9765 * indent on: load filetype.vim and indent.vim
9766 * indent off: load indoff.vim
9767 */
9768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009769ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771 char_u *arg = eap->arg;
9772 int plugin = FALSE;
9773 int indent = FALSE;
9774
9775 if (*eap->arg == NUL)
9776 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009777 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009778 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 filetype_detect ? "ON" : "OFF",
9780 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9781 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9782 return;
9783 }
9784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009785 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 for (;;)
9787 {
9788 if (STRNCMP(arg, "plugin", 6) == 0)
9789 {
9790 plugin = TRUE;
9791 arg = skipwhite(arg + 6);
9792 continue;
9793 }
9794 if (STRNCMP(arg, "indent", 6) == 0)
9795 {
9796 indent = TRUE;
9797 arg = skipwhite(arg + 6);
9798 continue;
9799 }
9800 break;
9801 }
9802 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9803 {
9804 if (*arg == 'o' || !filetype_detect)
9805 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009806 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 filetype_detect = TRUE;
9808 if (plugin)
9809 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009810 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 filetype_plugin = TRUE;
9812 }
9813 if (indent)
9814 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009815 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 filetype_indent = TRUE;
9817 }
9818 }
9819 if (*arg == 'd')
9820 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009821 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009822 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 }
9824 }
9825 else if (STRCMP(arg, "off") == 0)
9826 {
9827 if (plugin || indent)
9828 {
9829 if (plugin)
9830 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009831 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 filetype_plugin = FALSE;
9833 }
9834 if (indent)
9835 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009836 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 filetype_indent = FALSE;
9838 }
9839 }
9840 else
9841 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009842 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 filetype_detect = FALSE;
9844 }
9845 }
9846 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009847 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848}
9849
9850/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009851 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 */
9853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009854ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855{
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009856 if (did_filetype)
9857 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009858
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009859 char_u *arg = eap->arg;
9860 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9861 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009862
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009863 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9864 if (arg != eap->arg)
9865 did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009869ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870{
9871#ifdef FEAT_DIGRAPHS
9872 if (*eap->arg != NUL)
9873 putdigraph(eap->arg);
9874 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009875 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876#else
mityu61065042021-07-19 20:07:21 +02009877 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878#endif
9879}
9880
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009881#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9882 void
9883set_no_hlsearch(int flag)
9884{
9885 no_hlsearch = flag;
9886# ifdef FEAT_EVAL
9887 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9888# endif
9889}
9890
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891/*
9892 * ":nohlsearch"
9893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009895ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009897 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009898 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900#endif
9901
9902#ifdef FEAT_CRYPT
9903/*
9904 * ":X": Get crypt key
9905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009907ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009909 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009910 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911}
9912#endif
9913
9914#ifdef FEAT_FOLDING
9915 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009916ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917{
9918 if (foldManualAllowed(TRUE))
9919 foldCreate(eap->line1, eap->line2);
9920}
9921
9922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009923ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924{
9925 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9926 eap->forceit, FALSE);
9927}
9928
9929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009930ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931{
9932 linenr_T lnum;
9933
Bram Moolenaar4facea32019-10-12 20:17:40 +02009934# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009935 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009936# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009937
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009938 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9940 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9941 ml_setmarked(lnum);
9942
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009943 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009945 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009946# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009947 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009948# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949}
9950#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009951
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009952#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009953/*
9954 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9955 * instead of a quickfix command.
9956 */
9957 int
9958is_loclist_cmd(int cmdidx)
9959{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009960 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009961 return FALSE;
9962 return cmdnames[cmdidx].cmd_name[0] == 'l';
9963}
9964#endif
9965
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009966 int
9967get_pressedreturn(void)
9968{
9969 return ex_pressedreturn;
9970}
9971
9972 void
9973set_pressedreturn(int val)
9974{
9975 ex_pressedreturn = val;
9976}