blob: 807b470d8b2e3f40c82a11845fda0077628120c6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058# define ex_cclose ex_ni
59# define ex_copen ex_ni
60# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020061# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000063#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
64# define ex_cexpr ex_ni
65#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020067static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020068static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010070static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020071#if !defined(FEAT_PERL) \
72 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
73 || !defined(FEAT_TCL) \
74 || !defined(FEAT_RUBY) \
75 || !defined(FEAT_LUA) \
76 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000077# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010080static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000082#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#endif
John Marriotted908f72024-04-18 22:46:56 +020085static char_u *repl_cmdline(exarg_T *eap, char_u *src, size_t srclen, char_u *repl, char_u **cmdlinep);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static void ex_highlight(exarg_T *eap);
87static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void ex_cquit(exarg_T *eap);
89static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_close(exarg_T *eap);
91static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
92static void ex_only(exarg_T *eap);
93static void ex_resize(exarg_T *eap);
94static void ex_stag(exarg_T *eap);
95static void ex_tabclose(exarg_T *eap);
96static void ex_tabonly(exarg_T *eap);
97static void ex_tabnext(exarg_T *eap);
98static void ex_tabmove(exarg_T *eap);
99static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100101static void ex_pclose(exarg_T *eap);
102static void ex_ptag(exarg_T *eap);
103static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#else
105# define ex_pclose ex_ni
106# define ex_ptag ex_ni
107# define ex_pedit ex_ni
108#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_hide(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100110static void ex_exit(exarg_T *eap);
111static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100113static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#else
115# define ex_goto ex_ni
116#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100117static void ex_shell(exarg_T *eap);
118static void ex_preserve(exarg_T *eap);
119static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_mode(exarg_T *eap);
121static void ex_wrongmodifier(exarg_T *eap);
122static void ex_find(exarg_T *eap);
123static void ex_open(exarg_T *eap);
124static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#ifndef FEAT_GUI
126# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100127static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100129#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#else
132# define ex_tearoff ex_ni
133#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100134#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
135 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100136static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#else
138# define ex_popup ex_ni
139#endif
140#ifndef FEAT_GUI_MSWIN
141# define ex_simalt ex_ni
142#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000143#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144# define gui_mch_find_dialog ex_ni
145# define gui_mch_replace_dialog ex_ni
146#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000147#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148# define ex_helpfind ex_ni
149#endif
150#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100151# define ex_cscope ex_ni
152# define ex_scscope ex_ni
153# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#endif
155#ifndef FEAT_SYN_HL
156# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200157# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000158#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200159#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200160# define ex_syntime ex_ni
161#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000162#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000163# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000164# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000165# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000166# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000167# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000168#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200169#ifndef FEAT_PERSISTENT_UNDO
170# define ex_rundo ex_ni
171# define ex_wundo ex_ni
172#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200173#ifndef FEAT_LUA
174# define ex_lua ex_script_ni
175# define ex_luado ex_ni
176# define ex_luafile ex_ni
177#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000178#ifndef FEAT_MZSCHEME
179# define ex_mzscheme ex_script_ni
180# define ex_mzfile ex_ni
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#ifndef FEAT_PERL
183# define ex_perl ex_script_ni
184# define ex_perldo ex_ni
185#endif
186#ifndef FEAT_PYTHON
187# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200188# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189# define ex_pyfile ex_ni
190#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200191#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200192# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200193# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194# define ex_py3file ex_ni
195#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100196#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
197# define ex_pyx ex_script_ni
198# define ex_pyxdo ex_ni
199# define ex_pyxfile ex_ni
200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifndef FEAT_TCL
202# define ex_tcl ex_script_ni
203# define ex_tcldo ex_ni
204# define ex_tclfile ex_ni
205#endif
206#ifndef FEAT_RUBY
207# define ex_ruby ex_script_ni
208# define ex_rubydo ex_ni
209# define ex_rubyfile ex_ni
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211#ifndef FEAT_KEYMAP
212# define ex_loadkeymap ex_ni
213#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100214static void ex_swapname(exarg_T *eap);
215static void ex_syncbind(exarg_T *eap);
216static void ex_read(exarg_T *eap);
217static void ex_pwd(exarg_T *eap);
218static void ex_equal(exarg_T *eap);
219static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100220static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000222#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#else
225# define ex_winpos ex_ni
226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ex_operators(exarg_T *eap);
228static void ex_put(exarg_T *eap);
229static void ex_copymove(exarg_T *eap);
230static void ex_submagic(exarg_T *eap);
231static void ex_join(exarg_T *eap);
232static void ex_at(exarg_T *eap);
233static void ex_bang(exarg_T *eap);
234static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200235#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ex_wundo(exarg_T *eap);
237static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_redo(exarg_T *eap);
240static void ex_later(exarg_T *eap);
241static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100243static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_startinsert(exarg_T *eap);
247static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_checkpath(exarg_T *eap);
250static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#else
252# define ex_findpat ex_ni
253# define ex_checkpath ex_ni
254#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200255#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#else
258# define ex_psearch ex_ni
259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static void ex_tag(exarg_T *eap);
261static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200263# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200264# define ex_break ex_ni
265# define ex_breakadd ex_ni
266# define ex_breakdel ex_ni
267# define ex_breaklist ex_ni
268# define ex_call ex_ni
269# define ex_catch ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000270# define ex_class ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200272# define ex_continue ex_ni
273# define ex_debug ex_ni
274# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200275# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200281# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200282# define ex_endfunction ex_ni
283# define ex_endif ex_ni
284# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285# define ex_endwhile ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000286# define ex_enum ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
289# define ex_finally ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000290# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200291# define ex_finish ex_ni
292# define ex_function ex_ni
293# define ex_if ex_ni
294# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200295# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200296# define ex_lockvar ex_ni
297# define ex_oldfiles ex_ni
298# define ex_options ex_ni
299# define ex_packadd ex_ni
300# define ex_packloadall ex_ni
301# define ex_return ex_ni
302# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_throw ex_ni
304# define ex_try ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000305# define ex_type ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200307# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_import ex_ni
309# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200311#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_loadview ex_ni
313#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200314#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315# define ex_viminfo ex_ni
316#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_filetype(exarg_T *eap);
319static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000321# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322# define ex_diffpatch ex_ni
323# define ex_diffgetput ex_ni
324# define ex_diffsplit ex_ni
325# define ex_diffthis ex_ni
326# define ex_diffupdate ex_ni
327#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#else
332# define ex_nohlsearch ex_ni
333# define ex_match ex_ni
334#endif
335#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_X ex_ni
339#endif
340#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_fold(exarg_T *eap);
342static void ex_foldopen(exarg_T *eap);
343static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#else
345# define ex_fold ex_ni
346# define ex_foldopen ex_ni
347# define ex_folddo ex_ni
348#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100349#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350# define ex_language ex_ni
351#endif
352#ifndef FEAT_SIGNS
353# define ex_sign ex_ni
354#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar05159a02005-02-26 23:04:13 +0000361#ifndef FEAT_PROFILE
362# define ex_profile ex_ni
363#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200364#ifndef FEAT_TERMINAL
365# define ex_terminal ex_ni
366#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200367#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
368# define ex_xrestore ex_ni
369#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100370#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200371# define ex_popupclear ex_ni
372#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374/*
375 * Declare cmdnames[].
376 */
377#define DO_DECLARE_EXCMD
378#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200379#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381static char_u dollar_command[2] = {'$', 0};
382
383
384#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100385// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386typedef struct
387{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388 char_u *line; // command line
389 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390} wcmd_T;
391
392/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000393 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000395 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000397struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100399 garray_T *lines_gap; // growarray with line info
400 int current_line; // last read line from growarray
401 int repeating; // TRUE when looping a second time
402 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100403 char_u *(*lc_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 void *cookie;
405};
406
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200407static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static int store_loop_line(garray_T *gap, char_u *line);
409static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000410
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100411// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000412struct dbg_stuff
413{
414 int trylevel;
415 int force_abort;
416 except_T *caught_stack;
417 char_u *vv_exception;
418 char_u *vv_throwpoint;
419 int did_emsg;
420 int got_int;
421 int did_throw;
422 int need_rethrow;
423 int check_cstack;
424 except_T *current_exception;
425};
426
Bram Moolenaared203462004-06-16 11:19:22 +0000427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100428save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000429{
430 dsp->trylevel = trylevel; trylevel = 0;
431 dsp->force_abort = force_abort; force_abort = FALSE;
432 dsp->caught_stack = caught_stack; caught_stack = NULL;
433 dsp->vv_exception = v_exception(NULL);
434 dsp->vv_throwpoint = v_throwpoint(NULL);
435
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100436 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000437 dsp->did_emsg = did_emsg; did_emsg = FALSE;
438 dsp->got_int = got_int; got_int = FALSE;
439 dsp->did_throw = did_throw; did_throw = FALSE;
440 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
441 dsp->check_cstack = check_cstack; check_cstack = FALSE;
442 dsp->current_exception = current_exception; current_exception = NULL;
443}
444
445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100446restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000447{
448 suppress_errthrow = FALSE;
449 trylevel = dsp->trylevel;
450 force_abort = dsp->force_abort;
451 caught_stack = dsp->caught_stack;
452 (void)v_exception(dsp->vv_exception);
453 (void)v_throwpoint(dsp->vv_throwpoint);
454 did_emsg = dsp->did_emsg;
455 got_int = dsp->got_int;
456 did_throw = dsp->did_throw;
457 need_rethrow = dsp->need_rethrow;
458 check_cstack = dsp->check_cstack;
459 current_exception = dsp->current_exception;
460}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461#endif
462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463/*
Colin Kennedy65e580b2024-03-26 18:29:30 +0100464 * Check if ffname differs from fnum.
465 * fnum is a buffer number. 0 == current buffer, 1-or-more must be a valid buffer ID.
466 * ffname is a full path to where a buffer lives on-disk or would live on-disk.
467 *
468 */
469 static int
470is_other_file(int fnum, char_u *ffname)
471{
472 if (fnum != 0)
473 {
474 if (fnum == curbuf->b_fnum)
475 return FALSE;
476
477 return TRUE;
478 }
479
480 if (ffname == NULL)
481 return TRUE;
482
483 if (*ffname == NUL)
484 return FALSE;
485
486 // TODO: Need a reliable way to know whether a buffer is meant to live on-disk
487 // !curbuf->b_dev_valid is not always available (example: missing on Windows)
488 if (curbuf->b_sfname != NULL
489 && *curbuf->b_sfname != NUL)
490 // This occurs with unsaved buffers. In which case `ffname`
491 // actually corresponds to curbuf->b_sfname
492 return fnamecmp(ffname, curbuf->b_sfname) != 0;
493
494 return otherfile(ffname);
495}
496
497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
499 * command is given.
500 */
501 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100502do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100503 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504{
505 int save_msg_scroll;
506 int prev_msg_row;
507 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100508 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000509
510 if (improved)
511 exmode_active = EXMODE_VIM;
512 else
513 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100514 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100515 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000516
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100517 // When using ":global /pat/ visual" and then "Q" we return to continue
518 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000519 if (global_busy)
520 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
522 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100523 ++RedrawingDisabled; // don't redisplay the window
524 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100526 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 ++hold_gui_events;
528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
Bram Moolenaar32526b32019-01-19 17:43:09 +0100530 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 while (exmode_active)
532 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100533 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000534 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
535 {
536 exmode_active = FALSE;
537 break;
538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 msg_scroll = TRUE;
540 need_wait_return = FALSE;
541 ex_pressedreturn = FALSE;
542 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100543 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 prev_msg_row = msg_row;
545 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 if (improved)
547 {
548 cmdline_row = msg_row;
549 do_cmdline(NULL, getexline, NULL, 0);
550 }
551 else
552 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
553 lines_left = Rows - 1;
554
Bram Moolenaardf177f62005-02-22 08:39:57 +0000555 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100556 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000558 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000559 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000560 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000562 if (ex_pressedreturn)
563 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100564 // go up one line, to overwrite the ":<CR>" line, so the
565 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000566 msg_row = prev_msg_row;
567 if (prev_msg_row == Rows - 1)
568 msg_row--;
569 }
570 msg_col = 0;
571 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
572 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100575 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000576 {
577 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000578 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000579 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000580 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
583
584#ifdef FEAT_GUI
585 --hold_gui_events;
586#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100587 if (RedrawingDisabled > 0)
588 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100590 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 need_wait_return = FALSE;
592 msg_scroll = save_msg_scroll;
593}
594
595/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200596 * Print the executed command for when 'verbose' is set.
597 * When "lnum" is 0 only print the command.
598 */
599 static void
600msg_verbose_cmd(linenr_T lnum, char_u *cmd)
601{
602 ++no_wait_return;
603 verbose_enter_scroll();
604
605 if (lnum == 0)
606 smsg(_("Executing: %s"), cmd);
607 else
608 smsg(_("line %ld: %s"), (long)lnum, cmd);
609 if (msg_silent == 0)
610 msg_puts("\n"); // don't overwrite this
611
612 verbose_leave_scroll();
613 --no_wait_return;
614}
615
616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 * Execute a simple command line. Used for translated commands like "*".
618 */
619 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100620do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 return do_cmdline(cmd, NULL, NULL,
623 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
624}
625
626/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100627 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
628 * This allows for using a range without ":" in Vim9 script.
629 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200630 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100631do_cmd_argument(char_u *cmd)
632{
633 return do_cmdline(cmd, NULL, NULL,
634 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
635}
636
637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 * do_cmdline(): execute one Ex command line
639 *
640 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100641 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 * 2. Split up in parts separated with '|'.
643 *
644 * This function can be called recursively!
645 *
646 * flags:
647 * DOCMD_VERBOSE - The command will be included in the error message.
648 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100649 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 * DOCMD_KEYTYPED - Don't reset KeyTyped.
651 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
652 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
653 *
654 * return FAIL if cmdline could not be executed, OK otherwise
655 */
656 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100657do_cmdline(
658 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200659 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100660 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100661 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100663 char_u *next_cmdline; // next cmd to execute
664 char_u *cmdline_copy = NULL; // copy of cmd line
665 int used_getline = FALSE; // used "fgetline" to obtain command
666 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 int count = 0; // line number count
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100669 int did_inc_RedrawingDisabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 int retval = OK;
671#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100672 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100673 garray_T lines_ga; // keep lines for ":while"/":for"
674 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200675 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 char_u *fname = NULL; // function or script name
677 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
678 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
679 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200681 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200682 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100684 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200685 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000689 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100691# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692# define cmd_cookie cookie
693#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100694 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200695#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100696 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
697 // location for storing error messages to be converted to an exception.
698 // This ensures that the do_errthrow() call in do_one_cmd() does not
699 // combine the messages stored by an earlier invocation of do_one_cmd()
700 // with the command name of the later one. This would happen when
701 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 saved_msg_list = msg_list;
703 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#endif
705
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100706 // It's possible to create an endless loop with ":execute", catch that
707 // here. The value of 200 allows nested function calls, ":source", etc.
708 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100709 if (call_depth >= 200
710#ifdef FEAT_EVAL
711 && call_depth >= p_mfd
712#endif
713 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000715 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100717 // When converting to an exception, we do not include the command name
718 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100719 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 msg_list = saved_msg_list;
721#endif
722 return FAIL;
723 }
724 ++call_depth;
725
726#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100727 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000729 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100731 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100733 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100734 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000735 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 ++ex_nesting_level;
737
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100738 // Get the function or script name and the address where the next breakpoint
739 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000740 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {
742 fname = func_name(real_cookie);
743 breakpoint = func_breakpoint(real_cookie);
744 dbg_tick = func_dbg_tick(real_cookie);
745 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100746 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100748 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 breakpoint = source_breakpoint(real_cookie);
750 dbg_tick = source_dbg_tick(real_cookie);
751 }
752
753 /*
754 * Initialize "force_abort" and "suppress_errthrow" at the top level.
755 */
756 if (!recursive)
757 {
758 force_abort = FALSE;
759 suppress_errthrow = FALSE;
760 }
761
762 /*
763 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000764 * exception handling (used when debugging). Otherwise clear it to avoid
765 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000767 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000768 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000769 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200770 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 initial_trylevel = trylevel;
773
774 /*
775 * "did_throw" will be set to TRUE when an exception is being thrown.
776 */
777 did_throw = FALSE;
778#endif
779 /*
780 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * If force_abort is set, we cancel everything.
783 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100784#ifdef FEAT_EVAL
785 did_emsg_cumul += did_emsg;
786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 did_emsg = FALSE;
788
789 /*
790 * KeyTyped is only set when calling vgetc(). Reset it here when not
791 * calling vgetc() (sourced command lines).
792 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100793 if (!(flags & DOCMD_KEYTYPED)
794 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 KeyTyped = FALSE;
796
797 /*
798 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000799 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 * - for multiple commands on one line, separated with '|'
801 * - when repeating until there are no more lines (for ":source")
802 */
803 next_cmdline = cmdline;
804 do
805 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100807 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#endif
809
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100810 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (next_cmdline == NULL
812#ifdef FEAT_EVAL
813 && !force_abort
814 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816#endif
817 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100818 {
819#ifdef FEAT_EVAL
820 did_emsg_cumul += did_emsg;
821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000826 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 * 3. If a line is given: Make a copy, so we can mess with it.
829 */
830
831#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000833 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100835 // Each '|' separated command is stored separately in lines_ga, to
836 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100837 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100839 // Check if a function has returned or, unless it has an unclosed
840 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000841 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000844 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845 func_line_end(real_cookie);
846# endif
847 if (func_has_ended(real_cookie))
848 {
849 retval = FAIL;
850 break;
851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000854 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100855 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856 script_line_end();
857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100859 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100860 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
862 retval = FAIL;
863 break;
864 }
865
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100866 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (breakpoint != NULL && dbg_tick != NULL
868 && *dbg_tick != debug_tick)
869 {
870 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100872 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 *dbg_tick = debug_tick;
874 }
875
876 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100877 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100879 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100881 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100883 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100884 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100886 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100887 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 *dbg_tick = debug_tick;
889 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000891 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000892 {
893 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100894 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100895 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896 script_line_start();
897 }
898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#endif
901
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100902 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (next_cmdline == NULL)
904 {
905 /*
906 * Need to set msg_didout for the first line after an ":if",
907 * otherwise the ":if" will be overwritten.
908 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100909 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100911 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912#ifdef FEAT_EVAL
913 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
914#else
915 0
916#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100917 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
918 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100920 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100921 // returned for the end of a sourced file or executed function
922 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (KeyTyped && !(flags & DOCMD_REPEAT))
924 need_wait_return = FALSE;
925 retval = FAIL;
926 break;
927 }
928 used_getline = TRUE;
929
930 /*
931 * Keep the first typed line. Clear it when more lines are typed.
932 */
933 if (flags & DOCMD_KEEPLINE)
934 {
935 vim_free(repeat_cmdline);
936 if (count == 0)
937 repeat_cmdline = vim_strsave(next_cmdline);
938 else
939 repeat_cmdline = NULL;
940 }
941 }
942
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100943 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 else if (cmdline_copy == NULL)
945 {
946 next_cmdline = vim_strsave(next_cmdline);
947 if (next_cmdline == NULL)
948 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200949 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 retval = FAIL;
951 break;
952 }
953 }
954 cmdline_copy = next_cmdline;
955
956#ifdef FEAT_EVAL
957 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200958 * Inside a while/for loop, and when the command looks like a ":while"
959 * or ":for", the line is stored, because we may need it later when
960 * looping.
961 *
962 * When there is a '|' and another command, it is stored separately,
963 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000964 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200965 *
966 * Pass a different "fgetline" function to do_one_cmd() below,
967 * that it stores lines in or reads them from "lines_ga". Makes it
968 * possible to define a function inside a while/for loop and handles
969 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200971 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200973 cmd_getline = get_loop_line;
974 cmd_cookie = (void *)&cmd_loop_cookie;
975 cmd_loop_cookie.lines_gap = &lines_ga;
976 cmd_loop_cookie.current_line = current_line;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100977 cmd_loop_cookie.lc_getline = fgetline;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200978 cmd_loop_cookie.cookie = cookie;
979 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
980
981 // Save the current line when encountering it the first time.
982 if (current_line == lines_ga.ga_len
983 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 {
985 retval = FAIL;
986 break;
987 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200988 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200990 else
991 {
992 cmd_getline = fgetline;
993 cmd_cookie = cookie;
994 }
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 did_endif = FALSE;
997#endif
998
999 if (count++ == 0)
1000 {
1001 /*
1002 * All output from the commands is put below each other, without
1003 * waiting for a return. Don't do this when executing commands
1004 * from a script or when being called recursive (e.g. for ":e
1005 * +command file").
1006 */
1007 if (!(flags & DOCMD_NOWAIT) && !recursive)
1008 {
1009 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001010 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001012 msg_scroll = TRUE; // put messages below each other
1013 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001015 did_inc_RedrawingDisabled = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 }
1018
Bram Moolenaar823654b2020-05-29 23:03:09 +02001019 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001020 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
1022 /*
1023 * 2. Execute one '|' separated command.
1024 * do_one_cmd() will return NULL if there is no trailing '|'.
1025 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1026 */
1027 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001028 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#ifdef FEAT_EVAL
1030 &cstack,
1031#endif
1032 cmd_getline, cmd_cookie);
1033 --recursive;
1034
1035#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001037 // Use "current_line" from "cmd_loop_cookie", it may have been
1038 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040#endif
1041
1042 if (next_cmdline == NULL)
1043 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001044 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 /*
1047 * If the command was typed, remember it for the ':' register.
1048 * Do this AFTER executing the command to make :@: work.
1049 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001050 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 && new_last_cmdline != NULL)
1052 {
1053 vim_free(last_cmdline);
1054 last_cmdline = new_last_cmdline;
1055 new_last_cmdline = NULL;
1056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 }
1058 else
1059 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001060 // need to copy the command after the '|' to cmdline_copy, for the
1061 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001062 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 next_cmdline = cmdline_copy;
1064 }
1065
1066
1067#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001068 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001070 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001072 {
1073 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {
1079 ++current_line;
1080
1081 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 * An ":endwhile", ":endfor" and ":continue" is handled here.
1083 * If we were executing commands, jump back to the ":while" or
1084 * ":for".
1085 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001087 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001089 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001091 // Jump back to the matching ":while" or ":for". Be careful
1092 // not to use a cs_line[] from an entry that isn't a ":while"
1093 // or ":for": It would make "current_line" invalid and can
1094 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 if (!did_emsg && !got_int && !did_throw
1096 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 && (cstack.cs_flags[cstack.cs_idx]
1098 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 && cstack.cs_line[cstack.cs_idx] >= 0
1100 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1101 {
1102 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001103 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001105 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001107 // Check for the next breakpoint at or after the ":while"
1108 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001109 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
1111 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001112 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 fname,
1114 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1115 *dbg_tick = debug_tick;
1116 }
1117 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001120 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001122 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1123 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 }
1125 }
1126
1127 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001133 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 }
1136
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001137 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001138 if (breakpoint != NULL && has_watchexpr())
1139 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001140 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001141 *dbg_tick = debug_tick;
1142 }
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 /*
1145 * When not inside any ":while" loop, clear remembered lines.
1146 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 if (lines_ga.ga_len > 0)
1150 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001151 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1153 free_cmdlines(&lines_ga);
1154 }
1155 current_line = 0;
1156 }
1157
1158 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001159 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1160 * being restored at the ":endtry". Reset them here and set the
1161 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1162 * This includes the case where a missing ":endif", ":endwhile" or
1163 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 cstack.cs_lflags &= ~CSL_HAD_FINA;
1168 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1169 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 did_throw ? (void *)current_exception : NULL);
1171 did_emsg = got_int = did_throw = FALSE;
1172 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1173 }
1174
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001175 // Update global "trylevel" for recursive calls to do_cmdline() from
1176 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 trylevel = initial_trylevel + cstack.cs_trylevel;
1178
1179 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001180 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 * files) is aborted because of an error, an interrupt, or an uncaught
1182 * exception, cancel everything. If it is left normally, reset
1183 * force_abort to get the non-EH compatible abortion behavior for
1184 * the rest of the script.
1185 */
1186 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1187 force_abort = FALSE;
1188
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001189 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001191#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
1193 }
1194 /*
1195 * Continue executing command lines when:
1196 * - no CTRL-C typed, no aborting error, no exception thrown or try
1197 * conditionals need to be checked for executing finally clauses or
1198 * catching an interrupt exception
1199 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 * looping for ":source" command or function call.
1202 */
1203 while (!((got_int
1204#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001205 || (did_emsg && (force_abort || in_vim9script()))
1206 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#endif
1208 )
1209#ifdef FEAT_EVAL
1210 && cstack.cs_trylevel == 0
1211#endif
1212 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001213 && !(did_emsg
1214#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001215 // Keep going when inside try/catch, so that the error can be
1216 // deal with, except when it is a syntax error, it may cause
1217 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001218 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1219#endif
1220 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001221 && (getline_equal(fgetline, cookie, getexmodeline)
1222 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 && (next_cmdline != NULL
1224#ifdef FEAT_EVAL
1225 || cstack.cs_idx >= 0
1226#endif
1227 || (flags & DOCMD_REPEAT)));
1228
1229 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001230 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231#ifdef FEAT_EVAL
1232 free_cmdlines(&lines_ga);
1233 ga_clear(&lines_ga);
1234
1235 if (cstack.cs_idx >= 0)
1236 {
1237 /*
1238 * If a sourced file or executed function ran to its end, report the
1239 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001240 * In Vim9 script do not give a second error, executing aborts after
1241 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001243 if (!got_int && !did_throw && !aborting()
1244 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001245 && ((getline_equal(fgetline, cookie, getsourceline)
1246 && !source_finished(fgetline, cookie))
1247 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 && !func_has_ended(real_cookie))))
1249 {
1250 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001251 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001253 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001254 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001255 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001257 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 }
1259
1260 /*
1261 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1262 * ":endtry" in a sourced file or executed function. If the try
1263 * conditional is in its finally clause, ignore anything pending.
1264 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001265 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 */
1267 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001268 {
1269 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1270
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001271 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001272 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001273 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1274 &cstack.cs_looplevel);
1275 }
1276 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 trylevel = initial_trylevel;
1278 }
1279
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001280 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1281 // lack was reported above and the error message is to be converted to an
1282 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001283 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 ? (char_u *)"endfunction" : (char_u *)NULL);
1285
1286 if (trylevel == 0)
1287 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001288 // Just in case did_throw got set but current_exception wasn't.
1289 if (current_exception == NULL)
1290 did_throw = FALSE;
1291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 /*
1293 * When an exception is being thrown out of the outermost try
1294 * conditional, discard the uncaught exception, disable the conversion
1295 * of interrupts or errors to exceptions, and ensure that no more
1296 * commands are executed.
1297 */
1298 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001299 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300
1301 /*
1302 * On an interrupt or an aborting error not converted to an exception,
1303 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001304 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 * when force_abort is set and did_emsg unset in case of an interrupt
1306 * from a finally clause after an error.
1307 */
1308 else if (got_int || (did_emsg && force_abort))
1309 suppress_errthrow = TRUE;
1310 }
1311
1312 /*
1313 * The current cstack will be freed when do_cmdline() returns. An uncaught
1314 * exception will have to be rethrown in the previous cstack. If a function
1315 * has just returned or a script file was just finished and the previous
1316 * cstack belongs to the same function or, respectively, script file, it
1317 * will have to be checked for finally clauses to be executed due to the
1318 * ":return" or ":finish". This is done in do_one_cmd().
1319 */
1320 if (did_throw)
1321 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001324 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 && ex_nesting_level > func_level(real_cookie) + 1))
1326 {
1327 if (!did_throw)
1328 check_cstack = TRUE;
1329 }
1330 else
1331 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001332 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 --ex_nesting_level;
1335 /*
1336 * Go to debug mode when returning from a function in which we are
1337 * single-stepping.
1338 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 if ((getline_equal(fgetline, cookie, getsourceline)
1340 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001342 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 ? (char_u *)_("End of sourced file")
1344 : (char_u *)_("End of function"));
1345 }
1346
1347 /*
1348 * Restore the exception environment (done after returning from the
1349 * debugger).
1350 */
1351 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001352 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
1354 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001355
1356 // Cleanup if "cs_emsg_silent_list" remains.
1357 if (cstack.cs_emsg_silent_list != NULL)
1358 {
1359 eslist_T *elem, *temp;
1360
1361 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1362 {
1363 temp = elem->next;
1364 vim_free(elem);
1365 }
1366 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001367#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 /*
1370 * If there was too much output to fit on the command line, ask the user to
1371 * hit return before redrawing the screen. With the ":global" command we do
1372 * this only once after the command is finished.
1373 */
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001374 if (did_inc_RedrawingDisabled)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001376 if (RedrawingDisabled > 0)
1377 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 --no_wait_return;
1379 msg_scroll = FALSE;
1380
1381 /*
1382 * When just finished an ":if"-":else" which was typed, no need to
1383 * wait for hit-return. Also for an error situation.
1384 */
1385 if (retval == FAIL
1386#ifdef FEAT_EVAL
1387 || (did_endif && KeyTyped && !did_emsg)
1388#endif
1389 )
1390 {
1391 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001392 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394 else if (need_wait_return)
1395 {
1396 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001397 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 * here should not overwrite the command that may be shown before
1399 * doing that.
1400 */
1401 msg_didout |= msg_didout_before_start;
1402 wait_return(FALSE);
1403 }
1404 }
1405
Bram Moolenaar2a942252012-11-28 23:03:07 +01001406#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001407 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001408#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /*
1410 * Reset if_level, in case a sourced script file contains more ":if" than
1411 * ":endif" (could be ":if x | foo | endif").
1412 */
1413 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001414#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 --call_depth;
1417 return retval;
1418}
1419
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001420#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001421/*
1422 * Handle when "did_throw" is set after executing commands.
1423 */
1424 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001425handle_did_throw(void)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001426{
1427 char *p = NULL;
1428 msglist_T *messages = NULL;
ichizok7e5fe382023-04-15 13:17:50 +01001429 ESTACK_CHECK_DECLARATION;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001430
1431 /*
1432 * If the uncaught exception is a user exception, report it as an
1433 * error. If it is an error exception, display the saved error
1434 * message now. For an interrupt exception, do nothing; the
1435 * interrupt message is given elsewhere.
1436 */
1437 switch (current_exception->type)
1438 {
1439 case ET_USER:
1440 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001441 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001442 current_exception->value);
1443 p = (char *)vim_strsave(IObuff);
1444 break;
1445 case ET_ERROR:
1446 messages = current_exception->messages;
1447 current_exception->messages = NULL;
1448 break;
1449 case ET_INTERRUPT:
1450 break;
1451 }
1452
1453 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1454 current_exception->throw_lnum);
ichizok7e5fe382023-04-15 13:17:50 +01001455 ESTACK_CHECK_SETUP;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001456 current_exception->throw_name = NULL;
1457
1458 discard_current_exception(); // uses IObuff if 'verbose'
LemonBoy749ba0f2024-07-04 19:23:16 +02001459
1460 // If "silent!" is active the uncaught exception is not fatal.
1461 if (emsg_silent == 0)
1462 {
1463 suppress_errthrow = TRUE;
1464 force_abort = TRUE;
1465 }
Bram Moolenaar620c9592021-07-31 21:32:31 +02001466
1467 if (messages != NULL)
1468 {
1469 do
1470 {
1471 msglist_T *next = messages->next;
1472 int save_compiling = estack_compiling;
1473
1474 estack_compiling = messages->msg_compiling;
1475 emsg(messages->msg);
1476 vim_free(messages->msg);
1477 vim_free(messages->sfile);
1478 vim_free(messages);
1479 messages = next;
1480 estack_compiling = save_compiling;
1481 }
1482 while (messages != NULL);
1483 }
1484 else if (p != NULL)
1485 {
1486 emsg(p);
1487 vim_free(p);
1488 }
1489 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01001490 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001491 estack_pop();
1492}
1493
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001495 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 */
1497 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001498get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001500 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 wcmd_T *wp;
1502 char_u *line;
1503
1504 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1505 {
1506 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001507 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001509 // First time inside the ":while"/":for": get line normally.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001510 if (cp->lc_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001511 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001513 line = cp->lc_getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001514 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 ++cp->current_line;
1516
1517 return line;
1518 }
1519
1520 KeyTyped = FALSE;
1521 ++cp->current_line;
1522 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001523 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 return vim_strsave(wp->line);
1525}
1526
1527/*
1528 * Store a line in "gap" so that a ":while" loop can execute it again.
1529 */
1530 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001531store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 if (ga_grow(gap, 1) == FAIL)
1534 return FAIL;
1535 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001536 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 return OK;
1539}
1540
1541/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001542 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 */
1544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001545free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
1547 while (gap->ga_len > 0)
1548 {
1549 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1550 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 }
1552}
1553#endif
1554
1555/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001556 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1557 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001560getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001561 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001562 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001563 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001566 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001569 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1570 // function that's originally used to obtain the lines. This may be
1571 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001572 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001573 cp = (struct loop_cookie *)cookie;
1574 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001576 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 cp = cp->cookie;
1578 }
1579 return gp == func;
1580#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001581 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582#endif
1583}
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001586 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 * getline function. Otherwise return "cookie".
1588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001590getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001591 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001592 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593{
Bram Moolenaar13505972019-01-24 15:04:48 +01001594#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001595 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001596 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001598 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1599 // cookie that's originally used to obtain the lines. This may be nested
1600 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001601 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001602 cp = (struct loop_cookie *)cookie;
1603 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001605 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 cp = cp->cookie;
1607 }
1608 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001609#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001614#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001615/*
1616 * Get the next line source line without advancing.
1617 */
1618 char_u *
1619getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001620 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001621 void *cookie) // argument for fgetline()
1622{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001623 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001624 struct loop_cookie *cp;
1625 wcmd_T *wp;
1626
1627 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1628 // cookie that's originally used to obtain the lines. This may be nested
1629 // several levels.
1630 gp = fgetline;
1631 cp = (struct loop_cookie *)cookie;
1632 while (gp == get_loop_line)
1633 {
1634 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1635 {
1636 // executing lines a second time, use the stored copy
1637 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1638 return wp->line;
1639 }
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001640 gp = cp->lc_getline;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001641 cp = cp->cookie;
1642 }
1643 if (gp == getsourceline)
1644 return source_nextline(cp);
1645 return NULL;
1646}
1647#endif
1648
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001649
1650/*
1651 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1652 * ":bwipeout", etc.
1653 * Returns the buffer number.
1654 */
1655 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001656compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001657{
1658 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001659 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001660 int count = offset;
1661
1662 buf = firstbuf;
1663 while (buf->b_next != NULL && buf->b_fnum < lnum)
1664 buf = buf->b_next;
1665 while (count != 0)
1666 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001667 count += (offset < 0) ? 1 : -1;
1668 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1669 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001670 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001671 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001672 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001673 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001674 while (buf->b_ml.ml_mfp == NULL)
1675 {
1676 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1677 if (nextbuf == NULL)
1678 break;
1679 buf = nextbuf;
1680 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001681 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001682 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001683 if (addr_type == ADDR_LOADED_BUFFERS)
1684 while (buf->b_ml.ml_mfp == NULL)
1685 {
1686 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1687 if (nextbuf == NULL)
1688 break;
1689 buf = nextbuf;
1690 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001691 return buf->b_fnum;
1692}
1693
Bram Moolenaarb7316892019-05-01 18:08:42 +02001694/*
1695 * Return the window number of "win".
1696 * When "win" is NULL return the number of windows.
1697 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001698 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001699current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001700{
1701 win_T *wp;
1702 int nr = 0;
1703
Bram Moolenaar29323592016-07-24 22:04:11 +02001704 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001705 {
1706 ++nr;
1707 if (wp == win)
1708 break;
1709 }
1710 return nr;
1711}
1712
1713 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001714current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001715{
1716 tabpage_T *tp;
1717 int nr = 0;
1718
Bram Moolenaar29323592016-07-24 22:04:11 +02001719 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001720 {
1721 ++nr;
1722 if (tp == tab)
1723 break;
1724 }
1725 return nr;
1726}
1727
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001728 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001729comment_start(char_u *p, int starts_with_colon UNUSED)
1730{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001731 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001732 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001733 return *p == '"';
1734}
1735
Bram Moolenaarf240e182014-11-27 18:33:02 +01001736# define CURRENT_WIN_NR current_win_nr(curwin)
1737# define LAST_WIN_NR current_win_nr(NULL)
1738# define CURRENT_TAB_NR current_tab_nr(curtab)
1739# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741/*
1742 * Execute one Ex command.
1743 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001744 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1745 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 *
1747 * 1. skip comment lines and leading space
1748 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001749 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001750 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001751 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001752 * 6. parse arguments
1753 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001755 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 *
1757 * This function may be called recursively!
1758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001760do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001761 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001762 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001764 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001766 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001767 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001769 char_u *p;
1770 linenr_T lnum;
1771 long n;
1772 char *errormsg = NULL; // error message
1773 char_u *after_modifier = NULL;
1774 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001775 cmdmod_T save_cmdmod;
1776 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001777 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001778 int ni; // set when Not Implemented
1779 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001780 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001781 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001782#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001783 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001784#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001785 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001786 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
Bram Moolenaara80faa82020-04-12 19:37:17 +02001788 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 ea.line1 = 1;
1790 ea.line2 = 1;
1791#ifdef FEAT_EVAL
1792 ++ex_nesting_level;
1793#endif
1794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001795 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (quitmore
1797#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001798 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001799 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001800#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001801 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001802 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 --quitmore;
1804
1805 /*
1806 * Reset browse, confirm, etc.. They are restored when returning, for
1807 * recursive calls.
1808 */
1809 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001811 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001812 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1813 goto doend;
1814
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001815/*
1816 * 1. Skip comment lines and leading white space and colons.
1817 * 2. Handle command modifiers.
1818 */
1819 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001821 ea.cmdlinep = cmdlinep;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001822 ea.ea_getline = fgetline;
Bram Moolenaareffed932018-08-14 13:38:17 +02001823 ea.cookie = cookie;
1824#ifdef FEAT_EVAL
1825 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001826 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001828 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001829 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001830 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001831 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
1833#ifdef FEAT_EVAL
1834 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1835 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1836#else
1837 ea.skip = (if_level > 0);
1838#endif
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001841 * 3. Skip over the range to find the command. Let "p" point to after it.
1842 *
1843 * We need the command to know what kind of range it uses.
1844 */
1845 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001846
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001847 // In Vim9 script a colon is required before the range. This may also be
1848 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001849 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001850 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001851 {
1852 may_have_range = FALSE;
1853 for (p = ea.cmd; p >= *cmdlinep; --p)
1854 {
1855 if (*p == ':')
1856 may_have_range = TRUE;
1857 if (p < ea.cmd && !VIM_ISWHITE(*p))
1858 break;
1859 }
1860 }
1861 else
1862 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001863 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001864 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001866#ifdef FEAT_EVAL
1867 // Handle ":export" - it functions almost like a command modifier.
1868 // ":export var Name: type"
1869 // ":export def Name(..."
1870 // etc.
1871 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1872 is_export = TRUE;
1873#endif
1874
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001875 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001876 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001877 if (ea.cmd == cmd + 1 && *cmd == '$')
1878 // should be "$VAR = val"
1879 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001880#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001881 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001882#else
1883 p = find_ex_command(&ea, NULL, NULL, NULL);
1884#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001885 if (ea.cmdidx == CMD_SIZE)
1886 {
1887 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1888
1889 // If a ':' before the range is missing, give a clearer error
1890 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001891 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001892 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001893 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001894 goto doend;
1895 }
1896 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001900
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001901#ifdef FEAT_EVAL
1902# ifdef FEAT_PROFILE
1903 // Count this line for profiling if skip is TRUE.
1904 if (do_profiling == PROF_YES
1905 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1906 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1907 {
1908 int skip = did_emsg || got_int || did_throw;
1909
1910 if (ea.cmdidx == CMD_catch)
1911 skip = !skip && !(cstack->cs_idx >= 0
1912 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1913 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1914 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1915 skip = skip || !(cstack->cs_idx >= 0
1916 && !(cstack->cs_flags[cstack->cs_idx]
1917 & (CSF_ACTIVE | CSF_TRUE)));
1918 else if (ea.cmdidx == CMD_finally)
1919 skip = FALSE;
1920 else if (ea.cmdidx != CMD_endif
1921 && ea.cmdidx != CMD_endfor
1922 && ea.cmdidx != CMD_endtry
1923 && ea.cmdidx != CMD_endwhile)
1924 skip = ea.skip;
1925
1926 if (!skip)
1927 {
1928 if (getline_equal(fgetline, cookie, get_func_line))
1929 func_line_exec(getline_cookie(fgetline, cookie));
1930 else if (getline_equal(fgetline, cookie, getsourceline))
1931 script_line_exec();
1932 }
1933 }
1934# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001935#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001936
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001937 ea.cmd = cmd;
1938
1939#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001940 // May go to debug mode. If this happens and the ">quit" debug command is
1941 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001942 dbg_check_breakpoint(&ea);
1943 if (!ea.skip && got_int)
1944 {
1945 ea.skip = TRUE;
1946 (void)do_intthrow(cstack);
1947 }
1948#endif
1949
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001950/*
1951 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 *
1953 * where 'addr' is:
1954 *
1955 * % (entire file)
1956 * $ [+-NUM]
1957 * 'x [+-NUM] (where x denotes a currently defined mark)
1958 * . [+-NUM]
1959 * [+-NUM]..
1960 * NUM
1961 *
1962 * The ea.cmd pointer is updated to point to the first character following the
1963 * range spec. If an initial address is found, but no second, the upper bound
1964 * is equal to the lower.
1965 */
1966
Bram Moolenaar25190db2019-05-04 15:05:28 +02001967 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001968 if (!IS_USER_CMDIDX(ea.cmdidx))
1969 {
1970 if (ea.cmdidx != CMD_SIZE)
1971 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1972 else
1973 ea.addr_type = ADDR_LINES;
1974
Bram Moolenaar25190db2019-05-04 15:05:28 +02001975 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001976 if (ea.cmdidx == CMD_wincmd && p != NULL)
1977 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001978#ifdef FEAT_QUICKFIX
1979 // :.cc in quickfix window uses line number
1980 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1981 ea.addr_type = ADDR_OTHER;
1982#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001983 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001984
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001985 if (!may_have_range)
1986 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001987 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1988 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001991 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 */
1993
1994 /*
1995 * Skip ':' and any white space
1996 */
1997 ea.cmd = skipwhite(ea.cmd);
1998 while (*ea.cmd == ':')
1999 ea.cmd = skipwhite(ea.cmd + 1);
2000
2001 /*
2002 * If we got a line, but no command, then go to the line.
2003 * If we find a '|' or '\n' we set ea.nextcmd.
2004 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002005 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
2006 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 /*
2009 * strange vi behaviour:
2010 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01002011 * ":3|..." prints line 3 (not in Vim9 script)
2012 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002014 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002016 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 goto doend;
2018 }
2019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 // If this looks like an undefined user command and there are CmdUndefined
2021 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002022 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2023 && ASCII_ISUPPER(*ea.cmd)
2024 && has_cmdundefined())
2025 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002026 int ret;
2027
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002028 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002029 while (ASCII_ISALNUM(*p))
2030 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002031 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002032 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2033 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // If the autocommands did something and didn't cause an error, try
2035 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002036 p = (ret
2037#ifdef FEAT_EVAL
2038 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002039#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002041 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (p == NULL)
2044 {
2045 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002046 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 goto doend;
2048 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002049 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002050 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2051 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
2053 errormsg = uc_fun_cmd();
2054 goto doend;
2055 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (ea.cmdidx == CMD_SIZE)
2058 {
2059 if (!ea.skip)
2060 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002061 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002063 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002064 // If the modifier was parsed OK the error must be in the
2065 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002066 if (after_modifier != NULL)
2067 append_command(after_modifier);
2068 else
2069 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002070 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002071 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002072 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002073 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075 goto doend;
2076 }
2077
Bram Moolenaar958636c2014-10-21 20:01:58 +02002078 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2079 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002080#ifdef HAVE_EX_SCRIPT_NI
2081 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2082#endif
2083 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
2085#ifndef FEAT_EVAL
2086 /*
2087 * When the expression evaluation is disabled, recognize the ":if" and
2088 * ":endif" commands and ignore everything in between it.
2089 */
2090 if (ea.cmdidx == CMD_if)
2091 ++if_level;
2092 if (if_level)
2093 {
2094 if (ea.cmdidx == CMD_endif)
2095 --if_level;
2096 goto doend;
2097 }
2098
2099#endif
2100
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002101 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002102 if (*p == '!' && ea.cmdidx != CMD_substitute
2103 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
2105 ++p;
2106 ea.forceit = TRUE;
2107 }
2108 else
2109 ea.forceit = FALSE;
2110
2111/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002112 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002114 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002115 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117 if (!ea.skip)
2118 {
2119#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002120 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002122 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002123 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 goto doend;
2125 }
2126#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002127 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002128 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002129 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002130 goto doend;
2131 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002132 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002134 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002135 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 goto doend;
2137 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002138
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002139 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002141 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2142 {
2143 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002144 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002145 goto doend;
2146 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002147 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2148 {
2149 // Command not allowed when text is locked
2150 errormsg = _(get_text_locked_msg());
2151 goto doend;
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002155 // Disallow editing another buffer when "curbuf_lock" is set.
2156 // Do allow ":checktime" (it is postponed).
2157 // Do allow ":edit" (check for an argument later).
2158 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002159 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002160 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002161 && ea.cmdidx != CMD_edit
2162 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002163 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002164 && curbuf_locked())
2165 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002167 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002169 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 goto doend;
2171 }
2172 }
2173
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002174 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002176 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 goto doend;
2178 }
2179
2180 /*
2181 * Don't complain about the range if it is not used
2182 * (could happen if line_count is accidentally set to 0).
2183 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002184 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
2186 /*
2187 * If the range is backwards, ask for confirmation and, if given, swap
2188 * ea.line1 & ea.line2 so it's forwards again.
2189 * When global command is busy, don't ask, will fail below.
2190 */
2191 if (!global_busy && ea.line1 > ea.line2)
2192 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002193 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002195 if (sourcing || exmode_active)
2196 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002197 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002198 goto doend;
2199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 if (ask_yesno((char_u *)
2201 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002202 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204 lnum = ea.line1;
2205 ea.line1 = ea.line2;
2206 ea.line2 = lnum;
2207 }
2208 if ((errormsg = invalid_range(&ea)) != NULL)
2209 goto doend;
2210 }
2211
Bram Moolenaarb7316892019-05-01 18:08:42 +02002212 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2213 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 ea.line2 = 1;
2215
2216 correct_range(&ea);
2217
2218#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002219 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002220 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002222 // Put the first line at the start of a closed fold, put the last line
2223 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 (void)hasFolding(ea.line1, &ea.line1, NULL);
2225 (void)hasFolding(ea.line2, NULL, &ea.line2);
2226 }
2227#endif
2228
2229#ifdef FEAT_QUICKFIX
2230 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002231 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 * option here, so things like % get expanded.
2233 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002234 p = replace_makeprg(&ea, p, cmdlinep);
2235 if (p == NULL)
2236 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#endif
2238
2239 /*
2240 * Skip to start of argument.
2241 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2242 */
2243 if (ea.cmdidx == CMD_bang)
2244 ea.arg = p;
2245 else
2246 ea.arg = skipwhite(p);
2247
Bram Moolenaar379fb762018-08-30 15:58:28 +02002248 // ":file" cannot be run with an argument when "curbuf_lock" is set
2249 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2250 goto doend;
2251
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 /*
2253 * Check for "++opt=val" argument.
2254 * Must be first, allow ":w ++enc=utf8 !cmd"
2255 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002256 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2258 if (getargopt(&ea) == FAIL && !ni)
2259 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002260 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 goto doend;
2262 }
2263
2264 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2265 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002266 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002268 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002270 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 goto doend;
2272 }
2273 ea.arg = skipwhite(ea.arg + 1);
2274 ea.append = TRUE;
2275 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002276 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
2278 ++ea.arg;
2279 ea.usefilter = TRUE;
2280 }
2281 }
2282
2283 if (ea.cmdidx == CMD_read)
2284 {
2285 if (ea.forceit)
2286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002287 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 ea.forceit = FALSE;
2289 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002290 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 ++ea.arg;
2293 ea.usefilter = TRUE;
2294 }
2295 }
2296
2297 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2298 {
2299 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002300 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 ++ea.arg;
2303 ++ea.amount;
2304 }
2305 ea.arg = skipwhite(ea.arg);
2306 }
2307
2308 /*
2309 * Check for "+command" argument, before checking for next command.
2310 * Don't do this for ":read !cmd" and ":write !cmd".
2311 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002312 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2314
2315 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002316 * For commands that do not use '|' inside their argument: Check for '|' to
2317 * separate commands and '"' or '#' to start comments.
2318 *
2319 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002320 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002321 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002322 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002324 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2325 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002326 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002327 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002328 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002329 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002330 || ea.cmdidx == CMD_global
2331 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002332 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002333#ifdef FEAT_EVAL
2334 || inside_block(&ea)
2335#endif
2336 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
2338 for (p = ea.arg; *p; ++p)
2339 {
Mohamed Akramf3daa452024-07-06 17:12:09 +02002340 // Remove one backslash before a newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002342 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002343 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 ea.nextcmd = p + 1;
2346 *p = NUL;
2347 break;
2348 }
2349 }
2350 }
2351
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002352 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002353 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002355 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002356 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002358 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002359 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002362#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002363 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002364 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002366 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002367 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002370 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2371 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002372 {
2373 ea.regname = *ea.arg++;
2374#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002375 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002376 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2377 {
kuuote08d7b1c2021-10-04 22:17:36 +01002378 if (!ea.skip)
2379 {
2380 set_expr_line(vim_strsave(ea.arg), &ea);
2381 did_set_expr_line = TRUE;
2382 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002383 ea.arg += STRLEN(ea.arg);
2384 }
2385#endif
2386 ea.arg = skipwhite(ea.arg);
2387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
2389
2390 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002391 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 * count, it's a buffer name.
2393 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002394 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002395 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002396 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002398 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002400 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002402 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 goto doend;
2404 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002405 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
2407 ea.line2 = n;
2408 if (ea.addr_count == 0)
2409 ea.addr_count = 1;
2410 }
2411 else
2412 {
2413 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002414 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002415 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002416 else
2417 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 ++ea.addr_count;
2419 /*
2420 * Be vi compatible: no error message for out of range.
2421 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002422 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 ea.line2 = curbuf->b_ml.ml_line_count;
2424 }
2425 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002426
2427 /*
2428 * Check for flags: 'l', 'p' and '#'.
2429 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002430 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002431 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002432 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2433 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002435 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002436 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 goto doend;
2438 }
2439
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002440 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002442 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 goto doend;
2444 }
2445
2446#ifdef FEAT_EVAL
2447 /*
2448 * Skip the command when it's not going to be executed.
2449 * The commands like :if, :endif, etc. always need to be executed.
2450 * Also make an exception for commands that handle a trailing command
2451 * themselves.
2452 */
2453 if (ea.skip)
2454 {
2455 switch (ea.cmdidx)
2456 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002457 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_while:
2459 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002460 case CMD_for:
2461 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_if:
2463 case CMD_elseif:
2464 case CMD_else:
2465 case CMD_endif:
2466 case CMD_try:
2467 case CMD_catch:
2468 case CMD_finally:
2469 case CMD_endtry:
2470 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002471 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 break;
2473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002474 // Commands that handle '|' themselves. Check: A command should
2475 // either have the EX_TRLBAR flag, appear in this list or appear in
2476 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 case CMD_aboveleft:
2478 case CMD_and:
2479 case CMD_belowright:
2480 case CMD_botright:
2481 case CMD_browse:
2482 case CMD_call:
2483 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002484 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 case CMD_delfunction:
2486 case CMD_djump:
2487 case CMD_dlist:
2488 case CMD_dsearch:
2489 case CMD_dsplit:
2490 case CMD_echo:
2491 case CMD_echoerr:
2492 case CMD_echomsg:
2493 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002494 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002496 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002497 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 case CMD_help:
2499 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002500 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 case CMD_ijump:
2502 case CMD_ilist:
2503 case CMD_isearch:
2504 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002505 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 case CMD_keepjumps:
2507 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002508 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_leftabove:
2510 case CMD_let:
2511 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002512 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002513 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002515 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002516 case CMD_noautocmd:
2517 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 case CMD_perl:
2519 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002520 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002521 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002522 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 case CMD_return:
2524 case CMD_rightbelow:
2525 case CMD_ruby:
2526 case CMD_silent:
2527 case CMD_smagic:
2528 case CMD_snomagic:
2529 case CMD_substitute:
2530 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002531 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 case CMD_tcl:
2533 case CMD_throw:
2534 case CMD_tilde:
2535 case CMD_topleft:
2536 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002537 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002538 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 case CMD_verbose:
2540 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002541 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 break;
2543
2544 default: goto doend;
2545 }
2546 }
2547#endif
2548
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002549 if ((ea.argt & EX_XFILE)
2550 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2551 goto doend;
2552
2553#ifdef FEAT_EVAL
2554 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002556 emsg(_(e_invalid_command_after_export));
2557 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 /*
2562 * Accept buffer name. Cannot be used at the same time with a buffer
2563 * number. Don't do this for a user command.
2564 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002565 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002566 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
2568 /*
2569 * :bdelete, :bwipeout and :bunload take several arguments, separated
2570 * by spaces: find next space (skipping over escaped characters).
2571 * The others take one argument: ignore trailing spaces.
2572 */
2573 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2574 || ea.cmdidx == CMD_bunload)
2575 p = skiptowhite_esc(ea.arg);
2576 else
2577 {
2578 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002579 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 --p;
2581 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002582 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002583 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002584 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 goto doend;
2586 ea.addr_count = 1;
2587 ea.arg = skipwhite(p);
2588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002590 // The :try command saves the emsg_silent flag, reset it here when
2591 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002592 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002593 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002594 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002595 if (emsg_silent < 0)
2596 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002597 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002598 }
2599
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002601 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
Bram Moolenaar958636c2014-10-21 20:01:58 +02002604 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
2606 /*
2607 * Execute a user-defined command.
2608 */
2609 do_ucmd(&ea);
2610 }
2611 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
2613 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002614 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2617 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002618 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620
2621#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002622 // A command will reset "is_export" when exporting an item. If it is still
LemonBoy90c27b22023-08-27 19:28:15 +02002623 // set something went wrong or the command was never executed.
2624 if (!ea.skip && is_export)
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002625 {
2626 if (errormsg == NULL)
2627 errormsg = _(e_export_with_invalid_argument);
2628 is_export = FALSE;
2629 }
2630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002632 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002633 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002634 && current_sctx.sc_sid > 0
2635 && ea.cmdidx != CMD_endif
2636 && (cstack->cs_idx < 0
2637 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002638 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 /*
2641 * If the command just executed called do_cmdline(), any throw or ":return"
2642 * or ":finish" encountered there must also check the cstack of the still
2643 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2644 * exception, or reanimate a returned function or finished script file and
2645 * return or finish it again.
2646 */
2647 if (need_rethrow)
2648 do_throw(cstack);
2649 else if (check_cstack)
2650 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002651 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002653 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 && current_func_returned())
2655 do_return(&ea, TRUE, FALSE, NULL);
2656 }
2657 need_rethrow = check_cstack = FALSE;
2658#endif
2659
2660doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002661 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002664 curwin->w_cursor.col = 0;
2665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
2667 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2668 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002669 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002671 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
2673 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002674 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002676 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678 emsg(errormsg);
2679 }
2680#ifdef FEAT_EVAL
2681 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002682 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2683 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002684
2685 if (did_set_expr_line)
2686 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002687 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688#endif
2689
Bram Moolenaare1004402020-10-24 20:49:43 +02002690 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002692 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002693 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002695 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 ea.nextcmd = NULL;
2697
2698#ifdef FEAT_EVAL
2699 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002700 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701#endif
2702
2703 return ea.nextcmd;
2704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002706static char ex_error_buf[MSG_BUF_LEN];
2707
2708/*
2709 * Return an error message with argument included.
2710 * Uses a static buffer, only the last error will be kept.
2711 * "msg" will be translated, caller should use N_().
2712 */
2713 char *
2714ex_errmsg(char *msg, char_u *arg)
2715{
2716 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2717 return ex_error_buf;
2718}
2719
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720/*
zeertzjq7d664bf2024-07-14 10:22:54 +02002721 * The "+" string used in place of an empty command in Ex mode.
2722 * This string is used in pointer comparison.
2723 */
2724static char exmode_plus[] = "+";
2725
2726/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002727 * Handle a range without a command.
2728 * Returns an error message on failure.
2729 */
2730 char *
2731ex_range_without_command(exarg_T *eap)
2732{
2733 char *errormsg = NULL;
2734
zeertzjq7d664bf2024-07-14 10:22:54 +02002735 if ((*eap->cmd == '|' ||
2736 (exmode_active && eap->cmd != (char_u *)exmode_plus + 1))
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002737#ifdef FEAT_EVAL
2738 && !in_vim9script()
2739#endif
2740 )
2741 {
2742 eap->cmdidx = CMD_print;
2743 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2744 if ((errormsg = invalid_range(eap)) == NULL)
2745 {
2746 correct_range(eap);
2747 ex_print(eap);
2748 }
2749 }
2750 else if (eap->addr_count != 0)
2751 {
2752 if (eap->line2 > curbuf->b_ml.ml_line_count)
2753 {
2754 // With '-' in 'cpoptions' a line number past the file is an
2755 // error, otherwise put it at the end of the file.
2756 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2757 eap->line2 = -1;
2758 else
2759 eap->line2 = curbuf->b_ml.ml_line_count;
2760 }
2761
2762 if (eap->line2 < 0)
2763 errormsg = _(e_invalid_range);
2764 else
2765 {
2766 if (eap->line2 == 0)
2767 curwin->w_cursor.lnum = 1;
2768 else
2769 curwin->w_cursor.lnum = eap->line2;
2770 beginline(BL_SOL | BL_FIX);
2771 }
2772 }
2773 return errormsg;
2774}
2775
2776/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002777 * Check for an Ex command with optional tail.
2778 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002779 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002780 */
2781 static int
2782checkforcmd_opt(
2783 char_u **pp, // start of command
2784 char *cmd, // name of command
2785 int len, // required length
2786 int noparen)
2787{
2788 int i;
2789
2790 for (i = 0; cmd[i] != NUL; ++i)
2791 if (((char_u *)cmd)[i] != (*pp)[i])
2792 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002793 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002794 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002795 {
2796 *pp = skipwhite(*pp + i);
2797 return TRUE;
2798 }
2799 return FALSE;
2800}
2801
2802/*
2803 * Check for an Ex command with optional tail.
2804 * If there is a match advance "pp" to the argument and return TRUE.
2805 */
2806 int
2807checkforcmd(
2808 char_u **pp, // start of command
2809 char *cmd, // name of command
2810 int len) // required length
2811{
2812 return checkforcmd_opt(pp, cmd, len, FALSE);
2813}
2814
2815/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002816 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002817 * If there is a match advance "pp" to the argument and return TRUE.
2818 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002819 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002820checkforcmd_noparen(
2821 char_u **pp, // start of command
2822 char *cmd, // name of command
2823 int len) // required length
2824{
2825 return checkforcmd_opt(pp, cmd, len, TRUE);
2826}
2827
2828/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002829 * Parse and skip over command modifiers:
2830 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002831 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002833 * When "skip_only" is TRUE the global variables are not changed, except for
2834 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002835 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2836 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002837 * Call apply_cmdmod() to get the side effects of the modifiers:
2838 * - Increment "sandbox" for ":sandbox"
2839 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002840 * - set msg_silent for ":silent"
2841 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 * Return FAIL when the command is not to be executed.
2843 * May set "errormsg" to an error message.
2844 */
2845 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002846parse_command_modifiers(
2847 exarg_T *eap,
2848 char **errormsg,
2849 cmdmod_T *cmod,
2850 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002851{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002852 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002853 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002854 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002855 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002856 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002857 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002858
Bram Moolenaare1004402020-10-24 20:49:43 +02002859 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002860 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002861
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002862 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2863 {
2864 // The automatically inserted Visual area range is skipped, so that
2865 // typing ":cmdmod cmd" in Visual mode works without having to move the
zeertzjqd9be94c2024-07-14 10:20:20 +02002866 // range to after the modifiers. The command will be "'<,'>cmdmod cmd",
2867 // parse "cmdmod cmd" and then put back "'<,'>" before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002868 eap->cmd += 5;
2869 cmd_start = eap->cmd;
2870 has_visual_range = TRUE;
2871 }
2872
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002873 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002874 for (;;)
2875 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002876 char_u *p;
2877
Bram Moolenaareffed932018-08-14 13:38:17 +02002878 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002879 {
2880 if (*eap->cmd == ':')
2881 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002883 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002884
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002885 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002887 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2888 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2890 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002891 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002892 if (!skip_only)
2893 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002894 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002895 }
2896
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002897 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002898 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002899 {
2900 // a comment ends at a NL
zeertzjqf7b86092024-09-05 17:26:30 +02002901 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2902 if (eap->nextcmd != NULL)
2903 ++eap->nextcmd;
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002904 if (vim9script)
2905 {
2906 if (has_cmdmod(cmod, FALSE))
2907 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002908#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002909 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2910 && eap->cmd[2] != '{')
2911 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002912#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002913 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002915 }
zeertzjqf7b86092024-09-05 17:26:30 +02002916 if (*eap->cmd == '\n')
zeertzjq2432b4a2024-09-03 22:58:30 +02002917 {
2918 eap->nextcmd = eap->cmd + 1;
2919 return FAIL;
2920 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 if (*eap->cmd == NUL)
2922 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002923 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002924 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002925 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002926 if (vim9script && has_cmdmod(cmod, FALSE))
2927 *errormsg = _(e_command_modifier_without_command);
2928 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002929 return FAIL;
2930 }
2931
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002932 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002933
2934 // In Vim9 script a variable can shadow a command modifier:
2935 // verbose = 123
2936 // verbose += 123
2937 // silent! verbose = func()
2938 // verbose.member = 2
2939 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002940 // But not:
2941 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002942 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002943 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002944 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002945
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002946 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002947 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002948 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002949 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2950 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002951 break;
2952 }
2953
Bram Moolenaareffed932018-08-14 13:38:17 +02002954 switch (*p)
2955 {
John Marriotted908f72024-04-18 22:46:56 +02002956 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002957 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002959 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002960 continue;
2961
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002962 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002964 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002965 continue;
2966 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002967 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002968 {
2969#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002970 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002971#endif
2972 continue;
2973 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002974 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002975 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002976 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002977 continue;
2978
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002979 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002980 break;
2981#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002982 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002983#endif
2984 continue;
2985
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002986 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 continue;
2990 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002991 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002992 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002993 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 continue;
2995 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002996 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002998 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 continue;
3000 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003001 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003003 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 continue;
3005
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003006 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003008 char_u *reg_pat;
3009 char_u *nulp = NULL;
3010 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003011
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003012 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003013 || *p == NUL
3014 || (ends_excmd(*p)
3015#ifdef FEAT_EVAL
3016 // in ":filter #pat# cmd" # does not
3017 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003018 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003019#endif
3020 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 break;
3022 if (*p == '!')
3023 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003024 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003025 p = skipwhite(p + 1);
3026 if (*p == NUL || ends_excmd(*p))
3027 break;
3028 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003029#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003030 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003031 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003032 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003033#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003034 if (skip_only)
3035 p = skip_vimgrep_pat(p, NULL, NULL);
3036 else
3037 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003038 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3039 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003040 if (p == NULL || *p == NUL)
3041 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003042 if (!skip_only)
3043 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003044 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003045 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003046 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003047 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003048 // restore the character overwritten by NUL
3049 if (nulp != NULL)
3050 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003051 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003052 eap->cmd = p;
3053 continue;
3054 }
3055
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003056 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3057 {
3058 cmod->cmod_split |= WSP_HOR;
3059 continue;
3060 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003061 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003062 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003063 || *p == NUL || ends_excmd(*p))
3064 break;
3065 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003066 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003067 continue;
3068
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003069 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003070 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003071 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003072 continue;
3073 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003074 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3075 {
3076 if (ends_excmd2(p, eap->cmd))
3077 {
3078 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003079 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003080 return FAIL;
3081 }
3082 cmod->cmod_flags |= CMOD_LEGACY;
3083 continue;
3084 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003085
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003086 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003087 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003088 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003089 continue;
3090
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003091 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003092 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003093 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003094 continue;
3095 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003096 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003097 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003098 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003099 continue;
3100
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003101 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003102 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003103 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003104 continue;
3105
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003106 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003107 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003108 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003109 continue;
3110 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003111 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003112 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003113 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003114 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3115 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003116 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003117 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003118 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003119 }
3120 continue;
3121
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003122 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003123 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003124 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003125 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003126 long tabnr = get_address(eap, &eap->cmd,
3127 ADDR_TABS, eap->skip,
3128 skip_only, FALSE, 1);
3129 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003130 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003131 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003132 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003133 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3134 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003135 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003136 return FAIL;
3137 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003138 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003139 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003140 }
3141 eap->cmd = p;
3142 continue;
3143 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003144 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003145 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003146 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003147 continue;
3148
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003149 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003150 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003151 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003152 continue;
3153
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003154 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003155 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003156 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003157 continue;
3158 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003159 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003160 {
3161 if (ends_excmd2(p, eap->cmd))
3162 {
3163 *errormsg =
3164 _(e_vim9cmd_must_be_followed_by_command);
3165 return FAIL;
3166 }
3167 cmod->cmod_flags |= CMOD_VIM9CMD;
3168 continue;
3169 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003170 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003171 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003172 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003173 {
zeertzjqcd749632022-06-14 13:30:35 +01003174 // zero means not set, one is verbose == 0, etc.
3175 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003176 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003177 else
zeertzjqcd749632022-06-14 13:30:35 +01003178 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003179 eap->cmd = p;
3180 continue;
3181 }
3182 break;
3183 }
3184
Bram Moolenaar1501b632022-03-27 16:56:21 +01003185 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003186 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003187 if (eap->cmd > cmd_start)
3188 {
3189 // Move the '<,'> range to after the modifiers and insert a colon.
3190 // Since the modifiers have been parsed put the colon on top of the
3191 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3192 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003193 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003194 {
3195 size_t len = STRLEN(cmd_start);
3196
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003197 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003198 // "'<,'>mods" -> "mods *+
3199 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003200 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003201 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003202 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003203 }
3204 else
3205 {
3206 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3207 eap->cmd -= 5;
3208 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3209 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003210 }
3211 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003212 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003213 // Special case: change empty command to "+".
3214 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003215 eap->cmd = (char_u *)"'<,'>+";
3216 else
3217 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003218 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003219 else if (use_plus_cmd)
zeertzjq7d664bf2024-07-14 10:22:54 +02003220 eap->cmd = (char_u *)exmode_plus;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003221
Bram Moolenaareffed932018-08-14 13:38:17 +02003222 return OK;
3223}
3224
3225/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003226 * Return TRUE if "cmod" has anything set.
3227 */
3228 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003229has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003230{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003231 return (cmod->cmod_flags != 0 && (!ignore_silent
3232 || (cmod->cmod_flags
3233 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003234 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003235 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003236 || cmod->cmod_tab != 0
3237 || cmod->cmod_filter_regmatch.regprog != NULL;
3238}
3239
Bram Moolenaar8991be22022-02-14 21:51:46 +00003240#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003241/*
3242 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3243 */
3244 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003245cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003246{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003247 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003248 {
3249 emsg(_(e_misplaced_command_modifier));
3250 return TRUE;
3251 }
3252 return FALSE;
3253}
Dominique Pelle748b3082022-01-08 12:41:16 +00003254#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003255
3256/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003257 * Apply the command modifiers. Saves current state in "cmdmod", call
3258 * undo_cmdmod() later.
3259 */
3260 void
3261apply_cmdmod(cmdmod_T *cmod)
3262{
3263#ifdef HAVE_SANDBOX
3264 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3265 {
3266 ++sandbox;
3267 cmod->cmod_did_sandbox = TRUE;
3268 }
3269#endif
zeertzjqcd749632022-06-14 13:30:35 +01003270 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003271 {
3272 if (cmod->cmod_verbose_save == 0)
3273 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003274 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003275 }
3276
3277 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3278 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003279 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003280 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003281 cmod->cmod_save_msg_scroll = msg_scroll;
3282 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003283 if (cmod->cmod_flags & CMOD_SILENT)
3284 ++msg_silent;
3285 if (cmod->cmod_flags & CMOD_UNSILENT)
3286 msg_silent = 0;
3287
3288 if (cmod->cmod_flags & CMOD_ERRSILENT)
3289 {
3290 ++emsg_silent;
3291 ++cmod->cmod_did_esilent;
3292 }
3293
Bram Moolenaare1004402020-10-24 20:49:43 +02003294 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003295 {
3296 // Set 'eventignore' to "all".
3297 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003298 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003299 set_string_option_direct((char_u *)"ei", -1,
3300 (char_u *)"all", OPT_FREE, SID_NONE);
3301 }
3302}
3303
3304/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003305 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003306 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003307 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003308undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003309{
Bram Moolenaare1004402020-10-24 20:49:43 +02003310 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003311 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003312 p_verbose = cmod->cmod_verbose_save - 1;
3313 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003314 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003315
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003316#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003317 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003318 {
3319 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003320 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003321 }
3322#endif
3323
Bram Moolenaare1004402020-10-24 20:49:43 +02003324 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003325 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003326 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003327 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3328 OPT_FREE, SID_NONE);
3329 free_string_option(cmod->cmod_save_ei);
3330 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003331 }
3332
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003333 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003334
Bram Moolenaare1004402020-10-24 20:49:43 +02003335 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003336 {
3337 // messages could be enabled for a serious error, need to check if the
3338 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003339 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3340 msg_silent = cmod->cmod_save_msg_silent - 1;
3341 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003342 if (emsg_silent < 0)
3343 emsg_silent = 0;
3344 // Restore msg_scroll, it's set by file I/O commands, even when no
3345 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003346 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003347
3348 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3349 // somewhere in the line. Put it back in the first column.
3350 if (redirecting())
3351 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003352
Bram Moolenaare1004402020-10-24 20:49:43 +02003353 cmod->cmod_save_msg_silent = 0;
3354 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003355 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003356}
3357
3358/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003359 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003360 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003361 * Return FAIL and set "errormsg" or return OK.
3362 */
3363 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003364parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003365{
3366 int address_count = 1;
3367 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003368 int need_check_cursor = FALSE;
3369 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003370
3371 // Repeat for all ',' or ';' separated addresses.
3372 for (;;)
3373 {
3374 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003375 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003376 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003377 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003378 eap->addr_count == 0, address_count++);
3379 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003380 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003381 if (lnum == MAXLNUM)
3382 {
3383 if (*eap->cmd == '%') // '%' - all lines
3384 {
3385 ++eap->cmd;
3386 switch (eap->addr_type)
3387 {
3388 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003389 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003390 eap->line1 = 1;
3391 eap->line2 = curbuf->b_ml.ml_line_count;
3392 break;
3393 case ADDR_LOADED_BUFFERS:
3394 {
3395 buf_T *buf = firstbuf;
3396
3397 while (buf->b_next != NULL
3398 && buf->b_ml.ml_mfp == NULL)
3399 buf = buf->b_next;
3400 eap->line1 = buf->b_fnum;
3401 buf = lastbuf;
3402 while (buf->b_prev != NULL
3403 && buf->b_ml.ml_mfp == NULL)
3404 buf = buf->b_prev;
3405 eap->line2 = buf->b_fnum;
3406 break;
3407 }
3408 case ADDR_BUFFERS:
3409 eap->line1 = firstbuf->b_fnum;
3410 eap->line2 = lastbuf->b_fnum;
3411 break;
3412 case ADDR_WINDOWS:
3413 case ADDR_TABS:
3414 if (IS_USER_CMDIDX(eap->cmdidx))
3415 {
3416 eap->line1 = 1;
3417 eap->line2 = eap->addr_type == ADDR_WINDOWS
3418 ? LAST_WIN_NR : LAST_TAB_NR;
3419 }
3420 else
3421 {
3422 // there is no Vim command which uses '%' and
3423 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003424 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003425 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003426 }
3427 break;
3428 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003429 case ADDR_UNSIGNED:
3430 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003431 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003432 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003433 case ADDR_ARGUMENTS:
3434 if (ARGCOUNT == 0)
3435 eap->line1 = eap->line2 = 0;
3436 else
3437 {
3438 eap->line1 = 1;
3439 eap->line2 = ARGCOUNT;
3440 }
3441 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003442 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003443#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003444 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003445 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003446 if (eap->line2 == 0)
3447 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003448#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003449 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003450 case ADDR_NONE:
3451 // Will give an error later if a range is found.
3452 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003453 }
3454 ++eap->addr_count;
3455 }
3456 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3457 {
3458 pos_T *fp;
3459
3460 // '*' - visual area
3461 if (eap->addr_type != ADDR_LINES)
3462 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003463 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003464 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003465 }
3466
3467 ++eap->cmd;
3468 if (!eap->skip)
3469 {
3470 fp = getmark('<', FALSE);
3471 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003472 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003473 eap->line1 = fp->lnum;
3474 fp = getmark('>', FALSE);
3475 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003476 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003477 eap->line2 = fp->lnum;
3478 ++eap->addr_count;
3479 }
3480 }
3481 }
3482 else
3483 eap->line2 = lnum;
3484 eap->addr_count++;
3485
3486 if (*eap->cmd == ';')
3487 {
3488 if (!eap->skip)
3489 {
3490 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003491
Bram Moolenaar0e717042020-04-27 19:29:01 +02003492 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003493 // accept zero as address, so 0;/PATTERN/ works correctly
3494 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003495 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003496 if (eap->line2 > 0)
3497 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003498 else
3499 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003500 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003501 }
3502 }
3503 else if (*eap->cmd != ',')
3504 break;
3505 ++eap->cmd;
3506 }
3507
3508 // One address given: set start and end lines.
3509 if (eap->addr_count == 1)
3510 {
3511 eap->line1 = eap->line2;
3512 // ... but only implicit: really no address given
3513 if (lnum == MAXLNUM)
3514 eap->addr_count = 0;
3515 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003516 ret = OK;
3517
3518theend:
3519 if (need_check_cursor)
3520 check_cursor();
3521 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003522}
3523
3524/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003525 * Append "cmd" to the error message in IObuff.
3526 * Takes care of limiting the length and handling 0xa0, which would be
3527 * invisible otherwise.
3528 */
3529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003530append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003531{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003532 size_t len = STRLEN(IObuff);
3533 char_u *s = cmd;
3534 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003535
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003536 if (len > IOSIZE - 100)
3537 {
3538 // Not enough space, truncate and put in "...".
3539 d = IObuff + IOSIZE - 100;
3540 d -= mb_head_off(IObuff, d);
3541 STRCPY(d, "...");
3542 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003543 STRCAT(IObuff, ": ");
3544 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003545 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003546 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003547 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003548 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003549 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003550 STRCPY(d, "<a0>");
3551 d += 4;
3552 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003553 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3554 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003555 else
3556 MB_COPY_CHAR(s, d);
3557 }
3558 *d = NUL;
3559}
3560
Dominique Pelle748b3082022-01-08 12:41:16 +00003561#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003562/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003563 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3564 * the name. Otherwise just return "start".
3565 */
3566 char_u *
3567skip_option_env_lead(char_u *start)
3568{
3569 char_u *name = start;
3570
3571 if (*start == '&')
3572 {
3573 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3574 name += 3;
3575 else
3576 name += 1;
3577 }
3578 else if (*start == '$')
3579 name += 1;
3580 return name;
3581}
Dominique Pelle748b3082022-01-08 12:41:16 +00003582#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003583
3584/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003585 * Return TRUE and set "*idx" if "p" points to a one letter command.
3586 * If not in Vim9 script:
Doug Kearnsea842022024-09-29 17:17:41 +02003587 * - The 'k' command can directly be followed by any character
3588 * but :keepa[lt] is another command, as are :keepj[umps],
3589 * :kee[pmarks] and :keepp[atterns].
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003590 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3591 * but :sre[wind] is another command, as are :scr[iptnames],
3592 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3593 */
3594 static int
3595one_letter_cmd(char_u *p, cmdidx_T *idx)
3596{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003597 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003598 return FALSE;
Doug Kearnsea842022024-09-29 17:17:41 +02003599 if (p[0] == 'k'
3600 && (p[1] != 'e' || (p[1] == 'e' && p[2] != 'e')))
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003601 {
3602 *idx = CMD_k;
3603 return TRUE;
3604 }
3605 if (p[0] == 's'
3606 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3607 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3608 || p[1] == 'g'
3609 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3610 || p[1] == 'I'
3611 || (p[1] == 'r' && p[2] != 'e')))
3612 {
3613 *idx = CMD_substitute;
3614 return TRUE;
3615 }
3616 return FALSE;
3617}
3618
Dominique Pellee764d1b2023-03-12 21:20:59 +00003619#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003620/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003621 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3622 * call.
3623 */
3624 int
3625number_method(char_u *cmd)
3626{
3627 char_u *p = skipdigits(cmd);
3628
3629 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3630}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003631#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003632
3633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003636 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003637 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 *
3639 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3640 * assignment without "let". Sets eap->cmdidx to the command while returning
3641 * "eap->cmd".
3642 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 * Returns NULL for an ambiguous user command.
3644 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 char_u *
3646find_ex_command(
3647 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003648 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003649 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003650 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651{
3652 int len;
3653 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003654 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003655#ifndef FEAT_EVAL
3656 int vim9 = FALSE;
3657#else
3658 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 /*
3661 * Recognize a Vim9 script function/method call and assignment:
3662 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3663 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003664 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003665 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003667 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003669 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003670 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3671 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003673 int oplen;
3674 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003675 char_u *swp;
3676
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003677 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003678 || (eap->cmd[0] == '$'
3679 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003680 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003681 && (valid_yank_reg(eap->cmd[1], FALSE)
3682 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003683 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003684 if (*eap->cmd == '&')
3685 {
3686 p = eap->cmd + 1;
3687 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3688 p += 2;
3689 p = to_name_end(p, FALSE);
3690 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003691 else if (*eap->cmd == '$')
3692 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003693 else
3694 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003695 if (ends_excmd(*skipwhite(p)))
3696 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003697 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3698 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003699 eap->cmdidx = CMD_eval;
3700 return eap->cmd;
3701 }
3702 // "&option" can be followed by "->" or "=", check below
3703 }
3704
3705 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003707 if (
3708 // "(..." is an expression.
3709 // "funcname(" is always a function call.
3710 *p == '('
3711 || (p == eap->cmd
3712 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003713 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003714 *eap->cmd == '{'
3715 // "'string'->func()" is an expression.
3716 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003717 // '"string"->func()' is an expression.
3718 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003719 // '$"string"->func()' is an expression.
3720 // "$'string'->func()" is an expression.
3721 || (eap->cmd[0] == '$'
3722 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3723 // '0z1234->func()' is an expression.
3724 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003725 // "g:varname" is an expression.
3726 || eap->cmd[1] == ':'
3727 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003728 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003729 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003730 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003731 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3732 {
3733 // "{" by itself is the start of a block.
3734 eap->cmdidx = CMD_block;
3735 return eap->cmd + 1;
3736 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003737 eap->cmdidx = CMD_eval;
3738 return eap->cmd;
3739 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003740
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003741 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003742 // "varname[]" is an expression.
3743 *p == '['
3744 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003745 || (*p == '.'
3746 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3747 // g:[key] is an expression
3748 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003749 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003750 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003751
3752 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003753 // Skip over the whole thing, it can be:
3754 // name.member = val
3755 // name[a : b] = val
3756 // name[idx] = val
3757 // name[idx].member = val
3758 // etc.
3759 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003760 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003761 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003762 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003763 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003764 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003765 || (after[0] == '.' && after[1] == '.'
3766 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003767 eap->cmdidx = CMD_var;
3768 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003769 --emsg_silent;
3770 return eap->cmd;
3771 }
3772
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003773 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3774 // an assignment.
3775 // If there is no line break inside the "[...]" then "p" is
3776 // advanced to after the "]" by to_name_const_end(): check if a "="
3777 // follows.
3778 // If "[...]" has a line break "p" still points at the "[" and it
3779 // can't be an assignment.
3780 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003781 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003782 char_u *eq;
3783
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003784 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003785 if (p == eap->cmd && *p == '[')
3786 {
3787 int count = 0;
3788 int semicolon = FALSE;
3789
3790 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3791 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003792 eq = p;
3793 if (eq != NULL)
3794 {
3795 eq = skipwhite(eq);
3796 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3797 ++eq;
3798 }
3799 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003800 {
3801 eap->cmdidx = CMD_eval;
3802 return eap->cmd;
3803 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003804 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003805 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003806 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003807 return eap->cmd;
3808 }
3809 }
3810
3811 // Recognize an assignment if we recognize the variable name:
3812 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003813 // "@r = expr"
3814 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003815 // "var = expr" where "var" is a variable name or we are skipping
3816 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003817 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003818 oplen = assignment_len(skipwhite(p), &heredoc);
3819 if (oplen > 0)
3820 {
3821 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3822 || *eap->cmd == '&'
3823 || *eap->cmd == '$'
3824 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003825 || (eap->skip && IS_WHITE_OR_NUL(
3826 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003827 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003828 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003829 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003830 return eap->cmd;
3831 }
3832 }
3833
Bram Moolenaar9510d222022-09-11 15:14:05 +01003834 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003835 // "w:varname: number = 123".
3836 if (eap->cmd[1] == ':' && *p == ':')
3837 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003838 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003839 return eap->cmd;
3840 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003841 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003842
Bram Moolenaar31d99482022-05-26 22:24:43 +01003843 // 1234->func() is a method call
3844 if (number_method(eap->cmd))
3845 {
3846 eap->cmdidx = CMD_eval;
3847 return eap->cmd;
3848 }
3849
Bram Moolenaar7b829262021-10-13 15:04:34 +01003850 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3851 // an expression. A global/substitute/list command needs to use a
3852 // longer name.
3853 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3854 {
3855 eap->cmdidx = CMD_eval;
3856 return eap->cmd;
3857 }
3858
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003859 // If it is an ID it might be a variable with an operator on the next
3860 // line, if the variable exists it can't be an Ex command.
3861 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003862 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003863 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3864 {
3865 eap->cmdidx = CMD_eval;
3866 return eap->cmd;
3867 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003868
3869 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003870 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3871 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003872 {
3873 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3874 return eap->cmd + 2;
3875 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 }
3877#endif
3878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 /*
3880 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 */
3882 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003883 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 ++p;
Doug Kearnsea842022024-09-29 17:17:41 +02003886 if (full != NULL)
3887 *full = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
3889 else
3890 {
3891 while (ASCII_ISALPHA(*p))
3892 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003893 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003894 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003896 while (ASCII_ISALNUM(*p))
3897 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 }
3899 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3900 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003901 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 ++p;
3903 while (ASCII_ISALPHA(*p))
3904 ++p;
3905 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003906
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003907 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003908 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 ++p;
3910 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003911 // The "d" command can directly be followed by 'l' or 'p' flag, when
3912 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003913 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003914 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003915 // Check for ":dl", ":dell", etc. to ":deletel": that's
3916 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003917 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003918 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003919 break;
3920 if (i == len - 1)
3921 {
3922 --len;
3923 if (p[-1] == 'l')
3924 eap->flags |= EXFLAG_LIST;
3925 else
3926 eap->flags |= EXFLAG_PRINT;
3927 }
3928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003930 if (ASCII_ISLOWER(eap->cmd[0]))
3931 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003932 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003933 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003934
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003935 if (command_count != (int)CMD_SIZE)
3936 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003937 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003938 getout(1);
3939 }
3940
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003941 // Use a precomputed index for fast look-up in cmdnames[]
3942 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003943 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3944 if (ASCII_ISLOWER(c2))
3945 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3946 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003947 else if (ASCII_ISUPPER(eap->cmd[0]))
3948 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003950 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
3952 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3953 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3954 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3955 (size_t)len) == 0)
3956 {
3957#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003958 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 *full = TRUE;
3960#endif
3961 break;
3962 }
3963
Bram Moolenaar204852a2022-03-05 12:56:44 +00003964 // :Print and :mode are not supported in Vim9 script.
3965 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003966 if (vim9 && eap->cmdidx != CMD_SIZE)
3967 {
3968 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3969 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003970 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003971 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003972 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003973 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003974 eap->cmdidx = CMD_SIZE;
3975 }
3976 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003977
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003978 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003979 // 'cpoptions'.
3980 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3981 p = eap->cmd;
3982
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003983 // Look for a user defined command as a last resort. Let ":Print" be
3984 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003985 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3986 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003988 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 while (ASCII_ISALNUM(*p))
3990 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003991 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003993 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 eap->cmdidx = CMD_SIZE;
3995 }
3996
Bram Moolenaar204852a2022-03-05 12:56:44 +00003997 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3998 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003999 eap->cmdidx = CMD_finally;
4000
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004001#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004002 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004003 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02004004 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004005 && (eap->cmdidx < 0 ||
4006 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004007 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004008 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4009
4010 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004011 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004012 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004013 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004014#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004015
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 return p;
4017}
4018
4019#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004020typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004021{
4022 char *name;
4023 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004024 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004025} cmdmod_info_T;
4026
4027static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004028 {"aboveleft", 3, FALSE},
4029 {"belowright", 3, FALSE},
4030 {"botright", 2, FALSE},
4031 {"browse", 3, FALSE},
4032 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004033 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004034 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004035 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004036 {"keepalt", 5, FALSE},
4037 {"keepjumps", 5, FALSE},
4038 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004039 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004040 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004041 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004042 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004043 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004044 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004045 {"rightbelow", 6, FALSE},
4046 {"sandbox", 3, FALSE},
4047 {"silent", 3, FALSE},
4048 {"tab", 3, TRUE},
4049 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004050 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004051 {"verbose", 4, TRUE},
4052 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004053 {"vim9cmd", 4, FALSE}
4054}; // cmdmod_info_tab
4055
Bram Moolenaared53fb92007-11-24 20:50:24 +00004056/*
4057 * Return length of a command modifier (including optional count).
4058 * Return zero when it's not a modifier.
4059 */
4060 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004061modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004062{
Christian Brabandt70a11a62024-07-26 19:13:55 +02004063 int i, j;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004064 char_u *p = cmd;
4065
4066 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004067 p = skipwhite(skipdigits(cmd + 1));
Christian Brabandt70a11a62024-07-26 19:13:55 +02004068 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004069 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004070 for (j = 0; p[j] != NUL; ++j)
4071 if (p[j] != cmdmod_info_tab[i].name[j])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004072 break;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004073 if (!ASCII_ISALPHA(p[j]) && j >= cmdmod_info_tab[i].minlen
4074 && (p == cmd || cmdmod_info_tab[i].has_count))
4075 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004076 }
4077 return 0;
4078}
4079
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080/*
4081 * Return > 0 if an Ex command "name" exists.
4082 * Return 2 if there is an exact match.
4083 * Return 3 if there is an ambiguous match.
4084 */
4085 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004086cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087{
4088 exarg_T ea;
4089 int full = FALSE;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004090 int i;
4091 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004092 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
Christian Brabandt70a11a62024-07-26 19:13:55 +02004094 // Check command modifiers.
4095 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004097 for (j = 0; name[j] != NUL; ++j)
4098 if (name[j] != cmdmod_info_tab[i].name[j])
4099 break;
4100 if (name[j] == NUL && j >= cmdmod_info_tab[i].minlen)
4101 return (cmdmod_info_tab[i].name[j] == NUL ? 2 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004104 // Check built-in commands and user defined commands.
4105 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004106 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004108 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004110 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004112 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4113 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004114 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004115 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4117}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004118
4119/*
4120 * "fullcommand" function
4121 */
4122 void
4123f_fullcommand(typval_T *argvars, typval_T *rettv)
4124{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004125 exarg_T ea;
4126 char_u *name;
4127 char_u *p;
4128 int vim9script = in_vim9script();
4129 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004130
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004131 rettv->v_type = VAR_STRING;
4132 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004133
Bram Moolenaaraa534142022-09-15 21:46:02 +01004134 if (in_vim9script()
4135 && (check_for_string_arg(argvars, 0) == FAIL
4136 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004137 return;
4138
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004139 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004140 if (name == NULL)
4141 return;
4142
Bram Moolenaaraa534142022-09-15 21:46:02 +01004143 if (argvars[1].v_type != VAR_UNKNOWN)
4144 {
4145 vim9script = tv_get_bool(&argvars[1]);
4146 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4147 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4148 }
4149
zeertzjqc024ed92022-01-04 16:22:52 +00004150 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004151 name++;
4152 name = skip_range(name, TRUE, NULL);
4153
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004154 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4155 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004156 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004157 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004158 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004159 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004160 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004161 goto theend;
4162
4163 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004164 {
4165 int res;
4166
4167 ++emsg_silent;
4168 res = not_in_vim9(&ea);
4169 --emsg_silent;
4170
4171 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004172 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004173 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004174
4175 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004176 ? get_user_command_name(ea.useridx, ea.cmdidx)
4177 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004178theend:
4179 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004180}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181#endif
4182
Bram Moolenaard0190392019-08-23 21:17:35 +02004183 cmdidx_T
4184excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185{
Bram Moolenaard0190392019-08-23 21:17:35 +02004186 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004188 if (!one_letter_cmd(cmd, &idx))
4189 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4190 idx = (cmdidx_T)((int)idx + 1))
4191 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4192 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
Bram Moolenaard0190392019-08-23 21:17:35 +02004194 return idx;
4195}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
Bram Moolenaard0190392019-08-23 21:17:35 +02004197 long
4198excmd_get_argt(cmdidx_T idx)
4199{
4200 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201}
4202
4203/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004204 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 *
4206 * Backslashed delimiters after / or ? will be skipped, and commands will
4207 * not be expanded between /'s and ?'s or after "'".
4208 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004209 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 * Returns the "cmd" pointer advanced to beyond the range.
4211 */
4212 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004213skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004214 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004215 int skip_star, // skip "*" used for Visual range
4216 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004218 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004219 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004221 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004223 if (*cmd == '\\')
4224 {
4225 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4226 ++cmd;
4227 else
4228 break;
4229 }
4230 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004232 char_u *p = cmd;
4233
4234 // a quote is only valid at the start or after a separator
4235 while (p > cmd_start)
4236 {
4237 --p;
4238 if (!VIM_ISWHITE(*p))
4239 break;
4240 }
4241 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4242 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 if (*++cmd == NUL && ctx != NULL)
4244 *ctx = EXPAND_NOTHING;
4245 }
4246 else if (*cmd == '/' || *cmd == '?')
4247 {
4248 delim = *cmd++;
4249 while (*cmd != NUL && *cmd != delim)
4250 if (*cmd++ == '\\' && *cmd != NUL)
4251 ++cmd;
4252 if (*cmd == NUL && ctx != NULL)
4253 *ctx = EXPAND_NOTHING;
4254 }
4255 if (*cmd != NUL)
4256 ++cmd;
4257 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004258
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004259 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004260 while (*cmd == ':')
4261 cmd = skipwhite(cmd + 1);
4262
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004263 // Skip "*" used for Visual range.
4264 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4265 cmd = skipwhite(cmd + 1);
4266
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 return cmd;
4268}
4269
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004270 static void
4271addr_error(cmd_addr_T addr_type)
4272{
4273 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004274 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004275 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004276 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004277}
4278
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004280 * Return the default address for an address type.
4281 */
4282 static linenr_T
4283default_address(exarg_T *eap)
4284{
4285 linenr_T lnum = 0;
4286
4287 switch (eap->addr_type)
4288 {
4289 case ADDR_LINES:
4290 case ADDR_OTHER:
4291 // Default is the cursor line number. Avoid using an invalid
4292 // line number though.
4293 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4294 lnum = curbuf->b_ml.ml_line_count;
4295 else
4296 lnum = curwin->w_cursor.lnum;
4297 break;
4298 case ADDR_WINDOWS:
4299 lnum = CURRENT_WIN_NR;
4300 break;
4301 case ADDR_ARGUMENTS:
4302 lnum = curwin->w_arg_idx + 1;
4303 if (lnum > ARGCOUNT)
4304 lnum = ARGCOUNT;
4305 break;
4306 case ADDR_LOADED_BUFFERS:
4307 case ADDR_BUFFERS:
4308 lnum = curbuf->b_fnum;
4309 break;
4310 case ADDR_TABS:
4311 lnum = CURRENT_TAB_NR;
4312 break;
4313 case ADDR_TABS_RELATIVE:
4314 case ADDR_UNSIGNED:
4315 lnum = 1;
4316 break;
4317 case ADDR_QUICKFIX:
4318#ifdef FEAT_QUICKFIX
4319 lnum = qf_get_cur_idx(eap);
4320#endif
4321 break;
4322 case ADDR_QUICKFIX_VALID:
4323#ifdef FEAT_QUICKFIX
4324 lnum = qf_get_cur_valid_idx(eap);
4325#endif
4326 break;
4327 case ADDR_NONE:
4328 // Will give an error later if a range is found.
4329 break;
4330 }
4331 return lnum;
4332}
4333
4334/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004335 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 *
4337 * Set ptr to the next character after the part that was interpreted.
4338 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004339 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 *
4341 * Return MAXLNUM when no Ex address was found.
4342 */
4343 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004344get_address(
4345 exarg_T *eap UNUSED,
4346 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004347 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004348 int skip, // only skip the address, don't use it
4349 int silent, // no errors or side effects
4350 int to_other_file, // flag: may jump to other file
4351 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352{
4353 int c;
4354 int i;
4355 long n;
4356 char_u *cmd;
4357 pos_T pos;
4358 pos_T *fp;
4359 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004360 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
4362 cmd = skipwhite(*ptr);
4363 lnum = MAXLNUM;
4364 do
4365 {
4366 switch (*cmd)
4367 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004368 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004369 ++cmd;
4370 switch (addr_type)
4371 {
4372 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004373 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 lnum = curwin->w_cursor.lnum;
4375 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004376 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004377 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004378 break;
4379 case ADDR_ARGUMENTS:
4380 lnum = curwin->w_arg_idx + 1;
4381 break;
4382 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004383 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004384 lnum = curbuf->b_fnum;
4385 break;
4386 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004387 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004388 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004389 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004390 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004391 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004392 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004393 cmd = NULL;
4394 goto error;
4395 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004396 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004397#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004398 lnum = qf_get_cur_idx(eap);
4399#endif
4400 break;
4401 case ADDR_QUICKFIX_VALID:
4402#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004403 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004404#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004405 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 }
4407 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004409 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004410 ++cmd;
4411 switch (addr_type)
4412 {
4413 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004414 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 lnum = curbuf->b_ml.ml_line_count;
4416 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004418 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004419 break;
4420 case ADDR_ARGUMENTS:
4421 lnum = ARGCOUNT;
4422 break;
4423 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004424 buf = lastbuf;
4425 while (buf->b_ml.ml_mfp == NULL)
4426 {
4427 if (buf->b_prev == NULL)
4428 break;
4429 buf = buf->b_prev;
4430 }
4431 lnum = buf->b_fnum;
4432 break;
4433 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004434 lnum = lastbuf->b_fnum;
4435 break;
4436 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004437 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004438 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004439 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004440 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004441 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004442 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004443 cmd = NULL;
4444 goto error;
4445 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004446 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004447#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004448 lnum = qf_get_size(eap);
4449 if (lnum == 0)
4450 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004451#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004452 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004453 case ADDR_QUICKFIX_VALID:
4454#ifdef FEAT_QUICKFIX
4455 lnum = qf_get_valid_size(eap);
4456 if (lnum == 0)
4457 lnum = 1;
4458#endif
4459 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004460 }
4461 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004463 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004464 if (*++cmd == NUL)
4465 {
4466 cmd = NULL;
4467 goto error;
4468 }
4469 if (addr_type != ADDR_LINES)
4470 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004471 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004472 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004473 goto error;
4474 }
4475 if (skip)
4476 ++cmd;
4477 else
4478 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004479 // Only accept a mark in another file when it is
4480 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4482 ++cmd;
4483 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004484 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004485 lnum = curwin->w_cursor.lnum;
4486 else
4487 {
4488 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 {
4490 cmd = NULL;
4491 goto error;
4492 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 lnum = fp->lnum;
4494 }
4495 }
4496 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004499 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 c = *cmd++;
4501 if (addr_type != ADDR_LINES)
4502 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004503 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004504 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 goto error;
4506 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004507 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004508 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004509 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 if (*cmd == c)
4511 ++cmd;
4512 }
4513 else
4514 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004515 int flags;
4516
4517 pos = curwin->w_cursor; // save curwin->w_cursor
4518
4519 // When '/' or '?' follows another address, start from
4520 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004521 if (lnum > 0 && lnum != MAXLNUM)
4522 curwin->w_cursor.lnum =
4523 lnum > curbuf->b_ml.ml_line_count
4524 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004525
4526 // Start a forward search at the end of the line (unless
4527 // before the first line).
4528 // Start a backward search at the start of the line.
4529 // This makes sure we never match in the current
4530 // line, and can match anywhere in the
4531 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004532 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004533 curwin->w_cursor.col = MAXCOL;
4534 else
4535 curwin->w_cursor.col = 0;
4536 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004537 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004538 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004539 {
4540 curwin->w_cursor = pos;
4541 cmd = NULL;
4542 goto error;
4543 }
4544 lnum = curwin->w_cursor.lnum;
4545 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004546 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004547 cmd += searchcmdlen;
4548 }
4549 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004551 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004552 ++cmd;
4553 if (addr_type != ADDR_LINES)
4554 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004555 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004556 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004557 goto error;
4558 }
4559 if (*cmd == '&')
4560 i = RE_SUBST;
4561 else if (*cmd == '?' || *cmd == '/')
4562 i = RE_SEARCH;
4563 else
4564 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004565 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004566 cmd = NULL;
4567 goto error;
4568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004570 if (!skip)
4571 {
4572 /*
4573 * When search follows another address, start from
4574 * there.
4575 */
4576 if (lnum != MAXLNUM)
4577 pos.lnum = lnum;
4578 else
4579 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004581 /*
4582 * Start the search just like for the above
4583 * do_search().
4584 */
4585 if (*cmd != '?')
4586 pos.col = MAXCOL;
4587 else
4588 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004589 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004590 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004591 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004592 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004593 lnum = pos.lnum;
4594 else
4595 {
4596 cmd = NULL;
4597 goto error;
4598 }
4599 }
4600 ++cmd;
4601 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
4603 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004604 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004605 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
4607
4608 for (;;)
4609 {
4610 cmd = skipwhite(cmd);
4611 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4612 break;
4613
4614 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004615 {
4616 switch (addr_type)
4617 {
4618 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004619 case ADDR_OTHER:
4620 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004621 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004622 break;
4623 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004624 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004625 break;
4626 case ADDR_ARGUMENTS:
4627 lnum = curwin->w_arg_idx + 1;
4628 break;
4629 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004630 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004631 lnum = curbuf->b_fnum;
4632 break;
4633 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004634 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004635 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004636 case ADDR_TABS_RELATIVE:
4637 lnum = 1;
4638 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004639 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004640#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004641 lnum = qf_get_cur_idx(eap);
4642#endif
4643 break;
4644 case ADDR_QUICKFIX_VALID:
4645#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004646 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004647#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004648 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004649 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004650 case ADDR_UNSIGNED:
4651 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004652 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004653 }
4654 }
4655
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004657 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 else
4659 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004660 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 n = 1;
4662 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004663 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004664 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004666 if (n == MAXLNUM)
4667 {
4668 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004669 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004670 goto error;
4671 }
4672 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004673
4674 if (addr_type == ADDR_TABS_RELATIVE)
4675 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004676 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004677 cmd = NULL;
4678 goto error;
4679 }
4680 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004681 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004682 lnum = compute_buffer_local_count(
4683 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004685 {
4686#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004687 // Relative line addressing: need to adjust for lines in a
4688 // closed fold after the first address.
4689 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4690 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004691 (void)hasFolding(lnum, NULL, &lnum);
4692#endif
4693 if (i == '-')
4694 lnum -= n;
4695 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004696 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004697 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004698 {
4699 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004700 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004701 goto error;
4702 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004703 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004704 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
4707 } while (*cmd == '/' || *cmd == '?');
4708
4709error:
4710 *ptr = cmd;
4711 return lnum;
4712}
4713
4714/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004715 * Set eap->line1 and eap->line2 to the whole range.
4716 * Used for commands with the EX_DFLALL flag and no range given.
4717 */
4718 static void
4719address_default_all(exarg_T *eap)
4720{
4721 eap->line1 = 1;
4722 switch (eap->addr_type)
4723 {
4724 case ADDR_LINES:
4725 case ADDR_OTHER:
4726 eap->line2 = curbuf->b_ml.ml_line_count;
4727 break;
4728 case ADDR_LOADED_BUFFERS:
4729 {
4730 buf_T *buf = firstbuf;
4731
4732 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4733 buf = buf->b_next;
4734 eap->line1 = buf->b_fnum;
4735 buf = lastbuf;
4736 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4737 buf = buf->b_prev;
4738 eap->line2 = buf->b_fnum;
4739 }
4740 break;
4741 case ADDR_BUFFERS:
4742 eap->line1 = firstbuf->b_fnum;
4743 eap->line2 = lastbuf->b_fnum;
4744 break;
4745 case ADDR_WINDOWS:
4746 eap->line2 = LAST_WIN_NR;
4747 break;
4748 case ADDR_TABS:
4749 eap->line2 = LAST_TAB_NR;
4750 break;
4751 case ADDR_TABS_RELATIVE:
4752 eap->line2 = 1;
4753 break;
4754 case ADDR_ARGUMENTS:
4755 if (ARGCOUNT == 0)
4756 eap->line1 = eap->line2 = 0;
4757 else
4758 eap->line2 = ARGCOUNT;
4759 break;
4760 case ADDR_QUICKFIX_VALID:
4761#ifdef FEAT_QUICKFIX
4762 eap->line2 = qf_get_valid_size(eap);
4763 if (eap->line2 == 0)
4764 eap->line2 = 1;
4765#endif
4766 break;
4767 case ADDR_NONE:
4768 case ADDR_UNSIGNED:
4769 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004770 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004771 break;
4772 }
4773}
4774
4775
4776/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004777 * Get flags from an Ex command argument.
4778 */
4779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004780get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004781{
4782 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4783 {
4784 if (*eap->arg == 'l')
4785 eap->flags |= EXFLAG_LIST;
4786 else if (*eap->arg == 'p')
4787 eap->flags |= EXFLAG_PRINT;
4788 else
4789 eap->flags |= EXFLAG_NR;
4790 eap->arg = skipwhite(eap->arg + 1);
4791 }
4792}
4793
4794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 * Function called for command which is Not Implemented. NI!
4796 */
4797 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004798ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
4800 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004801 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004802 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803}
4804
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004805#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806/*
4807 * Function called for script command which is Not Implemented. NI!
4808 * Skips over ":perl <<EOF" constructs.
4809 */
4810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004811ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812{
4813 if (!eap->skip)
4814 ex_ni(eap);
4815 else
4816 vim_free(script_get(eap, eap->arg));
4817}
4818#endif
4819
4820/*
4821 * Check range in Ex command for validity.
4822 * Return NULL when valid, error message when invalid.
4823 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004824 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004825invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004827 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004828
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 if ( eap->line1 < 0
4830 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004831 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004832 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004833
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004834 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004835 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004836 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004837 {
4838 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004839 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004840#ifdef FEAT_DIFF
4841 + (eap->cmdidx == CMD_diffget)
4842#endif
4843 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004844 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004845 break;
4846 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004847 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004848 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004849 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004850 break;
4851 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004852 // Only a boundary check, not whether the buffers actually
4853 // exist.
4854 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004855 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004856 break;
4857 case ADDR_LOADED_BUFFERS:
4858 buf = firstbuf;
4859 while (buf->b_ml.ml_mfp == NULL)
4860 {
4861 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004862 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004863 buf = buf->b_next;
4864 }
4865 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004866 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004867 buf = lastbuf;
4868 while (buf->b_ml.ml_mfp == NULL)
4869 {
4870 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004871 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004872 buf = buf->b_prev;
4873 }
4874 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004875 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004876 break;
4877 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004878 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004879 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004880 break;
4881 case ADDR_TABS:
4882 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004883 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004884 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004885 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004886 case ADDR_OTHER:
4887 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004888 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004889 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004890#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004891 // No error for value that is too big, will use the last entry.
4892 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004893 {
4894 if (eap->addr_count == 0)
4895 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004896 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004897 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004898#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004899 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004900 case ADDR_QUICKFIX_VALID:
4901#ifdef FEAT_QUICKFIX
4902 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4903 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004904 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004905#endif
4906 break;
4907 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004908 case ADDR_NONE:
4909 // Will give an error elsewhere.
4910 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004911 }
4912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 return NULL;
4914}
4915
4916/*
4917 * Correct the range for zero line number, if required.
4918 */
4919 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004920correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004922 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
4924 if (eap->line1 == 0)
4925 eap->line1 = 1;
4926 if (eap->line2 == 0)
4927 eap->line2 = 1;
4928 }
4929}
4930
Bram Moolenaar748bf032005-02-02 23:04:36 +00004931#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004932/*
4933 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4934 * pattern. Otherwise return eap->arg.
4935 */
4936 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004937skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004938{
4939 char_u *p = eap->arg;
4940
Bram Moolenaara37420f2006-02-04 22:37:47 +00004941 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4942 || eap->cmdidx == CMD_vimgrepadd
4943 || eap->cmdidx == CMD_lvimgrepadd
4944 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004945 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004946 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004947 if (p == NULL)
4948 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004949 }
4950 return p;
4951}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004952
4953/*
4954 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4955 * in the command line, so that things like % get expanded.
4956 */
4957 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004958replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004959{
4960 char_u *new_cmdline;
4961 char_u *program;
4962 char_u *pos;
4963 char_u *ptr;
4964 int len;
4965 int i;
4966
4967 /*
4968 * Don't do it when ":vimgrep" is used for ":grep".
4969 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004970 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4971 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4972 || eap->cmdidx == CMD_grepadd
4973 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004974 && !grep_internal(eap->cmdidx))
4975 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004976 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4977 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004978 {
4979 if (*curbuf->b_p_gp == NUL)
4980 program = p_gp;
4981 else
4982 program = curbuf->b_p_gp;
4983 }
4984 else
4985 {
4986 if (*curbuf->b_p_mp == NUL)
4987 program = p_mp;
4988 else
4989 program = curbuf->b_p_mp;
4990 }
4991
4992 p = skipwhite(p);
4993
4994 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4995 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004996 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004997 i = 1;
4998 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4999 ++i;
5000 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005001 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005002 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005003 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005004 ptr = new_cmdline;
5005 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5006 {
5007 i = (int)(pos - program);
5008 STRNCPY(ptr, program, i);
5009 STRCPY(ptr += i, p);
5010 ptr += len;
5011 program = pos + 2;
5012 }
5013 STRCPY(ptr, program);
5014 }
5015 else
5016 {
John Marriotted908f72024-04-18 22:46:56 +02005017 size_t program_len = STRLEN(program);
5018
5019 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005020 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005021 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005022 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005023 STRCPY(new_cmdline + program_len, " ");
5024 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005025 }
5026 msg_make(p);
5027
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005028 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005029 vim_free(*cmdlinep);
5030 *cmdlinep = new_cmdline;
5031 p = new_cmdline;
5032 }
5033 return p;
5034}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005035#endif
5036
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037/*
5038 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005039 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 * Return FAIL for failure, OK otherwise.
5041 */
5042 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005043expand_filename(
5044 exarg_T *eap,
5045 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005046 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005048 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005050 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 char_u *p;
5052 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005053 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
Bram Moolenaar748bf032005-02-02 23:04:36 +00005055#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005056 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005057 p = skip_grep_pat(eap);
5058#else
5059 p = eap->arg;
5060#endif
5061
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 /*
5063 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5064 * the file name contains a wildcard it should not cause expanding.
5065 * (it will be expanded anyway if there is a wildcard before replacing).
5066 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005067 has_wildcards = mch_has_wildcard(p);
5068 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005070#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005071 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005072 if (p[0] == '`' && p[1] == '=')
5073 {
5074 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005075 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005076 if (*p == '`')
5077 ++p;
5078 continue;
5079 }
5080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 /*
5082 * Quick check if this cannot be the start of a special string.
5083 * Also removes backslash before '%', '#' and '<'.
5084 */
5085 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5086 {
5087 ++p;
5088 continue;
5089 }
5090
5091 /*
5092 * Try to find a match at this position.
5093 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005094 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005095 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005096 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005098 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
5100 p += srclen;
5101 continue;
5102 }
5103
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005104 // Wildcards won't be expanded below, the replacement is taken
5105 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005106 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5107 {
5108 char_u *l = repl;
5109
5110 repl = expand_env_save(repl);
5111 vim_free(l);
5112 }
5113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005114 // Need to escape white space et al. with a backslash.
5115 // Don't do this for:
5116 // - replacement that already has been escaped: "##"
5117 // - shell commands (may have to use quotes instead).
5118 // - non-unix systems when there is a single argument (spaces don't
5119 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005121 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 && eap->cmdidx != CMD_grep
5124 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005125 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005126 && eap->cmdidx != CMD_lgrep
5127 && eap->cmdidx != CMD_lgrepadd
5128 && eap->cmdidx != CMD_lmake
5129 && eap->cmdidx != CMD_make
5130 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005132 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133#endif
5134 )
5135 {
5136 char_u *l;
5137#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005138 // Don't escape a backslash here, because rem_backslash() doesn't
5139 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 static char_u *nobslash = (char_u *)" \t\"|";
5141# define ESCAPE_CHARS nobslash
5142#else
5143# define ESCAPE_CHARS escape_chars
5144#endif
5145
5146 for (l = repl; *l; ++l)
5147 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5148 {
5149 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5150 if (l != NULL)
5151 {
5152 vim_free(repl);
5153 repl = l;
5154 }
5155 break;
5156 }
5157 }
5158
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005159 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005160 if ((eap->usefilter || eap->cmdidx == CMD_bang
5161 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005162 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
5164 char_u *l;
5165
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005166 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 if (l != NULL)
5168 {
5169 vim_free(repl);
5170 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172 }
5173
Mike Williams51024bb2024-05-30 07:46:30 +02005174 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 vim_free(repl);
5176 if (p == NULL)
5177 return FAIL;
5178 }
5179
5180 /*
5181 * One file argument: Expand wildcards.
5182 * Don't do this with ":r !command" or ":w !command".
5183 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005184 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
5186 /*
5187 * May do this twice:
5188 * 1. Replace environment variables.
5189 * 2. Replace any other wildcards, remove backslashes.
5190 */
5191 for (n = 1; n <= 2; ++n)
5192 {
5193 if (n == 2)
5194 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 /*
5196 * Halve the number of backslashes (this is Vi compatible).
5197 * For Unix and OS/2, when wildcards are expanded, this is
5198 * done by ExpandOne() below.
5199 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005200#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (!has_wildcards)
5202#endif
5203 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
5205
5206 if (has_wildcards)
5207 {
5208 if (n == 1)
5209 {
5210 /*
5211 * First loop: May expand environment variables. This
5212 * can be done much faster with expand_env() than with
5213 * something else (e.g., calling a shell).
5214 * After expanding environment variables, check again
5215 * if there are still wildcards present.
5216 */
5217 if (vim_strchr(eap->arg, '$') != NULL
5218 || vim_strchr(eap->arg, '~') != NULL)
5219 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005220 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005221 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 has_wildcards = mch_has_wildcard(NameBuff);
5223 p = NameBuff;
5224 }
5225 else
5226 p = NULL;
5227 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005228 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
5230 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005231 int options = WILD_LIST_NOTFOUND
5232 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
5234 ExpandInit(&xpc);
5235 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005236 if (p_wic)
5237 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005239 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (p == NULL)
5241 return FAIL;
5242 }
5243 if (p != NULL)
5244 {
John Marriotted908f72024-04-18 22:46:56 +02005245 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005247 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 vim_free(p);
5249 }
5250 }
5251 }
5252 }
5253 return OK;
5254}
5255
5256/*
5257 * Replace part of the command line, keeping eap->cmd, eap->arg and
5258 * eap->nextcmd correct.
5259 * "src" points to the part that is to be replaced, of length "srclen".
5260 * "repl" is the replacement string.
5261 * Returns a pointer to the character after the replaced string.
5262 * Returns NULL for failure.
5263 */
5264 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005265repl_cmdline(
5266 exarg_T *eap,
5267 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005268 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005269 char_u *repl,
5270 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
John Marriotted908f72024-04-18 22:46:56 +02005272 size_t repllen;
5273 size_t taillen;
5274 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005276 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 /*
5279 * The new command line is build in new_cmdline[].
5280 * First allocate it.
5281 * Careful: a "+cmd" argument may have been NUL terminated.
5282 */
John Marriotted908f72024-04-18 22:46:56 +02005283 repllen = STRLEN(repl);
5284 taillen = STRLEN(src + srclen);
5285 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005286 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005287 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005288 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005289 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
5291 /*
5292 * Copy the stuff before the expanded part.
5293 * Copy the expanded stuff.
5294 * Copy what came after the expanded part.
5295 * Copy the next commands, if there are any.
5296 */
John Marriotted908f72024-04-18 22:46:56 +02005297 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5298 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005299
John Marriotted908f72024-04-18 22:46:56 +02005300 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5301 new_cmdlinelen += repllen; // remember the end of the string
5302 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5303 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005305 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 {
John Marriotted908f72024-04-18 22:46:56 +02005307 new_cmdlinelen += taillen + 1;
5308 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5309 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 }
5311 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5312 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5313 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5314 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5315 vim_free(*cmdlinep);
5316 *cmdlinep = new_cmdline;
5317
5318 return src;
5319}
5320
5321/*
5322 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005323 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 */
5325 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005326separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327{
5328 char_u *p;
5329
Bram Moolenaar86b68352004-12-27 21:59:20 +00005330#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005331 p = skip_grep_pat(eap);
5332#else
5333 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005334#endif
5335
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005336 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
5338 if (*p == Ctrl_V)
5339 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005340 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005341 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005343 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005344 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005345 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 break;
5347 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005348
5349#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005350 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005351 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005352 {
5353 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005354 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005355 if (*p == NUL) // stop at NUL after CTRL-V
5356 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005357 }
5358#endif
5359
Bram Moolenaareda29c92022-10-02 12:59:00 +01005360 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005361 // :@" and :*" do not start a comment!
5362 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005363 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005364 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005365 && !(eap->argt & EX_NOTRLCOM)
5366 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5367 || p != eap->arg)
5368 && (eap->cmdidx != CMD_redir
5369 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005370 || (*p == '#'
5371 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005372 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005373 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005374 || (*p == '|'
5375 && eap->cmdidx != CMD_append
5376 && eap->cmdidx != CMD_change
5377 && eap->cmdidx != CMD_insert)
5378 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
5380 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005381 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 * AND 'b' is present in 'cpoptions'.
5383 */
5384 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005385 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005387 if (!keep_backslash)
5388 {
5389 STRMOVE(p - 1, p); // remove the '\'
5390 --p;
5391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 }
5393 else
5394 {
5395 eap->nextcmd = check_nextcmd(p);
5396 *p = NUL;
5397 break;
5398 }
5399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005401
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005402 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 del_trailing_spaces(eap->arg);
5404}
5405
5406/*
5407 * get + command from ex argument
5408 */
5409 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005410getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 char_u *arg = *argp;
5413 char_u *command = NULL;
5414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005415 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
5417 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005418 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 command = dollar_command;
5420 else
5421 {
5422 command = arg;
5423 arg = skip_cmd_arg(command, TRUE);
5424 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005425 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 }
5427
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005428 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 *argp = arg;
5430 }
5431 return command;
5432}
5433
5434/*
5435 * Find end of "+command" argument. Skip over "\ " and "\\".
5436 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005437 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005438skip_cmd_arg(
5439 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005440 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 while (*p && !vim_isspace(*p))
5443 {
5444 if (*p == '\\' && p[1] != NUL)
5445 {
5446 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005447 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 else
5449 ++p;
5450 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005451 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
5453 return p;
5454}
5455
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005456 int
5457get_bad_opt(char_u *p, exarg_T *eap)
5458{
5459 if (STRICMP(p, "keep") == 0)
5460 eap->bad_char = BAD_KEEP;
5461 else if (STRICMP(p, "drop") == 0)
5462 eap->bad_char = BAD_DROP;
5463 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5464 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005465 else
5466 return FAIL;
5467 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005468}
5469
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005471 * Function given to ExpandGeneric() to obtain the list of bad= names.
5472 */
5473 static char_u *
5474get_bad_name(expand_T *xp UNUSED, int idx)
5475{
5476 // Note: Keep this in sync with getargopt.
5477 static char *(p_bad_values[]) =
5478 {
5479 "?",
5480 "keep",
5481 "drop",
5482 };
5483
John Marriotted908f72024-04-18 22:46:56 +02005484 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5485 return NULL;
5486
5487 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005488}
5489
5490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 * Get "++opt=arg" argument.
5492 * Return FAIL or OK.
5493 */
5494 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005495getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
5497 char_u *arg = eap->arg + 2;
5498 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005499 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005502 // Note: Keep this in sync with get_argopt_name.
5503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005504 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5506 {
5507 if (*arg == 'n')
5508 {
5509 arg += 2;
5510 eap->force_bin = FORCE_NOBIN;
5511 }
5512 else
5513 eap->force_bin = FORCE_BIN;
5514 if (!checkforcmd(&arg, "binary", 3))
5515 return FAIL;
5516 eap->arg = skipwhite(arg);
5517 return OK;
5518 }
5519
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005520 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005521 if (STRNCMP(arg, "edit", 4) == 0)
5522 {
5523 eap->read_edit = TRUE;
5524 eap->arg = skipwhite(arg + 4);
5525 return OK;
5526 }
5527
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 if (STRNCMP(arg, "ff", 2) == 0)
5529 {
5530 arg += 2;
5531 pp = &eap->force_ff;
5532 }
5533 else if (STRNCMP(arg, "fileformat", 10) == 0)
5534 {
5535 arg += 10;
5536 pp = &eap->force_ff;
5537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 else if (STRNCMP(arg, "enc", 3) == 0)
5539 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005540 if (STRNCMP(arg, "encoding", 8) == 0)
5541 arg += 8;
5542 else
5543 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 pp = &eap->force_enc;
5545 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005546 else if (STRNCMP(arg, "bad", 3) == 0)
5547 {
5548 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005549 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
5552 if (pp == NULL || *arg != '=')
5553 return FAIL;
5554
5555 ++arg;
5556 *pp = (int)(arg - eap->cmd);
5557 arg = skip_cmd_arg(arg, FALSE);
5558 eap->arg = skipwhite(arg);
5559 *arg = NUL;
5560
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 if (pp == &eap->force_ff)
5562 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5564 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005565 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005567 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005569 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5571 *p = TOLOWER_ASC(*p);
5572 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005573 else
5574 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005575 // Check ++bad= argument. Must be a single-byte character, "keep" or
5576 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005577 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005578 return FAIL;
5579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580
5581 return OK;
5582}
5583
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005584/*
5585 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5586 */
5587 static char_u *
5588get_argopt_name(expand_T *xp UNUSED, int idx)
5589{
5590 // Note: Keep this in sync with getargopt.
5591 static char *(p_opt_values[]) =
5592 {
5593 "fileformat=",
5594 "encoding=",
5595 "binary",
5596 "nobinary",
5597 "bad=",
5598 "edit",
5599 };
5600
John Marriotted908f72024-04-18 22:46:56 +02005601 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5602 return NULL;
5603
5604 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005605}
5606
5607/*
5608 * Command-line expansion for ++opt=name.
5609 */
5610 int
5611expand_argopt(
5612 char_u *pat,
5613 expand_T *xp,
5614 regmatch_T *rmp,
5615 char_u ***matches,
5616 int *numMatches)
5617{
5618 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5619 {
5620 char_u *(*cb)(expand_T *, int) = NULL;
5621
5622 char_u *name_end = xp->xp_pattern - 1;
5623 if (name_end - xp->xp_line >= 2
5624 && STRNCMP(name_end - 2, "ff", 2) == 0)
5625 cb = get_fileformat_name;
5626 else if (name_end - xp->xp_line >= 10
5627 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5628 cb = get_fileformat_name;
5629 else if (name_end - xp->xp_line >= 3
5630 && STRNCMP(name_end - 3, "enc", 3) == 0)
5631 cb = get_encoding_name;
5632 else if (name_end - xp->xp_line >= 8
5633 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5634 cb = get_encoding_name;
5635 else if (name_end - xp->xp_line >= 3
5636 && STRNCMP(name_end - 3, "bad", 3) == 0)
5637 cb = get_bad_name;
5638
5639 if (cb != NULL)
5640 {
5641 return ExpandGeneric(
5642 pat,
5643 xp,
5644 rmp,
5645 matches,
5646 numMatches,
5647 cb,
5648 FALSE);
5649 }
5650 return FAIL;
5651 }
5652
5653 // Special handling of "ff" which acts as a short form of
5654 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005655 if (xp->xp_pattern_len == 2
5656 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005657 {
5658 *matches = ALLOC_MULT(char_u *, 1);
5659 if (*matches == NULL)
5660 return FAIL;
5661 *numMatches = 1;
5662 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5663 return OK;
5664 }
5665
5666 return ExpandGeneric(
5667 pat,
5668 xp,
5669 rmp,
5670 matches,
5671 numMatches,
5672 get_argopt_name,
5673 FALSE);
5674}
5675
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005677ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678{
5679 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005680 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 * directory for security reasons.
5682 */
5683 if (secure)
5684 {
5685 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005686 eap->errmsg =
5687 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005690 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 else
5692 do_augroup(eap->arg, eap->forceit);
5693}
5694
5695/*
5696 * ":doautocmd": Apply the automatic commands to the current buffer.
5697 */
5698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005699ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005701 char_u *arg = eap->arg;
5702 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005703 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005704
Bram Moolenaar1610d052016-06-09 22:53:01 +02005705 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005706 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005707 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005708 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711/*
5712 * :[N]bunload[!] [N] [bufname] unload buffer
5713 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5714 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5715 */
5716 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005717ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005719 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005720 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 eap->errmsg = do_bufdel(
5722 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5723 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5724 : DOBUF_UNLOAD, eap->arg,
5725 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5726}
5727
5728/*
5729 * :[N]buffer [N] to buffer N
5730 * :[N]sbuffer [N] to buffer N
5731 */
5732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005733ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005735 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005736 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005738 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 else
5740 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005741 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5743 else
5744 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
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/*
5751 * :[N]bmodified [N] to next mod. buffer
5752 * :[N]sbmodified [N] to next mod. buffer
5753 */
5754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005755ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
5757 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005758 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005759 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760}
5761
5762/*
5763 * :[N]bnext [N] to next buffer
5764 * :[N]sbnext [N] split and to next buffer
5765 */
5766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005767ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005769 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005770 return;
5771
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005773 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005774 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775}
5776
5777/*
5778 * :[N]bNext [N] to previous buffer
5779 * :[N]bprevious [N] to previous buffer
5780 * :[N]sbNext [N] split and to previous buffer
5781 * :[N]sbprevious [N] split and to previous buffer
5782 */
5783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005784ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005786 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005787 return;
5788
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005790 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005791 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792}
5793
5794/*
5795 * :brewind to first buffer
5796 * :bfirst to first buffer
5797 * :sbrewind split and to first buffer
5798 * :sbfirst split and to first buffer
5799 */
5800 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005801ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005803 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005804 return;
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005807 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005808 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809}
5810
5811/*
5812 * :blast to last buffer
5813 * :sblast split and to last buffer
5814 */
5815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005816ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005818 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005819 return;
5820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005822 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005823 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
Bram Moolenaar7a092242020-04-16 22:10:49 +02005826/*
5827 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005828 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005831ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005833 int comment_char = '"';
5834
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005835 if (in_vim9script())
5836 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005837 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005838}
5839
5840/*
5841 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5842 * to "cmd_start" or has a white space character before it.
5843 */
5844 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005845ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005846{
5847 int c = *cmd;
5848
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005849 if (c == NUL || c == '|' || c == '\n')
5850 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005851 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005852 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5853 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005854 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005855 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856}
5857
5858#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5859 || defined(PROTO)
5860/*
5861 * Return the next command, after the first '|' or '\n'.
5862 * Return NULL if not found.
5863 */
5864 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005865find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
5867 while (*p != '|' && *p != '\n')
5868 {
5869 if (*p == NUL)
5870 return NULL;
5871 ++p;
5872 }
5873 return (p + 1);
5874}
5875#endif
5876
5877/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005878 * Check if *p is a separator between Ex commands, skipping over white space.
5879 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 */
5881 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005882check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005884 char_u *s = skipwhite(p);
5885
5886 if (*s == '|' || *s == '\n')
5887 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 else
5889 return NULL;
5890}
5891
5892/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005893 * If "eap->nextcmd" is not set, check for a next command at "p".
5894 */
5895 void
5896set_nextcmd(exarg_T *eap, char_u *arg)
5897{
5898 char_u *p = check_nextcmd(arg);
5899
5900 if (eap->nextcmd == NULL)
5901 eap->nextcmd = p;
5902 else if (p != NULL)
5903 // cannot use "| command" inside a {} block
5904 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5905}
5906
5907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 * - if there are more files to edit
5909 * - and this is the last window
5910 * - and forceit not used
5911 * - and not repeated twice on a row
5912 * return FAIL and give error message if 'message' TRUE
5913 * return OK otherwise
5914 */
5915 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005916check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005917 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005918 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919{
5920 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5921
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005922 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005923 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 {
5925 if (message)
5926 {
5927#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005928 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5929 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005931 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005933 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5934 NGETTEXT("%d more file to edit. Quit anyway?",
5935 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5937 return OK;
5938 return FAIL;
5939 }
5940#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005941 semsg(NGETTEXT(e_nr_more_file_to_edit,
5942 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005943 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 }
5945 return FAIL;
5946 }
5947 return OK;
5948}
5949
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950/*
5951 * Function given to ExpandGeneric() to obtain the list of command names.
5952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005954get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005957 return expand_user_command_name(idx);
Christian Brabandt70a11a62024-07-26 19:13:55 +02005958 // the following are no real commands
5959 if (STRNCMP(cmdnames[idx].cmd_name, "{", 1) == 0 ||
5960 STRNCMP(cmdnames[idx].cmd_name, "}", 1) == 0)
5961 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 return cmdnames[idx].cmd_name;
5963}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005966ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967{
Bram Moolenaare6850792010-05-14 15:28:44 +02005968 if (*eap->arg == NUL)
5969 {
5970#ifdef FEAT_EVAL
5971 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5972 char_u *p = NULL;
5973
5974 if (expr != NULL)
5975 {
5976 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005977 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005978 --emsg_off;
5979 vim_free(expr);
5980 }
5981 if (p != NULL)
5982 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005983 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005984 vim_free(p);
5985 }
5986 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005987 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005988#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005989 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005990#endif
5991 }
5992 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005993 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005994
5995#ifdef FEAT_VTP
5996 else if (has_vtp_working())
5997 {
5998 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005999 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006000 redrawcmd();
6001 }
6002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003}
6004
6005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006006ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
6008 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006009 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 do_highlight(eap->arg, eap->forceit, FALSE);
6011}
6012
6013
6014/*
6015 * Call this function if we thought we were going to exit, but we won't
6016 * (because of an error). May need to restore the terminal mode.
6017 */
6018 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006019not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020{
6021 exiting = FALSE;
6022 settmode(TMODE_RAW);
6023}
6024
Bram Moolenaar3552e742021-05-29 12:21:58 +02006025 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006026before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6027{
6028 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6029
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006030 // Bail out when autocommands closed the window.
6031 // Refuse to quit when the buffer in the last window is being closed (can
6032 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006033 if (!win_valid(wp)
6034 || curbuf_locked()
6035 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6036 return TRUE;
6037
6038 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6039 {
6040 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006041 // Refuse to quit when locked or when the window was closed or the
6042 // buffer in the last window is being closed (can only happen in
6043 // autocommands).
6044 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006045 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6046 return TRUE;
6047 }
6048
6049 return FALSE;
6050}
6051
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006053 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006054 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006055 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006057 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006058ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006060 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006061
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 if (cmdwin_type != 0)
6063 {
6064 cmdwin_result = Ctrl_C;
6065 return;
6066 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006067 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006068 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006069 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006070 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006071 return;
6072 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006073 if (eap->addr_count > 0)
6074 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006075 int wnr = eap->line2;
6076
6077 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6078 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006079 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006080 }
6081 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006082 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006083
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006084 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006085 if (curbuf_locked())
6086 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006087
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006088 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006089 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006090 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
6092#ifdef FEAT_NETBEANS_INTG
6093 netbeansForcedQuit = eap->forceit;
6094#endif
6095
6096 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006097 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 */
6099 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6100 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006101 if ((!buf_hide(wp->w_buffer)
6102 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006103 | (eap->forceit ? CCGD_FORCEIT : 0)
6104 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006106 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
6108 not_exiting();
6109 }
6110 else
6111 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006112 // quit last window
6113 // Note: only_one_window() returns true, even so a help window is
6114 // still open. In that case only quit, if no address has been
6115 // specified. Example:
6116 // :h|wincmd w|1q - don't quit
6117 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006118 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006120 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006121#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006123#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006124 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006125 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 }
6127}
6128
6129/*
6130 * ":cquit".
6131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006133ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006135 // this does not always pass on the exit code to the Manx compiler. why?
6136 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137}
6138
6139/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006140 * Do preparations for "qall" and "wqall".
6141 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006143 int
6144before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (cmdwin_type != 0)
6147 {
6148 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006149 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 else
6151 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006152 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006155 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006156 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006157 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006158 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006159 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006160 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006161
6162 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006163 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006164
Bram Moolenaar411da642023-05-10 16:53:27 +01006165 return OK;
6166}
6167
6168/*
6169 * ":qall": try to quit all windows
6170 */
6171 static void
6172ex_quit_all(exarg_T *eap)
6173{
6174 if (before_quit_all(eap) == FAIL)
6175 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006177 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 getout(0);
6179 not_exiting();
6180}
6181
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182/*
6183 * ":close": close current window, unless it is the last one
6184 */
6185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006186ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006188 win_T *win;
6189 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006191 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006193 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006194 {
6195 if (eap->addr_count == 0)
6196 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006197 else
6198 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006199 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006200 {
6201 winnr++;
6202 if (winnr == eap->line2)
6203 break;
6204 }
6205 if (win == NULL)
6206 win = lastwin;
6207 ex_win_close(eap->forceit, win, NULL);
6208 }
6209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210}
6211
Bram Moolenaar4033c552017-09-16 20:54:51 +02006212#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006213/*
6214 * ":pclose": Close any preview window.
6215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006217ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006218{
6219 win_T *win;
6220
Bram Moolenaar79648732019-07-18 21:43:07 +02006221 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006222 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006223 if (win->w_p_pvw)
6224 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006225 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006226 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006227 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006228# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006229 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006230 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006231# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006233#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006234
Bram Moolenaarf740b292006-02-16 22:11:02 +00006235/*
6236 * Close window "win" and take care of handling closing the last window for a
6237 * modified buffer.
6238 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006239 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006240ex_win_close(
6241 int forceit,
6242 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006243 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244{
6245 int need_hide;
6246 buf_T *buf = win->w_buffer;
6247
Bram Moolenaarcf844172020-06-26 19:44:06 +02006248 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006249 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006250 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006251 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006252 return;
6253 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006254 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006255 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006258 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006260#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006261 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006263# ifdef FEAT_TERMINAL
6264 if (term_job_running(buf->b_term))
6265 {
6266 if (term_confirm_stop(buf) == FAIL)
6267 return;
6268 // Manually kill the terminal here because this command will
6269 // hide it otherwise.
6270 free_terminal(buf);
6271 need_hide = FALSE;
6272 }
6273 else
6274# endif
6275 {
6276 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006277
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006278 set_bufref(&bufref, buf);
6279 dialog_changed(buf, FALSE);
6280 if (bufref_valid(&bufref) && bufIsChanged(buf))
6281 return;
6282 need_hide = FALSE;
6283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 }
6285 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006288 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 return;
6290 }
6291 }
6292
Bram Moolenaar4033c552017-09-16 20:54:51 +02006293#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006295#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006296
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006297 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006298 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006299 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006300 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006301 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302}
6303
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006305 * Handle the argument for a tabpage related ex command.
6306 * Returns a tabpage number.
6307 * When an error is encountered then eap->errmsg is set.
6308 */
6309 static int
6310get_tabpage_arg(exarg_T *eap)
6311{
6312 int tab_number;
6313 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6314
6315 if (eap->arg && *eap->arg != NUL)
6316 {
6317 char_u *p = eap->arg;
6318 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006319 int relative = 0; // argument +N/-N means: go to N places to the
6320 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006321
6322 if (*p == '-')
6323 {
6324 relative = -1;
6325 p++;
6326 }
6327 else if (*p == '+')
6328 {
6329 relative = 1;
6330 p++;
6331 }
6332
6333 p_save = p;
6334 tab_number = getdigits(&p);
6335
6336 if (relative == 0)
6337 {
6338 if (STRCMP(p, "$") == 0)
6339 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006340 else if (STRCMP(p, "#") == 0)
6341 if (valid_tabpage(lastused_tabpage))
6342 tab_number = tabpage_index(lastused_tabpage);
6343 else
6344 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006345 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6346 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006347 tab_number = 0;
6348 goto theend;
6349 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006350 else if (p == p_save || *p_save == '-' || *p != NUL
6351 || tab_number > LAST_TAB_NR)
6352 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006353 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006354 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006355 goto theend;
6356 }
6357 }
6358 else
6359 {
6360 if (*p_save == NUL)
6361 tab_number = 1;
6362 else if (p == p_save || *p_save == '-' || *p != NUL
6363 || tab_number == 0)
6364 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006365 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006366 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006367 goto theend;
6368 }
6369 tab_number = tab_number * relative + tabpage_index(curtab);
6370 if (!unaccept_arg0 && relative == -1)
6371 --tab_number;
6372 }
6373 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006374 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006375 }
6376 else if (eap->addr_count > 0)
6377 {
6378 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006379 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006380 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006381 tab_number = 0;
6382 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006383 else
6384 {
6385 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006386 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006387 {
zeertzjq076faac2024-03-25 16:41:06 +01006388 char_u *cmdp = eap->cmd;
6389
6390 while (--cmdp > *eap->cmdlinep
6391 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6392 ;
6393 if (*cmdp == '-')
6394 {
6395 --tab_number;
6396 if (tab_number < unaccept_arg0)
6397 eap->errmsg = _(e_invalid_range);
6398 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006399 }
6400 }
6401 }
6402 else
6403 {
6404 switch (eap->cmdidx)
6405 {
6406 case CMD_tabnext:
6407 tab_number = tabpage_index(curtab) + 1;
6408 if (tab_number > LAST_TAB_NR)
6409 tab_number = 1;
6410 break;
6411 case CMD_tabmove:
6412 tab_number = LAST_TAB_NR;
6413 break;
6414 default:
6415 tab_number = tabpage_index(curtab);
6416 }
6417 }
6418
6419theend:
6420 return tab_number;
6421}
6422
6423/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006424 * ":tabclose": close current tab page, unless it is the last one.
6425 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 */
6427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006428ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006430 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006431 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006433 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006434 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006435 cmdwin_result = K_IGNORE;
6436 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006437 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006438
6439 if (first_tabpage->tp_next == NULL)
6440 {
6441 emsg(_(e_cannot_close_last_tab_page));
6442 return;
6443 }
6444
6445 if (window_layout_locked(CMD_tabclose))
6446 return;
6447
6448 tab_number = get_tabpage_arg(eap);
6449 if (eap->errmsg != NULL)
6450 return;
6451
6452 tp = find_tabpage(tab_number);
6453 if (tp == NULL)
6454 {
6455 beep_flush();
6456 return;
6457 }
6458 if (tp != curtab)
6459 {
6460 tabpage_close_other(tp, eap->forceit);
6461 return;
6462 }
6463 else if (!text_locked() && !curbuf_locked())
6464 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006465}
6466
6467/*
6468 * ":tabonly": close all tab pages except the current one
6469 */
6470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006471ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006472{
6473 tabpage_T *tp;
6474 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006475 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006476
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006477 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006478 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006479 cmdwin_result = K_IGNORE;
6480 return;
6481 }
6482
6483 if (first_tabpage->tp_next == NULL)
6484 {
6485 msg(_("Already only one tab page"));
6486 return;
6487 }
6488
6489 if (window_layout_locked(CMD_tabonly))
6490 return;
6491
6492 tab_number = get_tabpage_arg(eap);
6493 if (eap->errmsg != NULL)
6494 return;
6495
6496 goto_tabpage(tab_number);
6497 // Repeat this up to a 1000 times, because autocommands may
6498 // mess up the lists.
6499 for (done = 0; done < 1000; ++done)
6500 {
6501 FOR_ALL_TABPAGES(tp)
6502 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006503 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006504 tabpage_close_other(tp, eap->forceit);
6505 // if we failed to close it quit
6506 if (valid_tabpage(tp))
6507 done = 1000;
6508 // start over, "tp" is now invalid
6509 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006510 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006511 if (first_tabpage->tp_next == NULL)
6512 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
6516/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006517 * Close the current tab page.
6518 */
6519 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006520tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006521{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006522 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006523 return;
6524
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006525 // First close all the windows but the current one. If that worked then
6526 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006527 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006528 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006529 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006530 ex_win_close(forceit, curwin, NULL);
6531# ifdef FEAT_GUI
6532 need_mouse_correct = TRUE;
6533# endif
6534}
6535
6536/*
6537 * Close tab page "tp", which is not the current tab page.
6538 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006539 * Also takes care of the tab pages line disappearing when closing the
6540 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006541 */
6542 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006543tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006544{
6545 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006546 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006547
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006548 // Limit to 1000 windows, autocommands may add a window while we close
6549 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006550 while (++done < 1000)
6551 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006552 wp = tp->tp_firstwin;
6553 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006555 // Autocommands may delete the tab page under our fingers and we may
6556 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006557 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006558 break;
6559 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006560
Bram Moolenaar29323592016-07-24 22:04:11 +02006561 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006562}
6563
6564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 * ":only".
6566 */
6567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006568ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006570 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006571 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572# ifdef FEAT_GUI
6573 need_mouse_correct = TRUE;
6574# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006575 if (eap->addr_count > 0)
6576 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006577 win_T *wp;
6578 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006579 for (wp = firstwin; --wnr > 0; )
6580 {
6581 if (wp->w_next == NULL)
6582 break;
6583 else
6584 wp = wp->w_next;
6585 }
6586 win_goto(wp);
6587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 close_others(TRUE, eap->forceit);
6589}
6590
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006592ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006594 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006595 if (eap->skip)
6596 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006597
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006598 if (window_layout_locked(CMD_hide))
6599 return;
6600#ifdef FEAT_GUI
6601 need_mouse_correct = TRUE;
6602#endif
6603 if (eap->addr_count == 0)
6604 win_close(curwin, FALSE); // don't free buffer
6605 else
6606 {
6607 int winnr = 0;
6608 win_T *win;
6609
6610 FOR_ALL_WINDOWS(win)
6611 {
6612 winnr++;
6613 if (winnr == eap->line2)
6614 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006616 if (win == NULL)
6617 win = lastwin;
6618 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 }
6620}
6621
6622/*
6623 * ":stop" and ":suspend": Suspend Vim.
6624 */
dbivolaruab16ad32021-12-29 19:41:47 +00006625 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006626ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
6628 /*
6629 * Disallow suspending for "rvim".
6630 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006631 if (check_restricted())
6632 return;
6633
6634 if (!eap->forceit)
6635 autowrite_all();
6636 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6637 windgoto((int)Rows - 1, 0);
6638 out_char('\n');
6639 out_flush();
6640 stoptermcap();
6641 out_flush(); // needed for SUN to restore xterm buffer
6642 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6643 ui_suspend(); // call machine specific function
6644 maketitle();
6645 resettitle(); // force updating the title
6646 starttermcap();
6647 scroll_start(); // scroll screen before redrawing
6648 redraw_later_clear();
6649 shell_resized(); // may have resized window
6650 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651}
6652
6653/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006654 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 */
6656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006657ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006659#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006660 if (not_in_vim9(eap) == FAIL)
6661 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 if (cmdwin_type != 0)
6664 {
6665 cmdwin_result = Ctrl_C;
6666 return;
6667 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006668 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006669 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006670 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006671 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006672 return;
6673 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006674
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006676 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 */
6678 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6679 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006680
6681 // Write the buffer for ":wq" or when it was changed.
6682 // Trigger QuitPre and ExitPre.
6683 // Check if we can exit now, after autocommands have changed things.
6684 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6685 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006687 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {
6689 not_exiting();
6690 }
6691 else
6692 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006693 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006695 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696# ifdef FEAT_GUI
6697 need_mouse_correct = TRUE;
6698# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006699 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006700 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 }
6702}
6703
6704/*
6705 * ":print", ":list", ":number".
6706 */
6707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006708ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006710 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006711 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006712 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006714 for ( ;!got_int; ui_breakcheck())
6715 {
6716 print_line(eap->line1,
6717 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6718 || (eap->flags & EXFLAG_NR)),
6719 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6720 if (++eap->line1 > eap->line2)
6721 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006722 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006723 }
6724 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006725 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006726 curwin->w_cursor.lnum = eap->line2;
6727 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 }
6729
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731}
6732
6733#ifdef FEAT_BYTEOFF
6734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006735ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
6737 goto_byte(eap->line2);
6738}
6739#endif
6740
6741/*
6742 * ":shell".
6743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006745ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
6747 do_shell(NULL, 0);
6748}
6749
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006750#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006752static int drop_busy = FALSE;
6753static int drop_filec;
6754static char_u **drop_filev = NULL;
6755static int drop_split;
6756static void (*drop_callback)(void *);
6757static void *drop_cookie;
6758
6759 static void
6760handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
6762 exarg_T ea;
6763 int save_msg_scroll = msg_scroll;
6764
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006765 // Setting the argument list may cause screen updates and being called
6766 // recursively. Avoid that by setting drop_busy.
6767 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006769 // Check whether the current buffer is changed. If so, we will need
6770 // to split the current window or data could be lost.
6771 // We don't need to check if the 'hidden' option is set, as in this
6772 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006773 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {
6775 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006776 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 --emsg_off;
6778 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006779 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 if (win_split(0, 0) == FAIL)
6782 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006783 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006785 // When splitting the window, create a new alist. Otherwise the
6786 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 alist_unlink(curwin->w_alist);
6788 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 }
6790
6791 /*
6792 * Set up the new argument list.
6793 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006794 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
6796 /*
6797 * Move to the first file.
6798 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006799 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006800 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 ea.cmd = (char_u *)"next";
6802 do_argfile(&ea, 0);
6803
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006804 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6805 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 need_start_insertmode = FALSE;
6807
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006808 // Restore msg_scroll, otherwise a following command may cause scrolling
6809 // unexpectedly. The screen will be redrawn by the caller, thus
6810 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006812
6813 if (drop_callback != NULL)
6814 drop_callback(drop_cookie);
6815
6816 drop_filev = NULL;
6817 drop_busy = FALSE;
6818}
6819
6820/*
6821 * Handle a file drop. The code is here because a drop is *nearly* like an
6822 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006823 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006824 * code is very similar to :args and hence needs access to a lot of the static
6825 * functions in this file.
6826 *
6827 * The "filev" list must have been allocated using alloc(), as should each item
6828 * in the list. This function takes over responsibility for freeing the "filev"
6829 * list.
6830 */
6831 void
6832handle_drop(
6833 int filec, // the number of files dropped
6834 char_u **filev, // the list of files dropped
6835 int split, // force splitting the window
6836 void (*callback)(void *), // to be called after setting the argument
6837 // list
6838 void *cookie) // argument for "callback" (allocated)
6839{
6840 // Cannot handle recursive drops, finish the pending one.
6841 if (drop_busy)
6842 {
6843 FreeWild(filec, filev);
6844 vim_free(cookie);
6845 return;
6846 }
6847
6848 // When calling handle_drop() more than once in a row we only use the last
6849 // one.
6850 if (drop_filev != NULL)
6851 {
6852 FreeWild(drop_filec, drop_filev);
6853 vim_free(drop_cookie);
6854 }
6855
6856 drop_filec = filec;
6857 drop_filev = filev;
6858 drop_split = split;
6859 drop_callback = callback;
6860 drop_cookie = cookie;
6861
6862 // Postpone this when:
6863 // - editing the command line
6864 // - not possible to change the current buffer
6865 // - updating the screen
6866 // As it may change buffers and window structures that are in use and cause
6867 // freed memory to be used.
6868 if (text_locked() || curbuf_locked() || updating_screen)
6869 return;
6870
6871 handle_drop_internal();
6872}
6873
6874/*
6875 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6876 * reset: Handle a postponed drop.
6877 */
6878 void
6879handle_any_postponed_drop(void)
6880{
6881 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006882 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006883 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884}
6885#endif
6886
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 * ":preserve".
6889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006891ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006893 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 ml_preserve(curbuf, TRUE);
6895}
6896
6897/*
6898 * ":recover".
6899 */
6900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006901ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006903 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006905 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6906 | CCGD_MULTWIN
6907 | (eap->forceit ? CCGD_FORCEIT : 0)
6908 | CCGD_EXCMD)
6909
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 && (*eap->arg == NUL
6911 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006912 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 recoverymode = FALSE;
6914}
6915
6916/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006917 * Command modifier used in a wrong way. Also for other commands that can't
6918 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 */
6920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006921ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006923 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924}
6925
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006926#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006927/*
6928 * Evaluate the 'findexpr' expression and return the result. When evaluating
6929 * the expression, v:fname is set to the ":find" command argument.
6930 */
6931 static list_T *
zeertzjq20e045f2024-10-28 22:05:26 +01006932eval_findexpr(char_u *pat, int cmdcomplete)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006933{
6934 sctx_T saved_sctx = current_sctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006935 char_u *findexpr;
6936 char_u *arg;
6937 typval_T tv;
6938 list_T *retlist = NULL;
6939
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006940 findexpr = get_findexpr();
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006941
zeertzjq20e045f2024-10-28 22:05:26 +01006942 set_vim_var_string(VV_FNAME, pat, -1);
6943 set_vim_var_nr(VV_CMDCOMPLETE, cmdcomplete ? VVAL_TRUE : VVAL_FALSE);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006944 current_sctx = curbuf->b_p_script_ctx[BV_FEXPR];
6945
6946 arg = skipwhite(findexpr);
6947
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006948 ++textlock;
6949
6950 // Evaluate the expression. If the expression is "FuncName()" call the
6951 // function directly.
6952 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
6953 retlist = NULL;
6954 else
6955 {
6956 if (tv.v_type == VAR_LIST)
6957 retlist = list_copy(tv.vval.v_list, TRUE, TRUE, get_copyID());
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006958 else
6959 emsg(_(e_invalid_return_type_from_findexpr));
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006960 clear_tv(&tv);
6961 }
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006962 --textlock;
6963 clear_evalarg(&EVALARG_EVALUATE, NULL);
6964
6965 set_vim_var_string(VV_FNAME, NULL, 0);
zeertzjq20e045f2024-10-28 22:05:26 +01006966 set_vim_var_nr(VV_CMDCOMPLETE, VVAL_FALSE);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006967 current_sctx = saved_sctx;
6968
6969 return retlist;
6970}
6971
6972/*
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006973 * Find file names matching "pat" using 'findexpr' and return it in "files".
6974 * Used for expanding the :find, :sfind and :tabfind command argument.
6975 * Returns OK on success and FAIL otherwise.
6976 */
6977 int
6978expand_findexpr(char_u *pat, char_u ***files, int *numMatches)
6979{
6980 list_T *l;
6981 int len;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006982
6983 *numMatches = 0;
6984 *files = NULL;
6985
zeertzjq20e045f2024-10-28 22:05:26 +01006986 l = eval_findexpr(pat, TRUE);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006987
6988 if (l == NULL)
6989 return FAIL;
6990
6991 len = list_len(l);
6992 if (len == 0) // empty List
6993 return FAIL;
6994
6995 *files = ALLOC_MULT(char_u *, len);
6996 if (*files == NULL)
6997 return FAIL;
6998
6999 // Copy all the List items
7000 listitem_T *li;
7001 int idx = 0;
7002 FOR_ALL_LIST_ITEMS(l, li)
7003 {
7004 if (li->li_tv.v_type == VAR_STRING)
7005 {
7006 (*files)[idx] = vim_strsave(li->li_tv.vval.v_string);
7007 idx++;
7008 }
7009 }
7010
7011 *numMatches = idx;
7012 list_free(l);
7013
7014 return OK;
7015}
7016
7017/*
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007018 * Use 'findexpr' to find file 'findarg'. The 'count' argument is used to find
7019 * the n'th matching file.
7020 */
7021 static char_u *
7022findexpr_find_file(char_u *findarg, int findarg_len, int count)
7023{
7024 list_T *fname_list;
7025 char_u *ret_fname = NULL;
7026 char_u cc;
7027 int fname_count;
7028
7029 cc = findarg[findarg_len];
7030 findarg[findarg_len] = NUL;
7031
zeertzjq20e045f2024-10-28 22:05:26 +01007032 fname_list = eval_findexpr(findarg, FALSE);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007033 fname_count = list_len(fname_list);
7034
7035 if (fname_count == 0)
7036 semsg(_(e_cant_find_file_str_in_path), findarg);
7037 else
7038 {
7039 if (count > fname_count)
7040 semsg(_(e_no_more_file_str_found_in_path), findarg);
7041 else
7042 {
7043 listitem_T *li = list_find(fname_list, count - 1);
7044 if (li != NULL && li->li_tv.v_type == VAR_STRING)
7045 ret_fname = vim_strsave(li->li_tv.vval.v_string);
7046 }
7047 }
7048
7049 if (fname_list != NULL)
7050 list_free(fname_list);
7051
7052 findarg[findarg_len] = cc;
7053
7054 return ret_fname;
7055}
7056#endif
7057
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058/*
7059 * :sview [+command] file split window with new file, read-only
7060 * :split [[+command] file] split window with current or new file
7061 * :vsplit [[+command] file] split window vertically with current or new file
7062 * :new [[+command] file] split window with no or new file
7063 * :vnew [[+command] file] split vertically window with no or new file
7064 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007065 *
7066 * :tabedit open new Tab page with empty window
7067 * :tabedit [+command] file open new Tab page and edit "file"
7068 * :tabnew [[+command] file] just like :tabedit
7069 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 */
7071 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007072ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007074 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007076#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007077 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02007078 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007079#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02007080 int use_tab = eap->cmdidx == CMD_tabedit
7081 || eap->cmdidx == CMD_tabfind
7082 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007084 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007085 return;
7086
Bram Moolenaar4033c552017-09-16 20:54:51 +02007087#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090
Bram Moolenaar4033c552017-09-16 20:54:51 +02007091#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007092 // A ":split" in the quickfix window works like ":new". Don't want two
7093 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02007094 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {
7096 if (eap->cmdidx == CMD_split)
7097 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 if (eap->cmdidx == CMD_vsplit)
7099 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007103 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007105 char_u *file_to_find = NULL;
7106 char *search_ctx = NULL;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007107
7108 if (*get_findexpr() != NUL)
7109 {
7110#ifdef FEAT_EVAL
7111 fname = findexpr_find_file(eap->arg, (int)STRLEN(eap->arg),
7112 eap->addr_count > 0 ? eap->line2 : 1);
7113#endif
7114 }
7115 else
7116 {
7117 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7118 FNAME_MESS, TRUE, curbuf->b_ffname,
7119 &file_to_find, &search_ctx);
7120 vim_free(file_to_find);
7121 vim_findfile_cleanup(search_ctx);
7122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 if (fname == NULL)
7124 goto theend;
7125 eap->arg = fname;
7126 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007127# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007128 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 && eap->cmdidx != CMD_new)
7131 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007132 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007133# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007134 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007135# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007136 au_has_group((char_u *)"FileExplorer"))
7137 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007138 // No browsing supported but we do have the file explorer:
7139 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007140 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007141 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007142 }
7143 else
7144 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02007145 fname = do_browse(0, (char_u *)(use_tab
7146 ? _("Edit File in new tab page")
7147 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007149 if (fname == NULL)
7150 goto theend;
7151 eap->arg = fname;
7152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007154 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007157 /*
7158 * Either open new tab page or split the window.
7159 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007160 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007161 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007162 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007163 : eap->addr_count == 0 ? 0
7164 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007165 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007166 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007167
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007168 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007169 if (curwin != old_curwin
7170 && win_valid(old_curwin)
7171 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007172 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007173 old_curwin->w_alt_fnum = curbuf->b_fnum;
7174 }
7175 }
7176 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007179 // Reset 'scrollbind' when editing another file, but keep it when
7180 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007181 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007182 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 else
7184 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 do_exedit(eap, old_curwin);
7186 }
7187
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007188# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007189 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192theend:
7193 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007195
7196/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007197 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007198 */
7199 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007200tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007201{
7202 exarg_T ea;
7203
Bram Moolenaara80faa82020-04-12 19:37:17 +02007204 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007205 ea.cmdidx = CMD_tabnew;
7206 ea.cmd = (char_u *)"tabn";
7207 ea.arg = (char_u *)"";
7208 ex_splitview(&ea);
7209}
7210
7211/*
7212 * :tabnext command
7213 */
7214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007215ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007216{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007217 int tab_number;
7218
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007219 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007220 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007221 switch (eap->cmdidx)
7222 {
7223 case CMD_tabfirst:
7224 case CMD_tabrewind:
7225 goto_tabpage(1);
7226 break;
7227 case CMD_tablast:
7228 goto_tabpage(9999);
7229 break;
7230 case CMD_tabprevious:
7231 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007232 if (eap->arg && *eap->arg != NUL)
7233 {
7234 char_u *p = eap->arg;
7235 char_u *p_save = p;
7236
7237 tab_number = getdigits(&p);
7238 if (p == p_save || *p_save == '-' || *p != NUL
7239 || tab_number == 0)
7240 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007241 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007242 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007243 return;
7244 }
7245 }
7246 else
7247 {
7248 if (eap->addr_count == 0)
7249 tab_number = 1;
7250 else
7251 {
7252 tab_number = eap->line2;
7253 if (tab_number < 1)
7254 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007255 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007256 return;
7257 }
7258 }
7259 }
7260 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007261 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007262 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007263 tab_number = get_tabpage_arg(eap);
7264 if (eap->errmsg == NULL)
7265 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007266 break;
7267 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007268}
7269
7270/*
7271 * :tabmove command
7272 */
7273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007274ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007275{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007276 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007277
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007278 tab_number = get_tabpage_arg(eap);
7279 if (eap->errmsg == NULL)
7280 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007281}
7282
7283/*
7284 * :tabs command: List tabs and their contents.
7285 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007287ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007288{
7289 tabpage_T *tp;
7290 win_T *wp;
7291 int tabcount = 1;
7292
7293 msg_start();
7294 msg_scroll = TRUE;
7295 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7296 {
7297 msg_putchar('\n');
7298 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007299 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007300 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007301 ui_breakcheck();
7302
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007303 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007304 wp = firstwin;
7305 else
7306 wp = tp->tp_firstwin;
7307 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7308 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007309 msg_putchar('\n');
7310 msg_putchar(wp == curwin ? '>' : ' ');
7311 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007312 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7313 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007314 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007315 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007316 else
7317 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7318 IObuff, IOSIZE, TRUE);
7319 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007320 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007321 ui_breakcheck();
7322 }
7323 }
7324}
7325
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326/*
7327 * ":mode": Set screen mode.
7328 * If no argument given, just get the screen size and redraw.
7329 */
7330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007331ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
7333 if (*eap->arg == NUL)
7334 shell_resized();
7335 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007336 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337}
7338
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339/*
7340 * ":resize".
7341 * set, increment or decrement current window height
7342 */
7343 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007344ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345{
7346 int n;
7347 win_T *wp = curwin;
7348
7349 if (eap->addr_count > 0)
7350 {
7351 n = eap->line2;
7352 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7353 ;
7354 }
7355
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007356# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007360 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 {
7362 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007363 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007364 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007366 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 }
7368 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
7370 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007371 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007372 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007374 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 }
7376}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
7378/*
7379 * ":find [+command] <file>" command.
7380 */
7381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007382ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
Colin Kennedy21570352024-03-03 16:16:47 +01007384 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007385 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007386
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007387 char_u *fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007389 char_u *file_to_find = NULL;
7390 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007392 if (*get_findexpr() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 {
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007394#ifdef FEAT_EVAL
7395 fname = findexpr_find_file(eap->arg, (int)STRLEN(eap->arg),
7396 eap->addr_count > 0 ? eap->line2 : 1);
7397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 }
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007399 else
7400 {
7401 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7402 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
7403 if (eap->addr_count > 0)
7404 {
7405 // Repeat finding the file "count" times. This matters when it appears
7406 // several times in the path.
7407 count = eap->line2;
7408 while (fname != NULL && --count > 0)
7409 {
7410 vim_free(fname);
7411 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7412 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
7413 }
7414 }
7415 VIM_CLEAR(file_to_find);
7416 vim_findfile_cleanup(search_ctx);
7417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007419 if (fname == NULL)
7420 return;
7421
7422 eap->arg = fname;
7423 do_exedit(eap, NULL);
7424 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425}
7426
7427/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007428 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007429 */
7430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007431ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007432{
7433 regmatch_T regmatch;
7434 char_u *p;
7435
Bram Moolenaar65088802021-03-13 21:07:21 +01007436#ifdef FEAT_EVAL
7437 if (not_in_vim9(eap) == FAIL)
7438 return;
7439#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007440 curwin->w_cursor.lnum = eap->line2;
7441 beginline(BL_SOL | BL_FIX);
7442 if (*eap->arg == '/')
7443 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007444 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007445 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007446 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007447 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007448 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007449 if (regmatch.regprog != NULL)
7450 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007451 // make a copy of the line, when searching for a mark it might be
7452 // flushed
7453 char_u *line = vim_strsave(ml_get_curline());
7454
Bram Moolenaardf177f62005-02-22 08:39:57 +00007455 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007456 if (vim_regexec(&regmatch, line, (colnr_T)0))
7457 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007458 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007459 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007460 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007461 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007462 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007463 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007464 eap->arg += STRLEN(eap->arg);
7465 }
7466 check_cursor();
7467
7468 eap->cmdidx = CMD_visual;
7469 do_exedit(eap, NULL);
7470}
7471
7472/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007473 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 */
7475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007476ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007478 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7479
Colin Kennedy21570352024-03-03 16:16:47 +01007480 // Exclude commands which keep the window's current buffer
7481 if (
7482 eap->cmdidx != CMD_badd
7483 && eap->cmdidx != CMD_balt
7484 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007485 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7486 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007487 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007488
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 do_exedit(eap, NULL);
7490}
7491
7492/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007493 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007496do_exedit(
7497 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007498 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499{
7500 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007502 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007504 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7505 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007506 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 /*
7508 * ":vi" command ends Ex mode.
7509 */
7510 if (exmode_active && (eap->cmdidx == CMD_visual
7511 || eap->cmdidx == CMD_view))
7512 {
7513 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007514 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007516 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007517 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007518 if (global_busy)
7519 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007520 if (eap->nextcmd != NULL)
7521 {
7522 stuffReadbuff(eap->nextcmd);
7523 eap->nextcmd = NULL;
7524 }
7525
7526 if (exmode_was != EXMODE_VIM)
7527 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007528 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007529 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007530 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007531 no_wait_return = 0;
7532 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007533 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007534 msg_scroll = 0;
7535#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007536 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007537 hold_gui_events = 0;
7538#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007539 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007540 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007541
7542 main_loop(FALSE, TRUE);
7543
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007544 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007545 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007546 no_wait_return = save_nwr;
7547 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007548#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007549 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007550#endif
7551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 }
7555
7556 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007557 || eap->cmdidx == CMD_tabnew
7558 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007559 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007561 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 setpcmark();
7563 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007564 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7565 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007567 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 || *eap->arg != NUL
7569#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007570 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571#endif
7572 )
7573 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007574 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7575 // Only ":edit" or ":script" can bring us here, others are stopped
7576 // earlier.
7577 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007578 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007579
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 n = readonlymode;
7581 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7582 readonlymode = TRUE;
7583 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007584 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7585 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007586 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7587 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7589 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007590 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7592 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7593 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007594 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007596 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007597 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007598 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7599 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007600 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007602 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 if (old_curwin != NULL)
7604 {
7605 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007606 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007608#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007609 cleanup_T cs;
7610
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007611 // Reset the error/interrupt/exception state here so that
7612 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007613 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007614#endif
7615#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007617#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007618 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007619
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007620#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007621 // Restore the error/interrupt/exception state if not
7622 // discarded by a new aborting error, interrupt, or
7623 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007624 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 }
7627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 }
7629 else if (readonlymode && curbuf->b_nwindows == 1)
7630 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007631 // When editing an already visited buffer, 'readonly' won't be set
7632 // but the previous value is kept. With ":view" and ":sview" we
7633 // want the file to be readonly, except when another window is
7634 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 curbuf->b_p_ro = TRUE;
7636 }
7637 readonlymode = n;
7638 }
7639 else
7640 {
7641 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007642 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 if (n != curwin->w_arg_idx_invalid)
7646 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 }
7648
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 /*
7650 * if ":split file" worked, set alternate file name in old window to new
7651 * file
7652 */
7653 if (old_curwin != NULL
7654 && *eap->arg != NUL
7655 && curwin != old_curwin
7656 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007657 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007658 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660
7661 ex_no_reprint = TRUE;
7662}
7663
7664#ifndef FEAT_GUI
7665/*
7666 * ":gui" and ":gvim" when there is no GUI.
7667 */
7668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007669ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007671 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672}
7673#endif
7674
Bram Moolenaar4f974752019-02-17 17:44:42 +01007675#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007677ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679 gui_make_tearoff(eap->arg);
7680}
7681#endif
7682
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007683#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7684 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007686ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007688# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7689 if (gui.in_use)
7690 gui_make_popup(eap->arg, eap->forceit);
7691# ifdef FEAT_TERM_POPUP_MENU
7692 else
7693# endif
7694# endif
7695# ifdef FEAT_TERM_POPUP_MENU
7696 pum_make_popup(eap->arg, eap->forceit);
7697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698}
7699#endif
7700
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007702ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703{
7704 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007705 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007707 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708}
7709
7710/*
7711 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7712 * offset.
7713 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007716ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007719 win_T *save_curwin = curwin;
7720 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 long topline;
7722 long y;
7723 linenr_T old_linenr = curwin->w_cursor.lnum;
7724
7725 setpcmark();
7726
7727 /*
7728 * determine max topline
7729 */
7730 if (curwin->w_p_scb)
7731 {
7732 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007733 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 {
7735 if (wp->w_p_scb && wp->w_buffer)
7736 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007737 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 if (topline > y)
7739 topline = y;
7740 }
7741 }
7742 if (topline < 1)
7743 topline = 1;
7744 }
7745 else
7746 {
7747 topline = 1;
7748 }
7749
7750
7751 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007752 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007754 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {
7756 if (curwin->w_p_scb)
7757 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007758 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 y = topline - curwin->w_topline;
7760 if (y > 0)
7761 scrollup(y, TRUE);
7762 else
7763 scrolldown(-y, TRUE);
7764 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007765 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 }
7769 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007770 curwin = save_curwin;
7771 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 if (curwin->w_p_scb)
7773 {
7774 did_syncbind = TRUE;
7775 checkpcmark();
7776 if (old_linenr != curwin->w_cursor.lnum)
7777 {
7778 char_u ctrl_o[2];
7779
7780 ctrl_o[0] = Ctrl_O;
7781 ctrl_o[1] = 0;
7782 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7783 }
7784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785}
7786
7787
7788 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007789ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007791 int i;
7792 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7793 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007795 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007797 do_bang(1, eap, FALSE, FALSE, TRUE);
7798 return;
7799 }
7800
7801 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7802 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803
7804#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007805 if (cmdmod.cmod_flags & CMOD_BROWSE)
7806 {
7807 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007809 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7810 NULL, NULL, NULL, curbuf);
7811 if (browseFile != NULL)
7812 {
7813 i = readfile(browseFile, NULL,
7814 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7815 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 }
7817 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007818 i = OK;
7819 }
7820 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007822 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007824 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 return;
7826 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007827 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 }
7829 else
7830 {
7831 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7832 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7833 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007834 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835
7836 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007837 if (i != OK)
7838 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007839#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007840 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007842 semsg(_(e_cant_open_file_str), eap->arg);
7843 }
7844 else
7845 {
7846 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007847 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007848 // Delete the empty line that remains. Historically ex does
7849 // this but vi doesn't.
7850 if (eap->line2 == 0)
7851 lnum = curbuf->b_ml.ml_line_count;
7852 else
7853 lnum = 1;
7854 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007855 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007856 ml_delete(lnum);
7857 if (curwin->w_cursor.lnum > 1
7858 && curwin->w_cursor.lnum >= lnum)
7859 --curwin->w_cursor.lnum;
7860 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007861 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007862 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007863 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865}
7866
Bram Moolenaarea408852005-06-25 22:49:46 +00007867static char_u *prev_dir = NULL;
7868
7869#if defined(EXITFREE) || defined(PROTO)
7870 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007871free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007872{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007873 VIM_CLEAR(prev_dir);
7874 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007875}
7876#endif
7877
Bram Moolenaarf4258302013-06-02 18:20:17 +02007878/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007879 * Get the previous directory for the given chdir scope.
7880 */
7881 static char_u *
7882get_prevdir(cdscope_T scope)
7883{
7884 if (scope == CDSCOPE_WINDOW)
7885 return curwin->w_prevdir;
7886 else if (scope == CDSCOPE_TABPAGE)
7887 return curtab->tp_prevdir;
7888 return prev_dir;
7889}
7890
7891/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007892 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007893 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7894 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007895 */
7896 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007897post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007898{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007899 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007900 // Clear tab local directory for both :cd and :tcd
7901 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007902 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007903 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007904 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007905 char_u *pdir = get_prevdir(scope);
7906
7907 // If still in the global directory, need to remember current
7908 // directory as the global directory.
7909 if (globaldir == NULL && pdir != NULL)
7910 globaldir = vim_strsave(pdir);
7911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007912 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007913 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007914 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007915 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007916 curtab->tp_localdir = vim_strsave(NameBuff);
7917 else
7918 curwin->w_localdir = vim_strsave(NameBuff);
7919 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007920 }
7921 else
7922 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007923 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007924 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007925 }
7926
zeertzjq64be6aa2021-11-19 11:59:08 +00007927 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007928 shorten_fnames(TRUE);
7929}
7930
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007931/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007932 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7933 */
7934 void
7935trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7936{
7937#ifdef FEAT_EVAL
7938 dict_T *v_event;
7939 save_v_event_T save_v_event;
7940
7941 v_event = get_v_event(&save_v_event);
7942 (void)dict_add_string(v_event, "directory", new_dir);
7943 dict_set_items_ro(v_event);
7944#endif
7945 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7946#ifdef FEAT_EVAL
7947 restore_v_event(v_event, &save_v_event);
7948#endif
7949}
7950
7951/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007952 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007953 * chdir() function.
7954 * scope == CDSCOPE_WINDOW: changes the window-local directory
7955 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7956 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007957 * Returns TRUE if the directory is successfully changed.
7958 */
7959 int
7960changedir_func(
7961 char_u *new_dir,
7962 int forceit,
7963 cdscope_T scope)
7964{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007965 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007966 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007967 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007968 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007969 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007970
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007971 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007972 return FALSE;
7973
7974 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7975 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007976 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007977 return FALSE;
7978 }
7979
7980 // ":cd -": Change to previous directory
7981 if (STRCMP(new_dir, "-") == 0)
7982 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007983 pdir = get_prevdir(scope);
7984 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007985 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007986 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007987 return FALSE;
7988 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007989 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007990 }
7991
7992 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007993 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007994 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007995 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007996 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007997
Bakudankun29f3a452021-12-11 12:28:08 +00007998 // For UNIX ":cd" means: go to home directory.
7999 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008000#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008001 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00008002#else
8003 if (*new_dir == NUL && p_cdh)
8004#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008005 {
8006 // use NameBuff for home directory name
8007# ifdef VMS
8008 char_u *p;
8009
8010 p = mch_getenv((char_u *)"SYS$LOGIN");
8011 if (p == NULL || *p == NUL) // empty is the same as not set
8012 NameBuff[0] = NUL;
8013 else
8014 vim_strncpy(NameBuff, p, MAXPATHL - 1);
8015# else
8016 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8017# endif
8018 new_dir = NameBuff;
8019 }
zeertzjqf38aad82021-12-30 13:45:57 +00008020 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008021 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
8022 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00008023 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008024 if (scope == CDSCOPE_WINDOW)
8025 acmd_fname = (char_u *)"window";
8026 else if (scope == CDSCOPE_TABPAGE)
8027 acmd_fname = (char_u *)"tabpage";
8028 else
8029 acmd_fname = (char_u *)"global";
8030 trigger_DirChangedPre(acmd_fname, new_dir);
8031
8032 if (vim_chdir(new_dir))
8033 {
8034 emsg(_(e_command_failed));
8035 vim_free(pdir);
8036 return FALSE;
8037 }
Richard Doty3d0abad2021-12-29 14:39:08 +00008038 }
zeertzjq73257142022-02-02 13:16:37 +00008039
8040 if (scope == CDSCOPE_WINDOW)
8041 pp = &curwin->w_prevdir;
8042 else if (scope == CDSCOPE_TABPAGE)
8043 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008044 else
zeertzjq73257142022-02-02 13:16:37 +00008045 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008046 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00008047 *pp = pdir;
8048
8049 post_chdir(scope);
8050
8051 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008052 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008053 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00008054 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008055}
Bram Moolenaarea408852005-06-25 22:49:46 +00008056
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008058 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008060 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008061ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01008063 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064
8065 new_dir = eap->arg;
8066#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00008067 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
8068 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008070 ex_pwd(NULL);
8071 return;
8072 }
8073#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008074
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008075 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008076
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008077 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8078 scope = CDSCOPE_WINDOW;
8079 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
8080 scope = CDSCOPE_TABPAGE;
8081
8082 if (changedir_func(new_dir, eap->forceit, scope))
8083 {
8084 // Echo the new current directory if the command was typed.
8085 if (KeyTyped || p_verbose >= 5)
8086 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 }
8088}
8089
8090/*
8091 * ":pwd".
8092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008094ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095{
8096 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8097 {
8098#ifdef BACKSLASH_IN_FILENAME
8099 slash_adjust(NameBuff);
8100#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02008101 if (p_verbose > 0)
8102 {
8103 char *context = "global";
8104
Bram Moolenaar05268152021-11-18 18:53:45 +00008105 if (last_chdir_reason != NULL)
8106 context = last_chdir_reason;
8107 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02008108 context = "window";
8109 else if (curtab->tp_localdir != NULL)
8110 context = "tabpage";
8111 smsg("[%s] %s", context, (char *)NameBuff);
8112 }
8113 else
8114 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 }
8116 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00008117 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118}
8119
8120/*
8121 * ":=".
8122 */
8123 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008124ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008126 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008127 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128}
8129
8130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008131ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008133 int n;
8134 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135
8136 if (cursor_valid())
8137 {
8138 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8139 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00008140 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008142
8143 len = eap->line2;
8144 switch (*eap->arg)
8145 {
8146 case 'm': break;
8147 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008148 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008149 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008150
8151 // Hide the cursor if invoked with !
8152 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153}
8154
8155/*
8156 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008157 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 */
8159 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008160do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161{
Bram Moolenaar52953192019-08-16 10:27:13 +02008162 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008163 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008164# ifdef ELAPSED_FUNC
8165 elapsed_T start_tv;
8166
8167 // Remember at what time we started, so that we know how much longer we
8168 // should wait after waiting for a bit.
8169 ELAPSED_INIT(start_tv);
8170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008172 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008173 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008174 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008175 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008176
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008177 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008178 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008180 wait_now = msec - done > 1000L ? 1000L : msec - done;
8181#ifdef FEAT_TIMERS
8182 {
8183 long due_time = check_due_timer();
8184
8185 if (due_time > 0 && due_time < wait_now)
8186 wait_now = due_time;
8187 }
8188#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008189#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008190 if (has_any_channel() && wait_now > 20L)
8191 wait_now = 20L;
8192#endif
8193#ifdef FEAT_SOUND
8194 if (has_any_sound_callback() && wait_now > 20L)
8195 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008196#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008197 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008198
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008199#ifdef FEAT_JOB_CHANNEL
8200 if (has_any_channel())
8201 ui_breakcheck_force(TRUE);
8202 else
8203#endif
8204 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008205#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008206 // Process the netbeans and clientserver messages that may have been
8207 // received in the call to ui_breakcheck() when the GUI is in use. This
8208 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008209 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008210#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008211
8212# ifdef ELAPSED_FUNC
8213 // actual time passed
8214 done = ELAPSED_FUNC(start_tv);
8215# else
8216 // guestimate time passed (will actually be more)
8217 done += wait_now;
8218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008220
8221 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8222 // input buffer, otherwise a following call to input() fails.
8223 if (got_int)
8224 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008225
8226 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008227 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228}
8229
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230/*
8231 * ":winsize" command (obsolete).
8232 */
8233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008234ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
8236 int w, h;
8237 char_u *arg = eap->arg;
8238 char_u *p;
8239
Keith Thompson184f71c2024-01-04 21:19:04 +01008240 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008241 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008242 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008243 return;
8244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 w = getdigits(&arg);
8246 arg = skipwhite(arg);
8247 p = arg;
8248 h = getdigits(&arg);
8249 if (*p != NUL && *arg == NUL)
8250 set_shellsize(w, h, TRUE);
8251 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008252 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253}
8254
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008256ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
8258 int xchar = NUL;
8259 char_u *p;
8260
8261 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8262 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008263 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 if (eap->arg[1] == NUL)
8265 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008266 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 return;
8268 }
8269 xchar = eap->arg[1];
8270 p = eap->arg + 2;
8271 }
8272 else
8273 p = eap->arg + 1;
8274
Bram Moolenaar63b91732021-08-05 20:40:03 +02008275 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008277 if (*p != NUL && *p != (
8278#ifdef FEAT_EVAL
8279 in_vim9script() ? '#' :
8280#endif
8281 '"')
8282 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008283 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008284 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008286 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008287 postponed_split_flags = cmdmod.cmod_split;
8288 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8290 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008291 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 }
8293}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294
Bram Moolenaar843ee412004-06-30 16:16:41 +00008295#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296/*
8297 * ":winpos".
8298 */
8299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008300ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301{
8302 int x, y;
8303 char_u *arg = eap->arg;
8304 char_u *p;
8305
8306 if (*arg == NUL)
8307 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008308# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008309# ifdef VIMDLL
8310 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8311 mch_get_winpos(&x, &y) != FAIL)
8312# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008314# else
8315 if (mch_get_winpos(&x, &y) != FAIL)
8316# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {
8318 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008319 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 }
8321 else
8322# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008323 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325 else
8326 {
8327 x = getdigits(&arg);
8328 arg = skipwhite(arg);
8329 p = arg;
8330 y = getdigits(&arg);
8331 if (*p == NUL || *arg != NUL)
8332 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008333 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 return;
8335 }
8336# ifdef FEAT_GUI
8337 if (gui.in_use)
8338 gui_mch_set_winpos(x, y);
8339 else if (gui.starting)
8340 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008341 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 gui_win_x = x;
8343 gui_win_y = y;
8344 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008345# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 else
8347# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008348# endif
8349# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008350 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351# endif
8352# ifdef HAVE_TGETENT
8353 if (*T_CWP)
8354 term_set_winpos(x, y);
8355# endif
8356 }
8357}
8358#endif
8359
8360/*
8361 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8362 */
8363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008364ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365{
8366 oparg_T oa;
8367
8368 clear_oparg(&oa);
8369 oa.regname = eap->regname;
8370 oa.start.lnum = eap->line1;
8371 oa.end.lnum = eap->line2;
8372 oa.line_count = eap->line2 - eap->line1 + 1;
8373 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008375 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {
8377 setpcmark();
8378 curwin->w_cursor.lnum = eap->line1;
8379 beginline(BL_SOL | BL_FIX);
8380 }
8381
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008382 if (VIsual_active)
8383 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008384
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 switch (eap->cmdidx)
8386 {
8387 case CMD_delete:
8388 oa.op_type = OP_DELETE;
8389 op_delete(&oa);
8390 break;
8391
8392 case CMD_yank:
8393 oa.op_type = OP_YANK;
8394 (void)op_yank(&oa, FALSE, TRUE);
8395 break;
8396
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008397 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008398 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008400 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8401#else
8402 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008404 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 oa.op_type = OP_RSHIFT;
8406 else
8407 oa.op_type = OP_LSHIFT;
8408 op_shift(&oa, FALSE, eap->amount);
8409 break;
8410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008412 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413}
8414
8415/*
8416 * ":put".
8417 */
8418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008419ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008421 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 if (eap->line2 == 0)
8423 {
8424 eap->line2 = 1;
8425 eap->forceit = TRUE;
8426 }
8427 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008428 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008429 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008430 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431}
8432
8433/*
8434 * Handle ":copy" and ":move".
8435 */
8436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008437ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438{
8439 long n;
8440
Bram Moolenaar491799b2020-08-01 19:23:43 +02008441#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008442 if (not_in_vim9(eap) == FAIL)
8443 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008444#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008445 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008446 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {
8448 eap->nextcmd = NULL;
8449 return;
8450 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008451 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452
8453 /*
8454 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8455 */
8456 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8457 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008458 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 return;
8460 }
8461
8462 if (eap->cmdidx == CMD_move)
8463 {
8464 if (do_move(eap->line1, eap->line2, n) == FAIL)
8465 return;
8466 }
8467 else
8468 ex_copy(eap->line1, eap->line2, n);
8469 u_clearline();
8470 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008471 ex_may_print(eap);
8472}
8473
8474/*
8475 * Print the current line if flags were given to the Ex command.
8476 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008477 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008478ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008479{
8480 if (eap->flags != 0)
8481 {
8482 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8483 (eap->flags & EXFLAG_LIST));
8484 ex_no_reprint = TRUE;
8485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486}
8487
8488/*
8489 * ":smagic" and ":snomagic".
8490 */
8491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008492ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008494 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008496 magic_overruled = eap->cmdidx == CMD_smagic
8497 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008498 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008499 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500}
8501
8502/*
8503 * ":join".
8504 */
8505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008506ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507{
8508 curwin->w_cursor.lnum = eap->line1;
8509 if (eap->line1 == eap->line2)
8510 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008511 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 return;
8513 if (eap->line2 == curbuf->b_ml.ml_line_count)
8514 {
8515 beep_flush();
8516 return;
8517 }
8518 ++eap->line2;
8519 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008520 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008522 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523}
8524
8525/*
8526 * ":[addr]@r" or ":[addr]*r": execute register
8527 */
8528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008529ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008532 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533
8534 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008535 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536
8537#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008538 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539#endif
8540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008541 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 c = *eap->arg;
8543 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8544 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008545 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008546 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8547 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008550 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008553 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008555 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008557 /*
8558 * Execute from the typeahead buffer.
8559 * Continue until the stuff buffer is empty and all added characters
8560 * have been consumed.
8561 */
8562 while (!stuff_empty() || typebuf.tb_len > prev_len)
8563 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8564
8565 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566}
8567
8568/*
8569 * ":!".
8570 */
8571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008572ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
8574 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8575}
8576
8577/*
8578 * ":undo".
8579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008581ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008583 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008584 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008585 else
8586 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587}
8588
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008589#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008591ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008592{
8593 char_u hash[UNDO_HASH_SIZE];
8594
8595 u_compute_hash(hash);
8596 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8597}
8598
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008600ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008601{
8602 char_u hash[UNDO_HASH_SIZE];
8603
8604 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008605 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008606}
8607#endif
8608
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609/*
8610 * ":redo".
8611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008613ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
8615 u_redo(1);
8616}
8617
8618/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008619 * ":earlier" and ":later".
8620 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008622ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008623{
8624 long count = 0;
8625 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008626 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008627 char_u *p = eap->arg;
8628
8629 if (*p == NUL)
8630 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008631 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008632 {
8633 count = getdigits(&p);
8634 switch (*p)
8635 {
8636 case 's': ++p; sec = TRUE; break;
8637 case 'm': ++p; sec = TRUE; count *= 60; break;
8638 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008639 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8640 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008641 }
8642 }
8643
8644 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008645 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008646 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008647 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8648 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008649}
8650
8651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 * ":redir": start/stop redirection.
8653 */
8654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008655ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656{
8657 char *mode;
8658 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008659 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660
Bram Moolenaarba768492016-07-08 15:32:54 +02008661#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008662 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008663 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008664 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008665 return;
8666 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008667#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008668
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 if (STRICMP(eap->arg, "END") == 0)
8670 close_redir();
8671 else
8672 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008673 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008675 ++arg;
8676 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008678 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 mode = "a";
8680 }
8681 else
8682 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008683 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684
8685 close_redir();
8686
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008687 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008688 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 if (fname == NULL)
8690 return;
8691#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008692 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 {
8694 char_u *browseFile;
8695
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008696 browseFile = do_browse(BROWSE_SAVE,
8697 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008698 fname, NULL, NULL,
8699 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008701 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 vim_free(fname);
8703 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008704 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 }
8706#endif
8707
8708 redir_fd = open_exfile(fname, eap->forceit, mode);
8709 vim_free(fname);
8710 }
8711#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008712 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008714 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008716 ++arg;
8717 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008719 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008720 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008722 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008724 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008725 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008726 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008727 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008729 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008730 if (*arg == '>')
8731 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008732 // Make register empty when not using @A-@Z and the
8733 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008734 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008735 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008737 }
8738 if (*arg != NUL)
8739 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008740 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008741 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008742 }
8743 }
8744 else if (*arg == '=' && arg[1] == '>')
8745 {
8746 int append;
8747
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008748 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008749 close_redir();
8750 arg += 2;
8751
8752 if (*arg == '>')
8753 {
8754 ++arg;
8755 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 }
8757 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008758 append = FALSE;
8759
8760 if (var_redir_start(skipwhite(arg), append) == OK)
8761 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 }
8763#endif
8764
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008765 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008768 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008771 // Make sure redirection is not off. Can happen for cmdline completion
8772 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008773 if (redir_fd != NULL
8774#ifdef FEAT_EVAL
8775 || redir_reg || redir_vname
8776#endif
8777 )
8778 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779}
8780
8781/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008782 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008784 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008785ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008787 redraw_cmd(eap->forceit);
8788}
8789
8790/*
8791 * ":redraw": force redraw, with clear if "clear" is TRUE.
8792 */
8793 void
8794redraw_cmd(int clear)
8795{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008796 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008798
8799 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008801
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008802 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008804 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 if (need_maketitle)
8806 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008807#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8808# ifdef VIMDLL
8809 if (!gui.in_use)
8810# endif
8811 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008812#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008813 RedrawingDisabled = save_RedrawingDisabled;
8814 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815
Bram Moolenaar5017c662022-04-07 18:06:08 +01008816 // After drawing the statusline screen_attr may still be set.
8817 screen_stop_highlight();
8818
Bram Moolenaara2a89732022-08-31 14:46:18 +01008819 // Reset msg_didout, so that a message that's there is overwritten.
8820 msg_didout = FALSE;
8821 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008823 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008824 need_wait_return = FALSE;
8825
Bram Moolenaara653e532022-04-19 11:38:24 +01008826 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008827 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008828 redrawcmdline();
8829
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 out_flush();
8831}
8832
8833/*
8834 * ":redrawstatus": force redraw of status line(s)
8835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008837ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 if (eap->forceit)
8840 status_redraw_all();
8841 else
8842 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008843 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008844 return; // redraw later
8845
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008846 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008847 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008848
8849 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008850 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008851
zeertzjq320d9102022-09-20 17:12:13 +01008852 if (State & MODE_CMDLINE)
8853 redraw_statuslines();
8854 else
8855 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008856 RedrawingDisabled = save_RedrawingDisabled;
8857 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859}
8860
Bram Moolenaare12bab32019-01-08 22:02:56 +01008861/*
8862 * ":redrawtabline": force redraw of the tabline
8863 */
8864 static void
8865ex_redrawtabline(exarg_T *eap UNUSED)
8866{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008867 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008868 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008869
8870 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008871 p_lz = FALSE;
8872
8873 draw_tabline();
8874
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008875 RedrawingDisabled = save_RedrawingDisabled;
8876 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008877 out_flush();
8878}
8879
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008881close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882{
8883 if (redir_fd != NULL)
8884 {
8885 fclose(redir_fd);
8886 redir_fd = NULL;
8887 }
8888#ifdef FEAT_EVAL
8889 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008890 if (redir_vname)
8891 {
8892 var_redir_stop();
8893 redir_vname = 0;
8894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895#endif
8896}
8897
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008898#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008899 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008900vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008901{
8902 if (vim_mkdir(name, prot) != 0)
8903 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008904 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008905 return FAIL;
8906 }
8907 return OK;
8908}
8909#endif
8910
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911/*
8912 * Open a file for writing for an Ex command, with some checks.
8913 * Return file descriptor, or NULL on failure.
8914 */
8915 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008916open_exfile(
8917 char_u *fname,
8918 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008919 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920{
8921 FILE *fd;
8922
8923#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008924 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 if (mch_isdir(fname))
8926 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008927 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 return NULL;
8929 }
8930#endif
8931 if (!forceit && *mode != 'a' && vim_fexists(fname))
8932 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008933 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 return NULL;
8935 }
8936
8937 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008938 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939
8940 return fd;
8941}
8942
8943/*
8944 * ":mark" and ":k".
8945 */
8946 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008947ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948{
8949 pos_T pos;
8950
Bram Moolenaar10b94212021-02-19 21:42:57 +01008951#ifdef FEAT_EVAL
8952 if (not_in_vim9(eap) == FAIL)
8953 return;
8954#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008955 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008957 emsg(_(e_argument_required));
8958 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008960
8961 if (eap->arg[1] != NUL) // more than one character?
8962 {
8963 semsg(_(e_trailing_characters_str), eap->arg);
8964 return;
8965 }
8966
8967 pos = curwin->w_cursor; // save curwin->w_cursor
8968 curwin->w_cursor.lnum = eap->line2;
8969 beginline(BL_WHITE | BL_FIX);
8970 if (setmark(*eap->arg) == FAIL) // set mark
8971 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
8972 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973}
8974
8975/*
8976 * Update w_topline, w_leftcol and the cursor position.
8977 */
8978 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008979update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008981 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 update_topline();
8983 if (!curwin->w_p_wrap)
8984 validate_cursor();
8985 update_curswant();
8986}
8987
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008989 * Save the current State and go to Normal mode.
8990 * Return TRUE if the typeahead could be saved.
8991 */
8992 int
8993save_current_state(save_state_T *sst)
8994{
8995 sst->save_msg_scroll = msg_scroll;
8996 sst->save_restart_edit = restart_edit;
8997 sst->save_msg_didout = msg_didout;
8998 sst->save_State = State;
8999 sst->save_insertmode = p_im;
9000 sst->save_finish_op = finish_op;
9001 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009002 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009003 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009004
Bram Moolenaar4324d872020-12-01 20:12:24 +01009005 msg_scroll = FALSE; // no msg scrolling in Normal mode
9006 restart_edit = 0; // don't go to Insert mode
9007 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01009008
Bram Moolenaara452b802020-12-01 21:47:59 +01009009 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01009010 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009011
9012 /*
9013 * Save the current typeahead. This is required to allow using ":normal"
9014 * from an event handler and makes sure we don't hang when the argument
9015 * ends with half a command.
9016 */
9017 save_typeahead(&sst->tabuf);
9018 return sst->tabuf.typebuf_valid;
9019}
9020
9021 void
9022restore_current_state(save_state_T *sst)
9023{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009024 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02009025 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009026
9027 msg_scroll = sst->save_msg_scroll;
9028 restart_edit = sst->save_restart_edit;
9029 p_im = sst->save_insertmode;
9030 finish_op = sst->save_finish_op;
9031 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009032 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009033 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009034 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01009035 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009036
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009037 // Restore the state (needed when called from a function executed for
9038 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009039 State = sst->save_State;
9040#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009041 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009042#endif
9043}
9044
9045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 * ":normal[!] {commands}": Execute normal mode commands.
9047 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009048 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009049ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009051 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 char_u *arg = NULL;
9053 int l;
9054 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009056 if (ex_normal_lock > 0)
9057 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00009058 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009059 return;
9060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 if (ex_normal_busy >= p_mmd)
9062 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009063 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 return;
9065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 /*
9068 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9069 * this for the K_SPECIAL leading byte, otherwise special keys will not
9070 * work.
9071 */
9072 if (has_mbyte)
9073 {
9074 int len = 0;
9075
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009076 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 for (p = eap->arg; *p != NUL; ++p)
9078 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009079#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009080 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01009082#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009083 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009084 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009085#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 )
9089 len += 2;
9090 }
9091 if (len > 0)
9092 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009093 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 if (arg != NULL)
9095 {
9096 len = 0;
9097 for (p = eap->arg; *p != NUL; ++p)
9098 {
9099 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01009100#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 if (*p == CSI)
9102 {
9103 arg[len++] = KS_EXTRA;
9104 arg[len++] = (int)KE_CSI;
9105 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009106#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009107 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 {
9109 arg[len++] = *++p;
9110 if (*p == K_SPECIAL)
9111 {
9112 arg[len++] = KS_SPECIAL;
9113 arg[len++] = KE_FILLER;
9114 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009115#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 else if (*p == CSI)
9117 {
9118 arg[len++] = KS_EXTRA;
9119 arg[len++] = (int)KE_CSI;
9120 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 }
9123 arg[len] = NUL;
9124 }
9125 }
9126 }
9127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009129 ++ex_normal_busy;
9130 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 {
9132 /*
9133 * Repeat the :normal command for each line in the range. When no
9134 * range given, execute it just once, without positioning the cursor
9135 * first.
9136 */
9137 do
9138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 if (eap->addr_count != 0)
9140 {
9141 curwin->w_cursor.lnum = eap->line1++;
9142 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02009143 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 }
9145
Bram Moolenaar13505972019-01-24 15:04:48 +01009146 exec_normal_cmd(arg != NULL
9147 ? arg
9148 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 }
9150 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9151 }
9152
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009153 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 update_topline_cursor();
9155
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009156 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009158 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009159#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009160 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009161#endif
9162
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164}
9165
9166/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009167 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 */
9169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009170ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009172 if (eap->forceit)
9173 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009174 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009175 if (!curwin->w_cursor.lnum)
9176 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009177 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009178 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009179#ifdef FEAT_TERMINAL
9180 // Ignore this when running in an active terminal.
9181 if (term_job_running(curbuf->b_term))
9182 return;
9183#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009184
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009185 // Ignore the command when already in Insert mode. Inserting an
9186 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009187 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009188 return;
9189
Bram Moolenaar64969662005-12-14 21:59:55 +00009190 if (eap->cmdidx == CMD_startinsert)
9191 restart_edit = 'a';
9192 else if (eap->cmdidx == CMD_startreplace)
9193 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009195 restart_edit = 'V';
9196
9197 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009199 if (eap->cmdidx == CMD_startinsert)
9200 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009201 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009203
9204 if (VIsual_active)
9205 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206}
9207
9208/*
9209 * ":stopinsert"
9210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009212ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213{
9214 restart_edit = 0;
9215 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009216 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009219/*
9220 * Execute normal mode command "cmd".
9221 * "remap" can be REMAP_NONE or REMAP_YES.
9222 */
9223 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009224exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009225{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009226 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009227 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009228 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009229}
Bram Moolenaar25281632016-01-21 23:32:32 +01009230
Bram Moolenaar25281632016-01-21 23:32:32 +01009231/*
9232 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009233 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009234 */
9235 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009236exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009237{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009238 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009239 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009240
Bram Moolenaar905dd902019-04-07 14:21:47 +02009241 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9242 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009243 clear_oparg(&oa);
9244 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009245 while ((!stuff_empty()
9246 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009247 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009248 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009249 {
9250 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009251#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009252 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009253 && oa.op_type == OP_NOP && oa.regname == NUL
9254 && !VIsual_active)
9255 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009256 // If terminal_loop() returns OK we got a key that is handled
9257 // in Normal model. With FAIL we first need to position the
9258 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009259 if (terminal_loop(TRUE) == OK)
9260 normal_cmd(&oa, TRUE);
9261 }
9262 else
9263#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009264 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009265 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009266 }
9267}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009268
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269#ifdef FEAT_FIND_ID
9270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009271ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272{
9273 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9274 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009275 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276}
9277
Bram Moolenaar4033c552017-09-16 20:54:51 +02009278#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279/*
9280 * ":psearch"
9281 */
9282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009283ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284{
9285 g_do_tagpreview = p_pvh;
9286 ex_findpat(eap);
9287 g_do_tagpreview = 0;
9288}
9289#endif
9290
9291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009292ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293{
9294 int whole = TRUE;
9295 long n;
9296 char_u *p;
9297 int action;
9298
9299 switch (cmdnames[eap->cmdidx].cmd_name[2])
9300 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009301 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9303 action = ACTION_GOTO;
9304 else
9305 action = ACTION_SHOW;
9306 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009307 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 action = ACTION_SHOW_ALL;
9309 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009310 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 action = ACTION_GOTO;
9312 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009313 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 action = ACTION_SPLIT;
9315 break;
9316 }
9317
9318 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009319 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 {
9321 n = getdigits(&eap->arg);
9322 eap->arg = skipwhite(eap->arg);
9323 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009324 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 {
9326 whole = FALSE;
9327 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009328 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 if (*p)
9330 {
9331 *p++ = NUL;
9332 p = skipwhite(p);
9333
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009334 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009335 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009336 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009338 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 }
9340 }
9341 if (!eap->skip)
9342 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9343 whole, !eap->forceit,
9344 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009345 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346}
9347#endif
9348
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349
Bram Moolenaar4033c552017-09-16 20:54:51 +02009350#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351/*
9352 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9353 */
9354 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009355ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009357 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9359}
9360
9361/*
9362 * ":pedit"
9363 */
9364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009365ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366{
9367 win_T *curwin_save = curwin;
9368
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009369 if (ERROR_IF_ANY_POPUP_WINDOW)
9370 return;
9371
Bram Moolenaar026587b2019-08-17 15:08:00 +02009372 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009374 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009375
Bram Moolenaar026587b2019-08-17 15:08:00 +02009376 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009378
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 if (curwin != curwin_save && win_valid(curwin_save))
9380 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009381 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009383 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 win_enter(curwin_save, TRUE);
9385 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009386# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009387 else if (WIN_IS_POPUP(curwin))
9388 {
9389 // can't keep focus in popup window
9390 win_enter(firstwin, TRUE);
9391 }
9392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 g_do_tagpreview = 0;
9394}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396
9397/*
9398 * ":stag", ":stselect" and ":stjump".
9399 */
9400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009401ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402{
9403 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009404 postponed_split_flags = cmdmod.cmod_split;
9405 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9407 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009408 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410
9411/*
9412 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9413 */
9414 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009415ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416{
9417 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9418}
9419
9420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009421ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 int cmd;
9424
9425 switch (name[1])
9426 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009427 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009429 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009431 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009433 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009435 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009437 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009439 case 'f': // ":tfirst"
9440 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009442 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009444 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009446 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009448 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 return;
9450 }
9451#endif
9452 cmd = DT_TAG;
9453 break;
9454 }
9455
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009456 if (name[0] == 'l')
9457 {
9458#ifndef FEAT_QUICKFIX
9459 ex_ni(eap);
9460 return;
9461#else
9462 cmd = DT_LTAG;
9463#endif
9464 }
9465
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9467 eap->forceit, TRUE);
9468}
9469
John Marriotted908f72024-04-18 22:46:56 +02009470enum {
9471 SPEC_PERC = 0,
9472 SPEC_HASH,
9473 SPEC_CWORD, // cursor word
9474 SPEC_CCWORD, // cursor WORD
9475 SPEC_CEXPR, // expr under cursor
9476 SPEC_CFILE, // cursor path name
9477 SPEC_SFILE, // ":so" file name
9478 SPEC_SLNUM, // ":so" file line number
9479 SPEC_STACK, // call stack
9480 SPEC_SCRIPT, // script file name
9481 SPEC_AFILE, // autocommand file name
9482 SPEC_ABUF, // autocommand buffer number
9483 SPEC_AMATCH, // autocommand match name
9484 SPEC_SFLNUM, // script file line number
9485 SPEC_SID // script ID: <SNR>123_
9486#ifdef FEAT_CLIENTSERVER
9487 , SPEC_CLIENT
9488#endif
9489};
9490
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009492 * Check "str" for starting with a special cmdline variable.
9493 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9494 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9495 */
9496 int
Mike Williams51024bb2024-05-30 07:46:30 +02009497find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009498{
John Marriotted908f72024-04-18 22:46:56 +02009499 // must be sorted by the 'value' field because it is used by bsearch()!
9500 static keyvalue_T spec_str_tab[] = {
9501 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9502 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9503 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9504 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9505 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9506 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9507 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009508#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009509 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009510#endif
John Marriotted908f72024-04-18 22:46:56 +02009511 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9512 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9513 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9514 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9515 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9516 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009517 };
John Marriotted908f72024-04-18 22:46:56 +02009518 keyvalue_T target;
9519 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009520
John Marriotted908f72024-04-18 22:46:56 +02009521 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009522 {
John Marriotted908f72024-04-18 22:46:56 +02009523 case '%':
9524 *usedlen = 1;
9525 return SPEC_PERC;
9526
9527 case '#':
9528 *usedlen = 1;
9529 return SPEC_HASH;
9530
9531 case '<':
9532 target.key = 0;
9533 target.value = (char *)src + 1; // skip over '<'
9534 target.length = 0; // not used, see cmp_keyvalue_value_n()
9535
9536 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab, ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]), cmp_keyvalue_value_n);
9537 if (entry == NULL)
9538 return -1;
9539
9540 *usedlen = entry->length + 1;
9541 return entry->key;
9542
9543 default:
9544 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009545 }
John Marriotted908f72024-04-18 22:46:56 +02009546
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009547 return -1;
9548}
9549
9550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 * Evaluate cmdline variables.
9552 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009553 * change "%" to curbuf->b_ffname
9554 * "#" to curwin->w_alt_fnum
9555 * "%%" to curwin->w_alt_fnum in Vim9 script
9556 * "<cword>" to word under the cursor
9557 * "<cWORD>" to WORD under the cursor
9558 * "<cexpr>" to C-expression under the cursor
9559 * "<cfile>" to path name under the cursor
9560 * "<sfile>" to sourced file name
9561 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009562 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009563 * "<slnum>" to sourced file line number
9564 * "<afile>" to file name for autocommand
9565 * "<abuf>" to buffer number for autocommand
9566 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 *
9568 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9569 * "" for error without a message) and NULL is returned.
9570 * Returns an allocated string if a valid match was found.
9571 * Returns NULL if no match was found. "usedlen" then still contains the
9572 * number of characters to skip.
9573 */
9574 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009575eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009576 char_u *src, // pointer into commandline
9577 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009578 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009579 linenr_T *lnump, // line number for :e command, or NULL
9580 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009581 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009582 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009583 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584{
9585 int i;
9586 char_u *s;
9587 char_u *result;
9588 char_u *resultbuf = NULL;
9589 int resultlen;
9590 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009591 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009593 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596
9597 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009598 if (escaped != NULL)
9599 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600
9601 /*
9602 * Check if there is something to do.
9603 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009604 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009605 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 {
9607 *usedlen = 1;
9608 return NULL;
9609 }
9610
9611 /*
9612 * Skip when preceded with a backslash "\%" and "\#".
9613 * Note: In "\\%" the % is also not recognized!
9614 */
9615 if (src > srcstart && src[-1] == '\\')
9616 {
9617 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009618 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 return NULL;
9620 }
9621
9622 /*
9623 * word or WORD under cursor
9624 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009625 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9626 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009628 resultlen = find_ident_under_cursor(&result,
9629 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9630 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9631 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 if (resultlen == 0)
9633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009634 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 return NULL;
9636 }
9637 }
9638
9639 /*
9640 * '#': Alternate file name
9641 * '%': Current file name
9642 * File name under the cursor
9643 * File name for autocommand
9644 * and following modifiers
9645 */
9646 else
9647 {
Mike Williams51024bb2024-05-30 07:46:30 +02009648 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009649
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 switch (spec_idx)
9651 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009652 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009653#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009654 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009657 // '%': current file
9658 if (curbuf->b_fname == NULL)
9659 {
9660 result = (char_u *)"";
9661 valid = 0; // Must have ":p:h" to be valid
9662 }
9663 else
9664 {
9665 result = curbuf->b_fname;
9666 tilde_file = STRCMP(result, "~") == 0;
9667 }
9668 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009670#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009671 // "%%" alternate file
9672 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009673#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009674 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009675 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009676 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009678 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 result = arg_all();
9680 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009681 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009682 if (escaped != NULL)
9683 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 break;
9686 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009687 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009688 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009689 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009691 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009692 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009693 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009694 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009696 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009698 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009699 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009700 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009701 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009702 return NULL;
9703 }
9704#ifdef FEAT_EVAL
9705 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9706 (long)i);
9707 if (result == NULL)
9708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009709 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009710 return NULL;
9711 }
9712#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009713 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 }
9717 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009718 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009719 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9720 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009721 buf = buflist_findnr(i);
9722 if (buf == NULL)
9723 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009724 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009725 return NULL;
9726 }
9727 if (lnump != NULL)
9728 *lnump = ECMD_LAST;
9729 if (buf->b_fname == NULL)
9730 {
9731 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009732 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009733 }
9734 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009735 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009736 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009737 tilde_file = STRCMP(result, "~") == 0;
9738 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 break;
9741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009742 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009743 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 if (result == NULL)
9745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009746 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 return NULL;
9748 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009749 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009752 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009754 if (result != NULL && !autocmd_fname_full)
9755 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009756 // Still need to turn the fname into a full path. It is
9757 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009758 autocmd_fname_full = TRUE;
9759 result = FullName_save(autocmd_fname, FALSE);
9760 vim_free(autocmd_fname);
9761 autocmd_fname = result;
9762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 if (result == NULL)
9764 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009765 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 return NULL;
9767 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009768 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 break;
9770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009771 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 if (autocmd_bufnr <= 0)
9773 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009774 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 return NULL;
9776 }
9777 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9778 result = strbuf;
9779 break;
9780
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009781 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 result = autocmd_match;
9783 if (result == NULL)
9784 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009785 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 return NULL;
9787 }
9788 break;
9789
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009790 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009791 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 if (result == NULL)
9793 {
LemonBoy6013d002022-04-09 21:42:10 +01009794 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9795 return NULL;
9796 }
9797 resultbuf = result; // remember allocated string
9798 break;
9799 case SPEC_STACK: // call stack
9800 result = estack_sfile(ESTACK_STACK);
9801 if (result == NULL)
9802 {
9803 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9804 return NULL;
9805 }
9806 resultbuf = result; // remember allocated string
9807 break;
9808 case SPEC_SCRIPT: // script file name
9809 result = estack_sfile(ESTACK_SCRIPT);
9810 if (result == NULL)
9811 {
9812 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 return NULL;
9814 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009815 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009817
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009818 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009819 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009820 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009821 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009822 return NULL;
9823 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009824 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009825 result = strbuf;
9826 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009827
9828#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009829 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009830 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009831 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009832 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009833 return NULL;
9834 }
9835 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009836 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009837 result = strbuf;
9838 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009839
Bram Moolenaar90944302020-08-01 20:45:11 +02009840 case SPEC_SID:
9841 if (current_sctx.sc_sid <= 0)
9842 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009843 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009844 return NULL;
9845 }
9846 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9847 result = strbuf;
9848 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009849#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009850
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009851#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009852 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009853 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9854 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 result = strbuf;
9856 break;
9857#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009858
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009859 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009860 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009861 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 }
9863
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009864 resultlen = (int)STRLEN(result); // length of new string
9865 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 {
9867 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 resultlen = (int)(s - result);
9870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 else if (!skip_mod)
9872 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009873 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 &resultlen);
9875 if (result == NULL)
9876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009877 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 return NULL;
9879 }
9880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 }
9882
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009883 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009885 if (empty_is_error)
9886 {
9887 if (valid != VALID_HEAD + VALID_PATH)
9888 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9889 else
9890 *errormsg = _(e_evaluates_to_an_empty_string);
9891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 result = NULL;
9893 }
9894 else
9895 result = vim_strnsave(result, resultlen);
9896 vim_free(resultbuf);
9897 return result;
9898}
9899
9900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 * Expand the <sfile> string in "arg".
9902 *
9903 * Returns an allocated string, or NULL for any error.
9904 */
9905 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009906expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009908 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +02009910 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +02009912 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +02009914 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +02009915 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 char_u *p;
9917
John Marriotted908f72024-04-18 22:46:56 +02009918 resultlen = STRLEN(arg);
9919 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 if (result == NULL)
9921 return NULL;
9922
9923 for (p = result; *p; )
9924 {
9925 if (STRNCMP(p, "<sfile>", 7) != 0)
9926 ++p;
9927 else
9928 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009929 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009930 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 if (errormsg != NULL)
9932 {
9933 if (*errormsg)
9934 emsg(errormsg);
9935 vim_free(result);
9936 return NULL;
9937 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009938 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 {
9940 p += srclen;
9941 continue;
9942 }
John Marriotted908f72024-04-18 22:46:56 +02009943 repllen = STRLEN(repl);
9944 resultlen += repllen - srclen;
9945 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 if (newres == NULL)
9947 {
9948 vim_free(repl);
9949 vim_free(result);
9950 return NULL;
9951 }
John Marriotted908f72024-04-18 22:46:56 +02009952 len = p - result;
9953 mch_memmove(newres, result, len);
9954 STRCPY(newres + len, repl);
9955 len += repllen;
9956 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 vim_free(repl);
9958 vim_free(result);
9959 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009960 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 }
9962 }
9963
9964 return result;
9965}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009968/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009969 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009970 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009973dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 if (fname == NULL)
9976 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009977 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978}
9979#endif
9980
9981/*
9982 * ":behave {mswin,xterm}"
9983 */
9984 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009985ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986{
9987 if (STRCMP(eap->arg, "mswin") == 0)
9988 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009989 set_option_value_give_err((char_u *)"selection",
9990 0L, (char_u *)"exclusive", 0);
9991 set_option_value_give_err((char_u *)"selectmode",
9992 0L, (char_u *)"mouse,key", 0);
9993 set_option_value_give_err((char_u *)"mousemodel",
9994 0L, (char_u *)"popup", 0);
9995 set_option_value_give_err((char_u *)"keymodel",
9996 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 }
9998 else if (STRCMP(eap->arg, "xterm") == 0)
9999 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010000 set_option_value_give_err((char_u *)"selection",
10001 0L, (char_u *)"inclusive", 0);
10002 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
10003 set_option_value_give_err((char_u *)"mousemodel",
10004 0L, (char_u *)"extend", 0);
10005 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 }
10007 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010008 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009}
10010
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011static int filetype_detect = FALSE;
10012static int filetype_plugin = FALSE;
10013static int filetype_indent = FALSE;
10014
10015/*
10016 * ":filetype [plugin] [indent] {on,off,detect}"
10017 * on: Load the filetype.vim file to install autocommands for file types.
10018 * off: Load the ftoff.vim file to remove all autocommands for file types.
10019 * plugin on: load filetype.vim and ftplugin.vim
10020 * plugin off: load ftplugof.vim
10021 * indent on: load filetype.vim and indent.vim
10022 * indent off: load indoff.vim
10023 */
10024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010025ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
10027 char_u *arg = eap->arg;
10028 int plugin = FALSE;
10029 int indent = FALSE;
10030
10031 if (*eap->arg == NUL)
10032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010033 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010034 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 filetype_detect ? "ON" : "OFF",
10036 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10037 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10038 return;
10039 }
10040
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010041 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 for (;;)
10043 {
10044 if (STRNCMP(arg, "plugin", 6) == 0)
10045 {
10046 plugin = TRUE;
10047 arg = skipwhite(arg + 6);
10048 continue;
10049 }
10050 if (STRNCMP(arg, "indent", 6) == 0)
10051 {
10052 indent = TRUE;
10053 arg = skipwhite(arg + 6);
10054 continue;
10055 }
10056 break;
10057 }
10058 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10059 {
10060 if (*arg == 'o' || !filetype_detect)
10061 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010062 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 filetype_detect = TRUE;
10064 if (plugin)
10065 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010066 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 filetype_plugin = TRUE;
10068 }
10069 if (indent)
10070 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010071 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 filetype_indent = TRUE;
10073 }
10074 }
10075 if (*arg == 'd')
10076 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010077 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010078 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 }
10080 }
10081 else if (STRCMP(arg, "off") == 0)
10082 {
10083 if (plugin || indent)
10084 {
10085 if (plugin)
10086 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010087 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 filetype_plugin = FALSE;
10089 }
10090 if (indent)
10091 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010092 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 filetype_indent = FALSE;
10094 }
10095 }
10096 else
10097 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010098 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 filetype_detect = FALSE;
10100 }
10101 }
10102 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010103 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104}
10105
10106/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010107 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 */
10109 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010110ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111{
zeertzjq5bf6c212024-03-31 18:41:27 +020010112 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010113 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010114
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010115 char_u *arg = eap->arg;
10116 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10117 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010118
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010119 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10120 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +020010121 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010125ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126{
10127#ifdef FEAT_DIGRAPHS
10128 if (*eap->arg != NUL)
10129 putdigraph(eap->arg);
10130 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010131 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#else
mityu61065042021-07-19 20:07:21 +020010133 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134#endif
10135}
10136
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010137#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10138 void
10139set_no_hlsearch(int flag)
10140{
10141 no_hlsearch = flag;
10142# ifdef FEAT_EVAL
10143 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10144# endif
10145}
10146
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147/*
10148 * ":nohlsearch"
10149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010151ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010153 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +010010154 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156#endif
10157
10158#ifdef FEAT_CRYPT
10159/*
10160 * ":X": Get crypt key
10161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010163ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010165 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010166 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167}
10168#endif
10169
10170#ifdef FEAT_FOLDING
10171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010172ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173{
10174 if (foldManualAllowed(TRUE))
10175 foldCreate(eap->line1, eap->line2);
10176}
10177
10178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010179ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180{
10181 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10182 eap->forceit, FALSE);
10183}
10184
10185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010186ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187{
10188 linenr_T lnum;
10189
Bram Moolenaar4facea32019-10-12 20:17:40 +020010190# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010191 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010192# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010193
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010194 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10196 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10197 ml_setmarked(lnum);
10198
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010199 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010201 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010202# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010203 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205}
10206#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010208#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010209/*
10210 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10211 * instead of a quickfix command.
10212 */
10213 int
10214is_loclist_cmd(int cmdidx)
10215{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010216 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010217 return FALSE;
10218 return cmdnames[cmdidx].cmd_name[0] == 'l';
10219}
10220#endif
10221
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010222 int
10223get_pressedreturn(void)
10224{
10225 return ex_pressedreturn;
10226}
10227
10228 void
10229set_pressedreturn(int val)
10230{
10231 ex_pressedreturn = val;
10232}