blob: 02bca38e04825708e9aaf4c5ab9720a1ad31de1e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058# define ex_cclose ex_ni
59# define ex_copen ex_ni
60# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020061# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000063#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
64# define ex_cexpr ex_ni
65#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020067static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020068static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010070static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020071#if !defined(FEAT_PERL) \
72 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
73 || !defined(FEAT_TCL) \
74 || !defined(FEAT_RUBY) \
75 || !defined(FEAT_LUA) \
76 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000077# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010080static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000082#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
86static void ex_highlight(exarg_T *eap);
87static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void ex_cquit(exarg_T *eap);
89static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_close(exarg_T *eap);
91static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
92static void ex_only(exarg_T *eap);
93static void ex_resize(exarg_T *eap);
94static void ex_stag(exarg_T *eap);
95static void ex_tabclose(exarg_T *eap);
96static void ex_tabonly(exarg_T *eap);
97static void ex_tabnext(exarg_T *eap);
98static void ex_tabmove(exarg_T *eap);
99static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100101static void ex_pclose(exarg_T *eap);
102static void ex_ptag(exarg_T *eap);
103static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#else
105# define ex_pclose ex_ni
106# define ex_ptag ex_ni
107# define ex_pedit ex_ni
108#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_hide(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100110static void ex_exit(exarg_T *eap);
111static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100113static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#else
115# define ex_goto ex_ni
116#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100117static void ex_shell(exarg_T *eap);
118static void ex_preserve(exarg_T *eap);
119static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_mode(exarg_T *eap);
121static void ex_wrongmodifier(exarg_T *eap);
122static void ex_find(exarg_T *eap);
123static void ex_open(exarg_T *eap);
124static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#ifndef FEAT_GUI
126# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100127static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100129#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#else
132# define ex_tearoff ex_ni
133#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100134#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
135 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100136static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#else
138# define ex_popup ex_ni
139#endif
140#ifndef FEAT_GUI_MSWIN
141# define ex_simalt ex_ni
142#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000143#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144# define gui_mch_find_dialog ex_ni
145# define gui_mch_replace_dialog ex_ni
146#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000147#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148# define ex_helpfind ex_ni
149#endif
150#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100151# define ex_cscope ex_ni
152# define ex_scscope ex_ni
153# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#endif
155#ifndef FEAT_SYN_HL
156# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200157# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000158#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200159#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200160# define ex_syntime ex_ni
161#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000162#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000163# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000164# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000165# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000166# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000167# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000168#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200169#ifndef FEAT_PERSISTENT_UNDO
170# define ex_rundo ex_ni
171# define ex_wundo ex_ni
172#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200173#ifndef FEAT_LUA
174# define ex_lua ex_script_ni
175# define ex_luado ex_ni
176# define ex_luafile ex_ni
177#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000178#ifndef FEAT_MZSCHEME
179# define ex_mzscheme ex_script_ni
180# define ex_mzfile ex_ni
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#ifndef FEAT_PERL
183# define ex_perl ex_script_ni
184# define ex_perldo ex_ni
185#endif
186#ifndef FEAT_PYTHON
187# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200188# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189# define ex_pyfile ex_ni
190#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200191#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200192# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200193# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194# define ex_py3file ex_ni
195#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100196#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
197# define ex_pyx ex_script_ni
198# define ex_pyxdo ex_ni
199# define ex_pyxfile ex_ni
200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifndef FEAT_TCL
202# define ex_tcl ex_script_ni
203# define ex_tcldo ex_ni
204# define ex_tclfile ex_ni
205#endif
206#ifndef FEAT_RUBY
207# define ex_ruby ex_script_ni
208# define ex_rubydo ex_ni
209# define ex_rubyfile ex_ni
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211#ifndef FEAT_KEYMAP
212# define ex_loadkeymap ex_ni
213#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100214static void ex_swapname(exarg_T *eap);
215static void ex_syncbind(exarg_T *eap);
216static void ex_read(exarg_T *eap);
217static void ex_pwd(exarg_T *eap);
218static void ex_equal(exarg_T *eap);
219static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100220static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000222#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#else
225# define ex_winpos ex_ni
226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ex_operators(exarg_T *eap);
228static void ex_put(exarg_T *eap);
229static void ex_copymove(exarg_T *eap);
230static void ex_submagic(exarg_T *eap);
231static void ex_join(exarg_T *eap);
232static void ex_at(exarg_T *eap);
233static void ex_bang(exarg_T *eap);
234static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200235#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ex_wundo(exarg_T *eap);
237static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_redo(exarg_T *eap);
240static void ex_later(exarg_T *eap);
241static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100243static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_startinsert(exarg_T *eap);
247static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_checkpath(exarg_T *eap);
250static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#else
252# define ex_findpat ex_ni
253# define ex_checkpath ex_ni
254#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200255#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#else
258# define ex_psearch ex_ni
259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static void ex_tag(exarg_T *eap);
261static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200263# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200264# define ex_break ex_ni
265# define ex_breakadd ex_ni
266# define ex_breakdel ex_ni
267# define ex_breaklist ex_ni
268# define ex_call ex_ni
269# define ex_catch ex_ni
270# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_continue ex_ni
272# define ex_debug ex_ni
273# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200274# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200275# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277# define ex_echo ex_ni
278# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200280# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200281# define ex_endfunction ex_ni
282# define ex_endif ex_ni
283# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_eval ex_ni
286# define ex_execute ex_ni
Bram Moolenaar340c59e2021-04-24 20:37:03 +0200287# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200288# define ex_finally ex_ni
289# define ex_finish ex_ni
290# define ex_function ex_ni
291# define ex_if ex_ni
292# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200293# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200294# define ex_lockvar ex_ni
295# define ex_oldfiles ex_ni
296# define ex_options ex_ni
297# define ex_packadd ex_ni
298# define ex_packloadall ex_ni
299# define ex_return ex_ni
300# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_throw ex_ni
302# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200304# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_import ex_ni
306# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200308#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_loadview ex_ni
310#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200311#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_viminfo ex_ni
313#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_filetype(exarg_T *eap);
316static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000318# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319# define ex_diffpatch ex_ni
320# define ex_diffgetput ex_ni
321# define ex_diffsplit ex_ni
322# define ex_diffthis ex_ni
323# define ex_diffupdate ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#else
329# define ex_nohlsearch ex_ni
330# define ex_match ex_ni
331#endif
332#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_X ex_ni
336#endif
337#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_fold(exarg_T *eap);
339static void ex_foldopen(exarg_T *eap);
340static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_fold ex_ni
343# define ex_foldopen ex_ni
344# define ex_folddo ex_ni
345#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100346#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_language ex_ni
348#endif
349#ifndef FEAT_SIGNS
350# define ex_sign ex_ni
351#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000352#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200353# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar05159a02005-02-26 23:04:13 +0000358#ifndef FEAT_PROFILE
359# define ex_profile ex_ni
360#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200361#ifndef FEAT_TERMINAL
362# define ex_terminal ex_ni
363#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200364#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
365# define ex_xrestore ex_ni
366#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100367#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200368# define ex_popupclear ex_ni
369#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000370
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371/*
372 * Declare cmdnames[].
373 */
374#define DO_DECLARE_EXCMD
375#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200376#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100377
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378static char_u dollar_command[2] = {'$', 0};
379
380
381#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100382// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383typedef struct
384{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100385 char_u *line; // command line
386 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387} wcmd_T;
388
389/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000390 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000392 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000394struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100396 garray_T *lines_gap; // growarray with line info
397 int current_line; // last read line from growarray
398 int repeating; // TRUE when looping a second time
399 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200400 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 void *cookie;
402};
403
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200404static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100405static int store_loop_line(garray_T *gap, char_u *line);
406static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000407
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100408// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000409struct dbg_stuff
410{
411 int trylevel;
412 int force_abort;
413 except_T *caught_stack;
414 char_u *vv_exception;
415 char_u *vv_throwpoint;
416 int did_emsg;
417 int got_int;
418 int did_throw;
419 int need_rethrow;
420 int check_cstack;
421 except_T *current_exception;
422};
423
Bram Moolenaared203462004-06-16 11:19:22 +0000424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100425save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000426{
427 dsp->trylevel = trylevel; trylevel = 0;
428 dsp->force_abort = force_abort; force_abort = FALSE;
429 dsp->caught_stack = caught_stack; caught_stack = NULL;
430 dsp->vv_exception = v_exception(NULL);
431 dsp->vv_throwpoint = v_throwpoint(NULL);
432
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100433 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000434 dsp->did_emsg = did_emsg; did_emsg = FALSE;
435 dsp->got_int = got_int; got_int = FALSE;
436 dsp->did_throw = did_throw; did_throw = FALSE;
437 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
438 dsp->check_cstack = check_cstack; check_cstack = FALSE;
439 dsp->current_exception = current_exception; current_exception = NULL;
440}
441
442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100443restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000444{
445 suppress_errthrow = FALSE;
446 trylevel = dsp->trylevel;
447 force_abort = dsp->force_abort;
448 caught_stack = dsp->caught_stack;
449 (void)v_exception(dsp->vv_exception);
450 (void)v_throwpoint(dsp->vv_throwpoint);
451 did_emsg = dsp->did_emsg;
452 got_int = dsp->got_int;
453 did_throw = dsp->did_throw;
454 need_rethrow = dsp->need_rethrow;
455 check_cstack = dsp->check_cstack;
456 current_exception = dsp->current_exception;
457}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458#endif
459
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460/*
461 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
462 * command is given.
463 */
464 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100465do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100466 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467{
468 int save_msg_scroll;
469 int prev_msg_row;
470 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100471 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000472
473 if (improved)
474 exmode_active = EXMODE_VIM;
475 else
476 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100477 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100478 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000479
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100480 // When using ":global /pat/ visual" and then "Q" we return to continue
481 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482 if (global_busy)
483 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484
485 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100486 ++RedrawingDisabled; // don't redisplay the window
487 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100489 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 ++hold_gui_events;
491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
Bram Moolenaar32526b32019-01-19 17:43:09 +0100493 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 while (exmode_active)
495 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000497 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
498 {
499 exmode_active = FALSE;
500 break;
501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 msg_scroll = TRUE;
503 need_wait_return = FALSE;
504 ex_pressedreturn = FALSE;
505 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100506 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 prev_msg_row = msg_row;
508 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 if (improved)
510 {
511 cmdline_row = msg_row;
512 do_cmdline(NULL, getexline, NULL, 0);
513 }
514 else
515 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
516 lines_left = Rows - 1;
517
Bram Moolenaardf177f62005-02-22 08:39:57 +0000518 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100519 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000521 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000522 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 if (ex_pressedreturn)
526 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100527 // go up one line, to overwrite the ":<CR>" line, so the
528 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000529 msg_row = prev_msg_row;
530 if (prev_msg_row == Rows - 1)
531 msg_row--;
532 }
533 msg_col = 0;
534 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
535 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100538 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000539 {
540 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000541 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000542 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000543 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
546
547#ifdef FEAT_GUI
548 --hold_gui_events;
549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 --RedrawingDisabled;
551 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100552 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 need_wait_return = FALSE;
554 msg_scroll = save_msg_scroll;
555}
556
557/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200558 * Print the executed command for when 'verbose' is set.
559 * When "lnum" is 0 only print the command.
560 */
561 static void
562msg_verbose_cmd(linenr_T lnum, char_u *cmd)
563{
564 ++no_wait_return;
565 verbose_enter_scroll();
566
567 if (lnum == 0)
568 smsg(_("Executing: %s"), cmd);
569 else
570 smsg(_("line %ld: %s"), (long)lnum, cmd);
571 if (msg_silent == 0)
572 msg_puts("\n"); // don't overwrite this
573
574 verbose_leave_scroll();
575 --no_wait_return;
576}
577
578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 * Execute a simple command line. Used for translated commands like "*".
580 */
581 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100582do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
584 return do_cmdline(cmd, NULL, NULL,
585 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
586}
587
588/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100589 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
590 * This allows for using a range without ":" in Vim9 script.
591 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200592 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100593do_cmd_argument(char_u *cmd)
594{
595 return do_cmdline(cmd, NULL, NULL,
596 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
597}
598
599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 * do_cmdline(): execute one Ex command line
601 *
602 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100603 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * 2. Split up in parts separated with '|'.
605 *
606 * This function can be called recursively!
607 *
608 * flags:
609 * DOCMD_VERBOSE - The command will be included in the error message.
610 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100611 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 * DOCMD_KEYTYPED - Don't reset KeyTyped.
613 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
614 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
615 *
616 * return FAIL if cmdline could not be executed, OK otherwise
617 */
618 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100619do_cmdline(
620 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200621 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100622 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100623 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100625 char_u *next_cmdline; // next cmd to execute
626 char_u *cmdline_copy = NULL; // copy of cmd line
627 int used_getline = FALSE; // used "fgetline" to obtain command
628 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100630 int count = 0; // line number count
631 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 int retval = OK;
633#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100634 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100635 garray_T lines_ga; // keep lines for ":while"/":for"
636 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200637 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100638 char_u *fname = NULL; // function or script name
639 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
640 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
641 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200643 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200644 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100646 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200647 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000649 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000651 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100653# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654# define cmd_cookie cookie
655#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100656 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200657#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100658 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
659 // location for storing error messages to be converted to an exception.
660 // This ensures that the do_errthrow() call in do_one_cmd() does not
661 // combine the messages stored by an earlier invocation of do_one_cmd()
662 // with the command name of the later one. This would happen when
663 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 saved_msg_list = msg_list;
665 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666#endif
667
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 // It's possible to create an endless loop with ":execute", catch that
669 // here. The value of 200 allows nested function calls, ":source", etc.
670 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100671 if (call_depth >= 200
672#ifdef FEAT_EVAL
673 && call_depth >= p_mfd
674#endif
675 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000677 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100679 // When converting to an exception, we do not include the command name
680 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100681 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 msg_list = saved_msg_list;
683#endif
684 return FAIL;
685 }
686 ++call_depth;
687
688#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100689 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000691 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100695 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ++ex_nesting_level;
699
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 // Get the function or script name and the address where the next breakpoint
701 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000702 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 fname = func_name(real_cookie);
705 breakpoint = func_breakpoint(real_cookie);
706 dbg_tick = func_dbg_tick(real_cookie);
707 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100708 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100710 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 breakpoint = source_breakpoint(real_cookie);
712 dbg_tick = source_dbg_tick(real_cookie);
713 }
714
715 /*
716 * Initialize "force_abort" and "suppress_errthrow" at the top level.
717 */
718 if (!recursive)
719 {
720 force_abort = FALSE;
721 suppress_errthrow = FALSE;
722 }
723
724 /*
725 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000726 * exception handling (used when debugging). Otherwise clear it to avoid
727 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000729 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000730 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200732 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734 initial_trylevel = trylevel;
735
736 /*
737 * "did_throw" will be set to TRUE when an exception is being thrown.
738 */
739 did_throw = FALSE;
740#endif
741 /*
742 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000743 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * If force_abort is set, we cancel everything.
745 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100746#ifdef FEAT_EVAL
747 did_emsg_cumul += did_emsg;
748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 did_emsg = FALSE;
750
751 /*
752 * KeyTyped is only set when calling vgetc(). Reset it here when not
753 * calling vgetc() (sourced command lines).
754 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100755 if (!(flags & DOCMD_KEYTYPED)
756 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 KeyTyped = FALSE;
758
759 /*
760 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000761 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 * - for multiple commands on one line, separated with '|'
763 * - when repeating until there are no more lines (for ":source")
764 */
765 next_cmdline = cmdline;
766 do
767 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100769 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770#endif
771
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100772 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (next_cmdline == NULL
774#ifdef FEAT_EVAL
775 && !force_abort
776 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#endif
779 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100780 {
781#ifdef FEAT_EVAL
782 did_emsg_cumul += did_emsg;
783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
787 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000788 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100789 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 * 3. If a line is given: Make a copy, so we can mess with it.
791 */
792
793#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100794 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000795 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100797 // Each '|' separated command is stored separately in lines_ga, to
798 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100799 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100801 // Check if a function has returned or, unless it has an unclosed
802 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000806 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807 func_line_end(real_cookie);
808# endif
809 if (func_has_ended(real_cookie))
810 {
811 retval = FAIL;
812 break;
813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000816 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100817 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818 script_line_end();
819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100821 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100822 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
824 retval = FAIL;
825 break;
826 }
827
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100828 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 if (breakpoint != NULL && dbg_tick != NULL
830 && *dbg_tick != debug_tick)
831 {
832 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100833 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100834 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 *dbg_tick = debug_tick;
836 }
837
838 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100839 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100841 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100843 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100845 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100846 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100848 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100849 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 *dbg_tick = debug_tick;
851 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000852# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000853 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854 {
855 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100856 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100857 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000858 script_line_start();
859 }
860# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862#endif
863
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100864 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if (next_cmdline == NULL)
866 {
867 /*
868 * Need to set msg_didout for the first line after an ":if",
869 * otherwise the ":if" will be overwritten.
870 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100873 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874#ifdef FEAT_EVAL
875 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
876#else
877 0
878#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100879 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
880 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100882 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100883 // returned for the end of a sourced file or executed function
884 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 if (KeyTyped && !(flags & DOCMD_REPEAT))
886 need_wait_return = FALSE;
887 retval = FAIL;
888 break;
889 }
890 used_getline = TRUE;
891
892 /*
893 * Keep the first typed line. Clear it when more lines are typed.
894 */
895 if (flags & DOCMD_KEEPLINE)
896 {
897 vim_free(repeat_cmdline);
898 if (count == 0)
899 repeat_cmdline = vim_strsave(next_cmdline);
900 else
901 repeat_cmdline = NULL;
902 }
903 }
904
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100905 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 else if (cmdline_copy == NULL)
907 {
908 next_cmdline = vim_strsave(next_cmdline);
909 if (next_cmdline == NULL)
910 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200911 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 retval = FAIL;
913 break;
914 }
915 }
916 cmdline_copy = next_cmdline;
917
918#ifdef FEAT_EVAL
919 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200920 * Inside a while/for loop, and when the command looks like a ":while"
921 * or ":for", the line is stored, because we may need it later when
922 * looping.
923 *
924 * When there is a '|' and another command, it is stored separately,
925 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000926 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200927 *
928 * Pass a different "fgetline" function to do_one_cmd() below,
929 * that it stores lines in or reads them from "lines_ga". Makes it
930 * possible to define a function inside a while/for loop and handles
931 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200933 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200935 cmd_getline = get_loop_line;
936 cmd_cookie = (void *)&cmd_loop_cookie;
937 cmd_loop_cookie.lines_gap = &lines_ga;
938 cmd_loop_cookie.current_line = current_line;
939 cmd_loop_cookie.getline = fgetline;
940 cmd_loop_cookie.cookie = cookie;
941 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
942
943 // Save the current line when encountering it the first time.
944 if (current_line == lines_ga.ga_len
945 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 {
947 retval = FAIL;
948 break;
949 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200950 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200952 else
953 {
954 cmd_getline = fgetline;
955 cmd_cookie = cookie;
956 }
957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 did_endif = FALSE;
959#endif
960
961 if (count++ == 0)
962 {
963 /*
964 * All output from the commands is put below each other, without
965 * waiting for a return. Don't do this when executing commands
966 * from a script or when being called recursive (e.g. for ":e
967 * +command file").
968 */
969 if (!(flags & DOCMD_NOWAIT) && !recursive)
970 {
971 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100972 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100974 msg_scroll = TRUE; // put messages below each other
975 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 ++RedrawingDisabled;
977 did_inc = TRUE;
978 }
979 }
980
Bram Moolenaar823654b2020-05-29 23:03:09 +0200981 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100982 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984 /*
985 * 2. Execute one '|' separated command.
986 * do_one_cmd() will return NULL if there is no trailing '|'.
987 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
988 */
989 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100990 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef FEAT_EVAL
992 &cstack,
993#endif
994 cmd_getline, cmd_cookie);
995 --recursive;
996
997#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000998 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100999 // Use "current_line" from "cmd_loop_cookie", it may have been
1000 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001001 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002#endif
1003
1004 if (next_cmdline == NULL)
1005 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001006 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 /*
1009 * If the command was typed, remember it for the ':' register.
1010 * Do this AFTER executing the command to make :@: work.
1011 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001012 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 && new_last_cmdline != NULL)
1014 {
1015 vim_free(last_cmdline);
1016 last_cmdline = new_last_cmdline;
1017 new_last_cmdline = NULL;
1018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 else
1021 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001022 // need to copy the command after the '|' to cmdline_copy, for the
1023 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001024 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 next_cmdline = cmdline_copy;
1026 }
1027
1028
1029#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001030 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001032 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001034 {
1035 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
1041 ++current_line;
1042
1043 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 * An ":endwhile", ":endfor" and ":continue" is handled here.
1045 * If we were executing commands, jump back to the ":while" or
1046 * ":for".
1047 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001053 // Jump back to the matching ":while" or ":for". Be careful
1054 // not to use a cs_line[] from an entry that isn't a ":while"
1055 // or ":for": It would make "current_line" invalid and can
1056 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (!did_emsg && !got_int && !did_throw
1058 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 && (cstack.cs_flags[cstack.cs_idx]
1060 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 && cstack.cs_line[cstack.cs_idx] >= 0
1062 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1063 {
1064 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001065 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001066 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001067 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001069 // Check for the next breakpoint at or after the ":while"
1070 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001071 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
1073 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001074 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 fname,
1076 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1077 *dbg_tick = debug_tick;
1078 }
1079 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001082 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001084 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1085 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 }
1088
1089 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001095 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
1097 }
1098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001099 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001100 if (breakpoint != NULL && has_watchexpr())
1101 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001102 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001103 *dbg_tick = debug_tick;
1104 }
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 /*
1107 * When not inside any ":while" loop, clear remembered lines.
1108 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
1111 if (lines_ga.ga_len > 0)
1112 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001113 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1115 free_cmdlines(&lines_ga);
1116 }
1117 current_line = 0;
1118 }
1119
1120 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1122 * being restored at the ":endtry". Reset them here and set the
1123 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1124 * This includes the case where a missing ":endif", ":endwhile" or
1125 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001127 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 cstack.cs_lflags &= ~CSL_HAD_FINA;
1130 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1131 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 did_throw ? (void *)current_exception : NULL);
1133 did_emsg = got_int = did_throw = FALSE;
1134 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1135 }
1136
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001137 // Update global "trylevel" for recursive calls to do_cmdline() from
1138 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 trylevel = initial_trylevel + cstack.cs_trylevel;
1140
1141 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001142 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 * files) is aborted because of an error, an interrupt, or an uncaught
1144 * exception, cancel everything. If it is left normally, reset
1145 * force_abort to get the non-EH compatible abortion behavior for
1146 * the rest of the script.
1147 */
1148 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1149 force_abort = FALSE;
1150
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001151 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001153#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155 }
1156 /*
1157 * Continue executing command lines when:
1158 * - no CTRL-C typed, no aborting error, no exception thrown or try
1159 * conditionals need to be checked for executing finally clauses or
1160 * catching an interrupt exception
1161 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 * looping for ":source" command or function call.
1164 */
1165 while (!((got_int
1166#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001167 || (did_emsg && (force_abort || in_vim9script()))
1168 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169#endif
1170 )
1171#ifdef FEAT_EVAL
1172 && cstack.cs_trylevel == 0
1173#endif
1174 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001175 && !(did_emsg
1176#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001177 // Keep going when inside try/catch, so that the error can be
1178 // deal with, except when it is a syntax error, it may cause
1179 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001180 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1181#endif
1182 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001183 && (getline_equal(fgetline, cookie, getexmodeline)
1184 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 && (next_cmdline != NULL
1186#ifdef FEAT_EVAL
1187 || cstack.cs_idx >= 0
1188#endif
1189 || (flags & DOCMD_REPEAT)));
1190
1191 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001192 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193#ifdef FEAT_EVAL
1194 free_cmdlines(&lines_ga);
1195 ga_clear(&lines_ga);
1196
1197 if (cstack.cs_idx >= 0)
1198 {
1199 /*
1200 * If a sourced file or executed function ran to its end, report the
1201 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001202 * In Vim9 script do not give a second error, executing aborts after
1203 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001205 if (!got_int && !did_throw && !aborting()
1206 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001207 && ((getline_equal(fgetline, cookie, getsourceline)
1208 && !source_finished(fgetline, cookie))
1209 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 && !func_has_ended(real_cookie))))
1211 {
1212 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001213 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001215 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001216 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001217 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001219 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 }
1221
1222 /*
1223 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1224 * ":endtry" in a sourced file or executed function. If the try
1225 * conditional is in its finally clause, ignore anything pending.
1226 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001227 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 */
1229 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001230 {
1231 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1232
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001233 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001234 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001235 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1236 &cstack.cs_looplevel);
1237 }
1238 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 trylevel = initial_trylevel;
1240 }
1241
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001242 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1243 // lack was reported above and the error message is to be converted to an
1244 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001245 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 ? (char_u *)"endfunction" : (char_u *)NULL);
1247
1248 if (trylevel == 0)
1249 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001250 // Just in case did_throw got set but current_exception wasn't.
1251 if (current_exception == NULL)
1252 did_throw = FALSE;
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 /*
1255 * When an exception is being thrown out of the outermost try
1256 * conditional, discard the uncaught exception, disable the conversion
1257 * of interrupts or errors to exceptions, and ensure that no more
1258 * commands are executed.
1259 */
1260 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001261 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
1263 /*
1264 * On an interrupt or an aborting error not converted to an exception,
1265 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001266 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * when force_abort is set and did_emsg unset in case of an interrupt
1268 * from a finally clause after an error.
1269 */
1270 else if (got_int || (did_emsg && force_abort))
1271 suppress_errthrow = TRUE;
1272 }
1273
1274 /*
1275 * The current cstack will be freed when do_cmdline() returns. An uncaught
1276 * exception will have to be rethrown in the previous cstack. If a function
1277 * has just returned or a script file was just finished and the previous
1278 * cstack belongs to the same function or, respectively, script file, it
1279 * will have to be checked for finally clauses to be executed due to the
1280 * ":return" or ":finish". This is done in do_one_cmd().
1281 */
1282 if (did_throw)
1283 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001284 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001286 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 && ex_nesting_level > func_level(real_cookie) + 1))
1288 {
1289 if (!did_throw)
1290 check_cstack = TRUE;
1291 }
1292 else
1293 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001294 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001295 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 --ex_nesting_level;
1297 /*
1298 * Go to debug mode when returning from a function in which we are
1299 * single-stepping.
1300 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001301 if ((getline_equal(fgetline, cookie, getsourceline)
1302 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001304 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 ? (char_u *)_("End of sourced file")
1306 : (char_u *)_("End of function"));
1307 }
1308
1309 /*
1310 * Restore the exception environment (done after returning from the
1311 * debugger).
1312 */
1313 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001314 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
1316 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001317
1318 // Cleanup if "cs_emsg_silent_list" remains.
1319 if (cstack.cs_emsg_silent_list != NULL)
1320 {
1321 eslist_T *elem, *temp;
1322
1323 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1324 {
1325 temp = elem->next;
1326 vim_free(elem);
1327 }
1328 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001329#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
1331 /*
1332 * If there was too much output to fit on the command line, ask the user to
1333 * hit return before redrawing the screen. With the ":global" command we do
1334 * this only once after the command is finished.
1335 */
1336 if (did_inc)
1337 {
1338 --RedrawingDisabled;
1339 --no_wait_return;
1340 msg_scroll = FALSE;
1341
1342 /*
1343 * When just finished an ":if"-":else" which was typed, no need to
1344 * wait for hit-return. Also for an error situation.
1345 */
1346 if (retval == FAIL
1347#ifdef FEAT_EVAL
1348 || (did_endif && KeyTyped && !did_emsg)
1349#endif
1350 )
1351 {
1352 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001353 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355 else if (need_wait_return)
1356 {
1357 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001358 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 * here should not overwrite the command that may be shown before
1360 * doing that.
1361 */
1362 msg_didout |= msg_didout_before_start;
1363 wait_return(FALSE);
1364 }
1365 }
1366
Bram Moolenaar2a942252012-11-28 23:03:07 +01001367#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001368 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001369#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /*
1371 * Reset if_level, in case a sourced script file contains more ":if" than
1372 * ":endif" (could be ":if x | foo | endif").
1373 */
1374 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001375#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 --call_depth;
1378 return retval;
1379}
1380
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001381#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001382/*
1383 * Handle when "did_throw" is set after executing commands.
1384 */
1385 void
1386handle_did_throw()
1387{
1388 char *p = NULL;
1389 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001390 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001391
1392 /*
1393 * If the uncaught exception is a user exception, report it as an
1394 * error. If it is an error exception, display the saved error
1395 * message now. For an interrupt exception, do nothing; the
1396 * interrupt message is given elsewhere.
1397 */
1398 switch (current_exception->type)
1399 {
1400 case ET_USER:
1401 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001402 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001403 current_exception->value);
1404 p = (char *)vim_strsave(IObuff);
1405 break;
1406 case ET_ERROR:
1407 messages = current_exception->messages;
1408 current_exception->messages = NULL;
1409 break;
1410 case ET_INTERRUPT:
1411 break;
1412 }
1413
1414 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1415 current_exception->throw_lnum);
1416 ESTACK_CHECK_SETUP
1417 current_exception->throw_name = NULL;
1418
1419 discard_current_exception(); // uses IObuff if 'verbose'
1420 suppress_errthrow = TRUE;
1421 force_abort = TRUE;
1422
1423 if (messages != NULL)
1424 {
1425 do
1426 {
1427 msglist_T *next = messages->next;
1428 int save_compiling = estack_compiling;
1429
1430 estack_compiling = messages->msg_compiling;
1431 emsg(messages->msg);
1432 vim_free(messages->msg);
1433 vim_free(messages->sfile);
1434 vim_free(messages);
1435 messages = next;
1436 estack_compiling = save_compiling;
1437 }
1438 while (messages != NULL);
1439 }
1440 else if (p != NULL)
1441 {
1442 emsg(p);
1443 vim_free(p);
1444 }
1445 vim_free(SOURCING_NAME);
1446 ESTACK_CHECK_NOW
1447 estack_pop();
1448}
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001451 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 */
1453 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001454get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001456 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 wcmd_T *wp;
1458 char_u *line;
1459
1460 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1461 {
1462 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001463 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001465 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (cp->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001467 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001469 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001470 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 ++cp->current_line;
1472
1473 return line;
1474 }
1475
1476 KeyTyped = FALSE;
1477 ++cp->current_line;
1478 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001479 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 return vim_strsave(wp->line);
1481}
1482
1483/*
1484 * Store a line in "gap" so that a ":while" loop can execute it again.
1485 */
1486 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001487store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 if (ga_grow(gap, 1) == FAIL)
1490 return FAIL;
1491 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001492 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 return OK;
1495}
1496
1497/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001498 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 */
1500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001501free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
1503 while (gap->ga_len > 0)
1504 {
1505 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1506 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
1508}
1509#endif
1510
1511/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001512 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1513 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001516getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001517 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001518 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001519 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
1521#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001522 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001525 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1526 // function that's originally used to obtain the lines. This may be
1527 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001528 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001529 cp = (struct loop_cookie *)cookie;
1530 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
1532 gp = cp->getline;
1533 cp = cp->cookie;
1534 }
1535 return gp == func;
1536#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001537 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538#endif
1539}
1540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001542 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 * getline function. Otherwise return "cookie".
1544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001546getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001547 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001548 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
Bram Moolenaar13505972019-01-24 15:04:48 +01001550#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001551 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001552 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001554 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1555 // cookie that's originally used to obtain the lines. This may be nested
1556 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001557 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001558 cp = (struct loop_cookie *)cookie;
1559 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
1561 gp = cp->getline;
1562 cp = cp->cookie;
1563 }
1564 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001565#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaard5053d02020-06-28 15:51:16 +02001570#if defined(FEAT_EVAL) || defined(PROT)
1571/*
1572 * Get the next line source line without advancing.
1573 */
1574 char_u *
1575getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001576 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001577 void *cookie) // argument for fgetline()
1578{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001579 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001580 struct loop_cookie *cp;
1581 wcmd_T *wp;
1582
1583 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1584 // cookie that's originally used to obtain the lines. This may be nested
1585 // several levels.
1586 gp = fgetline;
1587 cp = (struct loop_cookie *)cookie;
1588 while (gp == get_loop_line)
1589 {
1590 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1591 {
1592 // executing lines a second time, use the stored copy
1593 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1594 return wp->line;
1595 }
1596 gp = cp->getline;
1597 cp = cp->cookie;
1598 }
1599 if (gp == getsourceline)
1600 return source_nextline(cp);
1601 return NULL;
1602}
1603#endif
1604
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605
1606/*
1607 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1608 * ":bwipeout", etc.
1609 * Returns the buffer number.
1610 */
1611 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001612compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613{
1614 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001615 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001616 int count = offset;
1617
1618 buf = firstbuf;
1619 while (buf->b_next != NULL && buf->b_fnum < lnum)
1620 buf = buf->b_next;
1621 while (count != 0)
1622 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001623 count += (offset < 0) ? 1 : -1;
1624 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1625 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001626 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001627 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001629 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001630 while (buf->b_ml.ml_mfp == NULL)
1631 {
1632 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1633 if (nextbuf == NULL)
1634 break;
1635 buf = nextbuf;
1636 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001637 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001638 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001639 if (addr_type == ADDR_LOADED_BUFFERS)
1640 while (buf->b_ml.ml_mfp == NULL)
1641 {
1642 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1643 if (nextbuf == NULL)
1644 break;
1645 buf = nextbuf;
1646 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001647 return buf->b_fnum;
1648}
1649
Bram Moolenaarb7316892019-05-01 18:08:42 +02001650/*
1651 * Return the window number of "win".
1652 * When "win" is NULL return the number of windows.
1653 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001654 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001655current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001656{
1657 win_T *wp;
1658 int nr = 0;
1659
Bram Moolenaar29323592016-07-24 22:04:11 +02001660 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001661 {
1662 ++nr;
1663 if (wp == win)
1664 break;
1665 }
1666 return nr;
1667}
1668
1669 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001670current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001671{
1672 tabpage_T *tp;
1673 int nr = 0;
1674
Bram Moolenaar29323592016-07-24 22:04:11 +02001675 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001676 {
1677 ++nr;
1678 if (tp == tab)
1679 break;
1680 }
1681 return nr;
1682}
1683
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001684 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001685comment_start(char_u *p, int starts_with_colon UNUSED)
1686{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001687 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001688 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001689 return *p == '"';
1690}
1691
Bram Moolenaarf240e182014-11-27 18:33:02 +01001692# define CURRENT_WIN_NR current_win_nr(curwin)
1693# define LAST_WIN_NR current_win_nr(NULL)
1694# define CURRENT_TAB_NR current_tab_nr(curtab)
1695# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001696
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697/*
1698 * Execute one Ex command.
1699 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001700 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1701 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 *
1703 * 1. skip comment lines and leading space
1704 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001705 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001707 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001708 * 6. parse arguments
1709 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001711 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 *
1713 * This function may be called recursively!
1714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001716do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001717 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001718 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001720 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001722 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001723 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001725 char_u *p;
1726 linenr_T lnum;
1727 long n;
1728 char *errormsg = NULL; // error message
1729 char_u *after_modifier = NULL;
1730 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001731 cmdmod_T save_cmdmod;
1732 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001733 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001734 int ni; // set when Not Implemented
1735 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001736 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001737 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001738 int vim9script;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001739#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001740 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001741#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001742 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001743 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaara80faa82020-04-12 19:37:17 +02001745 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 ea.line1 = 1;
1747 ea.line2 = 1;
1748#ifdef FEAT_EVAL
1749 ++ex_nesting_level;
1750#endif
1751
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001752 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 if (quitmore
1754#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001755 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001756 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001757#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001758 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001759 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 --quitmore;
1761
1762 /*
1763 * Reset browse, confirm, etc.. They are restored when returning, for
1764 * recursive calls.
1765 */
1766 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001768 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001769 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1770 goto doend;
1771
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001772/*
1773 * 1. Skip comment lines and leading white space and colons.
1774 * 2. Handle command modifiers.
1775 */
1776 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001778 ea.cmdlinep = cmdlinep;
1779 ea.getline = fgetline;
1780 ea.cookie = cookie;
1781#ifdef FEAT_EVAL
1782 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001783 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001785 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001786 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001787 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001788 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
1790#ifdef FEAT_EVAL
1791 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1792 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1793#else
1794 ea.skip = (if_level > 0);
1795#endif
1796
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001798 * 3. Skip over the range to find the command. Let "p" point to after it.
1799 *
1800 * We need the command to know what kind of range it uses.
1801 */
1802 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001803
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001804 // In Vim9 script a colon is required before the range. This may also be
1805 // after command modifiers.
Bram Moolenaareda29c92022-10-02 12:59:00 +01001806 vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001807 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001808 {
1809 may_have_range = FALSE;
1810 for (p = ea.cmd; p >= *cmdlinep; --p)
1811 {
1812 if (*p == ':')
1813 may_have_range = TRUE;
1814 if (p < ea.cmd && !VIM_ISWHITE(*p))
1815 break;
1816 }
1817 }
1818 else
1819 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001820 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001821 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001823#ifdef FEAT_EVAL
1824 // Handle ":export" - it functions almost like a command modifier.
1825 // ":export var Name: type"
1826 // ":export def Name(..."
1827 // etc.
1828 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1829 is_export = TRUE;
1830#endif
1831
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001832 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001833 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001834 if (ea.cmd == cmd + 1 && *cmd == '$')
1835 // should be "$VAR = val"
1836 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001837#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001838 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001839#else
1840 p = find_ex_command(&ea, NULL, NULL, NULL);
1841#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001842 if (ea.cmdidx == CMD_SIZE)
1843 {
1844 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1845
1846 // If a ':' before the range is missing, give a clearer error
1847 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001848 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001849 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001850 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001851 goto doend;
1852 }
1853 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001854 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001857
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001858#ifdef FEAT_EVAL
1859# ifdef FEAT_PROFILE
1860 // Count this line for profiling if skip is TRUE.
1861 if (do_profiling == PROF_YES
1862 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1863 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1864 {
1865 int skip = did_emsg || got_int || did_throw;
1866
1867 if (ea.cmdidx == CMD_catch)
1868 skip = !skip && !(cstack->cs_idx >= 0
1869 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1870 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1871 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1872 skip = skip || !(cstack->cs_idx >= 0
1873 && !(cstack->cs_flags[cstack->cs_idx]
1874 & (CSF_ACTIVE | CSF_TRUE)));
1875 else if (ea.cmdidx == CMD_finally)
1876 skip = FALSE;
1877 else if (ea.cmdidx != CMD_endif
1878 && ea.cmdidx != CMD_endfor
1879 && ea.cmdidx != CMD_endtry
1880 && ea.cmdidx != CMD_endwhile)
1881 skip = ea.skip;
1882
1883 if (!skip)
1884 {
1885 if (getline_equal(fgetline, cookie, get_func_line))
1886 func_line_exec(getline_cookie(fgetline, cookie));
1887 else if (getline_equal(fgetline, cookie, getsourceline))
1888 script_line_exec();
1889 }
1890 }
1891# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001892#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001893
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001894 ea.cmd = cmd;
1895
1896#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001897 // May go to debug mode. If this happens and the ">quit" debug command is
1898 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001899 dbg_check_breakpoint(&ea);
1900 if (!ea.skip && got_int)
1901 {
1902 ea.skip = TRUE;
1903 (void)do_intthrow(cstack);
1904 }
1905#endif
1906
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001907/*
1908 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 *
1910 * where 'addr' is:
1911 *
1912 * % (entire file)
1913 * $ [+-NUM]
1914 * 'x [+-NUM] (where x denotes a currently defined mark)
1915 * . [+-NUM]
1916 * [+-NUM]..
1917 * NUM
1918 *
1919 * The ea.cmd pointer is updated to point to the first character following the
1920 * range spec. If an initial address is found, but no second, the upper bound
1921 * is equal to the lower.
1922 */
1923
Bram Moolenaar25190db2019-05-04 15:05:28 +02001924 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001925 if (!IS_USER_CMDIDX(ea.cmdidx))
1926 {
1927 if (ea.cmdidx != CMD_SIZE)
1928 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1929 else
1930 ea.addr_type = ADDR_LINES;
1931
Bram Moolenaar25190db2019-05-04 15:05:28 +02001932 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001933 if (ea.cmdidx == CMD_wincmd && p != NULL)
1934 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001935#ifdef FEAT_QUICKFIX
1936 // :.cc in quickfix window uses line number
1937 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1938 ea.addr_type = ADDR_OTHER;
1939#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001940 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001941
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001942 if (!may_have_range)
1943 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001944 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1945 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001948 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 */
1950
1951 /*
1952 * Skip ':' and any white space
1953 */
1954 ea.cmd = skipwhite(ea.cmd);
1955 while (*ea.cmd == ':')
1956 ea.cmd = skipwhite(ea.cmd + 1);
1957
1958 /*
1959 * If we got a line, but no command, then go to the line.
1960 * If we find a '|' or '\n' we set ea.nextcmd.
1961 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001962 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1963 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
1965 /*
1966 * strange vi behaviour:
1967 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001968 * ":3|..." prints line 3 (not in Vim9 script)
1969 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001971 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001973 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 goto doend;
1975 }
1976
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001977 // If this looks like an undefined user command and there are CmdUndefined
1978 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001979 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1980 && ASCII_ISUPPER(*ea.cmd)
1981 && has_cmdundefined())
1982 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001983 int ret;
1984
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001985 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001986 while (ASCII_ISALNUM(*p))
1987 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001988 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001989 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1990 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001991 // If the autocommands did something and didn't cause an error, try
1992 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001993 p = (ret
1994#ifdef FEAT_EVAL
1995 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001996#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001998 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001999
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (p == NULL)
2001 {
2002 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002003 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 goto doend;
2005 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002006 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002007 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2008 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
2010 errormsg = uc_fun_cmd();
2011 goto doend;
2012 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002013
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (ea.cmdidx == CMD_SIZE)
2015 {
2016 if (!ea.skip)
2017 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002018 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002020 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002021 // If the modifier was parsed OK the error must be in the
2022 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002023 if (after_modifier != NULL)
2024 append_command(after_modifier);
2025 else
2026 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002027 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002028 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002029 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002030 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 }
2032 goto doend;
2033 }
2034
Bram Moolenaar958636c2014-10-21 20:01:58 +02002035 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2036 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002037#ifdef HAVE_EX_SCRIPT_NI
2038 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2039#endif
2040 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042#ifndef FEAT_EVAL
2043 /*
2044 * When the expression evaluation is disabled, recognize the ":if" and
2045 * ":endif" commands and ignore everything in between it.
2046 */
2047 if (ea.cmdidx == CMD_if)
2048 ++if_level;
2049 if (if_level)
2050 {
2051 if (ea.cmdidx == CMD_endif)
2052 --if_level;
2053 goto doend;
2054 }
2055
2056#endif
2057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002058 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002059 if (*p == '!' && ea.cmdidx != CMD_substitute
2060 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
2062 ++p;
2063 ea.forceit = TRUE;
2064 }
2065 else
2066 ea.forceit = FALSE;
2067
2068/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002069 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002071 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002072 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 if (!ea.skip)
2075 {
2076#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002077 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002079 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002080 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 goto doend;
2082 }
2083#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002084 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002085 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002086 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002087 goto doend;
2088 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002089 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002091 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002092 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 goto doend;
2094 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002095
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002096 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002098 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2099 {
2100 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002101 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002102 goto doend;
2103 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002104 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2105 {
2106 // Command not allowed when text is locked
2107 errormsg = _(get_text_locked_msg());
2108 goto doend;
2109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002111
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002112 // Disallow editing another buffer when "curbuf_lock" is set.
2113 // Do allow ":checktime" (it is postponed).
2114 // Do allow ":edit" (check for an argument later).
2115 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002116 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002117 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002118 && ea.cmdidx != CMD_edit
2119 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002120 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002121 && curbuf_locked())
2122 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002124 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002126 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 goto doend;
2128 }
2129 }
2130
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002131 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002133 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 goto doend;
2135 }
2136
2137 /*
2138 * Don't complain about the range if it is not used
2139 * (could happen if line_count is accidentally set to 0).
2140 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002141 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
2143 /*
2144 * If the range is backwards, ask for confirmation and, if given, swap
2145 * ea.line1 & ea.line2 so it's forwards again.
2146 * When global command is busy, don't ask, will fail below.
2147 */
2148 if (!global_busy && ea.line1 > ea.line2)
2149 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002150 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002152 if (sourcing || exmode_active)
2153 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002154 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002155 goto doend;
2156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 if (ask_yesno((char_u *)
2158 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002159 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
2161 lnum = ea.line1;
2162 ea.line1 = ea.line2;
2163 ea.line2 = lnum;
2164 }
2165 if ((errormsg = invalid_range(&ea)) != NULL)
2166 goto doend;
2167 }
2168
Bram Moolenaarb7316892019-05-01 18:08:42 +02002169 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2170 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 ea.line2 = 1;
2172
2173 correct_range(&ea);
2174
2175#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002176 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002177 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002179 // Put the first line at the start of a closed fold, put the last line
2180 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 (void)hasFolding(ea.line1, &ea.line1, NULL);
2182 (void)hasFolding(ea.line2, NULL, &ea.line2);
2183 }
2184#endif
2185
2186#ifdef FEAT_QUICKFIX
2187 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002188 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 * option here, so things like % get expanded.
2190 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002191 p = replace_makeprg(&ea, p, cmdlinep);
2192 if (p == NULL)
2193 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194#endif
2195
2196 /*
2197 * Skip to start of argument.
2198 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2199 */
2200 if (ea.cmdidx == CMD_bang)
2201 ea.arg = p;
2202 else
2203 ea.arg = skipwhite(p);
2204
Bram Moolenaar379fb762018-08-30 15:58:28 +02002205 // ":file" cannot be run with an argument when "curbuf_lock" is set
2206 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2207 goto doend;
2208
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 /*
2210 * Check for "++opt=val" argument.
2211 * Must be first, allow ":w ++enc=utf8 !cmd"
2212 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002213 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2215 if (getargopt(&ea) == FAIL && !ni)
2216 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002217 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 goto doend;
2219 }
2220
2221 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2222 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002223 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002225 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002227 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 goto doend;
2229 }
2230 ea.arg = skipwhite(ea.arg + 1);
2231 ea.append = TRUE;
2232 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002233 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
2235 ++ea.arg;
2236 ea.usefilter = TRUE;
2237 }
2238 }
2239
2240 if (ea.cmdidx == CMD_read)
2241 {
2242 if (ea.forceit)
2243 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002244 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 ea.forceit = FALSE;
2246 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002247 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
2249 ++ea.arg;
2250 ea.usefilter = TRUE;
2251 }
2252 }
2253
2254 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2255 {
2256 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002257 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
2259 ++ea.arg;
2260 ++ea.amount;
2261 }
2262 ea.arg = skipwhite(ea.arg);
2263 }
2264
2265 /*
2266 * Check for "+command" argument, before checking for next command.
2267 * Don't do this for ":read !cmd" and ":write !cmd".
2268 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002269 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2271
2272 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002273 * For commands that do not use '|' inside their argument: Check for '|' to
2274 * separate commands and '"' or '#' to start comments.
2275 *
2276 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002277 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002278 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002279 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002281 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2282 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002283 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002284 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002285 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002286 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002287 || ea.cmdidx == CMD_global
2288 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002289 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002290#ifdef FEAT_EVAL
2291 || inside_block(&ea)
2292#endif
2293 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 for (p = ea.arg; *p; ++p)
2296 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002297 // Remove one backslash before a newline, so that it's possible to
2298 // pass a newline to the shell and also a newline that is preceded
2299 // with a backslash. This makes it impossible to end a shell
2300 // command in a backslash, but that doesn't appear useful.
2301 // Halving the number of backslashes is incompatible with previous
2302 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002304 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002305 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
2307 ea.nextcmd = p + 1;
2308 *p = NUL;
2309 break;
2310 }
2311 }
2312 }
2313
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002314 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002315 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002317 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002318 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002320 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002321 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002322 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002324#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002325 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002326 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002328 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002329 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002332 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2333 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002334 {
2335 ea.regname = *ea.arg++;
2336#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002337 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002338 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2339 {
kuuote08d7b1c2021-10-04 22:17:36 +01002340 if (!ea.skip)
2341 {
2342 set_expr_line(vim_strsave(ea.arg), &ea);
2343 did_set_expr_line = TRUE;
2344 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002345 ea.arg += STRLEN(ea.arg);
2346 }
2347#endif
2348 ea.arg = skipwhite(ea.arg);
2349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
2351
2352 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002353 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 * count, it's a buffer name.
2355 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002356 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002357 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002358 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002360 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002362 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002364 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 goto doend;
2366 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002367 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
2369 ea.line2 = n;
2370 if (ea.addr_count == 0)
2371 ea.addr_count = 1;
2372 }
2373 else
2374 {
2375 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002376 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002377 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002378 else
2379 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 ++ea.addr_count;
2381 /*
2382 * Be vi compatible: no error message for out of range.
2383 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002384 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 ea.line2 = curbuf->b_ml.ml_line_count;
2386 }
2387 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002388
2389 /*
2390 * Check for flags: 'l', 'p' and '#'.
2391 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002392 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002393 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002394 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2395 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002397 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002398 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 goto doend;
2400 }
2401
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002402 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002404 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 goto doend;
2406 }
2407
2408#ifdef FEAT_EVAL
2409 /*
2410 * Skip the command when it's not going to be executed.
2411 * The commands like :if, :endif, etc. always need to be executed.
2412 * Also make an exception for commands that handle a trailing command
2413 * themselves.
2414 */
2415 if (ea.skip)
2416 {
2417 switch (ea.cmdidx)
2418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002419 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 case CMD_while:
2421 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002422 case CMD_for:
2423 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_if:
2425 case CMD_elseif:
2426 case CMD_else:
2427 case CMD_endif:
2428 case CMD_try:
2429 case CMD_catch:
2430 case CMD_finally:
2431 case CMD_endtry:
2432 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002433 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 break;
2435
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002436 // Commands that handle '|' themselves. Check: A command should
2437 // either have the EX_TRLBAR flag, appear in this list or appear in
2438 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 case CMD_aboveleft:
2440 case CMD_and:
2441 case CMD_belowright:
2442 case CMD_botright:
2443 case CMD_browse:
2444 case CMD_call:
2445 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002446 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 case CMD_delfunction:
2448 case CMD_djump:
2449 case CMD_dlist:
2450 case CMD_dsearch:
2451 case CMD_dsplit:
2452 case CMD_echo:
2453 case CMD_echoerr:
2454 case CMD_echomsg:
2455 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002456 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002458 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002459 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 case CMD_help:
2461 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002462 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 case CMD_ijump:
2464 case CMD_ilist:
2465 case CMD_isearch:
2466 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002467 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 case CMD_keepjumps:
2469 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002470 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 case CMD_leftabove:
2472 case CMD_let:
2473 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002474 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002475 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002477 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002478 case CMD_noautocmd:
2479 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 case CMD_perl:
2481 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002482 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002483 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002484 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 case CMD_return:
2486 case CMD_rightbelow:
2487 case CMD_ruby:
2488 case CMD_silent:
2489 case CMD_smagic:
2490 case CMD_snomagic:
2491 case CMD_substitute:
2492 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002493 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 case CMD_tcl:
2495 case CMD_throw:
2496 case CMD_tilde:
2497 case CMD_topleft:
2498 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002499 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002500 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 case CMD_verbose:
2502 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002503 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 break;
2505
2506 default: goto doend;
2507 }
2508 }
2509#endif
2510
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002511 if ((ea.argt & EX_XFILE)
2512 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2513 goto doend;
2514
2515#ifdef FEAT_EVAL
2516 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002518 emsg(_(e_invalid_command_after_export));
2519 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 /*
2524 * Accept buffer name. Cannot be used at the same time with a buffer
2525 * number. Don't do this for a user command.
2526 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002527 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002528 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 /*
2531 * :bdelete, :bwipeout and :bunload take several arguments, separated
2532 * by spaces: find next space (skipping over escaped characters).
2533 * The others take one argument: ignore trailing spaces.
2534 */
2535 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2536 || ea.cmdidx == CMD_bunload)
2537 p = skiptowhite_esc(ea.arg);
2538 else
2539 {
2540 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002541 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 --p;
2543 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002544 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002545 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002546 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 goto doend;
2548 ea.addr_count = 1;
2549 ea.arg = skipwhite(p);
2550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002552 // The :try command saves the emsg_silent flag, reset it here when
2553 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002554 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002555 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002556 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002557 if (emsg_silent < 0)
2558 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002559 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002560 }
2561
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002563 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
Bram Moolenaar958636c2014-10-21 20:01:58 +02002566 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
2568 /*
2569 * Execute a user-defined command.
2570 */
2571 do_ucmd(&ea);
2572 }
2573 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
2575 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002576 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2579 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002580 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582
2583#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002584 // A command will reset "is_export" when exporting an item. If it is still
2585 // set something went wrong.
2586 if (is_export)
2587 {
2588 if (errormsg == NULL)
2589 errormsg = _(e_export_with_invalid_argument);
2590 is_export = FALSE;
2591 }
2592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002594 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002595 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002596 && current_sctx.sc_sid > 0
2597 && ea.cmdidx != CMD_endif
2598 && (cstack->cs_idx < 0
2599 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002600 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 /*
2603 * If the command just executed called do_cmdline(), any throw or ":return"
2604 * or ":finish" encountered there must also check the cstack of the still
2605 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2606 * exception, or reanimate a returned function or finished script file and
2607 * return or finish it again.
2608 */
2609 if (need_rethrow)
2610 do_throw(cstack);
2611 else if (check_cstack)
2612 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002613 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002615 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 && current_func_returned())
2617 do_return(&ea, TRUE, FALSE, NULL);
2618 }
2619 need_rethrow = check_cstack = FALSE;
2620#endif
2621
2622doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002623 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002626 curwin->w_cursor.col = 0;
2627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
2629 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2630 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002631 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002633 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
2635 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002636 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002638 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
2640 emsg(errormsg);
2641 }
2642#ifdef FEAT_EVAL
2643 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002644 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2645 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002646
2647 if (did_set_expr_line)
2648 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002649 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650#endif
2651
Bram Moolenaare1004402020-10-24 20:49:43 +02002652 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002654 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002655 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002657 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 ea.nextcmd = NULL;
2659
2660#ifdef FEAT_EVAL
2661 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002662 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663#endif
2664
2665 return ea.nextcmd;
2666}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002668static char ex_error_buf[MSG_BUF_LEN];
2669
2670/*
2671 * Return an error message with argument included.
2672 * Uses a static buffer, only the last error will be kept.
2673 * "msg" will be translated, caller should use N_().
2674 */
2675 char *
2676ex_errmsg(char *msg, char_u *arg)
2677{
2678 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2679 return ex_error_buf;
2680}
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002683 * Handle a range without a command.
2684 * Returns an error message on failure.
2685 */
2686 char *
2687ex_range_without_command(exarg_T *eap)
2688{
2689 char *errormsg = NULL;
2690
2691 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2692#ifdef FEAT_EVAL
2693 && !in_vim9script()
2694#endif
2695 )
2696 {
2697 eap->cmdidx = CMD_print;
2698 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2699 if ((errormsg = invalid_range(eap)) == NULL)
2700 {
2701 correct_range(eap);
2702 ex_print(eap);
2703 }
2704 }
2705 else if (eap->addr_count != 0)
2706 {
2707 if (eap->line2 > curbuf->b_ml.ml_line_count)
2708 {
2709 // With '-' in 'cpoptions' a line number past the file is an
2710 // error, otherwise put it at the end of the file.
2711 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2712 eap->line2 = -1;
2713 else
2714 eap->line2 = curbuf->b_ml.ml_line_count;
2715 }
2716
2717 if (eap->line2 < 0)
2718 errormsg = _(e_invalid_range);
2719 else
2720 {
2721 if (eap->line2 == 0)
2722 curwin->w_cursor.lnum = 1;
2723 else
2724 curwin->w_cursor.lnum = eap->line2;
2725 beginline(BL_SOL | BL_FIX);
2726 }
2727 }
2728 return errormsg;
2729}
2730
2731/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002732 * Check for an Ex command with optional tail.
2733 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002734 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002735 */
2736 static int
2737checkforcmd_opt(
2738 char_u **pp, // start of command
2739 char *cmd, // name of command
2740 int len, // required length
2741 int noparen)
2742{
2743 int i;
2744
2745 for (i = 0; cmd[i] != NUL; ++i)
2746 if (((char_u *)cmd)[i] != (*pp)[i])
2747 break;
Bram Moolenaar68854a82022-01-31 18:59:13 +00002748 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_'
2749 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002750 {
2751 *pp = skipwhite(*pp + i);
2752 return TRUE;
2753 }
2754 return FALSE;
2755}
2756
2757/*
2758 * Check for an Ex command with optional tail.
2759 * If there is a match advance "pp" to the argument and return TRUE.
2760 */
2761 int
2762checkforcmd(
2763 char_u **pp, // start of command
2764 char *cmd, // name of command
2765 int len) // required length
2766{
2767 return checkforcmd_opt(pp, cmd, len, FALSE);
2768}
2769
2770/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002771 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002772 * If there is a match advance "pp" to the argument and return TRUE.
2773 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002774 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002775checkforcmd_noparen(
2776 char_u **pp, // start of command
2777 char *cmd, // name of command
2778 int len) // required length
2779{
2780 return checkforcmd_opt(pp, cmd, len, TRUE);
2781}
2782
2783/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002784 * Parse and skip over command modifiers:
2785 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002786 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002787 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002788 * When "skip_only" is TRUE the global variables are not changed, except for
2789 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002790 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2791 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002792 * Call apply_cmdmod() to get the side effects of the modifiers:
2793 * - Increment "sandbox" for ":sandbox"
2794 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002795 * - set msg_silent for ":silent"
2796 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002797 * Return FAIL when the command is not to be executed.
2798 * May set "errormsg" to an error message.
2799 */
2800 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002801parse_command_modifiers(
2802 exarg_T *eap,
2803 char **errormsg,
2804 cmdmod_T *cmod,
2805 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002806{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002807 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002808 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002809 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002810 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002811 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002812 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002813
Bram Moolenaare1004402020-10-24 20:49:43 +02002814 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002815 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002816
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002817 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2818 {
2819 // The automatically inserted Visual area range is skipped, so that
2820 // typing ":cmdmod cmd" in Visual mode works without having to move the
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002821 // range to after the modififiers. The command will be
2822 // "'<,'>cmdmod cmd", parse "cmdmod cmd" and then put back "'<,'>"
2823 // before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002824 eap->cmd += 5;
2825 cmd_start = eap->cmd;
2826 has_visual_range = TRUE;
2827 }
2828
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002829 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002830 for (;;)
2831 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002832 char_u *p;
2833
Bram Moolenaareffed932018-08-14 13:38:17 +02002834 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002835 {
2836 if (*eap->cmd == ':')
2837 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002839 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002840
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002841 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 if (*eap->cmd == NUL && exmode_active
2843 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2844 || getline_equal(eap->getline, eap->cookie, getexline))
2845 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2846 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002847 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002848 if (!skip_only)
2849 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002850 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002851 }
2852
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002853 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002854 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002855 {
2856 // a comment ends at a NL
2857 if (eap->nextcmd == NULL)
2858 {
2859 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2860 if (eap->nextcmd != NULL)
2861 ++eap->nextcmd;
2862 }
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002863 if (vim9script)
2864 {
2865 if (has_cmdmod(cmod, FALSE))
2866 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002867#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002868 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2869 && eap->cmd[2] != '{')
2870 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002871#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002872 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002873 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002874 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 if (*eap->cmd == NUL)
2876 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002877 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002878 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002879 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002880 if (vim9script && has_cmdmod(cmod, FALSE))
2881 *errormsg = _(e_command_modifier_without_command);
2882 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002883 return FAIL;
2884 }
2885
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002886 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002887
2888 // In Vim9 script a variable can shadow a command modifier:
2889 // verbose = 123
2890 // verbose += 123
2891 // silent! verbose = func()
2892 // verbose.member = 2
2893 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002894 // But not:
2895 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002896 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002897 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002898 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002899
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002900 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002901 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002902 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002903 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2904 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002905 break;
2906 }
2907
Bram Moolenaareffed932018-08-14 13:38:17 +02002908 switch (*p)
2909 {
Bram Moolenaar91324262022-09-09 13:27:59 +01002910 // When adding an entry, also modify cmdmods[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002911 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002912 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002913 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 continue;
2915
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002916 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002917 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002918 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002919 continue;
2920 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002921 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002922 {
2923#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002924 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002925#endif
2926 continue;
2927 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002928 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002929 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002930 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002931 continue;
2932
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002933 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002934 break;
2935#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002936 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002937#endif
2938 continue;
2939
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002940 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002941 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002942 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002943 continue;
2944 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002945 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002946 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002947 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002948 continue;
2949 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002950 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002951 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002952 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002953 continue;
2954 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002955 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002956 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002957 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 continue;
2959
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002960 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002961 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002962 char_u *reg_pat;
2963 char_u *nulp = NULL;
2964 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002965
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002966 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002967 || *p == NUL
2968 || (ends_excmd(*p)
2969#ifdef FEAT_EVAL
2970 // in ":filter #pat# cmd" # does not
2971 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002972 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002973#endif
2974 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002975 break;
2976 if (*p == '!')
2977 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002978 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002979 p = skipwhite(p + 1);
2980 if (*p == NUL || ends_excmd(*p))
2981 break;
2982 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002983#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002984 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002985 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002986 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002987#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002988 if (skip_only)
2989 p = skip_vimgrep_pat(p, NULL, NULL);
2990 else
2991 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002992 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2993 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 if (p == NULL || *p == NUL)
2995 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002996 if (!skip_only)
2997 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002998 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003000 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003001 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003002 // restore the character overwritten by NUL
3003 if (nulp != NULL)
3004 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003005 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003006 eap->cmd = p;
3007 continue;
3008 }
3009
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003010 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3011 {
3012 cmod->cmod_split |= WSP_HOR;
3013 continue;
3014 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003015 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003016 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003017 || *p == NUL || ends_excmd(*p))
3018 break;
3019 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003020 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 continue;
3022
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003023 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003024 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003025 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003026 continue;
3027 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003028 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3029 {
3030 if (ends_excmd2(p, eap->cmd))
3031 {
3032 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003033 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003034 return FAIL;
3035 }
3036 cmod->cmod_flags |= CMOD_LEGACY;
3037 continue;
3038 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003039
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003040 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003041 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003042 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003043 continue;
3044
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003045 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003046 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003047 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003048 continue;
3049 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003050 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003051 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003052 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003053 continue;
3054
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003055 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003056 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003057 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003058 continue;
3059
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003060 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003061 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003062 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003063 continue;
3064 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003065 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003066 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003067 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003068 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003070 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003071 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003072 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003073 }
3074 continue;
3075
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003076 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003077 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003078 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003079 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003080 long tabnr = get_address(eap, &eap->cmd,
3081 ADDR_TABS, eap->skip,
3082 skip_only, FALSE, 1);
3083 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003084 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003085 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003086 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003087 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3088 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003089 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003090 return FAIL;
3091 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003092 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003093 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003094 }
3095 eap->cmd = p;
3096 continue;
3097 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003098 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003099 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003100 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003101 continue;
3102
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003103 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003104 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003105 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003106 continue;
3107
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003108 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003109 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003110 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003111 continue;
3112 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003113 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003114 {
3115 if (ends_excmd2(p, eap->cmd))
3116 {
3117 *errormsg =
3118 _(e_vim9cmd_must_be_followed_by_command);
3119 return FAIL;
3120 }
3121 cmod->cmod_flags |= CMOD_VIM9CMD;
3122 continue;
3123 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003124 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003125 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003126 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003127 {
zeertzjqcd749632022-06-14 13:30:35 +01003128 // zero means not set, one is verbose == 0, etc.
3129 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003130 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003131 else
zeertzjqcd749632022-06-14 13:30:35 +01003132 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003133 eap->cmd = p;
3134 continue;
3135 }
3136 break;
3137 }
3138
Bram Moolenaar1501b632022-03-27 16:56:21 +01003139 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003140 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003141 if (eap->cmd > cmd_start)
3142 {
3143 // Move the '<,'> range to after the modifiers and insert a colon.
3144 // Since the modifiers have been parsed put the colon on top of the
3145 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3146 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003147 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003148 {
3149 size_t len = STRLEN(cmd_start);
3150
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003151 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003152 // "'<,'>mods" -> "mods *+
3153 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003154 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003155 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003156 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003157 }
3158 else
3159 {
3160 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3161 eap->cmd -= 5;
3162 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3163 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003164 }
3165 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003166 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003167 // Special case: change empty command to "+".
3168 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003169 eap->cmd = (char_u *)"'<,'>+";
3170 else
3171 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003172 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003173 else if (use_plus_cmd)
3174 eap->cmd = (char_u *)"+";
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003175
Bram Moolenaareffed932018-08-14 13:38:17 +02003176 return OK;
3177}
3178
3179/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003180 * Return TRUE if "cmod" has anything set.
3181 */
3182 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003183has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003184{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003185 return (cmod->cmod_flags != 0 && (!ignore_silent
3186 || (cmod->cmod_flags
3187 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003188 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003189 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003190 || cmod->cmod_tab != 0
3191 || cmod->cmod_filter_regmatch.regprog != NULL;
3192}
3193
Bram Moolenaar8991be22022-02-14 21:51:46 +00003194#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003195/*
3196 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3197 */
3198 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003199cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003200{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003201 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003202 {
3203 emsg(_(e_misplaced_command_modifier));
3204 return TRUE;
3205 }
3206 return FALSE;
3207}
Dominique Pelle748b3082022-01-08 12:41:16 +00003208#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003209
3210/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003211 * Apply the command modifiers. Saves current state in "cmdmod", call
3212 * undo_cmdmod() later.
3213 */
3214 void
3215apply_cmdmod(cmdmod_T *cmod)
3216{
3217#ifdef HAVE_SANDBOX
3218 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3219 {
3220 ++sandbox;
3221 cmod->cmod_did_sandbox = TRUE;
3222 }
3223#endif
zeertzjqcd749632022-06-14 13:30:35 +01003224 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003225 {
3226 if (cmod->cmod_verbose_save == 0)
3227 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003228 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003229 }
3230
3231 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3232 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003233 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003234 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003235 cmod->cmod_save_msg_scroll = msg_scroll;
3236 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003237 if (cmod->cmod_flags & CMOD_SILENT)
3238 ++msg_silent;
3239 if (cmod->cmod_flags & CMOD_UNSILENT)
3240 msg_silent = 0;
3241
3242 if (cmod->cmod_flags & CMOD_ERRSILENT)
3243 {
3244 ++emsg_silent;
3245 ++cmod->cmod_did_esilent;
3246 }
3247
Bram Moolenaare1004402020-10-24 20:49:43 +02003248 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003249 {
3250 // Set 'eventignore' to "all".
3251 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003252 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003253 set_string_option_direct((char_u *)"ei", -1,
3254 (char_u *)"all", OPT_FREE, SID_NONE);
3255 }
3256}
3257
3258/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003259 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003260 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003261 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003262undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003263{
Bram Moolenaare1004402020-10-24 20:49:43 +02003264 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003265 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003266 p_verbose = cmod->cmod_verbose_save - 1;
3267 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003268 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003269
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003270#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003271 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003272 {
3273 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003274 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003275 }
3276#endif
3277
Bram Moolenaare1004402020-10-24 20:49:43 +02003278 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003279 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003280 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003281 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3282 OPT_FREE, SID_NONE);
3283 free_string_option(cmod->cmod_save_ei);
3284 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003285 }
3286
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003287 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003288
Bram Moolenaare1004402020-10-24 20:49:43 +02003289 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003290 {
3291 // messages could be enabled for a serious error, need to check if the
3292 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003293 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3294 msg_silent = cmod->cmod_save_msg_silent - 1;
3295 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003296 if (emsg_silent < 0)
3297 emsg_silent = 0;
3298 // Restore msg_scroll, it's set by file I/O commands, even when no
3299 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003300 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003301
3302 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3303 // somewhere in the line. Put it back in the first column.
3304 if (redirecting())
3305 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003306
Bram Moolenaare1004402020-10-24 20:49:43 +02003307 cmod->cmod_save_msg_silent = 0;
3308 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003309 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003310}
3311
3312/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003313 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003314 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003315 * Return FAIL and set "errormsg" or return OK.
3316 */
3317 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003318parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003319{
3320 int address_count = 1;
3321 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003322 int need_check_cursor = FALSE;
3323 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003324
3325 // Repeat for all ',' or ';' separated addresses.
3326 for (;;)
3327 {
3328 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003329 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003330 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003331 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003332 eap->addr_count == 0, address_count++);
3333 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003334 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003335 if (lnum == MAXLNUM)
3336 {
3337 if (*eap->cmd == '%') // '%' - all lines
3338 {
3339 ++eap->cmd;
3340 switch (eap->addr_type)
3341 {
3342 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003343 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003344 eap->line1 = 1;
3345 eap->line2 = curbuf->b_ml.ml_line_count;
3346 break;
3347 case ADDR_LOADED_BUFFERS:
3348 {
3349 buf_T *buf = firstbuf;
3350
3351 while (buf->b_next != NULL
3352 && buf->b_ml.ml_mfp == NULL)
3353 buf = buf->b_next;
3354 eap->line1 = buf->b_fnum;
3355 buf = lastbuf;
3356 while (buf->b_prev != NULL
3357 && buf->b_ml.ml_mfp == NULL)
3358 buf = buf->b_prev;
3359 eap->line2 = buf->b_fnum;
3360 break;
3361 }
3362 case ADDR_BUFFERS:
3363 eap->line1 = firstbuf->b_fnum;
3364 eap->line2 = lastbuf->b_fnum;
3365 break;
3366 case ADDR_WINDOWS:
3367 case ADDR_TABS:
3368 if (IS_USER_CMDIDX(eap->cmdidx))
3369 {
3370 eap->line1 = 1;
3371 eap->line2 = eap->addr_type == ADDR_WINDOWS
3372 ? LAST_WIN_NR : LAST_TAB_NR;
3373 }
3374 else
3375 {
3376 // there is no Vim command which uses '%' and
3377 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003378 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003379 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003380 }
3381 break;
3382 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003383 case ADDR_UNSIGNED:
3384 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003385 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003386 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003387 case ADDR_ARGUMENTS:
3388 if (ARGCOUNT == 0)
3389 eap->line1 = eap->line2 = 0;
3390 else
3391 {
3392 eap->line1 = 1;
3393 eap->line2 = ARGCOUNT;
3394 }
3395 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003396 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003397#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003398 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003399 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003400 if (eap->line2 == 0)
3401 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003402#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003403 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003404 case ADDR_NONE:
3405 // Will give an error later if a range is found.
3406 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003407 }
3408 ++eap->addr_count;
3409 }
3410 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3411 {
3412 pos_T *fp;
3413
3414 // '*' - visual area
3415 if (eap->addr_type != ADDR_LINES)
3416 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003417 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003418 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003419 }
3420
3421 ++eap->cmd;
3422 if (!eap->skip)
3423 {
3424 fp = getmark('<', FALSE);
3425 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003426 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003427 eap->line1 = fp->lnum;
3428 fp = getmark('>', FALSE);
3429 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003430 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003431 eap->line2 = fp->lnum;
3432 ++eap->addr_count;
3433 }
3434 }
3435 }
3436 else
3437 eap->line2 = lnum;
3438 eap->addr_count++;
3439
3440 if (*eap->cmd == ';')
3441 {
3442 if (!eap->skip)
3443 {
3444 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003445
Bram Moolenaar0e717042020-04-27 19:29:01 +02003446 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003447 // accept zero as address, so 0;/PATTERN/ works correctly
3448 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003449 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003450 if (eap->line2 > 0)
3451 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003452 else
3453 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003454 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003455 }
3456 }
3457 else if (*eap->cmd != ',')
3458 break;
3459 ++eap->cmd;
3460 }
3461
3462 // One address given: set start and end lines.
3463 if (eap->addr_count == 1)
3464 {
3465 eap->line1 = eap->line2;
3466 // ... but only implicit: really no address given
3467 if (lnum == MAXLNUM)
3468 eap->addr_count = 0;
3469 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003470 ret = OK;
3471
3472theend:
3473 if (need_check_cursor)
3474 check_cursor();
3475 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003476}
3477
3478/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003479 * Append "cmd" to the error message in IObuff.
3480 * Takes care of limiting the length and handling 0xa0, which would be
3481 * invisible otherwise.
3482 */
3483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003484append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003485{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003486 size_t len = STRLEN(IObuff);
3487 char_u *s = cmd;
3488 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003489
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003490 if (len > IOSIZE - 100)
3491 {
3492 // Not enough space, truncate and put in "...".
3493 d = IObuff + IOSIZE - 100;
3494 d -= mb_head_off(IObuff, d);
3495 STRCPY(d, "...");
3496 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003497 STRCAT(IObuff, ": ");
3498 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003499 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003500 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003501 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003502 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003503 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003504 STRCPY(d, "<a0>");
3505 d += 4;
3506 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003507 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3508 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003509 else
3510 MB_COPY_CHAR(s, d);
3511 }
3512 *d = NUL;
3513}
3514
Dominique Pelle748b3082022-01-08 12:41:16 +00003515#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003516/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003517 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3518 * the name. Otherwise just return "start".
3519 */
3520 char_u *
3521skip_option_env_lead(char_u *start)
3522{
3523 char_u *name = start;
3524
3525 if (*start == '&')
3526 {
3527 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3528 name += 3;
3529 else
3530 name += 1;
3531 }
3532 else if (*start == '$')
3533 name += 1;
3534 return name;
3535}
Dominique Pelle748b3082022-01-08 12:41:16 +00003536#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003537
3538/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003539 * Return TRUE and set "*idx" if "p" points to a one letter command.
3540 * If not in Vim9 script:
3541 * - The 'k' command can directly be followed by any character.
3542 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3543 * but :sre[wind] is another command, as are :scr[iptnames],
3544 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3545 */
3546 static int
3547one_letter_cmd(char_u *p, cmdidx_T *idx)
3548{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003549 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003550 return FALSE;
3551 if (*p == 'k')
3552 {
3553 *idx = CMD_k;
3554 return TRUE;
3555 }
3556 if (p[0] == 's'
3557 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3558 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3559 || p[1] == 'g'
3560 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3561 || p[1] == 'I'
3562 || (p[1] == 'r' && p[2] != 'e')))
3563 {
3564 *idx = CMD_substitute;
3565 return TRUE;
3566 }
3567 return FALSE;
3568}
3569
3570/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003571 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3572 * call.
3573 */
3574 int
3575number_method(char_u *cmd)
3576{
3577 char_u *p = skipdigits(cmd);
3578
3579 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3580}
3581
3582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003584 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003585 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003586 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 *
3588 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3589 * assignment without "let". Sets eap->cmdidx to the command while returning
3590 * "eap->cmd".
3591 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 * Returns NULL for an ambiguous user command.
3593 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 char_u *
3595find_ex_command(
3596 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003597 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003598 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003599 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600{
3601 int len;
3602 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003603 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003604#ifndef FEAT_EVAL
3605 int vim9 = FALSE;
3606#else
3607 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 /*
3610 * Recognize a Vim9 script function/method call and assignment:
3611 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3612 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003613 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003614 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003616 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003618 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003619 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3620 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003622 int oplen;
3623 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003624 char_u *swp;
3625
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003626 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003627 || (eap->cmd[0] == '$'
3628 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003629 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003630 && (valid_yank_reg(eap->cmd[1], FALSE)
3631 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003632 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003633 if (*eap->cmd == '&')
3634 {
3635 p = eap->cmd + 1;
3636 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3637 p += 2;
3638 p = to_name_end(p, FALSE);
3639 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003640 else if (*eap->cmd == '$')
3641 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003642 else
3643 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003644 if (ends_excmd(*skipwhite(p)))
3645 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003646 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3647 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003648 eap->cmdidx = CMD_eval;
3649 return eap->cmd;
3650 }
3651 // "&option" can be followed by "->" or "=", check below
3652 }
3653
3654 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003656 if (
3657 // "(..." is an expression.
3658 // "funcname(" is always a function call.
3659 *p == '('
3660 || (p == eap->cmd
3661 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003662 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003663 *eap->cmd == '{'
3664 // "'string'->func()" is an expression.
3665 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003666 // '"string"->func()' is an expression.
3667 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003668 // '$"string"->func()' is an expression.
3669 // "$'string'->func()" is an expression.
3670 || (eap->cmd[0] == '$'
3671 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3672 // '0z1234->func()' is an expression.
3673 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003674 // "g:varname" is an expression.
3675 || eap->cmd[1] == ':'
3676 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003677 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003678 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003679 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003680 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3681 {
3682 // "{" by itself is the start of a block.
3683 eap->cmdidx = CMD_block;
3684 return eap->cmd + 1;
3685 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003686 eap->cmdidx = CMD_eval;
3687 return eap->cmd;
3688 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003689
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003690 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003691 // "varname[]" is an expression.
3692 *p == '['
3693 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003694 || (*p == '.'
3695 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3696 // g:[key] is an expression
3697 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003698 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003699 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003700
3701 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003702 // Skip over the whole thing, it can be:
3703 // name.member = val
3704 // name[a : b] = val
3705 // name[idx] = val
3706 // name[idx].member = val
3707 // etc.
3708 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003709 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003710 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003711 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003712 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003713 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003714 || (after[0] == '.' && after[1] == '.'
3715 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003716 eap->cmdidx = CMD_var;
3717 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003718 --emsg_silent;
3719 return eap->cmd;
3720 }
3721
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003722 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3723 // an assignment.
3724 // If there is no line break inside the "[...]" then "p" is
3725 // advanced to after the "]" by to_name_const_end(): check if a "="
3726 // follows.
3727 // If "[...]" has a line break "p" still points at the "[" and it
3728 // can't be an assignment.
3729 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003730 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003731 char_u *eq;
3732
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003733 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003734 if (p == eap->cmd && *p == '[')
3735 {
3736 int count = 0;
3737 int semicolon = FALSE;
3738
3739 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3740 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003741 eq = p;
3742 if (eq != NULL)
3743 {
3744 eq = skipwhite(eq);
3745 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3746 ++eq;
3747 }
3748 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003749 {
3750 eap->cmdidx = CMD_eval;
3751 return eap->cmd;
3752 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003753 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003754 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003755 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003756 return eap->cmd;
3757 }
3758 }
3759
3760 // Recognize an assignment if we recognize the variable name:
3761 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003762 // "@r = expr"
3763 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003764 // "var = expr" where "var" is a variable name or we are skipping
3765 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003766 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003767 oplen = assignment_len(skipwhite(p), &heredoc);
3768 if (oplen > 0)
3769 {
3770 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3771 || *eap->cmd == '&'
3772 || *eap->cmd == '$'
3773 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003774 || (eap->skip && IS_WHITE_OR_NUL(
3775 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003776 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003777 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003778 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003779 return eap->cmd;
3780 }
3781 }
3782
Bram Moolenaar9510d222022-09-11 15:14:05 +01003783 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003784 // "w:varname: number = 123".
3785 if (eap->cmd[1] == ':' && *p == ':')
3786 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003787 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003788 return eap->cmd;
3789 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003790 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003791
Bram Moolenaar31d99482022-05-26 22:24:43 +01003792 // 1234->func() is a method call
3793 if (number_method(eap->cmd))
3794 {
3795 eap->cmdidx = CMD_eval;
3796 return eap->cmd;
3797 }
3798
Bram Moolenaar7b829262021-10-13 15:04:34 +01003799 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3800 // an expression. A global/substitute/list command needs to use a
3801 // longer name.
3802 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3803 {
3804 eap->cmdidx = CMD_eval;
3805 return eap->cmd;
3806 }
3807
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003808 // If it is an ID it might be a variable with an operator on the next
3809 // line, if the variable exists it can't be an Ex command.
3810 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003811 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003812 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3813 {
3814 eap->cmdidx = CMD_eval;
3815 return eap->cmd;
3816 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003817
3818 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003819 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3820 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003821 {
3822 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3823 return eap->cmd + 2;
3824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 }
3826#endif
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 /*
3829 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 */
3831 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003832 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 ++p;
3835 }
3836 else
3837 {
3838 while (ASCII_ISALPHA(*p))
3839 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003840 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003841 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003843 while (ASCII_ISALNUM(*p))
3844 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 }
3846 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3847 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003848 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 ++p;
3850 while (ASCII_ISALPHA(*p))
3851 ++p;
3852 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003853
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003854 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003855 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 ++p;
3857 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003858 // The "d" command can directly be followed by 'l' or 'p' flag, when
3859 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003860 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003861 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003862 // Check for ":dl", ":dell", etc. to ":deletel": that's
3863 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003864 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003865 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003866 break;
3867 if (i == len - 1)
3868 {
3869 --len;
3870 if (p[-1] == 'l')
3871 eap->flags |= EXFLAG_LIST;
3872 else
3873 eap->flags |= EXFLAG_PRINT;
3874 }
3875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003877 if (ASCII_ISLOWER(eap->cmd[0]))
3878 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003879 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003880 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003881
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003882 if (command_count != (int)CMD_SIZE)
3883 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003884 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003885 getout(1);
3886 }
3887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003888 // Use a precomputed index for fast look-up in cmdnames[]
3889 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003890 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3891 if (ASCII_ISLOWER(c2))
3892 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3893 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003894 else if (ASCII_ISUPPER(eap->cmd[0]))
3895 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003897 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898
3899 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3900 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3901 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3902 (size_t)len) == 0)
3903 {
3904#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003905 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 *full = TRUE;
3907#endif
3908 break;
3909 }
3910
Bram Moolenaar204852a2022-03-05 12:56:44 +00003911 // :Print and :mode are not supported in Vim9 script.
3912 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003913 if (vim9 && eap->cmdidx != CMD_SIZE)
3914 {
3915 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3916 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003917 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003918 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003919 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003920 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003921 eap->cmdidx = CMD_SIZE;
3922 }
3923 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003924
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003925 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003926 // 'cpoptions'.
3927 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3928 p = eap->cmd;
3929
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003930 // Look for a user defined command as a last resort. Let ":Print" be
3931 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003932 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3933 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003935 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 while (ASCII_ISALNUM(*p))
3937 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003938 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003940 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 eap->cmdidx = CMD_SIZE;
3942 }
3943
Bram Moolenaar204852a2022-03-05 12:56:44 +00003944 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3945 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003946 eap->cmdidx = CMD_finally;
3947
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003948#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003949 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003950 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003951 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003952 && (eap->cmdidx < 0 ||
3953 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003954 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003955 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3956
3957 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003958 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003959 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003960 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003961#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003962
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 return p;
3964}
3965
3966#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003967static struct cmdmod
3968{
3969 char *name;
3970 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003971 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003972} cmdmods[] = {
3973 {"aboveleft", 3, FALSE},
3974 {"belowright", 3, FALSE},
3975 {"botright", 2, FALSE},
3976 {"browse", 3, FALSE},
3977 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003978 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003979 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003980 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003981 {"keepalt", 5, FALSE},
3982 {"keepjumps", 5, FALSE},
3983 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003984 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003985 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003986 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003987 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003988 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003989 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003990 {"rightbelow", 6, FALSE},
3991 {"sandbox", 3, FALSE},
3992 {"silent", 3, FALSE},
3993 {"tab", 3, TRUE},
3994 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003995 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003996 {"verbose", 4, TRUE},
3997 {"vertical", 4, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003998 {"vim9cmd", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003999};
4000
4001/*
4002 * Return length of a command modifier (including optional count).
4003 * Return zero when it's not a modifier.
4004 */
4005 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004006modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004007{
4008 int i, j;
4009 char_u *p = cmd;
4010
4011 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004012 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02004013 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004014 {
4015 for (j = 0; p[j] != NUL; ++j)
4016 if (p[j] != cmdmods[i].name[j])
4017 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02004018 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00004019 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00004020 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004021 }
4022 return 0;
4023}
4024
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025/*
4026 * Return > 0 if an Ex command "name" exists.
4027 * Return 2 if there is an exact match.
4028 * Return 3 if there is an ambiguous match.
4029 */
4030 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004031cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032{
4033 exarg_T ea;
4034 int full = FALSE;
4035 int i;
4036 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004037 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004039 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02004040 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
4042 for (j = 0; name[j] != NUL; ++j)
4043 if (name[j] != cmdmods[i].name[j])
4044 break;
4045 if (name[j] == NUL && j >= cmdmods[i].minlen)
4046 return (cmdmods[i].name[j] == NUL ? 2 : 1);
4047 }
4048
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004049 // Check built-in commands and user defined commands.
4050 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004051 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004053 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004055 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004057 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4058 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004059 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004060 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4062}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004063
4064/*
4065 * "fullcommand" function
4066 */
4067 void
4068f_fullcommand(typval_T *argvars, typval_T *rettv)
4069{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004070 exarg_T ea;
4071 char_u *name;
4072 char_u *p;
4073 int vim9script = in_vim9script();
4074 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004075
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004076 rettv->v_type = VAR_STRING;
4077 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004078
Bram Moolenaaraa534142022-09-15 21:46:02 +01004079 if (in_vim9script()
4080 && (check_for_string_arg(argvars, 0) == FAIL
4081 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004082 return;
4083
4084 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004085 if (name == NULL)
4086 return;
4087
Bram Moolenaaraa534142022-09-15 21:46:02 +01004088 if (argvars[1].v_type != VAR_UNKNOWN)
4089 {
4090 vim9script = tv_get_bool(&argvars[1]);
4091 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4092 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4093 }
4094
zeertzjqc024ed92022-01-04 16:22:52 +00004095 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004096 name++;
4097 name = skip_range(name, TRUE, NULL);
4098
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004099 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4100 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004101 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004102 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004103 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004104 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004105 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004106 goto theend;
4107
4108 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004109 {
4110 int res;
4111
4112 ++emsg_silent;
4113 res = not_in_vim9(&ea);
4114 --emsg_silent;
4115
4116 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004117 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004118 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004119
4120 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004121 ? get_user_command_name(ea.useridx, ea.cmdidx)
4122 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004123theend:
4124 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004125}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126#endif
4127
Bram Moolenaard0190392019-08-23 21:17:35 +02004128 cmdidx_T
4129excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130{
Bram Moolenaard0190392019-08-23 21:17:35 +02004131 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004133 if (!one_letter_cmd(cmd, &idx))
4134 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4135 idx = (cmdidx_T)((int)idx + 1))
4136 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4137 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
Bram Moolenaard0190392019-08-23 21:17:35 +02004139 return idx;
4140}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
Bram Moolenaard0190392019-08-23 21:17:35 +02004142 long
4143excmd_get_argt(cmdidx_T idx)
4144{
4145 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146}
4147
4148/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004149 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 *
4151 * Backslashed delimiters after / or ? will be skipped, and commands will
4152 * not be expanded between /'s and ?'s or after "'".
4153 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004154 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 * Returns the "cmd" pointer advanced to beyond the range.
4156 */
4157 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004158skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004159 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004160 int skip_star, // skip "*" used for Visual range
4161 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004163 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004164 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004166 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004168 if (*cmd == '\\')
4169 {
4170 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4171 ++cmd;
4172 else
4173 break;
4174 }
4175 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004177 char_u *p = cmd;
4178
4179 // a quote is only valid at the start or after a separator
4180 while (p > cmd_start)
4181 {
4182 --p;
4183 if (!VIM_ISWHITE(*p))
4184 break;
4185 }
4186 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4187 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 if (*++cmd == NUL && ctx != NULL)
4189 *ctx = EXPAND_NOTHING;
4190 }
4191 else if (*cmd == '/' || *cmd == '?')
4192 {
4193 delim = *cmd++;
4194 while (*cmd != NUL && *cmd != delim)
4195 if (*cmd++ == '\\' && *cmd != NUL)
4196 ++cmd;
4197 if (*cmd == NUL && ctx != NULL)
4198 *ctx = EXPAND_NOTHING;
4199 }
4200 if (*cmd != NUL)
4201 ++cmd;
4202 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004203
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004204 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004205 while (*cmd == ':')
4206 cmd = skipwhite(cmd + 1);
4207
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004208 // Skip "*" used for Visual range.
4209 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4210 cmd = skipwhite(cmd + 1);
4211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return cmd;
4213}
4214
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004215 static void
4216addr_error(cmd_addr_T addr_type)
4217{
4218 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004219 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004220 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004221 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004222}
4223
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004225 * Return the default address for an address type.
4226 */
4227 static linenr_T
4228default_address(exarg_T *eap)
4229{
4230 linenr_T lnum = 0;
4231
4232 switch (eap->addr_type)
4233 {
4234 case ADDR_LINES:
4235 case ADDR_OTHER:
4236 // Default is the cursor line number. Avoid using an invalid
4237 // line number though.
4238 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4239 lnum = curbuf->b_ml.ml_line_count;
4240 else
4241 lnum = curwin->w_cursor.lnum;
4242 break;
4243 case ADDR_WINDOWS:
4244 lnum = CURRENT_WIN_NR;
4245 break;
4246 case ADDR_ARGUMENTS:
4247 lnum = curwin->w_arg_idx + 1;
4248 if (lnum > ARGCOUNT)
4249 lnum = ARGCOUNT;
4250 break;
4251 case ADDR_LOADED_BUFFERS:
4252 case ADDR_BUFFERS:
4253 lnum = curbuf->b_fnum;
4254 break;
4255 case ADDR_TABS:
4256 lnum = CURRENT_TAB_NR;
4257 break;
4258 case ADDR_TABS_RELATIVE:
4259 case ADDR_UNSIGNED:
4260 lnum = 1;
4261 break;
4262 case ADDR_QUICKFIX:
4263#ifdef FEAT_QUICKFIX
4264 lnum = qf_get_cur_idx(eap);
4265#endif
4266 break;
4267 case ADDR_QUICKFIX_VALID:
4268#ifdef FEAT_QUICKFIX
4269 lnum = qf_get_cur_valid_idx(eap);
4270#endif
4271 break;
4272 case ADDR_NONE:
4273 // Will give an error later if a range is found.
4274 break;
4275 }
4276 return lnum;
4277}
4278
4279/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004280 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 *
4282 * Set ptr to the next character after the part that was interpreted.
4283 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004284 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 *
4286 * Return MAXLNUM when no Ex address was found.
4287 */
4288 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004289get_address(
4290 exarg_T *eap UNUSED,
4291 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004292 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004293 int skip, // only skip the address, don't use it
4294 int silent, // no errors or side effects
4295 int to_other_file, // flag: may jump to other file
4296 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297{
4298 int c;
4299 int i;
4300 long n;
4301 char_u *cmd;
4302 pos_T pos;
4303 pos_T *fp;
4304 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004305 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306
4307 cmd = skipwhite(*ptr);
4308 lnum = MAXLNUM;
4309 do
4310 {
Bram Moolenaarfd3084b2022-11-11 01:40:48 +00004311#ifdef FEAT_FOLDING
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004312 int base_char = *cmd;
Bram Moolenaarfd3084b2022-11-11 01:40:48 +00004313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 switch (*cmd)
4315 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004316 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004317 ++cmd;
4318 switch (addr_type)
4319 {
4320 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004321 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 lnum = curwin->w_cursor.lnum;
4323 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004324 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004325 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004326 break;
4327 case ADDR_ARGUMENTS:
4328 lnum = curwin->w_arg_idx + 1;
4329 break;
4330 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004331 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004332 lnum = curbuf->b_fnum;
4333 break;
4334 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004335 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004336 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004337 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004338 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004339 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004340 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004341 cmd = NULL;
4342 goto error;
4343 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004344 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004345#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004346 lnum = qf_get_cur_idx(eap);
4347#endif
4348 break;
4349 case ADDR_QUICKFIX_VALID:
4350#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004351 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004352#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004353 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004354 }
4355 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004357 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 ++cmd;
4359 switch (addr_type)
4360 {
4361 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004362 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 lnum = curbuf->b_ml.ml_line_count;
4364 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004365 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004366 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004367 break;
4368 case ADDR_ARGUMENTS:
4369 lnum = ARGCOUNT;
4370 break;
4371 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004372 buf = lastbuf;
4373 while (buf->b_ml.ml_mfp == NULL)
4374 {
4375 if (buf->b_prev == NULL)
4376 break;
4377 buf = buf->b_prev;
4378 }
4379 lnum = buf->b_fnum;
4380 break;
4381 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004382 lnum = lastbuf->b_fnum;
4383 break;
4384 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004385 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004387 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004388 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004389 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004390 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004391 cmd = NULL;
4392 goto error;
4393 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004394 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004395#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004396 lnum = qf_get_size(eap);
4397 if (lnum == 0)
4398 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004399#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004400 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004401 case ADDR_QUICKFIX_VALID:
4402#ifdef FEAT_QUICKFIX
4403 lnum = qf_get_valid_size(eap);
4404 if (lnum == 0)
4405 lnum = 1;
4406#endif
4407 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004408 }
4409 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004411 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004412 if (*++cmd == NUL)
4413 {
4414 cmd = NULL;
4415 goto error;
4416 }
4417 if (addr_type != ADDR_LINES)
4418 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004419 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004420 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004421 goto error;
4422 }
4423 if (skip)
4424 ++cmd;
4425 else
4426 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004427 // Only accept a mark in another file when it is
4428 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4430 ++cmd;
4431 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004432 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 lnum = curwin->w_cursor.lnum;
4434 else
4435 {
4436 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
4438 cmd = NULL;
4439 goto error;
4440 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004441 lnum = fp->lnum;
4442 }
4443 }
4444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004447 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 c = *cmd++;
4449 if (addr_type != ADDR_LINES)
4450 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004451 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004452 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 goto error;
4454 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004455 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004456 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004457 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004458 if (*cmd == c)
4459 ++cmd;
4460 }
4461 else
4462 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004463 int flags;
4464
4465 pos = curwin->w_cursor; // save curwin->w_cursor
4466
4467 // When '/' or '?' follows another address, start from
4468 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004469 if (lnum > 0 && lnum != MAXLNUM)
4470 curwin->w_cursor.lnum =
4471 lnum > curbuf->b_ml.ml_line_count
4472 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004473
4474 // Start a forward search at the end of the line (unless
4475 // before the first line).
4476 // Start a backward search at the start of the line.
4477 // This makes sure we never match in the current
4478 // line, and can match anywhere in the
4479 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004480 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 curwin->w_cursor.col = MAXCOL;
4482 else
4483 curwin->w_cursor.col = 0;
4484 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004485 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004486 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 {
4488 curwin->w_cursor = pos;
4489 cmd = NULL;
4490 goto error;
4491 }
4492 lnum = curwin->w_cursor.lnum;
4493 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004494 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 cmd += searchcmdlen;
4496 }
4497 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004499 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 ++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 }
4507 if (*cmd == '&')
4508 i = RE_SUBST;
4509 else if (*cmd == '?' || *cmd == '/')
4510 i = RE_SEARCH;
4511 else
4512 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004513 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 cmd = NULL;
4515 goto error;
4516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 if (!skip)
4519 {
4520 /*
4521 * When search follows another address, start from
4522 * there.
4523 */
4524 if (lnum != MAXLNUM)
4525 pos.lnum = lnum;
4526 else
4527 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004529 /*
4530 * Start the search just like for the above
4531 * do_search().
4532 */
4533 if (*cmd != '?')
4534 pos.col = MAXCOL;
4535 else
4536 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004537 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004538 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004539 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004540 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004541 lnum = pos.lnum;
4542 else
4543 {
4544 cmd = NULL;
4545 goto error;
4546 }
4547 }
4548 ++cmd;
4549 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
4551 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004552 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004553 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555
4556 for (;;)
4557 {
4558 cmd = skipwhite(cmd);
4559 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4560 break;
4561
4562 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 {
4564 switch (addr_type)
4565 {
4566 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004567 case ADDR_OTHER:
4568 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004569 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004570 break;
4571 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004572 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004573 break;
4574 case ADDR_ARGUMENTS:
4575 lnum = curwin->w_arg_idx + 1;
4576 break;
4577 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004578 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 lnum = curbuf->b_fnum;
4580 break;
4581 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004582 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004583 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004584 case ADDR_TABS_RELATIVE:
4585 lnum = 1;
4586 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004587 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004588#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004589 lnum = qf_get_cur_idx(eap);
4590#endif
4591 break;
4592 case ADDR_QUICKFIX_VALID:
4593#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004594 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004595#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004596 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004597 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004598 case ADDR_UNSIGNED:
4599 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004600 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004601 }
4602 }
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004605 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 else
4607 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004608 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 n = 1;
4610 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004611 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004612 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004614 if (n == MAXLNUM)
4615 {
4616 emsg(_(e_line_number_out_of_range));
4617 goto error;
4618 }
4619 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004620
4621 if (addr_type == ADDR_TABS_RELATIVE)
4622 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004623 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004624 cmd = NULL;
4625 goto error;
4626 }
4627 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004628 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004629 lnum = compute_buffer_local_count(
4630 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004632 {
4633#ifdef FEAT_FOLDING
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004634 // Relative line addressing: need to adjust for closed folds
4635 // after the first address.
4636 // Subtle difference: "number,+number" and "number,-number"
4637 // adjusts to end of closed fold before adding/subtracting,
4638 // while "number,.+number" adjusts to end of closed fold after
4639 // adding to make "!!" expanded into ".,.+N" work correctly.
4640 int adjust_for_folding = addr_type == ADDR_LINES
4641 && (i == '-' || i == '+')
4642 && address_count >= 2;
4643 if (adjust_for_folding && (i == '-' || base_char != '.'))
Bram Moolenaarded27822017-01-02 14:27:34 +01004644 (void)hasFolding(lnum, NULL, &lnum);
4645#endif
4646 if (i == '-')
4647 lnum -= n;
4648 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004649 {
4650 if (n >= LONG_MAX - lnum)
4651 {
4652 emsg(_(e_line_number_out_of_range));
4653 goto error;
4654 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004655 lnum += n;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004656#ifdef FEAT_FOLDING
4657 // ".+number" rounds up to the end of a closed fold after
4658 // adding, so that ":!!sort" sorts one closed fold.
4659 if (adjust_for_folding && base_char == '.')
4660 (void)hasFolding(lnum, NULL, &lnum);
4661#endif
Bram Moolenaar03725c52021-11-24 12:17:53 +00004662 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
4665 } while (*cmd == '/' || *cmd == '?');
4666
4667error:
4668 *ptr = cmd;
4669 return lnum;
4670}
4671
4672/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004673 * Set eap->line1 and eap->line2 to the whole range.
4674 * Used for commands with the EX_DFLALL flag and no range given.
4675 */
4676 static void
4677address_default_all(exarg_T *eap)
4678{
4679 eap->line1 = 1;
4680 switch (eap->addr_type)
4681 {
4682 case ADDR_LINES:
4683 case ADDR_OTHER:
4684 eap->line2 = curbuf->b_ml.ml_line_count;
4685 break;
4686 case ADDR_LOADED_BUFFERS:
4687 {
4688 buf_T *buf = firstbuf;
4689
4690 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4691 buf = buf->b_next;
4692 eap->line1 = buf->b_fnum;
4693 buf = lastbuf;
4694 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4695 buf = buf->b_prev;
4696 eap->line2 = buf->b_fnum;
4697 }
4698 break;
4699 case ADDR_BUFFERS:
4700 eap->line1 = firstbuf->b_fnum;
4701 eap->line2 = lastbuf->b_fnum;
4702 break;
4703 case ADDR_WINDOWS:
4704 eap->line2 = LAST_WIN_NR;
4705 break;
4706 case ADDR_TABS:
4707 eap->line2 = LAST_TAB_NR;
4708 break;
4709 case ADDR_TABS_RELATIVE:
4710 eap->line2 = 1;
4711 break;
4712 case ADDR_ARGUMENTS:
4713 if (ARGCOUNT == 0)
4714 eap->line1 = eap->line2 = 0;
4715 else
4716 eap->line2 = ARGCOUNT;
4717 break;
4718 case ADDR_QUICKFIX_VALID:
4719#ifdef FEAT_QUICKFIX
4720 eap->line2 = qf_get_valid_size(eap);
4721 if (eap->line2 == 0)
4722 eap->line2 = 1;
4723#endif
4724 break;
4725 case ADDR_NONE:
4726 case ADDR_UNSIGNED:
4727 case ADDR_QUICKFIX:
4728 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4729 break;
4730 }
4731}
4732
4733
4734/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004735 * Get flags from an Ex command argument.
4736 */
4737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004738get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004739{
4740 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4741 {
4742 if (*eap->arg == 'l')
4743 eap->flags |= EXFLAG_LIST;
4744 else if (*eap->arg == 'p')
4745 eap->flags |= EXFLAG_PRINT;
4746 else
4747 eap->flags |= EXFLAG_NR;
4748 eap->arg = skipwhite(eap->arg + 1);
4749 }
4750}
4751
4752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 * Function called for command which is Not Implemented. NI!
4754 */
4755 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004756ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004759 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004760 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761}
4762
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004763#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764/*
4765 * Function called for script command which is Not Implemented. NI!
4766 * Skips over ":perl <<EOF" constructs.
4767 */
4768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004769ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771 if (!eap->skip)
4772 ex_ni(eap);
4773 else
4774 vim_free(script_get(eap, eap->arg));
4775}
4776#endif
4777
4778/*
4779 * Check range in Ex command for validity.
4780 * Return NULL when valid, error message when invalid.
4781 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004782 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004783invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004785 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004786
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if ( eap->line1 < 0
4788 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004789 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004790 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004791
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004792 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004793 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004794 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004795 {
4796 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004797 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004798#ifdef FEAT_DIFF
4799 + (eap->cmdidx == CMD_diffget)
4800#endif
4801 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004802 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004803 break;
4804 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004805 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004806 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004807 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004808 break;
4809 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004810 // Only a boundary check, not whether the buffers actually
4811 // exist.
4812 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004813 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004814 break;
4815 case ADDR_LOADED_BUFFERS:
4816 buf = firstbuf;
4817 while (buf->b_ml.ml_mfp == NULL)
4818 {
4819 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004820 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004821 buf = buf->b_next;
4822 }
4823 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004824 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004825 buf = lastbuf;
4826 while (buf->b_ml.ml_mfp == NULL)
4827 {
4828 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004829 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004830 buf = buf->b_prev;
4831 }
4832 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004833 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004834 break;
4835 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004836 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004837 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004838 break;
4839 case ADDR_TABS:
4840 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004841 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004842 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004843 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004844 case ADDR_OTHER:
4845 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004846 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004847 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004848#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004849 // No error for value that is too big, will use the last entry.
4850 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004851 {
4852 if (eap->addr_count == 0)
4853 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004854 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004855 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004856#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004857 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004858 case ADDR_QUICKFIX_VALID:
4859#ifdef FEAT_QUICKFIX
4860 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4861 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004862 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004863#endif
4864 break;
4865 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004866 case ADDR_NONE:
4867 // Will give an error elsewhere.
4868 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004869 }
4870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 return NULL;
4872}
4873
4874/*
4875 * Correct the range for zero line number, if required.
4876 */
4877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004878correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004880 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
4882 if (eap->line1 == 0)
4883 eap->line1 = 1;
4884 if (eap->line2 == 0)
4885 eap->line2 = 1;
4886 }
4887}
4888
Bram Moolenaar748bf032005-02-02 23:04:36 +00004889#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004890/*
4891 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4892 * pattern. Otherwise return eap->arg.
4893 */
4894 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004895skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004896{
4897 char_u *p = eap->arg;
4898
Bram Moolenaara37420f2006-02-04 22:37:47 +00004899 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4900 || eap->cmdidx == CMD_vimgrepadd
4901 || eap->cmdidx == CMD_lvimgrepadd
4902 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004903 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004904 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004905 if (p == NULL)
4906 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004907 }
4908 return p;
4909}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004910
4911/*
4912 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4913 * in the command line, so that things like % get expanded.
4914 */
4915 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004916replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004917{
4918 char_u *new_cmdline;
4919 char_u *program;
4920 char_u *pos;
4921 char_u *ptr;
4922 int len;
4923 int i;
4924
4925 /*
4926 * Don't do it when ":vimgrep" is used for ":grep".
4927 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004928 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4929 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4930 || eap->cmdidx == CMD_grepadd
4931 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004932 && !grep_internal(eap->cmdidx))
4933 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004934 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4935 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004936 {
4937 if (*curbuf->b_p_gp == NUL)
4938 program = p_gp;
4939 else
4940 program = curbuf->b_p_gp;
4941 }
4942 else
4943 {
4944 if (*curbuf->b_p_mp == NUL)
4945 program = p_mp;
4946 else
4947 program = curbuf->b_p_mp;
4948 }
4949
4950 p = skipwhite(p);
4951
4952 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4953 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004954 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004955 i = 1;
4956 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4957 ++i;
4958 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004959 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004960 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004961 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004962 ptr = new_cmdline;
4963 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4964 {
4965 i = (int)(pos - program);
4966 STRNCPY(ptr, program, i);
4967 STRCPY(ptr += i, p);
4968 ptr += len;
4969 program = pos + 2;
4970 }
4971 STRCPY(ptr, program);
4972 }
4973 else
4974 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004975 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004976 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004977 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004978 STRCPY(new_cmdline, program);
4979 STRCAT(new_cmdline, " ");
4980 STRCAT(new_cmdline, p);
4981 }
4982 msg_make(p);
4983
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004984 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004985 vim_free(*cmdlinep);
4986 *cmdlinep = new_cmdline;
4987 p = new_cmdline;
4988 }
4989 return p;
4990}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004991#endif
4992
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993/*
4994 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004995 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 * Return FAIL for failure, OK otherwise.
4997 */
4998 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004999expand_filename(
5000 exarg_T *eap,
5001 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005002 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005004 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 char_u *repl;
5006 int srclen;
5007 char_u *p;
5008 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005009 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010
Bram Moolenaar748bf032005-02-02 23:04:36 +00005011#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005012 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005013 p = skip_grep_pat(eap);
5014#else
5015 p = eap->arg;
5016#endif
5017
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 /*
5019 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5020 * the file name contains a wildcard it should not cause expanding.
5021 * (it will be expanded anyway if there is a wildcard before replacing).
5022 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005023 has_wildcards = mch_has_wildcard(p);
5024 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005026#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005027 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005028 if (p[0] == '`' && p[1] == '=')
5029 {
5030 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005031 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005032 if (*p == '`')
5033 ++p;
5034 continue;
5035 }
5036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 /*
5038 * Quick check if this cannot be the start of a special string.
5039 * Also removes backslash before '%', '#' and '<'.
5040 */
5041 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5042 {
5043 ++p;
5044 continue;
5045 }
5046
5047 /*
5048 * Try to find a match at this position.
5049 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005050 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005051 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005052 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005054 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
5056 p += srclen;
5057 continue;
5058 }
5059
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005060 // Wildcards won't be expanded below, the replacement is taken
5061 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005062 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5063 {
5064 char_u *l = repl;
5065
5066 repl = expand_env_save(repl);
5067 vim_free(l);
5068 }
5069
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005070 // Need to escape white space et al. with a backslash.
5071 // Don't do this for:
5072 // - replacement that already has been escaped: "##"
5073 // - shell commands (may have to use quotes instead).
5074 // - non-unix systems when there is a single argument (spaces don't
5075 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005077 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 && eap->cmdidx != CMD_grep
5080 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005081 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005082 && eap->cmdidx != CMD_lgrep
5083 && eap->cmdidx != CMD_lgrepadd
5084 && eap->cmdidx != CMD_lmake
5085 && eap->cmdidx != CMD_make
5086 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005088 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089#endif
5090 )
5091 {
5092 char_u *l;
5093#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005094 // Don't escape a backslash here, because rem_backslash() doesn't
5095 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 static char_u *nobslash = (char_u *)" \t\"|";
5097# define ESCAPE_CHARS nobslash
5098#else
5099# define ESCAPE_CHARS escape_chars
5100#endif
5101
5102 for (l = repl; *l; ++l)
5103 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5104 {
5105 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5106 if (l != NULL)
5107 {
5108 vim_free(repl);
5109 repl = l;
5110 }
5111 break;
5112 }
5113 }
5114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005115 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005116 if ((eap->usefilter || eap->cmdidx == CMD_bang
5117 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005118 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
5120 char_u *l;
5121
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005122 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 if (l != NULL)
5124 {
5125 vim_free(repl);
5126 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128 }
5129
5130 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5131 vim_free(repl);
5132 if (p == NULL)
5133 return FAIL;
5134 }
5135
5136 /*
5137 * One file argument: Expand wildcards.
5138 * Don't do this with ":r !command" or ":w !command".
5139 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005140 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
5142 /*
5143 * May do this twice:
5144 * 1. Replace environment variables.
5145 * 2. Replace any other wildcards, remove backslashes.
5146 */
5147 for (n = 1; n <= 2; ++n)
5148 {
5149 if (n == 2)
5150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 /*
5152 * Halve the number of backslashes (this is Vi compatible).
5153 * For Unix and OS/2, when wildcards are expanded, this is
5154 * done by ExpandOne() below.
5155 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005156#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (!has_wildcards)
5158#endif
5159 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161
5162 if (has_wildcards)
5163 {
5164 if (n == 1)
5165 {
5166 /*
5167 * First loop: May expand environment variables. This
5168 * can be done much faster with expand_env() than with
5169 * something else (e.g., calling a shell).
5170 * After expanding environment variables, check again
5171 * if there are still wildcards present.
5172 */
5173 if (vim_strchr(eap->arg, '$') != NULL
5174 || vim_strchr(eap->arg, '~') != NULL)
5175 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005176 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005177 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 has_wildcards = mch_has_wildcard(NameBuff);
5179 p = NameBuff;
5180 }
5181 else
5182 p = NULL;
5183 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005184 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
5186 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005187 int options = WILD_LIST_NOTFOUND
5188 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
5190 ExpandInit(&xpc);
5191 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005192 if (p_wic)
5193 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005195 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 if (p == NULL)
5197 return FAIL;
5198 }
5199 if (p != NULL)
5200 {
5201 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5202 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005203 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 vim_free(p);
5205 }
5206 }
5207 }
5208 }
5209 return OK;
5210}
5211
5212/*
5213 * Replace part of the command line, keeping eap->cmd, eap->arg and
5214 * eap->nextcmd correct.
5215 * "src" points to the part that is to be replaced, of length "srclen".
5216 * "repl" is the replacement string.
5217 * Returns a pointer to the character after the replaced string.
5218 * Returns NULL for failure.
5219 */
5220 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005221repl_cmdline(
5222 exarg_T *eap,
5223 char_u *src,
5224 int srclen,
5225 char_u *repl,
5226 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227{
5228 int len;
5229 int i;
5230 char_u *new_cmdline;
5231
5232 /*
5233 * The new command line is build in new_cmdline[].
5234 * First allocate it.
5235 * Careful: a "+cmd" argument may have been NUL terminated.
5236 */
5237 len = (int)STRLEN(repl);
5238 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005239 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005240 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005241 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005242 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243
5244 /*
5245 * Copy the stuff before the expanded part.
5246 * Copy the expanded stuff.
5247 * Copy what came after the expanded part.
5248 * Copy the next commands, if there are any.
5249 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005250 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005252
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005254 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005256 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005258 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
5260 i = (int)STRLEN(new_cmdline) + 1;
5261 STRCPY(new_cmdline + i, eap->nextcmd);
5262 eap->nextcmd = new_cmdline + i;
5263 }
5264 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5265 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5266 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5267 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5268 vim_free(*cmdlinep);
5269 *cmdlinep = new_cmdline;
5270
5271 return src;
5272}
5273
5274/*
5275 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005276 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 */
5278 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005279separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280{
5281 char_u *p;
5282
Bram Moolenaar86b68352004-12-27 21:59:20 +00005283#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005284 p = skip_grep_pat(eap);
5285#else
5286 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005287#endif
5288
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005289 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
5291 if (*p == Ctrl_V)
5292 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005293 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005294 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005296 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005297 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005298 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 break;
5300 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005301
5302#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005303 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005304 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005305 {
5306 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005307 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005308 if (*p == NUL) // stop at NUL after CTRL-V
5309 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005310 }
5311#endif
5312
Bram Moolenaareda29c92022-10-02 12:59:00 +01005313 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005314 // :@" and :*" do not start a comment!
5315 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005316 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005317 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005318 && !(eap->argt & EX_NOTRLCOM)
5319 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5320 || p != eap->arg)
5321 && (eap->cmdidx != CMD_redir
5322 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005323 || (*p == '#'
5324 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005325 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005326 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 || *p == '|' || *p == '\n')
5328 {
5329 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005330 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 * AND 'b' is present in 'cpoptions'.
5332 */
5333 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005334 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005336 if (!keep_backslash)
5337 {
5338 STRMOVE(p - 1, p); // remove the '\'
5339 --p;
5340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342 else
5343 {
5344 eap->nextcmd = check_nextcmd(p);
5345 *p = NUL;
5346 break;
5347 }
5348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005350
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005351 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 del_trailing_spaces(eap->arg);
5353}
5354
5355/*
5356 * get + command from ex argument
5357 */
5358 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005359getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360{
5361 char_u *arg = *argp;
5362 char_u *command = NULL;
5363
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005364 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 {
5366 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005367 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 command = dollar_command;
5369 else
5370 {
5371 command = arg;
5372 arg = skip_cmd_arg(command, TRUE);
5373 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005374 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 }
5376
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005377 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 *argp = arg;
5379 }
5380 return command;
5381}
5382
5383/*
5384 * Find end of "+command" argument. Skip over "\ " and "\\".
5385 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005386 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005387skip_cmd_arg(
5388 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005389 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390{
5391 while (*p && !vim_isspace(*p))
5392 {
5393 if (*p == '\\' && p[1] != NUL)
5394 {
5395 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005396 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 else
5398 ++p;
5399 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005400 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
5402 return p;
5403}
5404
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005405 int
5406get_bad_opt(char_u *p, exarg_T *eap)
5407{
5408 if (STRICMP(p, "keep") == 0)
5409 eap->bad_char = BAD_KEEP;
5410 else if (STRICMP(p, "drop") == 0)
5411 eap->bad_char = BAD_DROP;
5412 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5413 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005414 else
5415 return FAIL;
5416 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005417}
5418
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419/*
5420 * Get "++opt=arg" argument.
5421 * Return FAIL or OK.
5422 */
5423 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005424getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425{
5426 char_u *arg = eap->arg + 2;
5427 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005428 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005431 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5433 {
5434 if (*arg == 'n')
5435 {
5436 arg += 2;
5437 eap->force_bin = FORCE_NOBIN;
5438 }
5439 else
5440 eap->force_bin = FORCE_BIN;
5441 if (!checkforcmd(&arg, "binary", 3))
5442 return FAIL;
5443 eap->arg = skipwhite(arg);
5444 return OK;
5445 }
5446
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005447 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005448 if (STRNCMP(arg, "edit", 4) == 0)
5449 {
5450 eap->read_edit = TRUE;
5451 eap->arg = skipwhite(arg + 4);
5452 return OK;
5453 }
5454
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 if (STRNCMP(arg, "ff", 2) == 0)
5456 {
5457 arg += 2;
5458 pp = &eap->force_ff;
5459 }
5460 else if (STRNCMP(arg, "fileformat", 10) == 0)
5461 {
5462 arg += 10;
5463 pp = &eap->force_ff;
5464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 else if (STRNCMP(arg, "enc", 3) == 0)
5466 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005467 if (STRNCMP(arg, "encoding", 8) == 0)
5468 arg += 8;
5469 else
5470 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 pp = &eap->force_enc;
5472 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005473 else if (STRNCMP(arg, "bad", 3) == 0)
5474 {
5475 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005476 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
5479 if (pp == NULL || *arg != '=')
5480 return FAIL;
5481
5482 ++arg;
5483 *pp = (int)(arg - eap->cmd);
5484 arg = skip_cmd_arg(arg, FALSE);
5485 eap->arg = skipwhite(arg);
5486 *arg = NUL;
5487
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 if (pp == &eap->force_ff)
5489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5491 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005492 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005494 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005496 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5498 *p = TOLOWER_ASC(*p);
5499 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005500 else
5501 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005502 // Check ++bad= argument. Must be a single-byte character, "keep" or
5503 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005504 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005505 return FAIL;
5506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
5508 return OK;
5509}
5510
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005515 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 * directory for security reasons.
5517 */
5518 if (secure)
5519 {
5520 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005521 eap->errmsg =
5522 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 }
5524 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005525 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 else
5527 do_augroup(eap->arg, eap->forceit);
5528}
5529
5530/*
5531 * ":doautocmd": Apply the automatic commands to the current buffer.
5532 */
5533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005534ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005536 char_u *arg = eap->arg;
5537 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005538 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005539
Bram Moolenaar1610d052016-06-09 22:53:01 +02005540 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005541 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005542 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005543 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546/*
5547 * :[N]bunload[!] [N] [bufname] unload buffer
5548 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5549 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5550 */
5551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005552ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005554 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005555 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 eap->errmsg = do_bufdel(
5557 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5558 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5559 : DOBUF_UNLOAD, eap->arg,
5560 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5561}
5562
5563/*
5564 * :[N]buffer [N] to buffer N
5565 * :[N]sbuffer [N] to buffer N
5566 */
5567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005568ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005570 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005571 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005573 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 else
5575 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005576 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5578 else
5579 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005580 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005581 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583}
5584
5585/*
5586 * :[N]bmodified [N] to next mod. buffer
5587 * :[N]sbmodified [N] to next mod. buffer
5588 */
5589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005593 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005594 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595}
5596
5597/*
5598 * :[N]bnext [N] to next buffer
5599 * :[N]sbnext [N] split and to next buffer
5600 */
5601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005602ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005604 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005605 return;
5606
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005608 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005609 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610}
5611
5612/*
5613 * :[N]bNext [N] to previous buffer
5614 * :[N]bprevious [N] to previous buffer
5615 * :[N]sbNext [N] split and to previous buffer
5616 * :[N]sbprevious [N] split and to previous buffer
5617 */
5618 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005619ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005621 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005622 return;
5623
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005625 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005626 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627}
5628
5629/*
5630 * :brewind to first buffer
5631 * :bfirst to first buffer
5632 * :sbrewind split and to first buffer
5633 * :sbfirst split and to first buffer
5634 */
5635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005636ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005638 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005639 return;
5640
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005642 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005643 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644}
5645
5646/*
5647 * :blast to last buffer
5648 * :sblast split and to last buffer
5649 */
5650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005651ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005653 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005654 return;
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005657 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005658 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
Bram Moolenaar7a092242020-04-16 22:10:49 +02005661/*
5662 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005663 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005666ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005668 int comment_char = '"';
5669
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005670 if (in_vim9script())
5671 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005672 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005673}
5674
5675/*
5676 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5677 * to "cmd_start" or has a white space character before it.
5678 */
5679 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005680ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005681{
5682 int c = *cmd;
5683
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005684 if (c == NUL || c == '|' || c == '\n')
5685 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005686 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005687 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5688 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005689 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005690 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691}
5692
5693#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5694 || defined(PROTO)
5695/*
5696 * Return the next command, after the first '|' or '\n'.
5697 * Return NULL if not found.
5698 */
5699 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005700find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
5702 while (*p != '|' && *p != '\n')
5703 {
5704 if (*p == NUL)
5705 return NULL;
5706 ++p;
5707 }
5708 return (p + 1);
5709}
5710#endif
5711
5712/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005713 * Check if *p is a separator between Ex commands, skipping over white space.
5714 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 */
5716 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005717check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005719 char_u *s = skipwhite(p);
5720
5721 if (*s == '|' || *s == '\n')
5722 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 else
5724 return NULL;
5725}
5726
5727/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005728 * If "eap->nextcmd" is not set, check for a next command at "p".
5729 */
5730 void
5731set_nextcmd(exarg_T *eap, char_u *arg)
5732{
5733 char_u *p = check_nextcmd(arg);
5734
5735 if (eap->nextcmd == NULL)
5736 eap->nextcmd = p;
5737 else if (p != NULL)
5738 // cannot use "| command" inside a {} block
5739 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5740}
5741
5742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 * - if there are more files to edit
5744 * - and this is the last window
5745 * - and forceit not used
5746 * - and not repeated twice on a row
5747 * return FAIL and give error message if 'message' TRUE
5748 * return OK otherwise
5749 */
5750 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005751check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005752 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005753 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754{
5755 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5756
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005757 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005758 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 {
5760 if (message)
5761 {
5762#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005763 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5764 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005766 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005768 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5769 NGETTEXT("%d more file to edit. Quit anyway?",
5770 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5772 return OK;
5773 return FAIL;
5774 }
5775#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005776 semsg(NGETTEXT(e_nr_more_file_to_edit,
5777 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005778 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 }
5780 return FAIL;
5781 }
5782 return OK;
5783}
5784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785/*
5786 * Function given to ExpandGeneric() to obtain the list of command names.
5787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005789get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005792 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 return cmdnames[idx].cmd_name;
5794}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005797ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
Bram Moolenaare6850792010-05-14 15:28:44 +02005799 if (*eap->arg == NUL)
5800 {
5801#ifdef FEAT_EVAL
5802 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5803 char_u *p = NULL;
5804
5805 if (expr != NULL)
5806 {
5807 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005808 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005809 --emsg_off;
5810 vim_free(expr);
5811 }
5812 if (p != NULL)
5813 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005814 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005815 vim_free(p);
5816 }
5817 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005818 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005819#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005820 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005821#endif
5822 }
5823 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005824 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005825
5826#ifdef FEAT_VTP
5827 else if (has_vtp_working())
5828 {
5829 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005830 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005831 redrawcmd();
5832 }
5833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834}
5835
5836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005837ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005840 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 do_highlight(eap->arg, eap->forceit, FALSE);
5842}
5843
5844
5845/*
5846 * Call this function if we thought we were going to exit, but we won't
5847 * (because of an error). May need to restore the terminal mode.
5848 */
5849 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005850not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
5852 exiting = FALSE;
5853 settmode(TMODE_RAW);
5854}
5855
Bram Moolenaar3552e742021-05-29 12:21:58 +02005856 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005857before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5858{
5859 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5860
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005861 // Bail out when autocommands closed the window.
5862 // Refuse to quit when the buffer in the last window is being closed (can
5863 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005864 if (!win_valid(wp)
5865 || curbuf_locked()
5866 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5867 return TRUE;
5868
5869 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5870 {
5871 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005872 // Refuse to quit when locked or when the window was closed or the
5873 // buffer in the last window is being closed (can only happen in
5874 // autocommands).
5875 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005876 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5877 return TRUE;
5878 }
5879
5880 return FALSE;
5881}
5882
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005884 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005885 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005886 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005888 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005889ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005891 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005892
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 if (cmdwin_type != 0)
5894 {
5895 cmdwin_result = Ctrl_C;
5896 return;
5897 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005898 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005899 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005900 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005901 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005902 return;
5903 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005904 if (eap->addr_count > 0)
5905 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005906 int wnr = eap->line2;
5907
5908 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5909 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005910 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005911 }
5912 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005913 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005914
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005915 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005916 if (curbuf_locked())
5917 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005918
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005919 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005920 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005921 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922
5923#ifdef FEAT_NETBEANS_INTG
5924 netbeansForcedQuit = eap->forceit;
5925#endif
5926
5927 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005928 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 */
5930 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5931 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005932 if ((!buf_hide(wp->w_buffer)
5933 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005934 | (eap->forceit ? CCGD_FORCEIT : 0)
5935 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005937 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 {
5939 not_exiting();
5940 }
5941 else
5942 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005943 // quit last window
5944 // Note: only_one_window() returns true, even so a help window is
5945 // still open. In that case only quit, if no address has been
5946 // specified. Example:
5947 // :h|wincmd w|1q - don't quit
5948 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005949 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005951 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005952#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005954#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005955 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005956 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 }
5958}
5959
5960/*
5961 * ":cquit".
5962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005964ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005966 // this does not always pass on the exit code to the Manx compiler. why?
5967 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968}
5969
5970/*
5971 * ":qall": try to quit all windows
5972 */
5973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005974ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 if (cmdwin_type != 0)
5977 {
5978 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005979 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 else
5981 cmdwin_result = K_XF2;
5982 return;
5983 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005984
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005985 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005986 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005987 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005988 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005989 return;
5990 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005991
5992 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005993 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005994
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005996 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 getout(0);
5998 not_exiting();
5999}
6000
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001/*
6002 * ":close": close current window, unless it is the last one
6003 */
6004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006005ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006007 win_T *win;
6008 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006010 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006012 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006013 {
6014 if (eap->addr_count == 0)
6015 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006016 else
6017 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006018 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006019 {
6020 winnr++;
6021 if (winnr == eap->line2)
6022 break;
6023 }
6024 if (win == NULL)
6025 win = lastwin;
6026 ex_win_close(eap->forceit, win, NULL);
6027 }
6028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029}
6030
Bram Moolenaar4033c552017-09-16 20:54:51 +02006031#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006032/*
6033 * ":pclose": Close any preview window.
6034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006036ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006037{
6038 win_T *win;
6039
Bram Moolenaar79648732019-07-18 21:43:07 +02006040 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006041 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006042 if (win->w_p_pvw)
6043 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006044 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006045 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006046 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006047# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006048 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006049 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006050# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006051}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006052#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006053
Bram Moolenaarf740b292006-02-16 22:11:02 +00006054/*
6055 * Close window "win" and take care of handling closing the last window for a
6056 * modified buffer.
6057 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006059ex_win_close(
6060 int forceit,
6061 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006062 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
6064 int need_hide;
6065 buf_T *buf = win->w_buffer;
6066
Bram Moolenaarcf844172020-06-26 19:44:06 +02006067 // Never close the autocommand window.
6068 if (win == aucmd_win)
6069 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006070 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006071 return;
6072 }
6073
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006075 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006077#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006078 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006080# ifdef FEAT_TERMINAL
6081 if (term_job_running(buf->b_term))
6082 {
6083 if (term_confirm_stop(buf) == FAIL)
6084 return;
6085 // Manually kill the terminal here because this command will
6086 // hide it otherwise.
6087 free_terminal(buf);
6088 need_hide = FALSE;
6089 }
6090 else
6091# endif
6092 {
6093 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006094
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006095 set_bufref(&bufref, buf);
6096 dialog_changed(buf, FALSE);
6097 if (bufref_valid(&bufref) && bufIsChanged(buf))
6098 return;
6099 need_hide = FALSE;
6100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 }
6102 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006105 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 return;
6107 }
6108 }
6109
Bram Moolenaar4033c552017-09-16 20:54:51 +02006110#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006112#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006114 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006115 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006116 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006117 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006118 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119}
6120
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006122 * Handle the argument for a tabpage related ex command.
6123 * Returns a tabpage number.
6124 * When an error is encountered then eap->errmsg is set.
6125 */
6126 static int
6127get_tabpage_arg(exarg_T *eap)
6128{
6129 int tab_number;
6130 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6131
6132 if (eap->arg && *eap->arg != NUL)
6133 {
6134 char_u *p = eap->arg;
6135 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006136 int relative = 0; // argument +N/-N means: go to N places to the
6137 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006138
6139 if (*p == '-')
6140 {
6141 relative = -1;
6142 p++;
6143 }
6144 else if (*p == '+')
6145 {
6146 relative = 1;
6147 p++;
6148 }
6149
6150 p_save = p;
6151 tab_number = getdigits(&p);
6152
6153 if (relative == 0)
6154 {
6155 if (STRCMP(p, "$") == 0)
6156 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006157 else if (STRCMP(p, "#") == 0)
6158 if (valid_tabpage(lastused_tabpage))
6159 tab_number = tabpage_index(lastused_tabpage);
6160 else
6161 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006162 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006163 tab_number = 0;
6164 goto theend;
6165 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006166 else if (p == p_save || *p_save == '-' || *p != NUL
6167 || tab_number > LAST_TAB_NR)
6168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006169 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006170 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006171 goto theend;
6172 }
6173 }
6174 else
6175 {
6176 if (*p_save == NUL)
6177 tab_number = 1;
6178 else if (p == p_save || *p_save == '-' || *p != NUL
6179 || tab_number == 0)
6180 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006181 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006182 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006183 goto theend;
6184 }
6185 tab_number = tab_number * relative + tabpage_index(curtab);
6186 if (!unaccept_arg0 && relative == -1)
6187 --tab_number;
6188 }
6189 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006190 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006191 }
6192 else if (eap->addr_count > 0)
6193 {
6194 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006195 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006196 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006197 tab_number = 0;
6198 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006199 else
6200 {
6201 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006202 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006203 {
6204 --tab_number;
6205 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006206 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006207 }
6208 }
6209 }
6210 else
6211 {
6212 switch (eap->cmdidx)
6213 {
6214 case CMD_tabnext:
6215 tab_number = tabpage_index(curtab) + 1;
6216 if (tab_number > LAST_TAB_NR)
6217 tab_number = 1;
6218 break;
6219 case CMD_tabmove:
6220 tab_number = LAST_TAB_NR;
6221 break;
6222 default:
6223 tab_number = tabpage_index(curtab);
6224 }
6225 }
6226
6227theend:
6228 return tab_number;
6229}
6230
6231/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006232 * ":tabclose": close current tab page, unless it is the last one.
6233 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 */
6235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006236ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006238 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006239 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006240
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006241 if (cmdwin_type != 0)
6242 cmdwin_result = K_IGNORE;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006243 else if (first_tabpage->tp_next == NULL)
6244 emsg(_(e_cannot_close_last_tab_page));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006245 else
Martin Tournoij7904fa42022-10-04 16:28:45 +01006246 {
6247 tab_number = get_tabpage_arg(eap);
6248 if (eap->errmsg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006250 tp = find_tabpage(tab_number);
6251 if (tp == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006252 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006253 beep_flush();
6254 return;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006255 }
Martin Tournoij7904fa42022-10-04 16:28:45 +01006256 if (tp != curtab)
6257 {
6258 tabpage_close_other(tp, eap->forceit);
6259 return;
6260 }
6261 else if (!text_locked() && !curbuf_locked())
6262 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 }
Martin Tournoij7904fa42022-10-04 16:28:45 +01006264 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006265}
6266
6267/*
6268 * ":tabonly": close all tab pages except the current one
6269 */
6270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006271ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006272{
6273 tabpage_T *tp;
6274 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006275 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006276
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006277 if (cmdwin_type != 0)
6278 cmdwin_result = K_IGNORE;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006279 else if (first_tabpage->tp_next == NULL)
6280 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006281 else
Martin Tournoij7904fa42022-10-04 16:28:45 +01006282 {
6283 tab_number = get_tabpage_arg(eap);
6284 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006285 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006286 goto_tabpage(tab_number);
6287 // Repeat this up to a 1000 times, because autocommands may
6288 // mess up the lists.
6289 for (done = 0; done < 1000; ++done)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006290 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006291 FOR_ALL_TABPAGES(tp)
6292 if (tp->tp_topframe != topframe)
6293 {
6294 tabpage_close_other(tp, eap->forceit);
6295 // if we failed to close it quit
6296 if (valid_tabpage(tp))
6297 done = 1000;
6298 // start over, "tp" is now invalid
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006299 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006300 }
6301 if (first_tabpage->tp_next == NULL)
6302 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006303 }
6304 }
Martin Tournoij7904fa42022-10-04 16:28:45 +01006305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307
6308/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006309 * Close the current tab page.
6310 */
6311 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006312tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006313{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006314 // First close all the windows but the current one. If that worked then
6315 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006316 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006317 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006318 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006319 ex_win_close(forceit, curwin, NULL);
6320# ifdef FEAT_GUI
6321 need_mouse_correct = TRUE;
6322# endif
6323}
6324
6325/*
6326 * Close tab page "tp", which is not the current tab page.
6327 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006328 * Also takes care of the tab pages line disappearing when closing the
6329 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006330 */
6331 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006332tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006333{
6334 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006335 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006336
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006337 // Limit to 1000 windows, autocommands may add a window while we close
6338 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006339 while (++done < 1000)
6340 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006341 wp = tp->tp_firstwin;
6342 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006343
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006344 // Autocommands may delete the tab page under our fingers and we may
6345 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006346 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006347 break;
6348 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006349
Bram Moolenaar29323592016-07-24 22:04:11 +02006350 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006351}
6352
6353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 * ":only".
6355 */
6356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006357ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006359 win_T *wp;
6360 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361# ifdef FEAT_GUI
6362 need_mouse_correct = TRUE;
6363# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006364 if (eap->addr_count > 0)
6365 {
6366 wnr = eap->line2;
6367 for (wp = firstwin; --wnr > 0; )
6368 {
6369 if (wp->w_next == NULL)
6370 break;
6371 else
6372 wp = wp->w_next;
6373 }
6374 win_goto(wp);
6375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 close_others(TRUE, eap->forceit);
6377}
6378
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006380ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006382 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006383 if (!eap->skip)
6384 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006385#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006386 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006387#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006388 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006389 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006390 else
6391 {
6392 int winnr = 0;
6393 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006394
Bram Moolenaar2256c992016-11-15 21:17:07 +01006395 FOR_ALL_WINDOWS(win)
6396 {
6397 winnr++;
6398 if (winnr == eap->line2)
6399 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006400 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006401 if (win == NULL)
6402 win = lastwin;
6403 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 }
6406}
6407
6408/*
6409 * ":stop" and ":suspend": Suspend Vim.
6410 */
dbivolaruab16ad32021-12-29 19:41:47 +00006411 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006412ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
6414 /*
6415 * Disallow suspending for "rvim".
6416 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006417 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 {
6419 if (!eap->forceit)
6420 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006421 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 windgoto((int)Rows - 1, 0);
6423 out_char('\n');
6424 out_flush();
6425 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006426 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006427 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006428 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006430 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006432 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006434 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006435 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 }
6437}
6438
6439/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006440 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 */
6442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006443ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006445#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006446 if (not_in_vim9(eap) == FAIL)
6447 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 if (cmdwin_type != 0)
6450 {
6451 cmdwin_result = Ctrl_C;
6452 return;
6453 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006454 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006455 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006456 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006457 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006458 return;
6459 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006460
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006462 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 */
6464 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6465 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006466
6467 // Write the buffer for ":wq" or when it was changed.
6468 // Trigger QuitPre and ExitPre.
6469 // Check if we can exit now, after autocommands have changed things.
6470 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6471 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006473 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 {
6475 not_exiting();
6476 }
6477 else
6478 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006479 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006481 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482# ifdef FEAT_GUI
6483 need_mouse_correct = TRUE;
6484# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006485 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006486 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 }
6488}
6489
6490/*
6491 * ":print", ":list", ":number".
6492 */
6493 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006494ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006496 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006497 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006498 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006500 for ( ;!got_int; ui_breakcheck())
6501 {
6502 print_line(eap->line1,
6503 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6504 || (eap->flags & EXFLAG_NR)),
6505 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6506 if (++eap->line1 > eap->line2)
6507 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006508 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006509 }
6510 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006511 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006512 curwin->w_cursor.lnum = eap->line2;
6513 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 }
6515
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517}
6518
6519#ifdef FEAT_BYTEOFF
6520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006521ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
6523 goto_byte(eap->line2);
6524}
6525#endif
6526
6527/*
6528 * ":shell".
6529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006531ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532{
6533 do_shell(NULL, 0);
6534}
6535
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006536#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006538static int drop_busy = FALSE;
6539static int drop_filec;
6540static char_u **drop_filev = NULL;
6541static int drop_split;
6542static void (*drop_callback)(void *);
6543static void *drop_cookie;
6544
6545 static void
6546handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
6548 exarg_T ea;
6549 int save_msg_scroll = msg_scroll;
6550
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006551 // Setting the argument list may cause screen updates and being called
6552 // recursively. Avoid that by setting drop_busy.
6553 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006555 // Check whether the current buffer is changed. If so, we will need
6556 // to split the current window or data could be lost.
6557 // We don't need to check if the 'hidden' option is set, as in this
6558 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006559 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
6561 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006562 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 --emsg_off;
6564 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006565 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 if (win_split(0, 0) == FAIL)
6568 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006569 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006571 // When splitting the window, create a new alist. Otherwise the
6572 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 alist_unlink(curwin->w_alist);
6574 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 }
6576
6577 /*
6578 * Set up the new argument list.
6579 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006580 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
6582 /*
6583 * Move to the first file.
6584 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006585 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006586 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 ea.cmd = (char_u *)"next";
6588 do_argfile(&ea, 0);
6589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006590 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6591 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 need_start_insertmode = FALSE;
6593
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006594 // Restore msg_scroll, otherwise a following command may cause scrolling
6595 // unexpectedly. The screen will be redrawn by the caller, thus
6596 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006598
6599 if (drop_callback != NULL)
6600 drop_callback(drop_cookie);
6601
6602 drop_filev = NULL;
6603 drop_busy = FALSE;
6604}
6605
6606/*
6607 * Handle a file drop. The code is here because a drop is *nearly* like an
6608 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006609 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006610 * code is very similar to :args and hence needs access to a lot of the static
6611 * functions in this file.
6612 *
6613 * The "filev" list must have been allocated using alloc(), as should each item
6614 * in the list. This function takes over responsibility for freeing the "filev"
6615 * list.
6616 */
6617 void
6618handle_drop(
6619 int filec, // the number of files dropped
6620 char_u **filev, // the list of files dropped
6621 int split, // force splitting the window
6622 void (*callback)(void *), // to be called after setting the argument
6623 // list
6624 void *cookie) // argument for "callback" (allocated)
6625{
6626 // Cannot handle recursive drops, finish the pending one.
6627 if (drop_busy)
6628 {
6629 FreeWild(filec, filev);
6630 vim_free(cookie);
6631 return;
6632 }
6633
6634 // When calling handle_drop() more than once in a row we only use the last
6635 // one.
6636 if (drop_filev != NULL)
6637 {
6638 FreeWild(drop_filec, drop_filev);
6639 vim_free(drop_cookie);
6640 }
6641
6642 drop_filec = filec;
6643 drop_filev = filev;
6644 drop_split = split;
6645 drop_callback = callback;
6646 drop_cookie = cookie;
6647
6648 // Postpone this when:
6649 // - editing the command line
6650 // - not possible to change the current buffer
6651 // - updating the screen
6652 // As it may change buffers and window structures that are in use and cause
6653 // freed memory to be used.
6654 if (text_locked() || curbuf_locked() || updating_screen)
6655 return;
6656
6657 handle_drop_internal();
6658}
6659
6660/*
6661 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6662 * reset: Handle a postponed drop.
6663 */
6664 void
6665handle_any_postponed_drop(void)
6666{
6667 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006668 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006669 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670}
6671#endif
6672
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 * ":preserve".
6675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006677ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006679 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 ml_preserve(curbuf, TRUE);
6681}
6682
6683/*
6684 * ":recover".
6685 */
6686 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006687ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006689 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006691 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6692 | CCGD_MULTWIN
6693 | (eap->forceit ? CCGD_FORCEIT : 0)
6694 | CCGD_EXCMD)
6695
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 && (*eap->arg == NUL
6697 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006698 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 recoverymode = FALSE;
6700}
6701
6702/*
6703 * Command modifier used in a wrong way.
6704 */
6705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006706ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006708 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709}
6710
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711/*
6712 * :sview [+command] file split window with new file, read-only
6713 * :split [[+command] file] split window with current or new file
6714 * :vsplit [[+command] file] split window vertically with current or new file
6715 * :new [[+command] file] split window with no or new file
6716 * :vnew [[+command] file] split vertically window with no or new file
6717 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006718 *
6719 * :tabedit open new Tab page with empty window
6720 * :tabedit [+command] file open new Tab page and edit "file"
6721 * :tabnew [[+command] file] just like :tabedit
6722 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 */
6724 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006725ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006727 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006729#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006730 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006731 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006732#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006733 int use_tab = eap->cmdidx == CMD_tabedit
6734 || eap->cmdidx == CMD_tabfind
6735 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006737 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006738 return;
6739
Bram Moolenaar4033c552017-09-16 20:54:51 +02006740#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743
Bram Moolenaar4033c552017-09-16 20:54:51 +02006744#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006745 // A ":split" in the quickfix window works like ":new". Don't want two
6746 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006747 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 {
6749 if (eap->cmdidx == CMD_split)
6750 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 if (eap->cmdidx == CMD_vsplit)
6752 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006756 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 {
6758 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6759 FNAME_MESS, TRUE, curbuf->b_ffname);
6760 if (fname == NULL)
6761 goto theend;
6762 eap->arg = fname;
6763 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006764# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006765 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 && eap->cmdidx != CMD_new)
6768 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006769 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006770# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006771 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006772# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006773 au_has_group((char_u *)"FileExplorer"))
6774 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006775 // No browsing supported but we do have the file explorer:
6776 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006777 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006778 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006779 }
6780 else
6781 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006782 fname = do_browse(0, (char_u *)(use_tab
6783 ? _("Edit File in new tab page")
6784 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006786 if (fname == NULL)
6787 goto theend;
6788 eap->arg = fname;
6789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006791 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006794 /*
6795 * Either open new tab page or split the window.
6796 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006797 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006798 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006799 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006800 : eap->addr_count == 0 ? 0
6801 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006802 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006803 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006804
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006805 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006806 if (curwin != old_curwin
6807 && win_valid(old_curwin)
6808 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006809 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006810 old_curwin->w_alt_fnum = curbuf->b_fnum;
6811 }
6812 }
6813 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6815 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006816 // Reset 'scrollbind' when editing another file, but keep it when
6817 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006818 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006819 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 else
6821 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 do_exedit(eap, old_curwin);
6823 }
6824
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006825# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006826 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829theend:
6830 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006832
6833/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006834 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006835 */
6836 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006837tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006838{
6839 exarg_T ea;
6840
Bram Moolenaara80faa82020-04-12 19:37:17 +02006841 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006842 ea.cmdidx = CMD_tabnew;
6843 ea.cmd = (char_u *)"tabn";
6844 ea.arg = (char_u *)"";
6845 ex_splitview(&ea);
6846}
6847
6848/*
6849 * :tabnext command
6850 */
6851 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006852ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006853{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006854 int tab_number;
6855
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006856 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006857 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006858 switch (eap->cmdidx)
6859 {
6860 case CMD_tabfirst:
6861 case CMD_tabrewind:
6862 goto_tabpage(1);
6863 break;
6864 case CMD_tablast:
6865 goto_tabpage(9999);
6866 break;
6867 case CMD_tabprevious:
6868 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006869 if (eap->arg && *eap->arg != NUL)
6870 {
6871 char_u *p = eap->arg;
6872 char_u *p_save = p;
6873
6874 tab_number = getdigits(&p);
6875 if (p == p_save || *p_save == '-' || *p != NUL
6876 || tab_number == 0)
6877 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006878 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006879 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006880 return;
6881 }
6882 }
6883 else
6884 {
6885 if (eap->addr_count == 0)
6886 tab_number = 1;
6887 else
6888 {
6889 tab_number = eap->line2;
6890 if (tab_number < 1)
6891 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006892 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006893 return;
6894 }
6895 }
6896 }
6897 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006898 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006899 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006900 tab_number = get_tabpage_arg(eap);
6901 if (eap->errmsg == NULL)
6902 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006903 break;
6904 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006905}
6906
6907/*
6908 * :tabmove command
6909 */
6910 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006911ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006912{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006913 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006914
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006915 tab_number = get_tabpage_arg(eap);
6916 if (eap->errmsg == NULL)
6917 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006918}
6919
6920/*
6921 * :tabs command: List tabs and their contents.
6922 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006924ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006925{
6926 tabpage_T *tp;
6927 win_T *wp;
6928 int tabcount = 1;
6929
6930 msg_start();
6931 msg_scroll = TRUE;
6932 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6933 {
6934 msg_putchar('\n');
6935 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006936 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006937 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006938 ui_breakcheck();
6939
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006940 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006941 wp = firstwin;
6942 else
6943 wp = tp->tp_firstwin;
6944 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6945 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006946 msg_putchar('\n');
6947 msg_putchar(wp == curwin ? '>' : ' ');
6948 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006949 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6950 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006951 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006952 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006953 else
6954 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6955 IObuff, IOSIZE, TRUE);
6956 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006957 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006958 ui_breakcheck();
6959 }
6960 }
6961}
6962
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963/*
6964 * ":mode": Set screen mode.
6965 * If no argument given, just get the screen size and redraw.
6966 */
6967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006968ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969{
6970 if (*eap->arg == NUL)
6971 shell_resized();
6972 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006973 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974}
6975
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976/*
6977 * ":resize".
6978 * set, increment or decrement current window height
6979 */
6980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006981ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982{
6983 int n;
6984 win_T *wp = curwin;
6985
6986 if (eap->addr_count > 0)
6987 {
6988 n = eap->line2;
6989 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6990 ;
6991 }
6992
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006993# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006997 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 {
6999 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007000 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007001 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007003 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 }
7005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {
7007 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007008 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007009 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007011 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 }
7013}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
7015/*
7016 * ":find [+command] <file>" command.
7017 */
7018 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007019ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 char_u *fname;
7022 int count;
7023
7024 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7025 TRUE, curbuf->b_ffname);
7026 if (eap->addr_count > 0)
7027 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007028 // Repeat finding the file "count" times. This matters when it
7029 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 count = eap->line2;
7031 while (fname != NULL && --count > 0)
7032 {
7033 vim_free(fname);
7034 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7035 FALSE, curbuf->b_ffname);
7036 }
7037 }
7038
7039 if (fname != NULL)
7040 {
7041 eap->arg = fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 do_exedit(eap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 vim_free(fname);
7044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045}
7046
7047/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007048 * ":open" simulation: for now just work like ":visual".
7049 */
7050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007051ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007052{
7053 regmatch_T regmatch;
7054 char_u *p;
7055
Bram Moolenaar65088802021-03-13 21:07:21 +01007056#ifdef FEAT_EVAL
7057 if (not_in_vim9(eap) == FAIL)
7058 return;
7059#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007060 curwin->w_cursor.lnum = eap->line2;
7061 beginline(BL_SOL | BL_FIX);
7062 if (*eap->arg == '/')
7063 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007064 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007065 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007066 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007067 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007068 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007069 if (regmatch.regprog != NULL)
7070 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007071 // make a copy of the line, when searching for a mark it might be
7072 // flushed
7073 char_u *line = vim_strsave(ml_get_curline());
7074
Bram Moolenaardf177f62005-02-22 08:39:57 +00007075 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007076 if (vim_regexec(&regmatch, line, (colnr_T)0))
7077 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007078 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007079 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007080 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007081 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007082 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007083 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007084 eap->arg += STRLEN(eap->arg);
7085 }
7086 check_cursor();
7087
7088 eap->cmdidx = CMD_visual;
7089 do_exedit(eap, NULL);
7090}
7091
7092/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007093 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 */
7095 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007096ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097{
7098 do_exedit(eap, NULL);
7099}
7100
7101/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007102 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007105do_exedit(
7106 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007107 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
7109 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007111 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007113 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7114 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007115 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 /*
7117 * ":vi" command ends Ex mode.
7118 */
7119 if (exmode_active && (eap->cmdidx == CMD_visual
7120 || eap->cmdidx == CMD_view))
7121 {
7122 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007123 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007126 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007127 if (global_busy)
7128 {
7129 int rd = RedrawingDisabled;
7130 int nwr = no_wait_return;
7131 int ms = msg_scroll;
7132#ifdef FEAT_GUI
7133 int he = hold_gui_events;
7134#endif
7135
7136 if (eap->nextcmd != NULL)
7137 {
7138 stuffReadbuff(eap->nextcmd);
7139 eap->nextcmd = NULL;
7140 }
7141
7142 if (exmode_was != EXMODE_VIM)
7143 settmode(TMODE_RAW);
7144 RedrawingDisabled = 0;
7145 no_wait_return = 0;
7146 need_wait_return = FALSE;
7147 msg_scroll = 0;
7148#ifdef FEAT_GUI
7149 hold_gui_events = 0;
7150#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007151 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007152 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007153
7154 main_loop(FALSE, TRUE);
7155
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007156 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007157 RedrawingDisabled = rd;
7158 no_wait_return = nwr;
7159 msg_scroll = ms;
7160#ifdef FEAT_GUI
7161 hold_gui_events = he;
7162#endif
7163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 }
7167
7168 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007169 || eap->cmdidx == CMD_tabnew
7170 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007171 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007173 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 setpcmark();
7175 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007176 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7177 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007179 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 || *eap->arg != NUL
7181#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007182 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183#endif
7184 )
7185 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007186 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7187 // Only ":edit" or ":script" can bring us here, others are stopped
7188 // earlier.
7189 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007190 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007191
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 n = readonlymode;
7193 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7194 readonlymode = TRUE;
7195 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007196 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7197 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007198 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7199 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7201 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007202 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7204 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7205 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007206 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007208 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007209 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007210 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7211 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007212 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007214 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 if (old_curwin != NULL)
7216 {
7217 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007218 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007220#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007221 cleanup_T cs;
7222
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007223 // Reset the error/interrupt/exception state here so that
7224 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007225 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007226#endif
7227#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007229#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007230 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007231
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007232#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007233 // Restore the error/interrupt/exception state if not
7234 // discarded by a new aborting error, interrupt, or
7235 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007236 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 }
7239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 }
7241 else if (readonlymode && curbuf->b_nwindows == 1)
7242 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007243 // When editing an already visited buffer, 'readonly' won't be set
7244 // but the previous value is kept. With ":view" and ":sview" we
7245 // want the file to be readonly, except when another window is
7246 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 curbuf->b_p_ro = TRUE;
7248 }
7249 readonlymode = n;
7250 }
7251 else
7252 {
7253 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007254 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 if (n != curwin->w_arg_idx_invalid)
7258 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 }
7260
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 /*
7262 * if ":split file" worked, set alternate file name in old window to new
7263 * file
7264 */
7265 if (old_curwin != NULL
7266 && *eap->arg != NUL
7267 && curwin != old_curwin
7268 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007269 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007270 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
7273 ex_no_reprint = TRUE;
7274}
7275
7276#ifndef FEAT_GUI
7277/*
7278 * ":gui" and ":gvim" when there is no GUI.
7279 */
7280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007281ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007283 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284}
7285#endif
7286
Bram Moolenaar4f974752019-02-17 17:44:42 +01007287#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007289ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290{
7291 gui_make_tearoff(eap->arg);
7292}
7293#endif
7294
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007295#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7296 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007298ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007300# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7301 if (gui.in_use)
7302 gui_make_popup(eap->arg, eap->forceit);
7303# ifdef FEAT_TERM_POPUP_MENU
7304 else
7305# endif
7306# endif
7307# ifdef FEAT_TERM_POPUP_MENU
7308 pum_make_popup(eap->arg, eap->forceit);
7309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310}
7311#endif
7312
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007314ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
7316 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007317 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007319 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320}
7321
7322/*
7323 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7324 * offset.
7325 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007328ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007331 win_T *save_curwin = curwin;
7332 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 long topline;
7334 long y;
7335 linenr_T old_linenr = curwin->w_cursor.lnum;
7336
7337 setpcmark();
7338
7339 /*
7340 * determine max topline
7341 */
7342 if (curwin->w_p_scb)
7343 {
7344 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007345 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 {
7347 if (wp->w_p_scb && wp->w_buffer)
7348 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007349 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 if (topline > y)
7351 topline = y;
7352 }
7353 }
7354 if (topline < 1)
7355 topline = 1;
7356 }
7357 else
7358 {
7359 topline = 1;
7360 }
7361
7362
7363 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007364 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007366 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 {
7368 if (curwin->w_p_scb)
7369 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007370 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 y = topline - curwin->w_topline;
7372 if (y > 0)
7373 scrollup(y, TRUE);
7374 else
7375 scrolldown(-y, TRUE);
7376 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007377 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
7381 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007382 curwin = save_curwin;
7383 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 if (curwin->w_p_scb)
7385 {
7386 did_syncbind = TRUE;
7387 checkpcmark();
7388 if (old_linenr != curwin->w_cursor.lnum)
7389 {
7390 char_u ctrl_o[2];
7391
7392 ctrl_o[0] = Ctrl_O;
7393 ctrl_o[1] = 0;
7394 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7395 }
7396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397}
7398
7399
7400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007401ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007403 int i;
7404 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7405 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007407 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 do_bang(1, eap, FALSE, FALSE, TRUE);
7409 else
7410 {
7411 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7412 return;
7413
7414#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007415 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 {
7417 char_u *browseFile;
7418
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007419 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 NULL, NULL, NULL, curbuf);
7421 if (browseFile != NULL)
7422 {
7423 i = readfile(browseFile, NULL,
7424 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7425 vim_free(browseFile);
7426 }
7427 else
7428 i = OK;
7429 }
7430 else
7431#endif
7432 if (*eap->arg == NUL)
7433 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007434 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 return;
7436 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7437 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7438 }
7439 else
7440 {
7441 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7442 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7443 i = readfile(eap->arg, NULL,
7444 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7445
7446 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007447 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007449#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 if (!aborting())
7451#endif
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007452 semsg(_(e_cant_open_file_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
7454 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007455 {
7456 if (empty && exmode_active)
7457 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007458 // Delete the empty line that remains. Historically ex does
7459 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007460 if (eap->line2 == 0)
7461 lnum = curbuf->b_ml.ml_line_count;
7462 else
7463 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007464 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007465 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007466 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007467 if (curwin->w_cursor.lnum > 1
7468 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007469 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007470 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007471 }
7472 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007473 redraw_curbuf_later(UPD_VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 }
7476}
7477
Bram Moolenaarea408852005-06-25 22:49:46 +00007478static char_u *prev_dir = NULL;
7479
7480#if defined(EXITFREE) || defined(PROTO)
7481 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007482free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007483{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007484 VIM_CLEAR(prev_dir);
7485 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007486}
7487#endif
7488
Bram Moolenaarf4258302013-06-02 18:20:17 +02007489/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007490 * Get the previous directory for the given chdir scope.
7491 */
7492 static char_u *
7493get_prevdir(cdscope_T scope)
7494{
7495 if (scope == CDSCOPE_WINDOW)
7496 return curwin->w_prevdir;
7497 else if (scope == CDSCOPE_TABPAGE)
7498 return curtab->tp_prevdir;
7499 return prev_dir;
7500}
7501
7502/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007503 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007504 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7505 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007506 */
7507 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007508post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007509{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007510 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007511 // Clear tab local directory for both :cd and :tcd
7512 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007513 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007514 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007515 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007516 char_u *pdir = get_prevdir(scope);
7517
7518 // If still in the global directory, need to remember current
7519 // directory as the global directory.
7520 if (globaldir == NULL && pdir != NULL)
7521 globaldir = vim_strsave(pdir);
7522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007523 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007524 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007525 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007526 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007527 curtab->tp_localdir = vim_strsave(NameBuff);
7528 else
7529 curwin->w_localdir = vim_strsave(NameBuff);
7530 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007531 }
7532 else
7533 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007534 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007535 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007536 }
7537
zeertzjq64be6aa2021-11-19 11:59:08 +00007538 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007539 shorten_fnames(TRUE);
7540}
7541
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007542/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007543 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7544 */
7545 void
7546trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7547{
7548#ifdef FEAT_EVAL
7549 dict_T *v_event;
7550 save_v_event_T save_v_event;
7551
7552 v_event = get_v_event(&save_v_event);
7553 (void)dict_add_string(v_event, "directory", new_dir);
7554 dict_set_items_ro(v_event);
7555#endif
7556 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7557#ifdef FEAT_EVAL
7558 restore_v_event(v_event, &save_v_event);
7559#endif
7560}
7561
7562/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007563 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007564 * chdir() function.
7565 * scope == CDSCOPE_WINDOW: changes the window-local directory
7566 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7567 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007568 * Returns TRUE if the directory is successfully changed.
7569 */
7570 int
7571changedir_func(
7572 char_u *new_dir,
7573 int forceit,
7574 cdscope_T scope)
7575{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007576 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007577 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007578 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007579 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007580 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007581
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007582 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007583 return FALSE;
7584
7585 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7586 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007587 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007588 return FALSE;
7589 }
7590
7591 // ":cd -": Change to previous directory
7592 if (STRCMP(new_dir, "-") == 0)
7593 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007594 pdir = get_prevdir(scope);
7595 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007596 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007597 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007598 return FALSE;
7599 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007600 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007601 }
7602
7603 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007604 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007605 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007606 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007607 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007608
Bakudankun29f3a452021-12-11 12:28:08 +00007609 // For UNIX ":cd" means: go to home directory.
7610 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007611#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007612 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007613#else
7614 if (*new_dir == NUL && p_cdh)
7615#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007616 {
7617 // use NameBuff for home directory name
7618# ifdef VMS
7619 char_u *p;
7620
7621 p = mch_getenv((char_u *)"SYS$LOGIN");
7622 if (p == NULL || *p == NUL) // empty is the same as not set
7623 NameBuff[0] = NUL;
7624 else
7625 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7626# else
7627 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7628# endif
7629 new_dir = NameBuff;
7630 }
zeertzjqf38aad82021-12-30 13:45:57 +00007631 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007632 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7633 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007634 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007635 if (scope == CDSCOPE_WINDOW)
7636 acmd_fname = (char_u *)"window";
7637 else if (scope == CDSCOPE_TABPAGE)
7638 acmd_fname = (char_u *)"tabpage";
7639 else
7640 acmd_fname = (char_u *)"global";
7641 trigger_DirChangedPre(acmd_fname, new_dir);
7642
7643 if (vim_chdir(new_dir))
7644 {
7645 emsg(_(e_command_failed));
7646 vim_free(pdir);
7647 return FALSE;
7648 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007649 }
zeertzjq73257142022-02-02 13:16:37 +00007650
7651 if (scope == CDSCOPE_WINDOW)
7652 pp = &curwin->w_prevdir;
7653 else if (scope == CDSCOPE_TABPAGE)
7654 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007655 else
zeertzjq73257142022-02-02 13:16:37 +00007656 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007657 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007658 *pp = pdir;
7659
7660 post_chdir(scope);
7661
7662 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007663 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007664 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007665 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007666}
Bram Moolenaarea408852005-06-25 22:49:46 +00007667
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007669 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007671 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007672ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007674 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
7676 new_dir = eap->arg;
7677#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007678 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7679 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 ex_pwd(NULL);
7681 else
7682#endif
7683 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007684 cdscope_T scope = CDSCOPE_GLOBAL;
7685
7686 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7687 scope = CDSCOPE_WINDOW;
7688 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7689 scope = CDSCOPE_TABPAGE;
7690
7691 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007692 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007693 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007694 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 ex_pwd(eap);
7696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 }
7698}
7699
7700/*
7701 * ":pwd".
7702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007704ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705{
7706 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7707 {
7708#ifdef BACKSLASH_IN_FILENAME
7709 slash_adjust(NameBuff);
7710#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007711 if (p_verbose > 0)
7712 {
7713 char *context = "global";
7714
Bram Moolenaar05268152021-11-18 18:53:45 +00007715 if (last_chdir_reason != NULL)
7716 context = last_chdir_reason;
7717 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007718 context = "window";
7719 else if (curtab->tp_localdir != NULL)
7720 context = "tabpage";
7721 smsg("[%s] %s", context, (char *)NameBuff);
7722 }
7723 else
7724 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 }
7726 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007727 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728}
7729
7730/*
7731 * ":=".
7732 */
7733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007734ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007736 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007737 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738}
7739
7740 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007741ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007743 int n;
7744 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745
7746 if (cursor_valid())
7747 {
7748 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7749 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007750 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007752
7753 len = eap->line2;
7754 switch (*eap->arg)
7755 {
7756 case 'm': break;
7757 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007758 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007759 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007760
7761 // Hide the cursor if invoked with !
7762 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763}
7764
7765/*
7766 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007767 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 */
7769 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007770do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
Bram Moolenaar52953192019-08-16 10:27:13 +02007772 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007773 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007774# ifdef ELAPSED_FUNC
7775 elapsed_T start_tv;
7776
7777 // Remember at what time we started, so that we know how much longer we
7778 // should wait after waiting for a bit.
7779 ELAPSED_INIT(start_tv);
7780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007782 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007783 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007784 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007785 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007786
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007787 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007788 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007790 wait_now = msec - done > 1000L ? 1000L : msec - done;
7791#ifdef FEAT_TIMERS
7792 {
7793 long due_time = check_due_timer();
7794
7795 if (due_time > 0 && due_time < wait_now)
7796 wait_now = due_time;
7797 }
7798#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007799#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007800 if (has_any_channel() && wait_now > 20L)
7801 wait_now = 20L;
7802#endif
7803#ifdef FEAT_SOUND
7804 if (has_any_sound_callback() && wait_now > 20L)
7805 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007806#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007807 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007808
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007809#ifdef FEAT_JOB_CHANNEL
7810 if (has_any_channel())
7811 ui_breakcheck_force(TRUE);
7812 else
7813#endif
7814 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007815#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007816 // Process the netbeans and clientserver messages that may have been
7817 // received in the call to ui_breakcheck() when the GUI is in use. This
7818 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007819 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007820#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007821
7822# ifdef ELAPSED_FUNC
7823 // actual time passed
7824 done = ELAPSED_FUNC(start_tv);
7825# else
7826 // guestimate time passed (will actually be more)
7827 done += wait_now;
7828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007830
7831 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7832 // input buffer, otherwise a following call to input() fails.
7833 if (got_int)
7834 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007835
7836 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007837 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838}
7839
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840/*
7841 * ":winsize" command (obsolete).
7842 */
7843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007844ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846 int w, h;
7847 char_u *arg = eap->arg;
7848 char_u *p;
7849
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007850 if (!isdigit(*arg))
7851 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007852 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007853 return;
7854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 w = getdigits(&arg);
7856 arg = skipwhite(arg);
7857 p = arg;
7858 h = getdigits(&arg);
7859 if (*p != NUL && *arg == NUL)
7860 set_shellsize(w, h, TRUE);
7861 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007862 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863}
7864
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007866ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867{
7868 int xchar = NUL;
7869 char_u *p;
7870
7871 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007873 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 if (eap->arg[1] == NUL)
7875 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007876 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 return;
7878 }
7879 xchar = eap->arg[1];
7880 p = eap->arg + 2;
7881 }
7882 else
7883 p = eap->arg + 1;
7884
Bram Moolenaar63b91732021-08-05 20:40:03 +02007885 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007887 if (*p != NUL && *p != (
7888#ifdef FEAT_EVAL
7889 in_vim9script() ? '#' :
7890#endif
7891 '"')
7892 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007893 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007894 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007896 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007897 postponed_split_flags = cmdmod.cmod_split;
7898 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7900 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007901 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 }
7903}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904
Bram Moolenaar843ee412004-06-30 16:16:41 +00007905#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906/*
7907 * ":winpos".
7908 */
7909 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007910ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 int x, y;
7913 char_u *arg = eap->arg;
7914 char_u *p;
7915
7916 if (*arg == NUL)
7917 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007918# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007919# ifdef VIMDLL
7920 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7921 mch_get_winpos(&x, &y) != FAIL)
7922# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007924# else
7925 if (mch_get_winpos(&x, &y) != FAIL)
7926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {
7928 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007929 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 }
7931 else
7932# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007933 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 }
7935 else
7936 {
7937 x = getdigits(&arg);
7938 arg = skipwhite(arg);
7939 p = arg;
7940 y = getdigits(&arg);
7941 if (*p == NUL || *arg != NUL)
7942 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007943 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 return;
7945 }
7946# ifdef FEAT_GUI
7947 if (gui.in_use)
7948 gui_mch_set_winpos(x, y);
7949 else if (gui.starting)
7950 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007951 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 gui_win_x = x;
7953 gui_win_y = y;
7954 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007955# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 else
7957# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007958# endif
7959# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007960 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961# endif
7962# ifdef HAVE_TGETENT
7963 if (*T_CWP)
7964 term_set_winpos(x, y);
7965# endif
7966 }
7967}
7968#endif
7969
7970/*
7971 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7972 */
7973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007974ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975{
7976 oparg_T oa;
7977
7978 clear_oparg(&oa);
7979 oa.regname = eap->regname;
7980 oa.start.lnum = eap->line1;
7981 oa.end.lnum = eap->line2;
7982 oa.line_count = eap->line2 - eap->line1 + 1;
7983 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007985 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {
7987 setpcmark();
7988 curwin->w_cursor.lnum = eap->line1;
7989 beginline(BL_SOL | BL_FIX);
7990 }
7991
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007992 if (VIsual_active)
7993 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007994
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 switch (eap->cmdidx)
7996 {
7997 case CMD_delete:
7998 oa.op_type = OP_DELETE;
7999 op_delete(&oa);
8000 break;
8001
8002 case CMD_yank:
8003 oa.op_type = OP_YANK;
8004 (void)op_yank(&oa, FALSE, TRUE);
8005 break;
8006
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008007 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008008 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008010 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8011#else
8012 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008014 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 oa.op_type = OP_RSHIFT;
8016 else
8017 oa.op_type = OP_LSHIFT;
8018 op_shift(&oa, FALSE, eap->amount);
8019 break;
8020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008022 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023}
8024
8025/*
8026 * ":put".
8027 */
8028 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008029ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008031 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 if (eap->line2 == 0)
8033 {
8034 eap->line2 = 1;
8035 eap->forceit = TRUE;
8036 }
8037 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008038 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008039 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008040 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041}
8042
8043/*
8044 * Handle ":copy" and ":move".
8045 */
8046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008047ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048{
8049 long n;
8050
Bram Moolenaar491799b2020-08-01 19:23:43 +02008051#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008052 if (not_in_vim9(eap) == FAIL)
8053 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008054#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008055 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008056 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {
8058 eap->nextcmd = NULL;
8059 return;
8060 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008061 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062
8063 /*
8064 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8065 */
8066 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8067 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008068 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 return;
8070 }
8071
8072 if (eap->cmdidx == CMD_move)
8073 {
8074 if (do_move(eap->line1, eap->line2, n) == FAIL)
8075 return;
8076 }
8077 else
8078 ex_copy(eap->line1, eap->line2, n);
8079 u_clearline();
8080 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008081 ex_may_print(eap);
8082}
8083
8084/*
8085 * Print the current line if flags were given to the Ex command.
8086 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008087 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008088ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008089{
8090 if (eap->flags != 0)
8091 {
8092 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8093 (eap->flags & EXFLAG_LIST));
8094 ex_no_reprint = TRUE;
8095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096}
8097
8098/*
8099 * ":smagic" and ":snomagic".
8100 */
8101 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008102ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008104 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008106 magic_overruled = eap->cmdidx == CMD_smagic
8107 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008108 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008109 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110}
8111
8112/*
8113 * ":join".
8114 */
8115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008116ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117{
8118 curwin->w_cursor.lnum = eap->line1;
8119 if (eap->line1 == eap->line2)
8120 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008121 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 return;
8123 if (eap->line2 == curbuf->b_ml.ml_line_count)
8124 {
8125 beep_flush();
8126 return;
8127 }
8128 ++eap->line2;
8129 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008130 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008132 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133}
8134
8135/*
8136 * ":[addr]@r" or ":[addr]*r": execute register
8137 */
8138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008139ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140{
8141 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008142 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143
8144 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008145 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146
8147#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008148 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149#endif
8150
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008151 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 c = *eap->arg;
8153 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8154 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008155 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008156 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8157 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 else
8162 {
8163 int save_efr = exec_from_reg;
8164
8165 exec_from_reg = TRUE;
8166
8167 /*
8168 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008169 * Continue until the stuff buffer is empty and all added characters
8170 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008172 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8174
8175 exec_from_reg = save_efr;
8176 }
8177}
8178
8179/*
8180 * ":!".
8181 */
8182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008183ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184{
8185 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8186}
8187
8188/*
8189 * ":undo".
8190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008192ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008194 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008195 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008196 else
8197 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198}
8199
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008200#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008202ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008203{
8204 char_u hash[UNDO_HASH_SIZE];
8205
8206 u_compute_hash(hash);
8207 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8208}
8209
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008210 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008211ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008212{
8213 char_u hash[UNDO_HASH_SIZE];
8214
8215 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008216 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008217}
8218#endif
8219
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220/*
8221 * ":redo".
8222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008224ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225{
8226 u_redo(1);
8227}
8228
8229/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008230 * ":earlier" and ":later".
8231 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008233ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008234{
8235 long count = 0;
8236 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008237 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008238 char_u *p = eap->arg;
8239
8240 if (*p == NUL)
8241 count = 1;
8242 else if (isdigit(*p))
8243 {
8244 count = getdigits(&p);
8245 switch (*p)
8246 {
8247 case 's': ++p; sec = TRUE; break;
8248 case 'm': ++p; sec = TRUE; count *= 60; break;
8249 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008250 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8251 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008252 }
8253 }
8254
8255 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008256 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008257 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008258 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8259 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008260}
8261
8262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 * ":redir": start/stop redirection.
8264 */
8265 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008266ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267{
8268 char *mode;
8269 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008270 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271
Bram Moolenaarba768492016-07-08 15:32:54 +02008272#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008273 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008274 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008275 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008276 return;
8277 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008278#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008279
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 if (STRICMP(eap->arg, "END") == 0)
8281 close_redir();
8282 else
8283 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008284 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008286 ++arg;
8287 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008289 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 mode = "a";
8291 }
8292 else
8293 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008294 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295
8296 close_redir();
8297
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008298 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008299 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 if (fname == NULL)
8301 return;
8302#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008303 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {
8305 char_u *browseFile;
8306
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008307 browseFile = do_browse(BROWSE_SAVE,
8308 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008309 fname, NULL, NULL,
8310 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008312 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 vim_free(fname);
8314 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008315 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
8317#endif
8318
8319 redir_fd = open_exfile(fname, eap->forceit, mode);
8320 vim_free(fname);
8321 }
8322#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008323 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008325 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008327 ++arg;
8328 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008330 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008331 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008333 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008335 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008336 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008337 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008338 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008340 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008341 if (*arg == '>')
8342 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008343 // Make register empty when not using @A-@Z and the
8344 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008345 if (*arg == NUL && !isupper(redir_reg))
8346 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008348 }
8349 if (*arg != NUL)
8350 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008351 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008352 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008353 }
8354 }
8355 else if (*arg == '=' && arg[1] == '>')
8356 {
8357 int append;
8358
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008359 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008360 close_redir();
8361 arg += 2;
8362
8363 if (*arg == '>')
8364 {
8365 ++arg;
8366 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 }
8368 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008369 append = FALSE;
8370
8371 if (var_redir_start(skipwhite(arg), append) == OK)
8372 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 }
8374#endif
8375
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008376 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008379 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008382 // Make sure redirection is not off. Can happen for cmdline completion
8383 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008384 if (redir_fd != NULL
8385#ifdef FEAT_EVAL
8386 || redir_reg || redir_vname
8387#endif
8388 )
8389 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390}
8391
8392/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008393 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008395 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008396ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008398 redraw_cmd(eap->forceit);
8399}
8400
8401/*
8402 * ":redraw": force redraw, with clear if "clear" is TRUE.
8403 */
8404 void
8405redraw_cmd(int clear)
8406{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 int r = RedrawingDisabled;
8408 int p = p_lz;
8409
8410 RedrawingDisabled = 0;
8411 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008412 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008414 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 if (need_maketitle)
8416 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008417#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8418# ifdef VIMDLL
8419 if (!gui.in_use)
8420# endif
8421 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 RedrawingDisabled = r;
8424 p_lz = p;
8425
Bram Moolenaar5017c662022-04-07 18:06:08 +01008426 // After drawing the statusline screen_attr may still be set.
8427 screen_stop_highlight();
8428
Bram Moolenaara2a89732022-08-31 14:46:18 +01008429 // Reset msg_didout, so that a message that's there is overwritten.
8430 msg_didout = FALSE;
8431 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008433 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008434 need_wait_return = FALSE;
8435
Bram Moolenaara653e532022-04-19 11:38:24 +01008436 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008437 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008438 redrawcmdline();
8439
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 out_flush();
8441}
8442
8443/*
8444 * ":redrawstatus": force redraw of status line(s)
8445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008447ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 int r = RedrawingDisabled;
8450 int p = p_lz;
8451
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 if (eap->forceit)
8453 status_redraw_all();
8454 else
8455 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008456 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008457 return; // redraw later
8458
8459 RedrawingDisabled = 0;
8460 p_lz = FALSE;
zeertzjq320d9102022-09-20 17:12:13 +01008461 if (State & MODE_CMDLINE)
8462 redraw_statuslines();
8463 else
8464 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 RedrawingDisabled = r;
8466 p_lz = p;
8467 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468}
8469
Bram Moolenaare12bab32019-01-08 22:02:56 +01008470/*
8471 * ":redrawtabline": force redraw of the tabline
8472 */
8473 static void
8474ex_redrawtabline(exarg_T *eap UNUSED)
8475{
8476 int r = RedrawingDisabled;
8477 int p = p_lz;
8478
8479 RedrawingDisabled = 0;
8480 p_lz = FALSE;
8481
8482 draw_tabline();
8483
8484 RedrawingDisabled = r;
8485 p_lz = p;
8486 out_flush();
8487}
8488
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008490close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491{
8492 if (redir_fd != NULL)
8493 {
8494 fclose(redir_fd);
8495 redir_fd = NULL;
8496 }
8497#ifdef FEAT_EVAL
8498 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008499 if (redir_vname)
8500 {
8501 var_redir_stop();
8502 redir_vname = 0;
8503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504#endif
8505}
8506
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008507#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008508 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008509vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008510{
8511 if (vim_mkdir(name, prot) != 0)
8512 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008513 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008514 return FAIL;
8515 }
8516 return OK;
8517}
8518#endif
8519
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520/*
8521 * Open a file for writing for an Ex command, with some checks.
8522 * Return file descriptor, or NULL on failure.
8523 */
8524 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008525open_exfile(
8526 char_u *fname,
8527 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008528 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529{
8530 FILE *fd;
8531
8532#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008533 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 if (mch_isdir(fname))
8535 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008536 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 return NULL;
8538 }
8539#endif
8540 if (!forceit && *mode != 'a' && vim_fexists(fname))
8541 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008542 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 return NULL;
8544 }
8545
8546 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008547 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548
8549 return fd;
8550}
8551
8552/*
8553 * ":mark" and ":k".
8554 */
8555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008556ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
8558 pos_T pos;
8559
Bram Moolenaar10b94212021-02-19 21:42:57 +01008560#ifdef FEAT_EVAL
8561 if (not_in_vim9(eap) == FAIL)
8562 return;
8563#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008564 if (*eap->arg == NUL) // No argument?
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008565 emsg(_(e_argument_required));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008566 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar74409f62022-01-01 15:58:22 +00008567 semsg(_(e_trailing_characters_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 else
8569 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008570 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 curwin->w_cursor.lnum = eap->line2;
8572 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008573 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaar6d057012021-12-31 18:49:43 +00008574 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008575 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 }
8577}
8578
8579/*
8580 * Update w_topline, w_leftcol and the cursor position.
8581 */
8582 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008583update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008585 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 update_topline();
8587 if (!curwin->w_p_wrap)
8588 validate_cursor();
8589 update_curswant();
8590}
8591
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008593 * Save the current State and go to Normal mode.
8594 * Return TRUE if the typeahead could be saved.
8595 */
8596 int
8597save_current_state(save_state_T *sst)
8598{
8599 sst->save_msg_scroll = msg_scroll;
8600 sst->save_restart_edit = restart_edit;
8601 sst->save_msg_didout = msg_didout;
8602 sst->save_State = State;
8603 sst->save_insertmode = p_im;
8604 sst->save_finish_op = finish_op;
8605 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008606 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008607 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008608
Bram Moolenaar4324d872020-12-01 20:12:24 +01008609 msg_scroll = FALSE; // no msg scrolling in Normal mode
8610 restart_edit = 0; // don't go to Insert mode
8611 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008612
Bram Moolenaara452b802020-12-01 21:47:59 +01008613 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008614 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008615
8616 /*
8617 * Save the current typeahead. This is required to allow using ":normal"
8618 * from an event handler and makes sure we don't hang when the argument
8619 * ends with half a command.
8620 */
8621 save_typeahead(&sst->tabuf);
8622 return sst->tabuf.typebuf_valid;
8623}
8624
8625 void
8626restore_current_state(save_state_T *sst)
8627{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008628 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008629 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008630
8631 msg_scroll = sst->save_msg_scroll;
8632 restart_edit = sst->save_restart_edit;
8633 p_im = sst->save_insertmode;
8634 finish_op = sst->save_finish_op;
8635 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008636 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008637 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008638 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008639 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008640
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008641 // Restore the state (needed when called from a function executed for
8642 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008643 State = sst->save_State;
8644#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008645 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008646#endif
8647}
8648
8649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 * ":normal[!] {commands}": Execute normal mode commands.
8651 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008652 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008653ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008655 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 char_u *arg = NULL;
8657 int l;
8658 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008660 if (ex_normal_lock > 0)
8661 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008662 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008663 return;
8664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 if (ex_normal_busy >= p_mmd)
8666 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008667 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 return;
8669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 /*
8672 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8673 * this for the K_SPECIAL leading byte, otherwise special keys will not
8674 * work.
8675 */
8676 if (has_mbyte)
8677 {
8678 int len = 0;
8679
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008680 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 for (p = eap->arg; *p != NUL; ++p)
8682 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008683#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008684 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008686#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008687 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008688 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008689#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 )
8693 len += 2;
8694 }
8695 if (len > 0)
8696 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008697 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 if (arg != NULL)
8699 {
8700 len = 0;
8701 for (p = eap->arg; *p != NUL; ++p)
8702 {
8703 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008704#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 if (*p == CSI)
8706 {
8707 arg[len++] = KS_EXTRA;
8708 arg[len++] = (int)KE_CSI;
8709 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008710#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008711 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {
8713 arg[len++] = *++p;
8714 if (*p == K_SPECIAL)
8715 {
8716 arg[len++] = KS_SPECIAL;
8717 arg[len++] = KE_FILLER;
8718 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008719#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 else if (*p == CSI)
8721 {
8722 arg[len++] = KS_EXTRA;
8723 arg[len++] = (int)KE_CSI;
8724 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 }
8727 arg[len] = NUL;
8728 }
8729 }
8730 }
8731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008733 ++ex_normal_busy;
8734 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 {
8736 /*
8737 * Repeat the :normal command for each line in the range. When no
8738 * range given, execute it just once, without positioning the cursor
8739 * first.
8740 */
8741 do
8742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 if (eap->addr_count != 0)
8744 {
8745 curwin->w_cursor.lnum = eap->line1++;
8746 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008747 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 }
8749
Bram Moolenaar13505972019-01-24 15:04:48 +01008750 exec_normal_cmd(arg != NULL
8751 ? arg
8752 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 }
8754 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8755 }
8756
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008757 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 update_topline_cursor();
8759
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008760 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008762 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008763#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008764 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008765#endif
8766
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768}
8769
8770/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008771 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 */
8773 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008774ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008776 if (eap->forceit)
8777 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008778 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008779 if (!curwin->w_cursor.lnum)
8780 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008781 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008782 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008783#ifdef FEAT_TERMINAL
8784 // Ignore this when running in an active terminal.
8785 if (term_job_running(curbuf->b_term))
8786 return;
8787#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008788
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008789 // Ignore the command when already in Insert mode. Inserting an
8790 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01008791 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00008792 return;
8793
Bram Moolenaar64969662005-12-14 21:59:55 +00008794 if (eap->cmdidx == CMD_startinsert)
8795 restart_edit = 'a';
8796 else if (eap->cmdidx == CMD_startreplace)
8797 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008799 restart_edit = 'V';
8800
8801 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008803 if (eap->cmdidx == CMD_startinsert)
8804 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008805 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008807
8808 if (VIsual_active)
8809 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810}
8811
8812/*
8813 * ":stopinsert"
8814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008816ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817{
8818 restart_edit = 0;
8819 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008820 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008823/*
8824 * Execute normal mode command "cmd".
8825 * "remap" can be REMAP_NONE or REMAP_YES.
8826 */
8827 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008828exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008829{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008830 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008831 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008832 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008833}
Bram Moolenaar25281632016-01-21 23:32:32 +01008834
Bram Moolenaar25281632016-01-21 23:32:32 +01008835/*
8836 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008837 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008838 */
8839 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008840exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008841{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008842 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008843 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008844
Bram Moolenaar905dd902019-04-07 14:21:47 +02008845 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8846 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008847 clear_oparg(&oa);
8848 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008849 while ((!stuff_empty()
8850 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008851 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008852 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008853 {
8854 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008855#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008856 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008857 && oa.op_type == OP_NOP && oa.regname == NUL
8858 && !VIsual_active)
8859 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008860 // If terminal_loop() returns OK we got a key that is handled
8861 // in Normal model. With FAIL we first need to position the
8862 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008863 if (terminal_loop(TRUE) == OK)
8864 normal_cmd(&oa, TRUE);
8865 }
8866 else
8867#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008868 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008869 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008870 }
8871}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008872
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873#ifdef FEAT_FIND_ID
8874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008875ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
8877 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8878 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8879 (linenr_T)1, (linenr_T)MAXLNUM);
8880}
8881
Bram Moolenaar4033c552017-09-16 20:54:51 +02008882#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883/*
8884 * ":psearch"
8885 */
8886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008887ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 g_do_tagpreview = p_pvh;
8890 ex_findpat(eap);
8891 g_do_tagpreview = 0;
8892}
8893#endif
8894
8895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008896ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897{
8898 int whole = TRUE;
8899 long n;
8900 char_u *p;
8901 int action;
8902
8903 switch (cmdnames[eap->cmdidx].cmd_name[2])
8904 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008905 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8907 action = ACTION_GOTO;
8908 else
8909 action = ACTION_SHOW;
8910 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008911 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 action = ACTION_SHOW_ALL;
8913 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008914 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 action = ACTION_GOTO;
8916 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008917 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 action = ACTION_SPLIT;
8919 break;
8920 }
8921
8922 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008923 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 {
8925 n = getdigits(&eap->arg);
8926 eap->arg = skipwhite(eap->arg);
8927 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008928 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 {
8930 whole = FALSE;
8931 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008932 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 if (*p)
8934 {
8935 *p++ = NUL;
8936 p = skipwhite(p);
8937
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008938 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008939 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008940 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008942 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 }
8944 }
8945 if (!eap->skip)
8946 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8947 whole, !eap->forceit,
8948 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8949 n, action, eap->line1, eap->line2);
8950}
8951#endif
8952
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
Bram Moolenaar4033c552017-09-16 20:54:51 +02008954#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955/*
8956 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8957 */
8958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008959ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008961 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8963}
8964
8965/*
8966 * ":pedit"
8967 */
8968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008969ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970{
8971 win_T *curwin_save = curwin;
8972
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008973 if (ERROR_IF_ANY_POPUP_WINDOW)
8974 return;
8975
Bram Moolenaar026587b2019-08-17 15:08:00 +02008976 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008978 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008979
Bram Moolenaar026587b2019-08-17 15:08:00 +02008980 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008982
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 if (curwin != curwin_save && win_valid(curwin_save))
8984 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008985 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01008987 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 win_enter(curwin_save, TRUE);
8989 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008990# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008991 else if (WIN_IS_POPUP(curwin))
8992 {
8993 // can't keep focus in popup window
8994 win_enter(firstwin, TRUE);
8995 }
8996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 g_do_tagpreview = 0;
8998}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000
9001/*
9002 * ":stag", ":stselect" and ":stjump".
9003 */
9004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009005ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006{
9007 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009008 postponed_split_flags = cmdmod.cmod_split;
9009 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9011 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009012 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014
9015/*
9016 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9017 */
9018 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009019ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020{
9021 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9022}
9023
9024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009025ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
9027 int cmd;
9028
9029 switch (name[1])
9030 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009031 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009033 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009035 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009037 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009039 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009041 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009043 case 'f': // ":tfirst"
9044 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009046 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009048 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009050 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009052 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 return;
9054 }
9055#endif
9056 cmd = DT_TAG;
9057 break;
9058 }
9059
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009060 if (name[0] == 'l')
9061 {
9062#ifndef FEAT_QUICKFIX
9063 ex_ni(eap);
9064 return;
9065#else
9066 cmd = DT_LTAG;
9067#endif
9068 }
9069
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9071 eap->forceit, TRUE);
9072}
9073
9074/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009075 * Check "str" for starting with a special cmdline variable.
9076 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9077 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9078 */
9079 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009080find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009081{
9082 int len;
9083 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009084 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009085 "%",
9086#define SPEC_PERC 0
9087 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009088#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009089 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02009090#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009091 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02009092#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009093 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02009094#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009095 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009096#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009097 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009098#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009099 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02009100#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02009101 "<stack>", // call stack
9102#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01009103 "<script>", // script file name
9104#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009105 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01009106#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009107 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009108#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009109 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009110#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009111 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009112#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02009113 "<SID>", // script ID: <SNR>123_
9114#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009115#ifdef FEAT_CLIENTSERVER
9116 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02009117# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009118#endif
9119 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009120
K.Takataeeec2542021-06-02 13:28:16 +02009121 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009122 {
9123 len = (int)STRLEN(spec_str[i]);
9124 if (STRNCMP(src, spec_str[i], len) == 0)
9125 {
9126 *usedlen = len;
9127 return i;
9128 }
9129 }
9130 return -1;
9131}
9132
9133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 * Evaluate cmdline variables.
9135 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009136 * change "%" to curbuf->b_ffname
9137 * "#" to curwin->w_alt_fnum
9138 * "%%" to curwin->w_alt_fnum in Vim9 script
9139 * "<cword>" to word under the cursor
9140 * "<cWORD>" to WORD under the cursor
9141 * "<cexpr>" to C-expression under the cursor
9142 * "<cfile>" to path name under the cursor
9143 * "<sfile>" to sourced file name
9144 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009145 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009146 * "<slnum>" to sourced file line number
9147 * "<afile>" to file name for autocommand
9148 * "<abuf>" to buffer number for autocommand
9149 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 *
9151 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9152 * "" for error without a message) and NULL is returned.
9153 * Returns an allocated string if a valid match was found.
9154 * Returns NULL if no match was found. "usedlen" then still contains the
9155 * number of characters to skip.
9156 */
9157 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009158eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009159 char_u *src, // pointer into commandline
9160 char_u *srcstart, // beginning of valid memory for src
9161 int *usedlen, // characters after src that are used
9162 linenr_T *lnump, // line number for :e command, or NULL
9163 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009164 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009165 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009166 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
9168 int i;
9169 char_u *s;
9170 char_u *result;
9171 char_u *resultbuf = NULL;
9172 int resultlen;
9173 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009174 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009176 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179
9180 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009181 if (escaped != NULL)
9182 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183
9184 /*
9185 * Check if there is something to do.
9186 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009187 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009188 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 {
9190 *usedlen = 1;
9191 return NULL;
9192 }
9193
9194 /*
9195 * Skip when preceded with a backslash "\%" and "\#".
9196 * Note: In "\\%" the % is also not recognized!
9197 */
9198 if (src > srcstart && src[-1] == '\\')
9199 {
9200 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009201 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 return NULL;
9203 }
9204
9205 /*
9206 * word or WORD under cursor
9207 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009208 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9209 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009211 resultlen = find_ident_under_cursor(&result,
9212 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9213 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9214 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 if (resultlen == 0)
9216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009217 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 return NULL;
9219 }
9220 }
9221
9222 /*
9223 * '#': Alternate file name
9224 * '%': Current file name
9225 * File name under the cursor
9226 * File name for autocommand
9227 * and following modifiers
9228 */
9229 else
9230 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009231 int off = 0;
9232
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 switch (spec_idx)
9234 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009235 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009236#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009237 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009240 // '%': current file
9241 if (curbuf->b_fname == NULL)
9242 {
9243 result = (char_u *)"";
9244 valid = 0; // Must have ":p:h" to be valid
9245 }
9246 else
9247 {
9248 result = curbuf->b_fname;
9249 tilde_file = STRCMP(result, "~") == 0;
9250 }
9251 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009253#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009254 // "%%" alternate file
9255 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009256#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009257 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009258 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009259 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009261 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 result = arg_all();
9263 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009264 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009265 if (escaped != NULL)
9266 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 break;
9269 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009270 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009271 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009272 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009274 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009275 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009276 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009277 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009279 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009281 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009282 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009283 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009284 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009285 return NULL;
9286 }
9287#ifdef FEAT_EVAL
9288 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9289 (long)i);
9290 if (result == NULL)
9291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009292 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009293 return NULL;
9294 }
9295#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009296 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 }
9300 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009301 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009302 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9303 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009304 buf = buflist_findnr(i);
9305 if (buf == NULL)
9306 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009307 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009308 return NULL;
9309 }
9310 if (lnump != NULL)
9311 *lnump = ECMD_LAST;
9312 if (buf->b_fname == NULL)
9313 {
9314 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009315 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009316 }
9317 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009318 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009319 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009320 tilde_file = STRCMP(result, "~") == 0;
9321 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 break;
9324
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009325 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009326 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 if (result == NULL)
9328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009329 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 return NULL;
9331 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009332 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009335 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009337 if (result != NULL && !autocmd_fname_full)
9338 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009339 // Still need to turn the fname into a full path. It is
9340 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009341 autocmd_fname_full = TRUE;
9342 result = FullName_save(autocmd_fname, FALSE);
9343 vim_free(autocmd_fname);
9344 autocmd_fname = result;
9345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 if (result == NULL)
9347 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009348 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 return NULL;
9350 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009351 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 break;
9353
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009354 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 if (autocmd_bufnr <= 0)
9356 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009357 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 return NULL;
9359 }
9360 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9361 result = strbuf;
9362 break;
9363
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009364 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 result = autocmd_match;
9366 if (result == NULL)
9367 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009368 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 return NULL;
9370 }
9371 break;
9372
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009373 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009374 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 if (result == NULL)
9376 {
LemonBoy6013d002022-04-09 21:42:10 +01009377 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9378 return NULL;
9379 }
9380 resultbuf = result; // remember allocated string
9381 break;
9382 case SPEC_STACK: // call stack
9383 result = estack_sfile(ESTACK_STACK);
9384 if (result == NULL)
9385 {
9386 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9387 return NULL;
9388 }
9389 resultbuf = result; // remember allocated string
9390 break;
9391 case SPEC_SCRIPT: // script file name
9392 result = estack_sfile(ESTACK_SCRIPT);
9393 if (result == NULL)
9394 {
9395 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 return NULL;
9397 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009398 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009400
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009401 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009402 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009403 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009404 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009405 return NULL;
9406 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009407 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009408 result = strbuf;
9409 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009410
9411#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009412 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009413 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009414 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009415 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009416 return NULL;
9417 }
9418 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009419 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009420 result = strbuf;
9421 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009422
Bram Moolenaar90944302020-08-01 20:45:11 +02009423 case SPEC_SID:
9424 if (current_sctx.sc_sid <= 0)
9425 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009426 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009427 return NULL;
9428 }
9429 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9430 result = strbuf;
9431 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009432#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009433
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009434#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009435 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009436 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9437 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 result = strbuf;
9439 break;
9440#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009441
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009442 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009443 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 }
9446
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009447 resultlen = (int)STRLEN(result); // length of new string
9448 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 {
9450 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 resultlen = (int)(s - result);
9453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 else if (!skip_mod)
9455 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009456 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 &resultlen);
9458 if (result == NULL)
9459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009460 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 return NULL;
9462 }
9463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 }
9465
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009466 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009468 if (empty_is_error)
9469 {
9470 if (valid != VALID_HEAD + VALID_PATH)
9471 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9472 else
9473 *errormsg = _(e_evaluates_to_an_empty_string);
9474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 result = NULL;
9476 }
9477 else
9478 result = vim_strnsave(result, resultlen);
9479 vim_free(resultbuf);
9480 return result;
9481}
9482
9483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 * Expand the <sfile> string in "arg".
9485 *
9486 * Returns an allocated string, or NULL for any error.
9487 */
9488 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009489expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009491 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 int len;
9493 char_u *result;
9494 char_u *newres;
9495 char_u *repl;
9496 int srclen;
9497 char_u *p;
9498
9499 result = vim_strsave(arg);
9500 if (result == NULL)
9501 return NULL;
9502
9503 for (p = result; *p; )
9504 {
9505 if (STRNCMP(p, "<sfile>", 7) != 0)
9506 ++p;
9507 else
9508 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009509 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009510 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 if (errormsg != NULL)
9512 {
9513 if (*errormsg)
9514 emsg(errormsg);
9515 vim_free(result);
9516 return NULL;
9517 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009518 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 {
9520 p += srclen;
9521 continue;
9522 }
9523 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9524 newres = alloc(len);
9525 if (newres == NULL)
9526 {
9527 vim_free(repl);
9528 vim_free(result);
9529 return NULL;
9530 }
9531 mch_memmove(newres, result, (size_t)(p - result));
9532 STRCPY(newres + (p - result), repl);
9533 len = (int)STRLEN(newres);
9534 STRCAT(newres, p + srclen);
9535 vim_free(repl);
9536 vim_free(result);
9537 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009538 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 }
9540 }
9541
9542 return result;
9543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009546/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009547 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009548 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009551dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 if (fname == NULL)
9554 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009555 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556}
9557#endif
9558
9559/*
9560 * ":behave {mswin,xterm}"
9561 */
9562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009563ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 if (STRCMP(eap->arg, "mswin") == 0)
9566 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009567 set_option_value_give_err((char_u *)"selection",
9568 0L, (char_u *)"exclusive", 0);
9569 set_option_value_give_err((char_u *)"selectmode",
9570 0L, (char_u *)"mouse,key", 0);
9571 set_option_value_give_err((char_u *)"mousemodel",
9572 0L, (char_u *)"popup", 0);
9573 set_option_value_give_err((char_u *)"keymodel",
9574 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 }
9576 else if (STRCMP(eap->arg, "xterm") == 0)
9577 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009578 set_option_value_give_err((char_u *)"selection",
9579 0L, (char_u *)"inclusive", 0);
9580 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9581 set_option_value_give_err((char_u *)"mousemodel",
9582 0L, (char_u *)"extend", 0);
9583 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 }
9585 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009586 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587}
9588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589static int filetype_detect = FALSE;
9590static int filetype_plugin = FALSE;
9591static int filetype_indent = FALSE;
9592
9593/*
9594 * ":filetype [plugin] [indent] {on,off,detect}"
9595 * on: Load the filetype.vim file to install autocommands for file types.
9596 * off: Load the ftoff.vim file to remove all autocommands for file types.
9597 * plugin on: load filetype.vim and ftplugin.vim
9598 * plugin off: load ftplugof.vim
9599 * indent on: load filetype.vim and indent.vim
9600 * indent off: load indoff.vim
9601 */
9602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009603ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604{
9605 char_u *arg = eap->arg;
9606 int plugin = FALSE;
9607 int indent = FALSE;
9608
9609 if (*eap->arg == NUL)
9610 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009611 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009612 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 filetype_detect ? "ON" : "OFF",
9614 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9615 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9616 return;
9617 }
9618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009619 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 for (;;)
9621 {
9622 if (STRNCMP(arg, "plugin", 6) == 0)
9623 {
9624 plugin = TRUE;
9625 arg = skipwhite(arg + 6);
9626 continue;
9627 }
9628 if (STRNCMP(arg, "indent", 6) == 0)
9629 {
9630 indent = TRUE;
9631 arg = skipwhite(arg + 6);
9632 continue;
9633 }
9634 break;
9635 }
9636 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9637 {
9638 if (*arg == 'o' || !filetype_detect)
9639 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009640 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 filetype_detect = TRUE;
9642 if (plugin)
9643 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009644 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 filetype_plugin = TRUE;
9646 }
9647 if (indent)
9648 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009649 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 filetype_indent = TRUE;
9651 }
9652 }
9653 if (*arg == 'd')
9654 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009655 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009656 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 }
9658 }
9659 else if (STRCMP(arg, "off") == 0)
9660 {
9661 if (plugin || indent)
9662 {
9663 if (plugin)
9664 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009665 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 filetype_plugin = FALSE;
9667 }
9668 if (indent)
9669 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009670 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 filetype_indent = FALSE;
9672 }
9673 }
9674 else
9675 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009676 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 filetype_detect = FALSE;
9678 }
9679 }
9680 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009681 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682}
9683
9684/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009685 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 */
9687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009688ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689{
9690 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009691 {
9692 char_u *arg = eap->arg;
9693
9694 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9695 arg += 9;
9696
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009697 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
Bram Moolenaar3e545692017-06-04 19:00:32 +02009698 if (arg != eap->arg)
9699 did_filetype = FALSE;
9700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009704ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
9706#ifdef FEAT_DIGRAPHS
9707 if (*eap->arg != NUL)
9708 putdigraph(eap->arg);
9709 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009710 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711#else
mityu61065042021-07-19 20:07:21 +02009712 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713#endif
9714}
9715
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009716#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9717 void
9718set_no_hlsearch(int flag)
9719{
9720 no_hlsearch = flag;
9721# ifdef FEAT_EVAL
9722 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9723# endif
9724}
9725
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726/*
9727 * ":nohlsearch"
9728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009730ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009732 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009733 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735#endif
9736
9737#ifdef FEAT_CRYPT
9738/*
9739 * ":X": Get crypt key
9740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009742ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009744 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009745 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746}
9747#endif
9748
9749#ifdef FEAT_FOLDING
9750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009751ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752{
9753 if (foldManualAllowed(TRUE))
9754 foldCreate(eap->line1, eap->line2);
9755}
9756
9757 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009758ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759{
9760 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9761 eap->forceit, FALSE);
9762}
9763
9764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009765ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
9767 linenr_T lnum;
9768
Bram Moolenaar4facea32019-10-12 20:17:40 +02009769# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009770 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009771# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009772
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009773 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9775 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9776 ml_setmarked(lnum);
9777
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009778 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009780 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009781# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009782 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784}
9785#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009787#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009788/*
9789 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9790 * instead of a quickfix command.
9791 */
9792 int
9793is_loclist_cmd(int cmdidx)
9794{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009795 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009796 return FALSE;
9797 return cmdnames[cmdidx].cmd_name[0] == 'l';
9798}
9799#endif
9800
Bram Moolenaar4facea32019-10-12 20:17:40 +02009801#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009802 int
9803get_pressedreturn(void)
9804{
9805 return ex_pressedreturn;
9806}
9807
9808 void
9809set_pressedreturn(int val)
9810{
9811 ex_pressedreturn = val;
9812}
9813#endif