blob: 3ffbf2b9064c203f16f4511a2585629ee7b154f1 [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
1892
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001893 // May go to debug mode. If this happens and the ">quit" debug command is
1894 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001895 dbg_check_breakpoint(&ea);
1896 if (!ea.skip && got_int)
1897 {
1898 ea.skip = TRUE;
1899 (void)do_intthrow(cstack);
1900 }
1901#endif
1902
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001903/*
1904 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 *
1906 * where 'addr' is:
1907 *
1908 * % (entire file)
1909 * $ [+-NUM]
1910 * 'x [+-NUM] (where x denotes a currently defined mark)
1911 * . [+-NUM]
1912 * [+-NUM]..
1913 * NUM
1914 *
1915 * The ea.cmd pointer is updated to point to the first character following the
1916 * range spec. If an initial address is found, but no second, the upper bound
1917 * is equal to the lower.
1918 */
1919
Bram Moolenaar25190db2019-05-04 15:05:28 +02001920 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001921 if (!IS_USER_CMDIDX(ea.cmdidx))
1922 {
1923 if (ea.cmdidx != CMD_SIZE)
1924 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1925 else
1926 ea.addr_type = ADDR_LINES;
1927
Bram Moolenaar25190db2019-05-04 15:05:28 +02001928 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001929 if (ea.cmdidx == CMD_wincmd && p != NULL)
1930 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001931#ifdef FEAT_QUICKFIX
1932 // :.cc in quickfix window uses line number
1933 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1934 ea.addr_type = ADDR_OTHER;
1935#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001936 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001937
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001938 ea.cmd = cmd;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001939 if (!may_have_range)
1940 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001941 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1942 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001945 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 */
1947
1948 /*
1949 * Skip ':' and any white space
1950 */
1951 ea.cmd = skipwhite(ea.cmd);
1952 while (*ea.cmd == ':')
1953 ea.cmd = skipwhite(ea.cmd + 1);
1954
1955 /*
1956 * If we got a line, but no command, then go to the line.
1957 * If we find a '|' or '\n' we set ea.nextcmd.
1958 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001959 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1960 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
1962 /*
1963 * strange vi behaviour:
1964 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001965 * ":3|..." prints line 3 (not in Vim9 script)
1966 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001968 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001970 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 goto doend;
1972 }
1973
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001974 // If this looks like an undefined user command and there are CmdUndefined
1975 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001976 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1977 && ASCII_ISUPPER(*ea.cmd)
1978 && has_cmdundefined())
1979 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001980 int ret;
1981
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001982 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001983 while (ASCII_ISALNUM(*p))
1984 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001985 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001986 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1987 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001988 // If the autocommands did something and didn't cause an error, try
1989 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001990 p = (ret
1991#ifdef FEAT_EVAL
1992 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001993#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001995 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (p == NULL)
1998 {
1999 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002000 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 goto doend;
2002 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002003 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002004 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2005 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 {
2007 errormsg = uc_fun_cmd();
2008 goto doend;
2009 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002010
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (ea.cmdidx == CMD_SIZE)
2012 {
2013 if (!ea.skip)
2014 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002015 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002017 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002018 // If the modifier was parsed OK the error must be in the
2019 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002020 if (after_modifier != NULL)
2021 append_command(after_modifier);
2022 else
2023 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002024 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002025 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002026 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002027 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 goto doend;
2030 }
2031
Bram Moolenaar958636c2014-10-21 20:01:58 +02002032 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2033 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002034#ifdef HAVE_EX_SCRIPT_NI
2035 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2036#endif
2037 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038
2039#ifndef FEAT_EVAL
2040 /*
2041 * When the expression evaluation is disabled, recognize the ":if" and
2042 * ":endif" commands and ignore everything in between it.
2043 */
2044 if (ea.cmdidx == CMD_if)
2045 ++if_level;
2046 if (if_level)
2047 {
2048 if (ea.cmdidx == CMD_endif)
2049 --if_level;
2050 goto doend;
2051 }
2052
2053#endif
2054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002055 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002056 if (*p == '!' && ea.cmdidx != CMD_substitute
2057 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
2059 ++p;
2060 ea.forceit = TRUE;
2061 }
2062 else
2063 ea.forceit = FALSE;
2064
2065/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002066 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002068 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002069 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
2071 if (!ea.skip)
2072 {
2073#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002074 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002076 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002077 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 goto doend;
2079 }
2080#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002081 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002082 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002083 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002084 goto doend;
2085 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002086 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002088 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002089 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 goto doend;
2091 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002092
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002093 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002095 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2096 {
2097 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002098 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002099 goto doend;
2100 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002101 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2102 {
2103 // Command not allowed when text is locked
2104 errormsg = _(get_text_locked_msg());
2105 goto doend;
2106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002109 // Disallow editing another buffer when "curbuf_lock" is set.
2110 // Do allow ":checktime" (it is postponed).
2111 // Do allow ":edit" (check for an argument later).
2112 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002113 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002114 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002115 && ea.cmdidx != CMD_edit
2116 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002117 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002118 && curbuf_locked())
2119 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002121 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002123 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 goto doend;
2125 }
2126 }
2127
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002128 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002130 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 goto doend;
2132 }
2133
2134 /*
2135 * Don't complain about the range if it is not used
2136 * (could happen if line_count is accidentally set to 0).
2137 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002138 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
2140 /*
2141 * If the range is backwards, ask for confirmation and, if given, swap
2142 * ea.line1 & ea.line2 so it's forwards again.
2143 * When global command is busy, don't ask, will fail below.
2144 */
2145 if (!global_busy && ea.line1 > ea.line2)
2146 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002147 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002149 if (sourcing || exmode_active)
2150 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002151 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002152 goto doend;
2153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 if (ask_yesno((char_u *)
2155 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002156 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 lnum = ea.line1;
2159 ea.line1 = ea.line2;
2160 ea.line2 = lnum;
2161 }
2162 if ((errormsg = invalid_range(&ea)) != NULL)
2163 goto doend;
2164 }
2165
Bram Moolenaarb7316892019-05-01 18:08:42 +02002166 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2167 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 ea.line2 = 1;
2169
2170 correct_range(&ea);
2171
2172#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002173 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002174 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002176 // Put the first line at the start of a closed fold, put the last line
2177 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 (void)hasFolding(ea.line1, &ea.line1, NULL);
2179 (void)hasFolding(ea.line2, NULL, &ea.line2);
2180 }
2181#endif
2182
2183#ifdef FEAT_QUICKFIX
2184 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002185 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 * option here, so things like % get expanded.
2187 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002188 p = replace_makeprg(&ea, p, cmdlinep);
2189 if (p == NULL)
2190 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191#endif
2192
2193 /*
2194 * Skip to start of argument.
2195 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2196 */
2197 if (ea.cmdidx == CMD_bang)
2198 ea.arg = p;
2199 else
2200 ea.arg = skipwhite(p);
2201
Bram Moolenaar379fb762018-08-30 15:58:28 +02002202 // ":file" cannot be run with an argument when "curbuf_lock" is set
2203 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2204 goto doend;
2205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 /*
2207 * Check for "++opt=val" argument.
2208 * Must be first, allow ":w ++enc=utf8 !cmd"
2209 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002210 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2212 if (getargopt(&ea) == FAIL && !ni)
2213 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002214 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 goto doend;
2216 }
2217
2218 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002220 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002222 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002224 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 goto doend;
2226 }
2227 ea.arg = skipwhite(ea.arg + 1);
2228 ea.append = TRUE;
2229 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002230 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
2232 ++ea.arg;
2233 ea.usefilter = TRUE;
2234 }
2235 }
2236
2237 if (ea.cmdidx == CMD_read)
2238 {
2239 if (ea.forceit)
2240 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002241 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 ea.forceit = FALSE;
2243 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002244 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
2246 ++ea.arg;
2247 ea.usefilter = TRUE;
2248 }
2249 }
2250
2251 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2252 {
2253 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002254 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 {
2256 ++ea.arg;
2257 ++ea.amount;
2258 }
2259 ea.arg = skipwhite(ea.arg);
2260 }
2261
2262 /*
2263 * Check for "+command" argument, before checking for next command.
2264 * Don't do this for ":read !cmd" and ":write !cmd".
2265 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002266 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2268
2269 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002270 * For commands that do not use '|' inside their argument: Check for '|' to
2271 * separate commands and '"' or '#' to start comments.
2272 *
2273 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002274 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002275 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002276 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002278 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2279 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002280 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002281 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002282 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002283 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002284 || ea.cmdidx == CMD_global
2285 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002286 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002287#ifdef FEAT_EVAL
2288 || inside_block(&ea)
2289#endif
2290 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 for (p = ea.arg; *p; ++p)
2293 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002294 // Remove one backslash before a newline, so that it's possible to
2295 // pass a newline to the shell and also a newline that is preceded
2296 // with a backslash. This makes it impossible to end a shell
2297 // command in a backslash, but that doesn't appear useful.
2298 // Halving the number of backslashes is incompatible with previous
2299 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002301 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002302 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 ea.nextcmd = p + 1;
2305 *p = NUL;
2306 break;
2307 }
2308 }
2309 }
2310
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002311 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002312 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002314 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002315 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002317 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002318 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002319 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002321#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002322 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002323 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002325 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002326 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 }
2328#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002329 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2330 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002331 {
2332 ea.regname = *ea.arg++;
2333#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002334 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002335 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2336 {
kuuote08d7b1c2021-10-04 22:17:36 +01002337 if (!ea.skip)
2338 {
2339 set_expr_line(vim_strsave(ea.arg), &ea);
2340 did_set_expr_line = TRUE;
2341 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002342 ea.arg += STRLEN(ea.arg);
2343 }
2344#endif
2345 ea.arg = skipwhite(ea.arg);
2346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
2348
2349 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002350 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 * count, it's a buffer name.
2352 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002353 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002354 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002355 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002357 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002359 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002361 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 goto doend;
2363 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002364 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
2366 ea.line2 = n;
2367 if (ea.addr_count == 0)
2368 ea.addr_count = 1;
2369 }
2370 else
2371 {
2372 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002373 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002374 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002375 else
2376 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 ++ea.addr_count;
2378 /*
2379 * Be vi compatible: no error message for out of range.
2380 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002381 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 ea.line2 = curbuf->b_ml.ml_line_count;
2383 }
2384 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002385
2386 /*
2387 * Check for flags: 'l', 'p' and '#'.
2388 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002389 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002390 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002391 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2392 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002394 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002395 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 goto doend;
2397 }
2398
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002399 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002401 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 goto doend;
2403 }
2404
2405#ifdef FEAT_EVAL
2406 /*
2407 * Skip the command when it's not going to be executed.
2408 * The commands like :if, :endif, etc. always need to be executed.
2409 * Also make an exception for commands that handle a trailing command
2410 * themselves.
2411 */
2412 if (ea.skip)
2413 {
2414 switch (ea.cmdidx)
2415 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002416 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 case CMD_while:
2418 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002419 case CMD_for:
2420 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 case CMD_if:
2422 case CMD_elseif:
2423 case CMD_else:
2424 case CMD_endif:
2425 case CMD_try:
2426 case CMD_catch:
2427 case CMD_finally:
2428 case CMD_endtry:
2429 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002430 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 break;
2432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002433 // Commands that handle '|' themselves. Check: A command should
2434 // either have the EX_TRLBAR flag, appear in this list or appear in
2435 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 case CMD_aboveleft:
2437 case CMD_and:
2438 case CMD_belowright:
2439 case CMD_botright:
2440 case CMD_browse:
2441 case CMD_call:
2442 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002443 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 case CMD_delfunction:
2445 case CMD_djump:
2446 case CMD_dlist:
2447 case CMD_dsearch:
2448 case CMD_dsplit:
2449 case CMD_echo:
2450 case CMD_echoerr:
2451 case CMD_echomsg:
2452 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002453 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002455 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002456 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 case CMD_help:
2458 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002459 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 case CMD_ijump:
2461 case CMD_ilist:
2462 case CMD_isearch:
2463 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002464 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_keepjumps:
2466 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002467 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 case CMD_leftabove:
2469 case CMD_let:
2470 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002471 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002472 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002474 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002475 case CMD_noautocmd:
2476 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 case CMD_perl:
2478 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002479 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002480 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002481 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 case CMD_return:
2483 case CMD_rightbelow:
2484 case CMD_ruby:
2485 case CMD_silent:
2486 case CMD_smagic:
2487 case CMD_snomagic:
2488 case CMD_substitute:
2489 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002490 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 case CMD_tcl:
2492 case CMD_throw:
2493 case CMD_tilde:
2494 case CMD_topleft:
2495 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002496 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002497 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 case CMD_verbose:
2499 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002500 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 break;
2502
2503 default: goto doend;
2504 }
2505 }
2506#endif
2507
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002508 if ((ea.argt & EX_XFILE)
2509 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2510 goto doend;
2511
2512#ifdef FEAT_EVAL
2513 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002515 emsg(_(e_invalid_command_after_export));
2516 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 /*
2521 * Accept buffer name. Cannot be used at the same time with a buffer
2522 * number. Don't do this for a user command.
2523 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002524 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002525 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 /*
2528 * :bdelete, :bwipeout and :bunload take several arguments, separated
2529 * by spaces: find next space (skipping over escaped characters).
2530 * The others take one argument: ignore trailing spaces.
2531 */
2532 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2533 || ea.cmdidx == CMD_bunload)
2534 p = skiptowhite_esc(ea.arg);
2535 else
2536 {
2537 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002538 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 --p;
2540 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002541 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002542 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002543 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 goto doend;
2545 ea.addr_count = 1;
2546 ea.arg = skipwhite(p);
2547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002549 // The :try command saves the emsg_silent flag, reset it here when
2550 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002551 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002552 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002553 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002554 if (emsg_silent < 0)
2555 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002556 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002557 }
2558
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002560 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
Bram Moolenaar958636c2014-10-21 20:01:58 +02002563 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
2565 /*
2566 * Execute a user-defined command.
2567 */
2568 do_ucmd(&ea);
2569 }
2570 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 {
2572 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002573 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2576 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002577 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579
2580#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002581 // A command will reset "is_export" when exporting an item. If it is still
2582 // set something went wrong.
2583 if (is_export)
2584 {
2585 if (errormsg == NULL)
2586 errormsg = _(e_export_with_invalid_argument);
2587 is_export = FALSE;
2588 }
2589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002591 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002592 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002593 && current_sctx.sc_sid > 0
2594 && ea.cmdidx != CMD_endif
2595 && (cstack->cs_idx < 0
2596 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002597 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 /*
2600 * If the command just executed called do_cmdline(), any throw or ":return"
2601 * or ":finish" encountered there must also check the cstack of the still
2602 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2603 * exception, or reanimate a returned function or finished script file and
2604 * return or finish it again.
2605 */
2606 if (need_rethrow)
2607 do_throw(cstack);
2608 else if (check_cstack)
2609 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002610 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002612 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 && current_func_returned())
2614 do_return(&ea, TRUE, FALSE, NULL);
2615 }
2616 need_rethrow = check_cstack = FALSE;
2617#endif
2618
2619doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002620 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002623 curwin->w_cursor.col = 0;
2624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625
2626 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2627 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002628 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
2632 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002633 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002635 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637 emsg(errormsg);
2638 }
2639#ifdef FEAT_EVAL
2640 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002641 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2642 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002643
2644 if (did_set_expr_line)
2645 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002646 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647#endif
2648
Bram Moolenaare1004402020-10-24 20:49:43 +02002649 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002651 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002652 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002654 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 ea.nextcmd = NULL;
2656
2657#ifdef FEAT_EVAL
2658 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002659 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660#endif
2661
2662 return ea.nextcmd;
2663}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002665static char ex_error_buf[MSG_BUF_LEN];
2666
2667/*
2668 * Return an error message with argument included.
2669 * Uses a static buffer, only the last error will be kept.
2670 * "msg" will be translated, caller should use N_().
2671 */
2672 char *
2673ex_errmsg(char *msg, char_u *arg)
2674{
2675 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2676 return ex_error_buf;
2677}
2678
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002680 * Handle a range without a command.
2681 * Returns an error message on failure.
2682 */
2683 char *
2684ex_range_without_command(exarg_T *eap)
2685{
2686 char *errormsg = NULL;
2687
2688 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2689#ifdef FEAT_EVAL
2690 && !in_vim9script()
2691#endif
2692 )
2693 {
2694 eap->cmdidx = CMD_print;
2695 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2696 if ((errormsg = invalid_range(eap)) == NULL)
2697 {
2698 correct_range(eap);
2699 ex_print(eap);
2700 }
2701 }
2702 else if (eap->addr_count != 0)
2703 {
2704 if (eap->line2 > curbuf->b_ml.ml_line_count)
2705 {
2706 // With '-' in 'cpoptions' a line number past the file is an
2707 // error, otherwise put it at the end of the file.
2708 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2709 eap->line2 = -1;
2710 else
2711 eap->line2 = curbuf->b_ml.ml_line_count;
2712 }
2713
2714 if (eap->line2 < 0)
2715 errormsg = _(e_invalid_range);
2716 else
2717 {
2718 if (eap->line2 == 0)
2719 curwin->w_cursor.lnum = 1;
2720 else
2721 curwin->w_cursor.lnum = eap->line2;
2722 beginline(BL_SOL | BL_FIX);
2723 }
2724 }
2725 return errormsg;
2726}
2727
2728/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002729 * Check for an Ex command with optional tail.
2730 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002731 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002732 */
2733 static int
2734checkforcmd_opt(
2735 char_u **pp, // start of command
2736 char *cmd, // name of command
2737 int len, // required length
2738 int noparen)
2739{
2740 int i;
2741
2742 for (i = 0; cmd[i] != NUL; ++i)
2743 if (((char_u *)cmd)[i] != (*pp)[i])
2744 break;
Bram Moolenaar68854a82022-01-31 18:59:13 +00002745 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_'
2746 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002747 {
2748 *pp = skipwhite(*pp + i);
2749 return TRUE;
2750 }
2751 return FALSE;
2752}
2753
2754/*
2755 * Check for an Ex command with optional tail.
2756 * If there is a match advance "pp" to the argument and return TRUE.
2757 */
2758 int
2759checkforcmd(
2760 char_u **pp, // start of command
2761 char *cmd, // name of command
2762 int len) // required length
2763{
2764 return checkforcmd_opt(pp, cmd, len, FALSE);
2765}
2766
2767/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002768 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002769 * If there is a match advance "pp" to the argument and return TRUE.
2770 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002771 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002772checkforcmd_noparen(
2773 char_u **pp, // start of command
2774 char *cmd, // name of command
2775 int len) // required length
2776{
2777 return checkforcmd_opt(pp, cmd, len, TRUE);
2778}
2779
2780/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002781 * Parse and skip over command modifiers:
2782 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002783 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002784 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002785 * When "skip_only" is TRUE the global variables are not changed, except for
2786 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002787 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2788 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002789 * Call apply_cmdmod() to get the side effects of the modifiers:
2790 * - Increment "sandbox" for ":sandbox"
2791 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002792 * - set msg_silent for ":silent"
2793 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002794 * Return FAIL when the command is not to be executed.
2795 * May set "errormsg" to an error message.
2796 */
2797 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002798parse_command_modifiers(
2799 exarg_T *eap,
2800 char **errormsg,
2801 cmdmod_T *cmod,
2802 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002803{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002804 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002805 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002806 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002807 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002808 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002809 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002810
Bram Moolenaare1004402020-10-24 20:49:43 +02002811 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002812 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002813
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002814 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2815 {
2816 // The automatically inserted Visual area range is skipped, so that
2817 // typing ":cmdmod cmd" in Visual mode works without having to move the
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002818 // range to after the modififiers. The command will be
2819 // "'<,'>cmdmod cmd", parse "cmdmod cmd" and then put back "'<,'>"
2820 // before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002821 eap->cmd += 5;
2822 cmd_start = eap->cmd;
2823 has_visual_range = TRUE;
2824 }
2825
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002826 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002827 for (;;)
2828 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002829 char_u *p;
2830
Bram Moolenaareffed932018-08-14 13:38:17 +02002831 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002832 {
2833 if (*eap->cmd == ':')
2834 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002836 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002837
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002838 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 if (*eap->cmd == NUL && exmode_active
2840 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2841 || getline_equal(eap->getline, eap->cookie, getexline))
2842 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2843 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002844 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002845 if (!skip_only)
2846 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002847 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002848 }
2849
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002850 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002851 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002852 {
2853 // a comment ends at a NL
2854 if (eap->nextcmd == NULL)
2855 {
2856 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2857 if (eap->nextcmd != NULL)
2858 ++eap->nextcmd;
2859 }
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002860 if (vim9script)
2861 {
2862 if (has_cmdmod(cmod, FALSE))
2863 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002864#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002865 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2866 && eap->cmd[2] != '{')
2867 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002868#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002869 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002871 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002872 if (*eap->cmd == NUL)
2873 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002874 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002875 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002876 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002877 if (vim9script && has_cmdmod(cmod, FALSE))
2878 *errormsg = _(e_command_modifier_without_command);
2879 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 return FAIL;
2881 }
2882
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002883 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002884
2885 // In Vim9 script a variable can shadow a command modifier:
2886 // verbose = 123
2887 // verbose += 123
2888 // silent! verbose = func()
2889 // verbose.member = 2
2890 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002891 // But not:
2892 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002893 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002894 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002895 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002896
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002897 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002898 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002899 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002900 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2901 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002902 break;
2903 }
2904
Bram Moolenaareffed932018-08-14 13:38:17 +02002905 switch (*p)
2906 {
Bram Moolenaar91324262022-09-09 13:27:59 +01002907 // When adding an entry, also modify cmdmods[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002908 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002910 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002911 continue;
2912
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002913 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002915 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002916 continue;
2917 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002918 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002919 {
2920#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002921 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002922#endif
2923 continue;
2924 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002925 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002926 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002927 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002928 continue;
2929
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002930 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002931 break;
2932#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002933 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002934#endif
2935 continue;
2936
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002937 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002938 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002939 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002940 continue;
2941 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002942 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002943 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002944 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002945 continue;
2946 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002947 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002948 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002949 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002950 continue;
2951 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002952 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002953 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002954 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002955 continue;
2956
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002957 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002959 char_u *reg_pat;
2960 char_u *nulp = NULL;
2961 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002962
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002963 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002964 || *p == NUL
2965 || (ends_excmd(*p)
2966#ifdef FEAT_EVAL
2967 // in ":filter #pat# cmd" # does not
2968 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002969 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002970#endif
2971 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002972 break;
2973 if (*p == '!')
2974 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002975 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002976 p = skipwhite(p + 1);
2977 if (*p == NUL || ends_excmd(*p))
2978 break;
2979 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002980#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002981 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002982 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002983 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002984#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002985 if (skip_only)
2986 p = skip_vimgrep_pat(p, NULL, NULL);
2987 else
2988 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002989 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2990 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002991 if (p == NULL || *p == NUL)
2992 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002993 if (!skip_only)
2994 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002995 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002996 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002997 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002998 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002999 // restore the character overwritten by NUL
3000 if (nulp != NULL)
3001 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003002 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003003 eap->cmd = p;
3004 continue;
3005 }
3006
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003007 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3008 {
3009 cmod->cmod_split |= WSP_HOR;
3010 continue;
3011 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003012 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003013 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003014 || *p == NUL || ends_excmd(*p))
3015 break;
3016 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003017 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003018 continue;
3019
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003020 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003022 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003023 continue;
3024 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003025 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3026 {
3027 if (ends_excmd2(p, eap->cmd))
3028 {
3029 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003030 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003031 return FAIL;
3032 }
3033 cmod->cmod_flags |= CMOD_LEGACY;
3034 continue;
3035 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003036
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003037 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003038 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003039 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003040 continue;
3041
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003042 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003043 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003044 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003045 continue;
3046 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003047 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003048 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003049 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003050 continue;
3051
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003052 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003053 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003054 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003055 continue;
3056
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003057 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003058 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003059 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003060 continue;
3061 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003062 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003063 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003064 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003065 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3066 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003067 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003068 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003069 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003070 }
3071 continue;
3072
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003073 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003074 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003075 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003076 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003077 long tabnr = get_address(eap, &eap->cmd,
3078 ADDR_TABS, eap->skip,
3079 skip_only, FALSE, 1);
3080 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003081 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003082 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003083 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003084 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3085 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003086 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003087 return FAIL;
3088 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003089 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003090 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003091 }
3092 eap->cmd = p;
3093 continue;
3094 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003095 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003096 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003097 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003098 continue;
3099
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003100 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003101 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003102 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003103 continue;
3104
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003105 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003106 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003107 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003108 continue;
3109 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003110 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003111 {
3112 if (ends_excmd2(p, eap->cmd))
3113 {
3114 *errormsg =
3115 _(e_vim9cmd_must_be_followed_by_command);
3116 return FAIL;
3117 }
3118 cmod->cmod_flags |= CMOD_VIM9CMD;
3119 continue;
3120 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003121 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003122 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003123 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003124 {
zeertzjqcd749632022-06-14 13:30:35 +01003125 // zero means not set, one is verbose == 0, etc.
3126 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003127 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003128 else
zeertzjqcd749632022-06-14 13:30:35 +01003129 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003130 eap->cmd = p;
3131 continue;
3132 }
3133 break;
3134 }
3135
Bram Moolenaar1501b632022-03-27 16:56:21 +01003136 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003137 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003138 if (eap->cmd > cmd_start)
3139 {
3140 // Move the '<,'> range to after the modifiers and insert a colon.
3141 // Since the modifiers have been parsed put the colon on top of the
3142 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3143 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003144 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003145 {
3146 size_t len = STRLEN(cmd_start);
3147
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003148 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003149 // "'<,'>mods" -> "mods *+
3150 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003151 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003152 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003153 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003154 }
3155 else
3156 {
3157 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3158 eap->cmd -= 5;
3159 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3160 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003161 }
3162 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003163 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003164 // Special case: change empty command to "+".
3165 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003166 eap->cmd = (char_u *)"'<,'>+";
3167 else
3168 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003169 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003170 else if (use_plus_cmd)
3171 eap->cmd = (char_u *)"+";
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003172
Bram Moolenaareffed932018-08-14 13:38:17 +02003173 return OK;
3174}
3175
3176/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003177 * Return TRUE if "cmod" has anything set.
3178 */
3179 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003180has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003181{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003182 return (cmod->cmod_flags != 0 && (!ignore_silent
3183 || (cmod->cmod_flags
3184 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003185 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003186 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003187 || cmod->cmod_tab != 0
3188 || cmod->cmod_filter_regmatch.regprog != NULL;
3189}
3190
Bram Moolenaar8991be22022-02-14 21:51:46 +00003191#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003192/*
3193 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3194 */
3195 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003196cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003197{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003198 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003199 {
3200 emsg(_(e_misplaced_command_modifier));
3201 return TRUE;
3202 }
3203 return FALSE;
3204}
Dominique Pelle748b3082022-01-08 12:41:16 +00003205#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003206
3207/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003208 * Apply the command modifiers. Saves current state in "cmdmod", call
3209 * undo_cmdmod() later.
3210 */
3211 void
3212apply_cmdmod(cmdmod_T *cmod)
3213{
3214#ifdef HAVE_SANDBOX
3215 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3216 {
3217 ++sandbox;
3218 cmod->cmod_did_sandbox = TRUE;
3219 }
3220#endif
zeertzjqcd749632022-06-14 13:30:35 +01003221 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003222 {
3223 if (cmod->cmod_verbose_save == 0)
3224 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003225 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003226 }
3227
3228 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3229 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003230 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003231 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003232 cmod->cmod_save_msg_scroll = msg_scroll;
3233 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003234 if (cmod->cmod_flags & CMOD_SILENT)
3235 ++msg_silent;
3236 if (cmod->cmod_flags & CMOD_UNSILENT)
3237 msg_silent = 0;
3238
3239 if (cmod->cmod_flags & CMOD_ERRSILENT)
3240 {
3241 ++emsg_silent;
3242 ++cmod->cmod_did_esilent;
3243 }
3244
Bram Moolenaare1004402020-10-24 20:49:43 +02003245 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003246 {
3247 // Set 'eventignore' to "all".
3248 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003249 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003250 set_string_option_direct((char_u *)"ei", -1,
3251 (char_u *)"all", OPT_FREE, SID_NONE);
3252 }
3253}
3254
3255/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003256 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003257 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003258 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003259undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003260{
Bram Moolenaare1004402020-10-24 20:49:43 +02003261 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003262 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003263 p_verbose = cmod->cmod_verbose_save - 1;
3264 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003265 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003266
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003267#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003268 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003269 {
3270 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003271 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003272 }
3273#endif
3274
Bram Moolenaare1004402020-10-24 20:49:43 +02003275 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003276 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003277 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003278 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3279 OPT_FREE, SID_NONE);
3280 free_string_option(cmod->cmod_save_ei);
3281 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003282 }
3283
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003284 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003285
Bram Moolenaare1004402020-10-24 20:49:43 +02003286 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003287 {
3288 // messages could be enabled for a serious error, need to check if the
3289 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003290 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3291 msg_silent = cmod->cmod_save_msg_silent - 1;
3292 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003293 if (emsg_silent < 0)
3294 emsg_silent = 0;
3295 // Restore msg_scroll, it's set by file I/O commands, even when no
3296 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003297 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003298
3299 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3300 // somewhere in the line. Put it back in the first column.
3301 if (redirecting())
3302 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003303
Bram Moolenaare1004402020-10-24 20:49:43 +02003304 cmod->cmod_save_msg_silent = 0;
3305 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003306 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003307}
3308
3309/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003310 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003311 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003312 * Return FAIL and set "errormsg" or return OK.
3313 */
3314 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003315parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003316{
3317 int address_count = 1;
3318 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003319 int need_check_cursor = FALSE;
3320 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003321
3322 // Repeat for all ',' or ';' separated addresses.
3323 for (;;)
3324 {
3325 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003326 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003327 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003328 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003329 eap->addr_count == 0, address_count++);
3330 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003331 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003332 if (lnum == MAXLNUM)
3333 {
3334 if (*eap->cmd == '%') // '%' - all lines
3335 {
3336 ++eap->cmd;
3337 switch (eap->addr_type)
3338 {
3339 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003340 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003341 eap->line1 = 1;
3342 eap->line2 = curbuf->b_ml.ml_line_count;
3343 break;
3344 case ADDR_LOADED_BUFFERS:
3345 {
3346 buf_T *buf = firstbuf;
3347
3348 while (buf->b_next != NULL
3349 && buf->b_ml.ml_mfp == NULL)
3350 buf = buf->b_next;
3351 eap->line1 = buf->b_fnum;
3352 buf = lastbuf;
3353 while (buf->b_prev != NULL
3354 && buf->b_ml.ml_mfp == NULL)
3355 buf = buf->b_prev;
3356 eap->line2 = buf->b_fnum;
3357 break;
3358 }
3359 case ADDR_BUFFERS:
3360 eap->line1 = firstbuf->b_fnum;
3361 eap->line2 = lastbuf->b_fnum;
3362 break;
3363 case ADDR_WINDOWS:
3364 case ADDR_TABS:
3365 if (IS_USER_CMDIDX(eap->cmdidx))
3366 {
3367 eap->line1 = 1;
3368 eap->line2 = eap->addr_type == ADDR_WINDOWS
3369 ? LAST_WIN_NR : LAST_TAB_NR;
3370 }
3371 else
3372 {
3373 // there is no Vim command which uses '%' and
3374 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003375 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003376 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003377 }
3378 break;
3379 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003380 case ADDR_UNSIGNED:
3381 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003382 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003383 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003384 case ADDR_ARGUMENTS:
3385 if (ARGCOUNT == 0)
3386 eap->line1 = eap->line2 = 0;
3387 else
3388 {
3389 eap->line1 = 1;
3390 eap->line2 = ARGCOUNT;
3391 }
3392 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003393 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003394#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003395 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003396 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003397 if (eap->line2 == 0)
3398 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003399#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003400 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003401 case ADDR_NONE:
3402 // Will give an error later if a range is found.
3403 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003404 }
3405 ++eap->addr_count;
3406 }
3407 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3408 {
3409 pos_T *fp;
3410
3411 // '*' - visual area
3412 if (eap->addr_type != ADDR_LINES)
3413 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003414 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003415 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003416 }
3417
3418 ++eap->cmd;
3419 if (!eap->skip)
3420 {
3421 fp = getmark('<', FALSE);
3422 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003423 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003424 eap->line1 = fp->lnum;
3425 fp = getmark('>', FALSE);
3426 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003427 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003428 eap->line2 = fp->lnum;
3429 ++eap->addr_count;
3430 }
3431 }
3432 }
3433 else
3434 eap->line2 = lnum;
3435 eap->addr_count++;
3436
3437 if (*eap->cmd == ';')
3438 {
3439 if (!eap->skip)
3440 {
3441 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003442
Bram Moolenaar0e717042020-04-27 19:29:01 +02003443 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003444 // accept zero as address, so 0;/PATTERN/ works correctly
3445 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003446 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003447 if (eap->line2 > 0)
3448 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003449 else
3450 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003451 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003452 }
3453 }
3454 else if (*eap->cmd != ',')
3455 break;
3456 ++eap->cmd;
3457 }
3458
3459 // One address given: set start and end lines.
3460 if (eap->addr_count == 1)
3461 {
3462 eap->line1 = eap->line2;
3463 // ... but only implicit: really no address given
3464 if (lnum == MAXLNUM)
3465 eap->addr_count = 0;
3466 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003467 ret = OK;
3468
3469theend:
3470 if (need_check_cursor)
3471 check_cursor();
3472 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003473}
3474
3475/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003476 * Append "cmd" to the error message in IObuff.
3477 * Takes care of limiting the length and handling 0xa0, which would be
3478 * invisible otherwise.
3479 */
3480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003481append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003482{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003483 size_t len = STRLEN(IObuff);
3484 char_u *s = cmd;
3485 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003486
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003487 if (len > IOSIZE - 100)
3488 {
3489 // Not enough space, truncate and put in "...".
3490 d = IObuff + IOSIZE - 100;
3491 d -= mb_head_off(IObuff, d);
3492 STRCPY(d, "...");
3493 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003494 STRCAT(IObuff, ": ");
3495 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003496 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003497 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003498 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003499 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003500 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003501 STRCPY(d, "<a0>");
3502 d += 4;
3503 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003504 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3505 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003506 else
3507 MB_COPY_CHAR(s, d);
3508 }
3509 *d = NUL;
3510}
3511
Dominique Pelle748b3082022-01-08 12:41:16 +00003512#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003513/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003514 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3515 * the name. Otherwise just return "start".
3516 */
3517 char_u *
3518skip_option_env_lead(char_u *start)
3519{
3520 char_u *name = start;
3521
3522 if (*start == '&')
3523 {
3524 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3525 name += 3;
3526 else
3527 name += 1;
3528 }
3529 else if (*start == '$')
3530 name += 1;
3531 return name;
3532}
Dominique Pelle748b3082022-01-08 12:41:16 +00003533#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003534
3535/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003536 * Return TRUE and set "*idx" if "p" points to a one letter command.
3537 * If not in Vim9 script:
3538 * - The 'k' command can directly be followed by any character.
3539 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3540 * but :sre[wind] is another command, as are :scr[iptnames],
3541 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3542 */
3543 static int
3544one_letter_cmd(char_u *p, cmdidx_T *idx)
3545{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003546 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003547 return FALSE;
3548 if (*p == 'k')
3549 {
3550 *idx = CMD_k;
3551 return TRUE;
3552 }
3553 if (p[0] == 's'
3554 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3555 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3556 || p[1] == 'g'
3557 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3558 || p[1] == 'I'
3559 || (p[1] == 'r' && p[2] != 'e')))
3560 {
3561 *idx = CMD_substitute;
3562 return TRUE;
3563 }
3564 return FALSE;
3565}
3566
3567/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003568 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3569 * call.
3570 */
3571 int
3572number_method(char_u *cmd)
3573{
3574 char_u *p = skipdigits(cmd);
3575
3576 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3577}
3578
3579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003581 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003582 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003583 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 *
3585 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3586 * assignment without "let". Sets eap->cmdidx to the command while returning
3587 * "eap->cmd".
3588 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 * Returns NULL for an ambiguous user command.
3590 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 char_u *
3592find_ex_command(
3593 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003594 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003595 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003596 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597{
3598 int len;
3599 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003600 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003601#ifndef FEAT_EVAL
3602 int vim9 = FALSE;
3603#else
3604 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 /*
3607 * Recognize a Vim9 script function/method call and assignment:
3608 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3609 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003610 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003611 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003613 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003615 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003616 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3617 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003619 int oplen;
3620 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003621 char_u *swp;
3622
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003623 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003624 || (eap->cmd[0] == '$'
3625 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003626 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003627 && (valid_yank_reg(eap->cmd[1], FALSE)
3628 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003629 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003630 if (*eap->cmd == '&')
3631 {
3632 p = eap->cmd + 1;
3633 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3634 p += 2;
3635 p = to_name_end(p, FALSE);
3636 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003637 else if (*eap->cmd == '$')
3638 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003639 else
3640 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003641 if (ends_excmd(*skipwhite(p)))
3642 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003643 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3644 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003645 eap->cmdidx = CMD_eval;
3646 return eap->cmd;
3647 }
3648 // "&option" can be followed by "->" or "=", check below
3649 }
3650
3651 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003653 if (
3654 // "(..." is an expression.
3655 // "funcname(" is always a function call.
3656 *p == '('
3657 || (p == eap->cmd
3658 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003659 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003660 *eap->cmd == '{'
3661 // "'string'->func()" is an expression.
3662 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003663 // '"string"->func()' is an expression.
3664 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003665 // '$"string"->func()' is an expression.
3666 // "$'string'->func()" is an expression.
3667 || (eap->cmd[0] == '$'
3668 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3669 // '0z1234->func()' is an expression.
3670 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003671 // "g:varname" is an expression.
3672 || eap->cmd[1] == ':'
3673 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003674 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003675 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003676 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003677 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3678 {
3679 // "{" by itself is the start of a block.
3680 eap->cmdidx = CMD_block;
3681 return eap->cmd + 1;
3682 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003683 eap->cmdidx = CMD_eval;
3684 return eap->cmd;
3685 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003686
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003687 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003688 // "varname[]" is an expression.
3689 *p == '['
3690 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003691 || (*p == '.'
3692 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3693 // g:[key] is an expression
3694 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003695 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003696 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003697
3698 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003699 // Skip over the whole thing, it can be:
3700 // name.member = val
3701 // name[a : b] = val
3702 // name[idx] = val
3703 // name[idx].member = val
3704 // etc.
3705 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003706 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003707 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003708 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003709 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003710 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003711 || (after[0] == '.' && after[1] == '.'
3712 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003713 eap->cmdidx = CMD_var;
3714 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003715 --emsg_silent;
3716 return eap->cmd;
3717 }
3718
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003719 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3720 // an assignment.
3721 // If there is no line break inside the "[...]" then "p" is
3722 // advanced to after the "]" by to_name_const_end(): check if a "="
3723 // follows.
3724 // If "[...]" has a line break "p" still points at the "[" and it
3725 // can't be an assignment.
3726 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003727 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003728 char_u *eq;
3729
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003730 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003731 if (p == eap->cmd && *p == '[')
3732 {
3733 int count = 0;
3734 int semicolon = FALSE;
3735
3736 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3737 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003738 eq = p;
3739 if (eq != NULL)
3740 {
3741 eq = skipwhite(eq);
3742 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3743 ++eq;
3744 }
3745 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003746 {
3747 eap->cmdidx = CMD_eval;
3748 return eap->cmd;
3749 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003750 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003751 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003752 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003753 return eap->cmd;
3754 }
3755 }
3756
3757 // Recognize an assignment if we recognize the variable name:
3758 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003759 // "@r = expr"
3760 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003761 // "var = expr" where "var" is a variable name or we are skipping
3762 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003763 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003764 oplen = assignment_len(skipwhite(p), &heredoc);
3765 if (oplen > 0)
3766 {
3767 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3768 || *eap->cmd == '&'
3769 || *eap->cmd == '$'
3770 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003771 || (eap->skip && IS_WHITE_OR_NUL(
3772 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003773 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003774 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003775 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003776 return eap->cmd;
3777 }
3778 }
3779
Bram Moolenaar9510d222022-09-11 15:14:05 +01003780 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003781 // "w:varname: number = 123".
3782 if (eap->cmd[1] == ':' && *p == ':')
3783 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003784 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003785 return eap->cmd;
3786 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003787 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003788
Bram Moolenaar31d99482022-05-26 22:24:43 +01003789 // 1234->func() is a method call
3790 if (number_method(eap->cmd))
3791 {
3792 eap->cmdidx = CMD_eval;
3793 return eap->cmd;
3794 }
3795
Bram Moolenaar7b829262021-10-13 15:04:34 +01003796 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3797 // an expression. A global/substitute/list command needs to use a
3798 // longer name.
3799 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3800 {
3801 eap->cmdidx = CMD_eval;
3802 return eap->cmd;
3803 }
3804
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003805 // If it is an ID it might be a variable with an operator on the next
3806 // line, if the variable exists it can't be an Ex command.
3807 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003808 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003809 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3810 {
3811 eap->cmdidx = CMD_eval;
3812 return eap->cmd;
3813 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003814
3815 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003816 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3817 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003818 {
3819 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3820 return eap->cmd + 2;
3821 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 }
3823#endif
3824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 /*
3826 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 */
3828 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003829 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 ++p;
3832 }
3833 else
3834 {
3835 while (ASCII_ISALPHA(*p))
3836 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003837 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003838 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003840 while (ASCII_ISALNUM(*p))
3841 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 }
3843 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3844 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003845 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 ++p;
3847 while (ASCII_ISALPHA(*p))
3848 ++p;
3849 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003850
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003851 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003852 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 ++p;
3854 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003855 // The "d" command can directly be followed by 'l' or 'p' flag, when
3856 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003857 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003858 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003859 // Check for ":dl", ":dell", etc. to ":deletel": that's
3860 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003861 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003862 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003863 break;
3864 if (i == len - 1)
3865 {
3866 --len;
3867 if (p[-1] == 'l')
3868 eap->flags |= EXFLAG_LIST;
3869 else
3870 eap->flags |= EXFLAG_PRINT;
3871 }
3872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003874 if (ASCII_ISLOWER(eap->cmd[0]))
3875 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003876 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003877 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003878
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003879 if (command_count != (int)CMD_SIZE)
3880 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003881 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003882 getout(1);
3883 }
3884
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003885 // Use a precomputed index for fast look-up in cmdnames[]
3886 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003887 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3888 if (ASCII_ISLOWER(c2))
3889 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3890 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003891 else if (ASCII_ISUPPER(eap->cmd[0]))
3892 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003894 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895
3896 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3897 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3898 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3899 (size_t)len) == 0)
3900 {
3901#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003902 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 *full = TRUE;
3904#endif
3905 break;
3906 }
3907
Bram Moolenaar204852a2022-03-05 12:56:44 +00003908 // :Print and :mode are not supported in Vim9 script.
3909 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003910 if (vim9 && eap->cmdidx != CMD_SIZE)
3911 {
3912 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3913 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003914 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003915 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003916 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003917 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003918 eap->cmdidx = CMD_SIZE;
3919 }
3920 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003921
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003922 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003923 // 'cpoptions'.
3924 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3925 p = eap->cmd;
3926
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003927 // Look for a user defined command as a last resort. Let ":Print" be
3928 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003929 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3930 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003932 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 while (ASCII_ISALNUM(*p))
3934 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003935 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003937 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 eap->cmdidx = CMD_SIZE;
3939 }
3940
Bram Moolenaar204852a2022-03-05 12:56:44 +00003941 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3942 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003943 eap->cmdidx = CMD_finally;
3944
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003945#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003946 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003947 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003948 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003949 && (eap->cmdidx < 0 ||
3950 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003951 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003952 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3953
3954 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003955 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003956 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003957 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003958#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003959
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 return p;
3961}
3962
3963#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003964static struct cmdmod
3965{
3966 char *name;
3967 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003968 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003969} cmdmods[] = {
3970 {"aboveleft", 3, FALSE},
3971 {"belowright", 3, FALSE},
3972 {"botright", 2, FALSE},
3973 {"browse", 3, FALSE},
3974 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003975 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003976 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003977 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003978 {"keepalt", 5, FALSE},
3979 {"keepjumps", 5, FALSE},
3980 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003981 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003982 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003983 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003984 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003985 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003986 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003987 {"rightbelow", 6, FALSE},
3988 {"sandbox", 3, FALSE},
3989 {"silent", 3, FALSE},
3990 {"tab", 3, TRUE},
3991 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003992 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003993 {"verbose", 4, TRUE},
3994 {"vertical", 4, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003995 {"vim9cmd", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003996};
3997
3998/*
3999 * Return length of a command modifier (including optional count).
4000 * Return zero when it's not a modifier.
4001 */
4002 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004003modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004004{
4005 int i, j;
4006 char_u *p = cmd;
4007
4008 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004009 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02004010 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004011 {
4012 for (j = 0; p[j] != NUL; ++j)
4013 if (p[j] != cmdmods[i].name[j])
4014 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02004015 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00004016 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00004017 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004018 }
4019 return 0;
4020}
4021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022/*
4023 * Return > 0 if an Ex command "name" exists.
4024 * Return 2 if there is an exact match.
4025 * Return 3 if there is an ambiguous match.
4026 */
4027 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004028cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029{
4030 exarg_T ea;
4031 int full = FALSE;
4032 int i;
4033 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004034 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004036 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02004037 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
4039 for (j = 0; name[j] != NUL; ++j)
4040 if (name[j] != cmdmods[i].name[j])
4041 break;
4042 if (name[j] == NUL && j >= cmdmods[i].minlen)
4043 return (cmdmods[i].name[j] == NUL ? 2 : 1);
4044 }
4045
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004046 // Check built-in commands and user defined commands.
4047 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004048 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004050 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004052 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004054 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4055 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004056 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004057 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4059}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004060
4061/*
4062 * "fullcommand" function
4063 */
4064 void
4065f_fullcommand(typval_T *argvars, typval_T *rettv)
4066{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004067 exarg_T ea;
4068 char_u *name;
4069 char_u *p;
4070 int vim9script = in_vim9script();
4071 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004072
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004073 rettv->v_type = VAR_STRING;
4074 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004075
Bram Moolenaaraa534142022-09-15 21:46:02 +01004076 if (in_vim9script()
4077 && (check_for_string_arg(argvars, 0) == FAIL
4078 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004079 return;
4080
4081 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004082 if (name == NULL)
4083 return;
4084
Bram Moolenaaraa534142022-09-15 21:46:02 +01004085 if (argvars[1].v_type != VAR_UNKNOWN)
4086 {
4087 vim9script = tv_get_bool(&argvars[1]);
4088 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4089 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4090 }
4091
zeertzjqc024ed92022-01-04 16:22:52 +00004092 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004093 name++;
4094 name = skip_range(name, TRUE, NULL);
4095
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004096 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4097 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004098 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004099 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004100 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004101 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004102 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004103 goto theend;
4104
4105 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004106 {
4107 int res;
4108
4109 ++emsg_silent;
4110 res = not_in_vim9(&ea);
4111 --emsg_silent;
4112
4113 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004114 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004115 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004116
4117 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004118 ? get_user_command_name(ea.useridx, ea.cmdidx)
4119 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004120theend:
4121 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004122}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123#endif
4124
Bram Moolenaard0190392019-08-23 21:17:35 +02004125 cmdidx_T
4126excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
Bram Moolenaard0190392019-08-23 21:17:35 +02004128 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004130 if (!one_letter_cmd(cmd, &idx))
4131 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4132 idx = (cmdidx_T)((int)idx + 1))
4133 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4134 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
Bram Moolenaard0190392019-08-23 21:17:35 +02004136 return idx;
4137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
Bram Moolenaard0190392019-08-23 21:17:35 +02004139 long
4140excmd_get_argt(cmdidx_T idx)
4141{
4142 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143}
4144
4145/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004146 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 *
4148 * Backslashed delimiters after / or ? will be skipped, and commands will
4149 * not be expanded between /'s and ?'s or after "'".
4150 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004151 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 * Returns the "cmd" pointer advanced to beyond the range.
4153 */
4154 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004155skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004156 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004157 int skip_star, // skip "*" used for Visual range
4158 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004160 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004161 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004163 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004165 if (*cmd == '\\')
4166 {
4167 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4168 ++cmd;
4169 else
4170 break;
4171 }
4172 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004174 char_u *p = cmd;
4175
4176 // a quote is only valid at the start or after a separator
4177 while (p > cmd_start)
4178 {
4179 --p;
4180 if (!VIM_ISWHITE(*p))
4181 break;
4182 }
4183 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4184 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 if (*++cmd == NUL && ctx != NULL)
4186 *ctx = EXPAND_NOTHING;
4187 }
4188 else if (*cmd == '/' || *cmd == '?')
4189 {
4190 delim = *cmd++;
4191 while (*cmd != NUL && *cmd != delim)
4192 if (*cmd++ == '\\' && *cmd != NUL)
4193 ++cmd;
4194 if (*cmd == NUL && ctx != NULL)
4195 *ctx = EXPAND_NOTHING;
4196 }
4197 if (*cmd != NUL)
4198 ++cmd;
4199 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004200
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004201 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004202 while (*cmd == ':')
4203 cmd = skipwhite(cmd + 1);
4204
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004205 // Skip "*" used for Visual range.
4206 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4207 cmd = skipwhite(cmd + 1);
4208
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return cmd;
4210}
4211
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004212 static void
4213addr_error(cmd_addr_T addr_type)
4214{
4215 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004216 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004217 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004218 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004219}
4220
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004222 * Return the default address for an address type.
4223 */
4224 static linenr_T
4225default_address(exarg_T *eap)
4226{
4227 linenr_T lnum = 0;
4228
4229 switch (eap->addr_type)
4230 {
4231 case ADDR_LINES:
4232 case ADDR_OTHER:
4233 // Default is the cursor line number. Avoid using an invalid
4234 // line number though.
4235 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4236 lnum = curbuf->b_ml.ml_line_count;
4237 else
4238 lnum = curwin->w_cursor.lnum;
4239 break;
4240 case ADDR_WINDOWS:
4241 lnum = CURRENT_WIN_NR;
4242 break;
4243 case ADDR_ARGUMENTS:
4244 lnum = curwin->w_arg_idx + 1;
4245 if (lnum > ARGCOUNT)
4246 lnum = ARGCOUNT;
4247 break;
4248 case ADDR_LOADED_BUFFERS:
4249 case ADDR_BUFFERS:
4250 lnum = curbuf->b_fnum;
4251 break;
4252 case ADDR_TABS:
4253 lnum = CURRENT_TAB_NR;
4254 break;
4255 case ADDR_TABS_RELATIVE:
4256 case ADDR_UNSIGNED:
4257 lnum = 1;
4258 break;
4259 case ADDR_QUICKFIX:
4260#ifdef FEAT_QUICKFIX
4261 lnum = qf_get_cur_idx(eap);
4262#endif
4263 break;
4264 case ADDR_QUICKFIX_VALID:
4265#ifdef FEAT_QUICKFIX
4266 lnum = qf_get_cur_valid_idx(eap);
4267#endif
4268 break;
4269 case ADDR_NONE:
4270 // Will give an error later if a range is found.
4271 break;
4272 }
4273 return lnum;
4274}
4275
4276/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004277 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 *
4279 * Set ptr to the next character after the part that was interpreted.
4280 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004281 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 *
4283 * Return MAXLNUM when no Ex address was found.
4284 */
4285 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004286get_address(
4287 exarg_T *eap UNUSED,
4288 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004289 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004290 int skip, // only skip the address, don't use it
4291 int silent, // no errors or side effects
4292 int to_other_file, // flag: may jump to other file
4293 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294{
4295 int c;
4296 int i;
4297 long n;
4298 char_u *cmd;
4299 pos_T pos;
4300 pos_T *fp;
4301 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004302 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
4304 cmd = skipwhite(*ptr);
4305 lnum = MAXLNUM;
4306 do
4307 {
4308 switch (*cmd)
4309 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004310 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004311 ++cmd;
4312 switch (addr_type)
4313 {
4314 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004315 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 lnum = curwin->w_cursor.lnum;
4317 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004318 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004319 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004320 break;
4321 case ADDR_ARGUMENTS:
4322 lnum = curwin->w_arg_idx + 1;
4323 break;
4324 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004325 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004326 lnum = curbuf->b_fnum;
4327 break;
4328 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004329 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004330 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004331 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004332 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004333 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004334 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004335 cmd = NULL;
4336 goto error;
4337 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004338 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004339#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004340 lnum = qf_get_cur_idx(eap);
4341#endif
4342 break;
4343 case ADDR_QUICKFIX_VALID:
4344#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004345 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004346#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004347 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004348 }
4349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004351 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004352 ++cmd;
4353 switch (addr_type)
4354 {
4355 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004356 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 lnum = curbuf->b_ml.ml_line_count;
4358 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004359 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004360 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004361 break;
4362 case ADDR_ARGUMENTS:
4363 lnum = ARGCOUNT;
4364 break;
4365 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004366 buf = lastbuf;
4367 while (buf->b_ml.ml_mfp == NULL)
4368 {
4369 if (buf->b_prev == NULL)
4370 break;
4371 buf = buf->b_prev;
4372 }
4373 lnum = buf->b_fnum;
4374 break;
4375 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004376 lnum = lastbuf->b_fnum;
4377 break;
4378 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004379 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004380 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004381 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004382 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004383 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004384 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004385 cmd = NULL;
4386 goto error;
4387 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004388 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004389#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004390 lnum = qf_get_size(eap);
4391 if (lnum == 0)
4392 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004393#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004394 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004395 case ADDR_QUICKFIX_VALID:
4396#ifdef FEAT_QUICKFIX
4397 lnum = qf_get_valid_size(eap);
4398 if (lnum == 0)
4399 lnum = 1;
4400#endif
4401 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 }
4403 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004405 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 if (*++cmd == NUL)
4407 {
4408 cmd = NULL;
4409 goto error;
4410 }
4411 if (addr_type != ADDR_LINES)
4412 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004413 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004414 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 goto error;
4416 }
4417 if (skip)
4418 ++cmd;
4419 else
4420 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004421 // Only accept a mark in another file when it is
4422 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4424 ++cmd;
4425 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004426 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004427 lnum = curwin->w_cursor.lnum;
4428 else
4429 {
4430 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
4432 cmd = NULL;
4433 goto error;
4434 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 lnum = fp->lnum;
4436 }
4437 }
4438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004441 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 c = *cmd++;
4443 if (addr_type != ADDR_LINES)
4444 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004445 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004446 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004447 goto error;
4448 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004449 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004451 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004452 if (*cmd == c)
4453 ++cmd;
4454 }
4455 else
4456 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004457 int flags;
4458
4459 pos = curwin->w_cursor; // save curwin->w_cursor
4460
4461 // When '/' or '?' follows another address, start from
4462 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004463 if (lnum > 0 && lnum != MAXLNUM)
4464 curwin->w_cursor.lnum =
4465 lnum > curbuf->b_ml.ml_line_count
4466 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004467
4468 // Start a forward search at the end of the line (unless
4469 // before the first line).
4470 // Start a backward search at the start of the line.
4471 // This makes sure we never match in the current
4472 // line, and can match anywhere in the
4473 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004474 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004475 curwin->w_cursor.col = MAXCOL;
4476 else
4477 curwin->w_cursor.col = 0;
4478 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004479 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004480 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 {
4482 curwin->w_cursor = pos;
4483 cmd = NULL;
4484 goto error;
4485 }
4486 lnum = curwin->w_cursor.lnum;
4487 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004488 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004489 cmd += searchcmdlen;
4490 }
4491 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004493 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004494 ++cmd;
4495 if (addr_type != ADDR_LINES)
4496 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004497 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004498 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 goto error;
4500 }
4501 if (*cmd == '&')
4502 i = RE_SUBST;
4503 else if (*cmd == '?' || *cmd == '/')
4504 i = RE_SEARCH;
4505 else
4506 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004507 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004508 cmd = NULL;
4509 goto error;
4510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004512 if (!skip)
4513 {
4514 /*
4515 * When search follows another address, start from
4516 * there.
4517 */
4518 if (lnum != MAXLNUM)
4519 pos.lnum = lnum;
4520 else
4521 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004523 /*
4524 * Start the search just like for the above
4525 * do_search().
4526 */
4527 if (*cmd != '?')
4528 pos.col = MAXCOL;
4529 else
4530 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004531 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004532 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004533 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004534 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004535 lnum = pos.lnum;
4536 else
4537 {
4538 cmd = NULL;
4539 goto error;
4540 }
4541 }
4542 ++cmd;
4543 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004546 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004547 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549
4550 for (;;)
4551 {
4552 cmd = skipwhite(cmd);
4553 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4554 break;
4555
4556 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004557 {
4558 switch (addr_type)
4559 {
4560 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004561 case ADDR_OTHER:
4562 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004563 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004564 break;
4565 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004566 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004567 break;
4568 case ADDR_ARGUMENTS:
4569 lnum = curwin->w_arg_idx + 1;
4570 break;
4571 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004572 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004573 lnum = curbuf->b_fnum;
4574 break;
4575 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004576 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004577 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004578 case ADDR_TABS_RELATIVE:
4579 lnum = 1;
4580 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004581 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004582#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004583 lnum = qf_get_cur_idx(eap);
4584#endif
4585 break;
4586 case ADDR_QUICKFIX_VALID:
4587#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004588 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004589#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004590 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004591 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004592 case ADDR_UNSIGNED:
4593 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004594 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004595 }
4596 }
4597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004599 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 else
4601 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004602 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 n = 1;
4604 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004607 if (n == MAXLNUM)
4608 {
4609 emsg(_(e_line_number_out_of_range));
4610 goto error;
4611 }
4612 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004613
4614 if (addr_type == ADDR_TABS_RELATIVE)
4615 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004616 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004617 cmd = NULL;
4618 goto error;
4619 }
4620 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004621 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004622 lnum = compute_buffer_local_count(
4623 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004625 {
4626#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004627 // Relative line addressing, need to adjust for folded lines
4628 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004629 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004630 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004631 (void)hasFolding(lnum, NULL, &lnum);
4632#endif
4633 if (i == '-')
4634 lnum -= n;
4635 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004636 {
4637 if (n >= LONG_MAX - lnum)
4638 {
4639 emsg(_(e_line_number_out_of_range));
4640 goto error;
4641 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004642 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004643 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 }
4646 } while (*cmd == '/' || *cmd == '?');
4647
4648error:
4649 *ptr = cmd;
4650 return lnum;
4651}
4652
4653/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004654 * Set eap->line1 and eap->line2 to the whole range.
4655 * Used for commands with the EX_DFLALL flag and no range given.
4656 */
4657 static void
4658address_default_all(exarg_T *eap)
4659{
4660 eap->line1 = 1;
4661 switch (eap->addr_type)
4662 {
4663 case ADDR_LINES:
4664 case ADDR_OTHER:
4665 eap->line2 = curbuf->b_ml.ml_line_count;
4666 break;
4667 case ADDR_LOADED_BUFFERS:
4668 {
4669 buf_T *buf = firstbuf;
4670
4671 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4672 buf = buf->b_next;
4673 eap->line1 = buf->b_fnum;
4674 buf = lastbuf;
4675 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4676 buf = buf->b_prev;
4677 eap->line2 = buf->b_fnum;
4678 }
4679 break;
4680 case ADDR_BUFFERS:
4681 eap->line1 = firstbuf->b_fnum;
4682 eap->line2 = lastbuf->b_fnum;
4683 break;
4684 case ADDR_WINDOWS:
4685 eap->line2 = LAST_WIN_NR;
4686 break;
4687 case ADDR_TABS:
4688 eap->line2 = LAST_TAB_NR;
4689 break;
4690 case ADDR_TABS_RELATIVE:
4691 eap->line2 = 1;
4692 break;
4693 case ADDR_ARGUMENTS:
4694 if (ARGCOUNT == 0)
4695 eap->line1 = eap->line2 = 0;
4696 else
4697 eap->line2 = ARGCOUNT;
4698 break;
4699 case ADDR_QUICKFIX_VALID:
4700#ifdef FEAT_QUICKFIX
4701 eap->line2 = qf_get_valid_size(eap);
4702 if (eap->line2 == 0)
4703 eap->line2 = 1;
4704#endif
4705 break;
4706 case ADDR_NONE:
4707 case ADDR_UNSIGNED:
4708 case ADDR_QUICKFIX:
4709 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4710 break;
4711 }
4712}
4713
4714
4715/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004716 * Get flags from an Ex command argument.
4717 */
4718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004719get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004720{
4721 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4722 {
4723 if (*eap->arg == 'l')
4724 eap->flags |= EXFLAG_LIST;
4725 else if (*eap->arg == 'p')
4726 eap->flags |= EXFLAG_PRINT;
4727 else
4728 eap->flags |= EXFLAG_NR;
4729 eap->arg = skipwhite(eap->arg + 1);
4730 }
4731}
4732
4733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 * Function called for command which is Not Implemented. NI!
4735 */
4736 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004737ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738{
4739 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004740 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004741 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742}
4743
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004744#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745/*
4746 * Function called for script command which is Not Implemented. NI!
4747 * Skips over ":perl <<EOF" constructs.
4748 */
4749 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004750ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751{
4752 if (!eap->skip)
4753 ex_ni(eap);
4754 else
4755 vim_free(script_get(eap, eap->arg));
4756}
4757#endif
4758
4759/*
4760 * Check range in Ex command for validity.
4761 * Return NULL when valid, error message when invalid.
4762 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004763 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004764invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004766 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004767
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if ( eap->line1 < 0
4769 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004770 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004771 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004772
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004773 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004774 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004775 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004776 {
4777 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004778 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004779#ifdef FEAT_DIFF
4780 + (eap->cmdidx == CMD_diffget)
4781#endif
4782 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004783 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004784 break;
4785 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004786 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004787 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004788 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004789 break;
4790 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004791 // Only a boundary check, not whether the buffers actually
4792 // exist.
4793 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004794 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004795 break;
4796 case ADDR_LOADED_BUFFERS:
4797 buf = firstbuf;
4798 while (buf->b_ml.ml_mfp == NULL)
4799 {
4800 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004801 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004802 buf = buf->b_next;
4803 }
4804 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004805 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004806 buf = lastbuf;
4807 while (buf->b_ml.ml_mfp == NULL)
4808 {
4809 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004810 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004811 buf = buf->b_prev;
4812 }
4813 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004814 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004815 break;
4816 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004817 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004818 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004819 break;
4820 case ADDR_TABS:
4821 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004822 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004823 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004824 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004825 case ADDR_OTHER:
4826 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004827 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004828 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004829#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004830 // No error for value that is too big, will use the last entry.
4831 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004832 {
4833 if (eap->addr_count == 0)
4834 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004835 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004836 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004837#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004838 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004839 case ADDR_QUICKFIX_VALID:
4840#ifdef FEAT_QUICKFIX
4841 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4842 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004843 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004844#endif
4845 break;
4846 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004847 case ADDR_NONE:
4848 // Will give an error elsewhere.
4849 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004850 }
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 return NULL;
4853}
4854
4855/*
4856 * Correct the range for zero line number, if required.
4857 */
4858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004859correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004861 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 {
4863 if (eap->line1 == 0)
4864 eap->line1 = 1;
4865 if (eap->line2 == 0)
4866 eap->line2 = 1;
4867 }
4868}
4869
Bram Moolenaar748bf032005-02-02 23:04:36 +00004870#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004871/*
4872 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4873 * pattern. Otherwise return eap->arg.
4874 */
4875 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004876skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004877{
4878 char_u *p = eap->arg;
4879
Bram Moolenaara37420f2006-02-04 22:37:47 +00004880 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4881 || eap->cmdidx == CMD_vimgrepadd
4882 || eap->cmdidx == CMD_lvimgrepadd
4883 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004884 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004885 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004886 if (p == NULL)
4887 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004888 }
4889 return p;
4890}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004891
4892/*
4893 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4894 * in the command line, so that things like % get expanded.
4895 */
4896 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004897replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004898{
4899 char_u *new_cmdline;
4900 char_u *program;
4901 char_u *pos;
4902 char_u *ptr;
4903 int len;
4904 int i;
4905
4906 /*
4907 * Don't do it when ":vimgrep" is used for ":grep".
4908 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004909 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4910 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4911 || eap->cmdidx == CMD_grepadd
4912 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004913 && !grep_internal(eap->cmdidx))
4914 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004915 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4916 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004917 {
4918 if (*curbuf->b_p_gp == NUL)
4919 program = p_gp;
4920 else
4921 program = curbuf->b_p_gp;
4922 }
4923 else
4924 {
4925 if (*curbuf->b_p_mp == NUL)
4926 program = p_mp;
4927 else
4928 program = curbuf->b_p_mp;
4929 }
4930
4931 p = skipwhite(p);
4932
4933 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4934 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004935 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004936 i = 1;
4937 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4938 ++i;
4939 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004940 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004941 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004942 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004943 ptr = new_cmdline;
4944 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4945 {
4946 i = (int)(pos - program);
4947 STRNCPY(ptr, program, i);
4948 STRCPY(ptr += i, p);
4949 ptr += len;
4950 program = pos + 2;
4951 }
4952 STRCPY(ptr, program);
4953 }
4954 else
4955 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004956 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004957 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004958 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004959 STRCPY(new_cmdline, program);
4960 STRCAT(new_cmdline, " ");
4961 STRCAT(new_cmdline, p);
4962 }
4963 msg_make(p);
4964
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004965 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004966 vim_free(*cmdlinep);
4967 *cmdlinep = new_cmdline;
4968 p = new_cmdline;
4969 }
4970 return p;
4971}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004972#endif
4973
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974/*
4975 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004976 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 * Return FAIL for failure, OK otherwise.
4978 */
4979 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004980expand_filename(
4981 exarg_T *eap,
4982 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004983 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004985 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 char_u *repl;
4987 int srclen;
4988 char_u *p;
4989 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004990 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991
Bram Moolenaar748bf032005-02-02 23:04:36 +00004992#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004993 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004994 p = skip_grep_pat(eap);
4995#else
4996 p = eap->arg;
4997#endif
4998
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 /*
5000 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5001 * the file name contains a wildcard it should not cause expanding.
5002 * (it will be expanded anyway if there is a wildcard before replacing).
5003 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005004 has_wildcards = mch_has_wildcard(p);
5005 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005007#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005008 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005009 if (p[0] == '`' && p[1] == '=')
5010 {
5011 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005012 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005013 if (*p == '`')
5014 ++p;
5015 continue;
5016 }
5017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 /*
5019 * Quick check if this cannot be the start of a special string.
5020 * Also removes backslash before '%', '#' and '<'.
5021 */
5022 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5023 {
5024 ++p;
5025 continue;
5026 }
5027
5028 /*
5029 * Try to find a match at this position.
5030 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005031 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005032 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005033 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005035 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
5037 p += srclen;
5038 continue;
5039 }
5040
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005041 // Wildcards won't be expanded below, the replacement is taken
5042 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005043 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5044 {
5045 char_u *l = repl;
5046
5047 repl = expand_env_save(repl);
5048 vim_free(l);
5049 }
5050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005051 // Need to escape white space et al. with a backslash.
5052 // Don't do this for:
5053 // - replacement that already has been escaped: "##"
5054 // - shell commands (may have to use quotes instead).
5055 // - non-unix systems when there is a single argument (spaces don't
5056 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005058 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 && eap->cmdidx != CMD_grep
5061 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005062 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005063 && eap->cmdidx != CMD_lgrep
5064 && eap->cmdidx != CMD_lgrepadd
5065 && eap->cmdidx != CMD_lmake
5066 && eap->cmdidx != CMD_make
5067 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005069 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070#endif
5071 )
5072 {
5073 char_u *l;
5074#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005075 // Don't escape a backslash here, because rem_backslash() doesn't
5076 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 static char_u *nobslash = (char_u *)" \t\"|";
5078# define ESCAPE_CHARS nobslash
5079#else
5080# define ESCAPE_CHARS escape_chars
5081#endif
5082
5083 for (l = repl; *l; ++l)
5084 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5085 {
5086 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5087 if (l != NULL)
5088 {
5089 vim_free(repl);
5090 repl = l;
5091 }
5092 break;
5093 }
5094 }
5095
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005096 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005097 if ((eap->usefilter || eap->cmdidx == CMD_bang
5098 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005099 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
5101 char_u *l;
5102
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005103 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 if (l != NULL)
5105 {
5106 vim_free(repl);
5107 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 }
5110
5111 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5112 vim_free(repl);
5113 if (p == NULL)
5114 return FAIL;
5115 }
5116
5117 /*
5118 * One file argument: Expand wildcards.
5119 * Don't do this with ":r !command" or ":w !command".
5120 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005121 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
5123 /*
5124 * May do this twice:
5125 * 1. Replace environment variables.
5126 * 2. Replace any other wildcards, remove backslashes.
5127 */
5128 for (n = 1; n <= 2; ++n)
5129 {
5130 if (n == 2)
5131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 /*
5133 * Halve the number of backslashes (this is Vi compatible).
5134 * For Unix and OS/2, when wildcards are expanded, this is
5135 * done by ExpandOne() below.
5136 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005137#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (!has_wildcards)
5139#endif
5140 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142
5143 if (has_wildcards)
5144 {
5145 if (n == 1)
5146 {
5147 /*
5148 * First loop: May expand environment variables. This
5149 * can be done much faster with expand_env() than with
5150 * something else (e.g., calling a shell).
5151 * After expanding environment variables, check again
5152 * if there are still wildcards present.
5153 */
5154 if (vim_strchr(eap->arg, '$') != NULL
5155 || vim_strchr(eap->arg, '~') != NULL)
5156 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005157 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005158 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 has_wildcards = mch_has_wildcard(NameBuff);
5160 p = NameBuff;
5161 }
5162 else
5163 p = NULL;
5164 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005165 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 {
5167 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005168 int options = WILD_LIST_NOTFOUND
5169 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
5171 ExpandInit(&xpc);
5172 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005173 if (p_wic)
5174 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005176 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 if (p == NULL)
5178 return FAIL;
5179 }
5180 if (p != NULL)
5181 {
5182 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5183 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005184 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 vim_free(p);
5186 }
5187 }
5188 }
5189 }
5190 return OK;
5191}
5192
5193/*
5194 * Replace part of the command line, keeping eap->cmd, eap->arg and
5195 * eap->nextcmd correct.
5196 * "src" points to the part that is to be replaced, of length "srclen".
5197 * "repl" is the replacement string.
5198 * Returns a pointer to the character after the replaced string.
5199 * Returns NULL for failure.
5200 */
5201 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005202repl_cmdline(
5203 exarg_T *eap,
5204 char_u *src,
5205 int srclen,
5206 char_u *repl,
5207 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208{
5209 int len;
5210 int i;
5211 char_u *new_cmdline;
5212
5213 /*
5214 * The new command line is build in new_cmdline[].
5215 * First allocate it.
5216 * Careful: a "+cmd" argument may have been NUL terminated.
5217 */
5218 len = (int)STRLEN(repl);
5219 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005220 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005221 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005222 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005223 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224
5225 /*
5226 * Copy the stuff before the expanded part.
5227 * Copy the expanded stuff.
5228 * Copy what came after the expanded part.
5229 * Copy the next commands, if there are any.
5230 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005231 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005233
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005235 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005237 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005239 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 i = (int)STRLEN(new_cmdline) + 1;
5242 STRCPY(new_cmdline + i, eap->nextcmd);
5243 eap->nextcmd = new_cmdline + i;
5244 }
5245 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5246 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5247 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5248 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5249 vim_free(*cmdlinep);
5250 *cmdlinep = new_cmdline;
5251
5252 return src;
5253}
5254
5255/*
5256 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005257 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 */
5259 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005260separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262 char_u *p;
5263
Bram Moolenaar86b68352004-12-27 21:59:20 +00005264#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005265 p = skip_grep_pat(eap);
5266#else
5267 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005268#endif
5269
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005270 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 {
5272 if (*p == Ctrl_V)
5273 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005274 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005275 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005277 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005278 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005279 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 break;
5281 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005282
5283#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005284 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005285 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005286 {
5287 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005288 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005289 if (*p == NUL) // stop at NUL after CTRL-V
5290 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005291 }
5292#endif
5293
Bram Moolenaareda29c92022-10-02 12:59:00 +01005294 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005295 // :@" and :*" do not start a comment!
5296 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005297 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005298 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005299 && !(eap->argt & EX_NOTRLCOM)
5300 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5301 || p != eap->arg)
5302 && (eap->cmdidx != CMD_redir
5303 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005304 || (*p == '#'
5305 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005306 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005307 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 || *p == '|' || *p == '\n')
5309 {
5310 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005311 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 * AND 'b' is present in 'cpoptions'.
5313 */
5314 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005315 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005317 if (!keep_backslash)
5318 {
5319 STRMOVE(p - 1, p); // remove the '\'
5320 --p;
5321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
5323 else
5324 {
5325 eap->nextcmd = check_nextcmd(p);
5326 *p = NUL;
5327 break;
5328 }
5329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005331
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005332 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 del_trailing_spaces(eap->arg);
5334}
5335
5336/*
5337 * get + command from ex argument
5338 */
5339 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005340getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
5342 char_u *arg = *argp;
5343 char_u *command = NULL;
5344
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005345 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
5347 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005348 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 command = dollar_command;
5350 else
5351 {
5352 command = arg;
5353 arg = skip_cmd_arg(command, TRUE);
5354 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005355 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005358 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 *argp = arg;
5360 }
5361 return command;
5362}
5363
5364/*
5365 * Find end of "+command" argument. Skip over "\ " and "\\".
5366 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005367 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005368skip_cmd_arg(
5369 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005370 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371{
5372 while (*p && !vim_isspace(*p))
5373 {
5374 if (*p == '\\' && p[1] != NUL)
5375 {
5376 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005377 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 else
5379 ++p;
5380 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005381 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 }
5383 return p;
5384}
5385
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005386 int
5387get_bad_opt(char_u *p, exarg_T *eap)
5388{
5389 if (STRICMP(p, "keep") == 0)
5390 eap->bad_char = BAD_KEEP;
5391 else if (STRICMP(p, "drop") == 0)
5392 eap->bad_char = BAD_DROP;
5393 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5394 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005395 else
5396 return FAIL;
5397 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005398}
5399
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400/*
5401 * Get "++opt=arg" argument.
5402 * Return FAIL or OK.
5403 */
5404 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005405getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407 char_u *arg = eap->arg + 2;
5408 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005409 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005412 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5414 {
5415 if (*arg == 'n')
5416 {
5417 arg += 2;
5418 eap->force_bin = FORCE_NOBIN;
5419 }
5420 else
5421 eap->force_bin = FORCE_BIN;
5422 if (!checkforcmd(&arg, "binary", 3))
5423 return FAIL;
5424 eap->arg = skipwhite(arg);
5425 return OK;
5426 }
5427
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005428 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005429 if (STRNCMP(arg, "edit", 4) == 0)
5430 {
5431 eap->read_edit = TRUE;
5432 eap->arg = skipwhite(arg + 4);
5433 return OK;
5434 }
5435
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 if (STRNCMP(arg, "ff", 2) == 0)
5437 {
5438 arg += 2;
5439 pp = &eap->force_ff;
5440 }
5441 else if (STRNCMP(arg, "fileformat", 10) == 0)
5442 {
5443 arg += 10;
5444 pp = &eap->force_ff;
5445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 else if (STRNCMP(arg, "enc", 3) == 0)
5447 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005448 if (STRNCMP(arg, "encoding", 8) == 0)
5449 arg += 8;
5450 else
5451 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 pp = &eap->force_enc;
5453 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005454 else if (STRNCMP(arg, "bad", 3) == 0)
5455 {
5456 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005457 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
5460 if (pp == NULL || *arg != '=')
5461 return FAIL;
5462
5463 ++arg;
5464 *pp = (int)(arg - eap->cmd);
5465 arg = skip_cmd_arg(arg, FALSE);
5466 eap->arg = skipwhite(arg);
5467 *arg = NUL;
5468
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 if (pp == &eap->force_ff)
5470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5472 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005473 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005475 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005477 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5479 *p = TOLOWER_ASC(*p);
5480 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005481 else
5482 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005483 // Check ++bad= argument. Must be a single-byte character, "keep" or
5484 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005485 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005486 return FAIL;
5487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
5489 return OK;
5490}
5491
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005493ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005496 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 * directory for security reasons.
5498 */
5499 if (secure)
5500 {
5501 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005502 eap->errmsg =
5503 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 }
5505 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005506 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 else
5508 do_augroup(eap->arg, eap->forceit);
5509}
5510
5511/*
5512 * ":doautocmd": Apply the automatic commands to the current buffer.
5513 */
5514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005515ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005517 char_u *arg = eap->arg;
5518 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005519 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005520
Bram Moolenaar1610d052016-06-09 22:53:01 +02005521 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005522 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005523 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005524 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527/*
5528 * :[N]bunload[!] [N] [bufname] unload buffer
5529 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5530 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5531 */
5532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005533ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005535 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005536 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 eap->errmsg = do_bufdel(
5538 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5539 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5540 : DOBUF_UNLOAD, eap->arg,
5541 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5542}
5543
5544/*
5545 * :[N]buffer [N] to buffer N
5546 * :[N]sbuffer [N] to buffer N
5547 */
5548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005549ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005551 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005552 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005554 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 else
5556 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005557 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5559 else
5560 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005561 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005562 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
5564}
5565
5566/*
5567 * :[N]bmodified [N] to next mod. buffer
5568 * :[N]sbmodified [N] to next mod. buffer
5569 */
5570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005571ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572{
5573 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005574 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005575 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576}
5577
5578/*
5579 * :[N]bnext [N] to next buffer
5580 * :[N]sbnext [N] split and to next buffer
5581 */
5582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005583ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005585 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005586 return;
5587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005589 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005590 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591}
5592
5593/*
5594 * :[N]bNext [N] to previous buffer
5595 * :[N]bprevious [N] to previous buffer
5596 * :[N]sbNext [N] split and to previous buffer
5597 * :[N]sbprevious [N] split and to previous buffer
5598 */
5599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005600ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005602 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005603 return;
5604
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005606 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005607 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608}
5609
5610/*
5611 * :brewind to first buffer
5612 * :bfirst to first buffer
5613 * :sbrewind split and to first buffer
5614 * :sbfirst split and to first buffer
5615 */
5616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005617ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005619 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005620 return;
5621
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005623 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005624 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625}
5626
5627/*
5628 * :blast to last buffer
5629 * :sblast split and to last buffer
5630 */
5631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005632ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005634 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005635 return;
5636
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005638 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005639 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
Bram Moolenaar7a092242020-04-16 22:10:49 +02005642/*
5643 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005644 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005647ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005649 int comment_char = '"';
5650
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005651 if (in_vim9script())
5652 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005653 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005654}
5655
5656/*
5657 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5658 * to "cmd_start" or has a white space character before it.
5659 */
5660 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005661ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005662{
5663 int c = *cmd;
5664
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005665 if (c == NUL || c == '|' || c == '\n')
5666 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005667 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005668 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5669 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005670 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005671 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672}
5673
5674#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5675 || defined(PROTO)
5676/*
5677 * Return the next command, after the first '|' or '\n'.
5678 * Return NULL if not found.
5679 */
5680 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005681find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
5683 while (*p != '|' && *p != '\n')
5684 {
5685 if (*p == NUL)
5686 return NULL;
5687 ++p;
5688 }
5689 return (p + 1);
5690}
5691#endif
5692
5693/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005694 * Check if *p is a separator between Ex commands, skipping over white space.
5695 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 */
5697 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005698check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005700 char_u *s = skipwhite(p);
5701
5702 if (*s == '|' || *s == '\n')
5703 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 else
5705 return NULL;
5706}
5707
5708/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005709 * If "eap->nextcmd" is not set, check for a next command at "p".
5710 */
5711 void
5712set_nextcmd(exarg_T *eap, char_u *arg)
5713{
5714 char_u *p = check_nextcmd(arg);
5715
5716 if (eap->nextcmd == NULL)
5717 eap->nextcmd = p;
5718 else if (p != NULL)
5719 // cannot use "| command" inside a {} block
5720 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5721}
5722
5723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 * - if there are more files to edit
5725 * - and this is the last window
5726 * - and forceit not used
5727 * - and not repeated twice on a row
5728 * return FAIL and give error message if 'message' TRUE
5729 * return OK otherwise
5730 */
5731 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005732check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005733 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005734 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
5736 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5737
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005738 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005739 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 {
5741 if (message)
5742 {
5743#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005744 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5745 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005747 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005749 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5750 NGETTEXT("%d more file to edit. Quit anyway?",
5751 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5753 return OK;
5754 return FAIL;
5755 }
5756#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005757 semsg(NGETTEXT(e_nr_more_file_to_edit,
5758 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005759 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761 return FAIL;
5762 }
5763 return OK;
5764}
5765
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766/*
5767 * Function given to ExpandGeneric() to obtain the list of command names.
5768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005770get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
5772 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005773 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 return cmdnames[idx].cmd_name;
5775}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005778ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
Bram Moolenaare6850792010-05-14 15:28:44 +02005780 if (*eap->arg == NUL)
5781 {
5782#ifdef FEAT_EVAL
5783 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5784 char_u *p = NULL;
5785
5786 if (expr != NULL)
5787 {
5788 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005789 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005790 --emsg_off;
5791 vim_free(expr);
5792 }
5793 if (p != NULL)
5794 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005795 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005796 vim_free(p);
5797 }
5798 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005799 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005800#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005801 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005802#endif
5803 }
5804 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005805 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005806
5807#ifdef FEAT_VTP
5808 else if (has_vtp_working())
5809 {
5810 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005811 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005812 redrawcmd();
5813 }
5814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815}
5816
5817 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005818ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819{
5820 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005821 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 do_highlight(eap->arg, eap->forceit, FALSE);
5823}
5824
5825
5826/*
5827 * Call this function if we thought we were going to exit, but we won't
5828 * (because of an error). May need to restore the terminal mode.
5829 */
5830 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005831not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
5833 exiting = FALSE;
5834 settmode(TMODE_RAW);
5835}
5836
Bram Moolenaar3552e742021-05-29 12:21:58 +02005837 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005838before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5839{
5840 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5841
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005842 // Bail out when autocommands closed the window.
5843 // Refuse to quit when the buffer in the last window is being closed (can
5844 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005845 if (!win_valid(wp)
5846 || curbuf_locked()
5847 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5848 return TRUE;
5849
5850 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5851 {
5852 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005853 // Refuse to quit when locked or when the window was closed or the
5854 // buffer in the last window is being closed (can only happen in
5855 // autocommands).
5856 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005857 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5858 return TRUE;
5859 }
5860
5861 return FALSE;
5862}
5863
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005865 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005866 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005867 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005869 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005870ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005872 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005873
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 if (cmdwin_type != 0)
5875 {
5876 cmdwin_result = Ctrl_C;
5877 return;
5878 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005879 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005880 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005881 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005882 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005883 return;
5884 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005885 if (eap->addr_count > 0)
5886 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005887 int wnr = eap->line2;
5888
5889 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5890 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005891 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005892 }
5893 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005894 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005895
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005896 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005897 if (curbuf_locked())
5898 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005899
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005900 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005901 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005902 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
5904#ifdef FEAT_NETBEANS_INTG
5905 netbeansForcedQuit = eap->forceit;
5906#endif
5907
5908 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005909 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 */
5911 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5912 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005913 if ((!buf_hide(wp->w_buffer)
5914 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005915 | (eap->forceit ? CCGD_FORCEIT : 0)
5916 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005918 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 {
5920 not_exiting();
5921 }
5922 else
5923 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005924 // quit last window
5925 // Note: only_one_window() returns true, even so a help window is
5926 // still open. In that case only quit, if no address has been
5927 // specified. Example:
5928 // :h|wincmd w|1q - don't quit
5929 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005930 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005932 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005933#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005935#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005936 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005937 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 }
5939}
5940
5941/*
5942 * ":cquit".
5943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005945ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005947 // this does not always pass on the exit code to the Manx compiler. why?
5948 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949}
5950
5951/*
5952 * ":qall": try to quit all windows
5953 */
5954 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005955ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 if (cmdwin_type != 0)
5958 {
5959 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005960 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 else
5962 cmdwin_result = K_XF2;
5963 return;
5964 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005965
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005966 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005967 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005968 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005969 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005970 return;
5971 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005972
5973 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005974 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005975
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005977 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 getout(0);
5979 not_exiting();
5980}
5981
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982/*
5983 * ":close": close current window, unless it is the last one
5984 */
5985 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005986ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005988 win_T *win;
5989 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005991 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005993 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005994 {
5995 if (eap->addr_count == 0)
5996 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005997 else
5998 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005999 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006000 {
6001 winnr++;
6002 if (winnr == eap->line2)
6003 break;
6004 }
6005 if (win == NULL)
6006 win = lastwin;
6007 ex_win_close(eap->forceit, win, NULL);
6008 }
6009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010}
6011
Bram Moolenaar4033c552017-09-16 20:54:51 +02006012#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006013/*
6014 * ":pclose": Close any preview window.
6015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006017ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006018{
6019 win_T *win;
6020
Bram Moolenaar79648732019-07-18 21:43:07 +02006021 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006022 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006023 if (win->w_p_pvw)
6024 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006025 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006026 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006027 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006028# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006029 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006030 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006031# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006032}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006033#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006034
Bram Moolenaarf740b292006-02-16 22:11:02 +00006035/*
6036 * Close window "win" and take care of handling closing the last window for a
6037 * modified buffer.
6038 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006040ex_win_close(
6041 int forceit,
6042 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006043 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044{
6045 int need_hide;
6046 buf_T *buf = win->w_buffer;
6047
Bram Moolenaarcf844172020-06-26 19:44:06 +02006048 // Never close the autocommand window.
6049 if (win == aucmd_win)
6050 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006051 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006052 return;
6053 }
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006056 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006058#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006059 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006061 bufref_T bufref;
6062
6063 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006065 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 return;
6067 need_hide = FALSE;
6068 }
6069 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006072 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 return;
6074 }
6075 }
6076
Bram Moolenaar4033c552017-09-16 20:54:51 +02006077#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006079#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006080
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006081 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006082 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006083 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006084 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006085 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086}
6087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006089 * Handle the argument for a tabpage related ex command.
6090 * Returns a tabpage number.
6091 * When an error is encountered then eap->errmsg is set.
6092 */
6093 static int
6094get_tabpage_arg(exarg_T *eap)
6095{
6096 int tab_number;
6097 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6098
6099 if (eap->arg && *eap->arg != NUL)
6100 {
6101 char_u *p = eap->arg;
6102 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 int relative = 0; // argument +N/-N means: go to N places to the
6104 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006105
6106 if (*p == '-')
6107 {
6108 relative = -1;
6109 p++;
6110 }
6111 else if (*p == '+')
6112 {
6113 relative = 1;
6114 p++;
6115 }
6116
6117 p_save = p;
6118 tab_number = getdigits(&p);
6119
6120 if (relative == 0)
6121 {
6122 if (STRCMP(p, "$") == 0)
6123 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006124 else if (STRCMP(p, "#") == 0)
6125 if (valid_tabpage(lastused_tabpage))
6126 tab_number = tabpage_index(lastused_tabpage);
6127 else
6128 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006129 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006130 tab_number = 0;
6131 goto theend;
6132 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006133 else if (p == p_save || *p_save == '-' || *p != NUL
6134 || tab_number > LAST_TAB_NR)
6135 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006136 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006137 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006138 goto theend;
6139 }
6140 }
6141 else
6142 {
6143 if (*p_save == NUL)
6144 tab_number = 1;
6145 else if (p == p_save || *p_save == '-' || *p != NUL
6146 || tab_number == 0)
6147 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006148 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006149 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006150 goto theend;
6151 }
6152 tab_number = tab_number * relative + tabpage_index(curtab);
6153 if (!unaccept_arg0 && relative == -1)
6154 --tab_number;
6155 }
6156 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006157 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006158 }
6159 else if (eap->addr_count > 0)
6160 {
6161 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006162 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006163 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006164 tab_number = 0;
6165 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006166 else
6167 {
6168 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006169 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006170 {
6171 --tab_number;
6172 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006173 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006174 }
6175 }
6176 }
6177 else
6178 {
6179 switch (eap->cmdidx)
6180 {
6181 case CMD_tabnext:
6182 tab_number = tabpage_index(curtab) + 1;
6183 if (tab_number > LAST_TAB_NR)
6184 tab_number = 1;
6185 break;
6186 case CMD_tabmove:
6187 tab_number = LAST_TAB_NR;
6188 break;
6189 default:
6190 tab_number = tabpage_index(curtab);
6191 }
6192 }
6193
6194theend:
6195 return tab_number;
6196}
6197
6198/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006199 * ":tabclose": close current tab page, unless it is the last one.
6200 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 */
6202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006203ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006205 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006206 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006207
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006208 if (cmdwin_type != 0)
6209 cmdwin_result = K_IGNORE;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006210 else if (first_tabpage->tp_next == NULL)
6211 emsg(_(e_cannot_close_last_tab_page));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006212 else
Martin Tournoij7904fa42022-10-04 16:28:45 +01006213 {
6214 tab_number = get_tabpage_arg(eap);
6215 if (eap->errmsg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006217 tp = find_tabpage(tab_number);
6218 if (tp == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006219 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006220 beep_flush();
6221 return;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006222 }
Martin Tournoij7904fa42022-10-04 16:28:45 +01006223 if (tp != curtab)
6224 {
6225 tabpage_close_other(tp, eap->forceit);
6226 return;
6227 }
6228 else if (!text_locked() && !curbuf_locked())
6229 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 }
Martin Tournoij7904fa42022-10-04 16:28:45 +01006231 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006232}
6233
6234/*
6235 * ":tabonly": close all tab pages except the current one
6236 */
6237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006238ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006239{
6240 tabpage_T *tp;
6241 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006242 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006243
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006244 if (cmdwin_type != 0)
6245 cmdwin_result = K_IGNORE;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006246 else if (first_tabpage->tp_next == NULL)
6247 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006248 else
Martin Tournoij7904fa42022-10-04 16:28:45 +01006249 {
6250 tab_number = get_tabpage_arg(eap);
6251 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006252 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006253 goto_tabpage(tab_number);
6254 // Repeat this up to a 1000 times, because autocommands may
6255 // mess up the lists.
6256 for (done = 0; done < 1000; ++done)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006257 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006258 FOR_ALL_TABPAGES(tp)
6259 if (tp->tp_topframe != topframe)
6260 {
6261 tabpage_close_other(tp, eap->forceit);
6262 // if we failed to close it quit
6263 if (valid_tabpage(tp))
6264 done = 1000;
6265 // start over, "tp" is now invalid
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006266 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006267 }
6268 if (first_tabpage->tp_next == NULL)
6269 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006270 }
6271 }
Martin Tournoij7904fa42022-10-04 16:28:45 +01006272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
6275/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006276 * Close the current tab page.
6277 */
6278 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006279tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006280{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006281 // First close all the windows but the current one. If that worked then
6282 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006283 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006284 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006285 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006286 ex_win_close(forceit, curwin, NULL);
6287# ifdef FEAT_GUI
6288 need_mouse_correct = TRUE;
6289# endif
6290}
6291
6292/*
6293 * Close tab page "tp", which is not the current tab page.
6294 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006295 * Also takes care of the tab pages line disappearing when closing the
6296 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006297 */
6298 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006299tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006300{
6301 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006302 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006303
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006304 // Limit to 1000 windows, autocommands may add a window while we close
6305 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006306 while (++done < 1000)
6307 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006308 wp = tp->tp_firstwin;
6309 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006311 // Autocommands may delete the tab page under our fingers and we may
6312 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006313 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006314 break;
6315 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006316
Bram Moolenaar29323592016-07-24 22:04:11 +02006317 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006318}
6319
6320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 * ":only".
6322 */
6323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006324ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006326 win_T *wp;
6327 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328# ifdef FEAT_GUI
6329 need_mouse_correct = TRUE;
6330# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006331 if (eap->addr_count > 0)
6332 {
6333 wnr = eap->line2;
6334 for (wp = firstwin; --wnr > 0; )
6335 {
6336 if (wp->w_next == NULL)
6337 break;
6338 else
6339 wp = wp->w_next;
6340 }
6341 win_goto(wp);
6342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 close_others(TRUE, eap->forceit);
6344}
6345
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006347ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006349 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006350 if (!eap->skip)
6351 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006352#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006353 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006354#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006355 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006356 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006357 else
6358 {
6359 int winnr = 0;
6360 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006361
Bram Moolenaar2256c992016-11-15 21:17:07 +01006362 FOR_ALL_WINDOWS(win)
6363 {
6364 winnr++;
6365 if (winnr == eap->line2)
6366 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006367 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006368 if (win == NULL)
6369 win = lastwin;
6370 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 }
6373}
6374
6375/*
6376 * ":stop" and ":suspend": Suspend Vim.
6377 */
dbivolaruab16ad32021-12-29 19:41:47 +00006378 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006379ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
6381 /*
6382 * Disallow suspending for "rvim".
6383 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006384 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 {
6386 if (!eap->forceit)
6387 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006388 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 windgoto((int)Rows - 1, 0);
6390 out_char('\n');
6391 out_flush();
6392 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006393 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006394 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006395 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006397 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006399 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006401 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006402 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 }
6404}
6405
6406/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006407 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 */
6409 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006410ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006412#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006413 if (not_in_vim9(eap) == FAIL)
6414 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if (cmdwin_type != 0)
6417 {
6418 cmdwin_result = Ctrl_C;
6419 return;
6420 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006421 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006422 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006423 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006424 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006425 return;
6426 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006427
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006429 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 */
6431 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6432 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006433
6434 // Write the buffer for ":wq" or when it was changed.
6435 // Trigger QuitPre and ExitPre.
6436 // Check if we can exit now, after autocommands have changed things.
6437 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6438 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006440 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 {
6442 not_exiting();
6443 }
6444 else
6445 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006446 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006448 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449# ifdef FEAT_GUI
6450 need_mouse_correct = TRUE;
6451# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006452 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006453 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 }
6455}
6456
6457/*
6458 * ":print", ":list", ":number".
6459 */
6460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006461ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006463 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006464 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006467 for ( ;!got_int; ui_breakcheck())
6468 {
6469 print_line(eap->line1,
6470 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6471 || (eap->flags & EXFLAG_NR)),
6472 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6473 if (++eap->line1 > eap->line2)
6474 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006475 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006476 }
6477 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006478 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006479 curwin->w_cursor.lnum = eap->line2;
6480 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 }
6482
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
6486#ifdef FEAT_BYTEOFF
6487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006488ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490 goto_byte(eap->line2);
6491}
6492#endif
6493
6494/*
6495 * ":shell".
6496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006498ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
6500 do_shell(NULL, 0);
6501}
6502
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006503#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006505static int drop_busy = FALSE;
6506static int drop_filec;
6507static char_u **drop_filev = NULL;
6508static int drop_split;
6509static void (*drop_callback)(void *);
6510static void *drop_cookie;
6511
6512 static void
6513handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514{
6515 exarg_T ea;
6516 int save_msg_scroll = msg_scroll;
6517
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006518 // Setting the argument list may cause screen updates and being called
6519 // recursively. Avoid that by setting drop_busy.
6520 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006522 // Check whether the current buffer is changed. If so, we will need
6523 // to split the current window or data could be lost.
6524 // We don't need to check if the 'hidden' option is set, as in this
6525 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006526 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
6528 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006529 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 --emsg_off;
6531 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006532 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 if (win_split(0, 0) == FAIL)
6535 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006536 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006538 // When splitting the window, create a new alist. Otherwise the
6539 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 alist_unlink(curwin->w_alist);
6541 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 }
6543
6544 /*
6545 * Set up the new argument list.
6546 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006547 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549 /*
6550 * Move to the first file.
6551 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006552 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006553 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 ea.cmd = (char_u *)"next";
6555 do_argfile(&ea, 0);
6556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006557 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6558 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 need_start_insertmode = FALSE;
6560
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006561 // Restore msg_scroll, otherwise a following command may cause scrolling
6562 // unexpectedly. The screen will be redrawn by the caller, thus
6563 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006565
6566 if (drop_callback != NULL)
6567 drop_callback(drop_cookie);
6568
6569 drop_filev = NULL;
6570 drop_busy = FALSE;
6571}
6572
6573/*
6574 * Handle a file drop. The code is here because a drop is *nearly* like an
6575 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006576 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006577 * code is very similar to :args and hence needs access to a lot of the static
6578 * functions in this file.
6579 *
6580 * The "filev" list must have been allocated using alloc(), as should each item
6581 * in the list. This function takes over responsibility for freeing the "filev"
6582 * list.
6583 */
6584 void
6585handle_drop(
6586 int filec, // the number of files dropped
6587 char_u **filev, // the list of files dropped
6588 int split, // force splitting the window
6589 void (*callback)(void *), // to be called after setting the argument
6590 // list
6591 void *cookie) // argument for "callback" (allocated)
6592{
6593 // Cannot handle recursive drops, finish the pending one.
6594 if (drop_busy)
6595 {
6596 FreeWild(filec, filev);
6597 vim_free(cookie);
6598 return;
6599 }
6600
6601 // When calling handle_drop() more than once in a row we only use the last
6602 // one.
6603 if (drop_filev != NULL)
6604 {
6605 FreeWild(drop_filec, drop_filev);
6606 vim_free(drop_cookie);
6607 }
6608
6609 drop_filec = filec;
6610 drop_filev = filev;
6611 drop_split = split;
6612 drop_callback = callback;
6613 drop_cookie = cookie;
6614
6615 // Postpone this when:
6616 // - editing the command line
6617 // - not possible to change the current buffer
6618 // - updating the screen
6619 // As it may change buffers and window structures that are in use and cause
6620 // freed memory to be used.
6621 if (text_locked() || curbuf_locked() || updating_screen)
6622 return;
6623
6624 handle_drop_internal();
6625}
6626
6627/*
6628 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6629 * reset: Handle a postponed drop.
6630 */
6631 void
6632handle_any_postponed_drop(void)
6633{
6634 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006635 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006636 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637}
6638#endif
6639
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 * ":preserve".
6642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006644ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006646 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 ml_preserve(curbuf, TRUE);
6648}
6649
6650/*
6651 * ":recover".
6652 */
6653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006654ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006656 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006658 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6659 | CCGD_MULTWIN
6660 | (eap->forceit ? CCGD_FORCEIT : 0)
6661 | CCGD_EXCMD)
6662
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 && (*eap->arg == NUL
6664 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006665 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 recoverymode = FALSE;
6667}
6668
6669/*
6670 * Command modifier used in a wrong way.
6671 */
6672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006673ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006675 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676}
6677
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678/*
6679 * :sview [+command] file split window with new file, read-only
6680 * :split [[+command] file] split window with current or new file
6681 * :vsplit [[+command] file] split window vertically with current or new file
6682 * :new [[+command] file] split window with no or new file
6683 * :vnew [[+command] file] split vertically window with no or new file
6684 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006685 *
6686 * :tabedit open new Tab page with empty window
6687 * :tabedit [+command] file open new Tab page and edit "file"
6688 * :tabnew [[+command] file] just like :tabedit
6689 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 */
6691 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006692ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006694 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006696#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006697 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006698 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006699#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006700 int use_tab = eap->cmdidx == CMD_tabedit
6701 || eap->cmdidx == CMD_tabfind
6702 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006704 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006705 return;
6706
Bram Moolenaar4033c552017-09-16 20:54:51 +02006707#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
Bram Moolenaar4033c552017-09-16 20:54:51 +02006711#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006712 // A ":split" in the quickfix window works like ":new". Don't want two
6713 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006714 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {
6716 if (eap->cmdidx == CMD_split)
6717 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 if (eap->cmdidx == CMD_vsplit)
6719 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006723 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 {
6725 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6726 FNAME_MESS, TRUE, curbuf->b_ffname);
6727 if (fname == NULL)
6728 goto theend;
6729 eap->arg = fname;
6730 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006731# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006732 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 && eap->cmdidx != CMD_new)
6735 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006736 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006737# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006738 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006739# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006740 au_has_group((char_u *)"FileExplorer"))
6741 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006742 // No browsing supported but we do have the file explorer:
6743 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006744 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006745 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006746 }
6747 else
6748 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006749 fname = do_browse(0, (char_u *)(use_tab
6750 ? _("Edit File in new tab page")
6751 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006753 if (fname == NULL)
6754 goto theend;
6755 eap->arg = fname;
6756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006758 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006761 /*
6762 * Either open new tab page or split the window.
6763 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006764 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006765 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006766 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006767 : eap->addr_count == 0 ? 0
6768 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006769 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006770 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006772 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006773 if (curwin != old_curwin
6774 && win_valid(old_curwin)
6775 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006776 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006777 old_curwin->w_alt_fnum = curbuf->b_fnum;
6778 }
6779 }
6780 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6782 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006783 // Reset 'scrollbind' when editing another file, but keep it when
6784 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006785 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006786 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 else
6788 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 do_exedit(eap, old_curwin);
6790 }
6791
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006792# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006793 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006794# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796theend:
6797 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006799
6800/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006801 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006802 */
6803 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006804tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006805{
6806 exarg_T ea;
6807
Bram Moolenaara80faa82020-04-12 19:37:17 +02006808 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006809 ea.cmdidx = CMD_tabnew;
6810 ea.cmd = (char_u *)"tabn";
6811 ea.arg = (char_u *)"";
6812 ex_splitview(&ea);
6813}
6814
6815/*
6816 * :tabnext command
6817 */
6818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006819ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006820{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006821 int tab_number;
6822
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006823 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006824 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006825 switch (eap->cmdidx)
6826 {
6827 case CMD_tabfirst:
6828 case CMD_tabrewind:
6829 goto_tabpage(1);
6830 break;
6831 case CMD_tablast:
6832 goto_tabpage(9999);
6833 break;
6834 case CMD_tabprevious:
6835 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006836 if (eap->arg && *eap->arg != NUL)
6837 {
6838 char_u *p = eap->arg;
6839 char_u *p_save = p;
6840
6841 tab_number = getdigits(&p);
6842 if (p == p_save || *p_save == '-' || *p != NUL
6843 || tab_number == 0)
6844 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006845 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006846 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006847 return;
6848 }
6849 }
6850 else
6851 {
6852 if (eap->addr_count == 0)
6853 tab_number = 1;
6854 else
6855 {
6856 tab_number = eap->line2;
6857 if (tab_number < 1)
6858 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006859 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006860 return;
6861 }
6862 }
6863 }
6864 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006865 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006866 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006867 tab_number = get_tabpage_arg(eap);
6868 if (eap->errmsg == NULL)
6869 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006870 break;
6871 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006872}
6873
6874/*
6875 * :tabmove command
6876 */
6877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006878ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006879{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006880 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006881
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006882 tab_number = get_tabpage_arg(eap);
6883 if (eap->errmsg == NULL)
6884 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006885}
6886
6887/*
6888 * :tabs command: List tabs and their contents.
6889 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006890 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006891ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006892{
6893 tabpage_T *tp;
6894 win_T *wp;
6895 int tabcount = 1;
6896
6897 msg_start();
6898 msg_scroll = TRUE;
6899 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6900 {
6901 msg_putchar('\n');
6902 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006903 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006904 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006905 ui_breakcheck();
6906
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006907 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006908 wp = firstwin;
6909 else
6910 wp = tp->tp_firstwin;
6911 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6912 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006913 msg_putchar('\n');
6914 msg_putchar(wp == curwin ? '>' : ' ');
6915 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006916 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6917 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006918 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006919 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006920 else
6921 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6922 IObuff, IOSIZE, TRUE);
6923 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006924 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006925 ui_breakcheck();
6926 }
6927 }
6928}
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930/*
6931 * ":mode": Set screen mode.
6932 * If no argument given, just get the screen size and redraw.
6933 */
6934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006935ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
6937 if (*eap->arg == NUL)
6938 shell_resized();
6939 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006940 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941}
6942
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943/*
6944 * ":resize".
6945 * set, increment or decrement current window height
6946 */
6947 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006948ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
6950 int n;
6951 win_T *wp = curwin;
6952
6953 if (eap->addr_count > 0)
6954 {
6955 n = eap->line2;
6956 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6957 ;
6958 }
6959
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006960# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006962# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006964 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {
6966 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006967 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006968 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006970 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 }
6972 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {
6974 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006975 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006976 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006978 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
6980}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
6982/*
6983 * ":find [+command] <file>" command.
6984 */
6985 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006986ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 char_u *fname;
6989 int count;
6990
6991 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6992 TRUE, curbuf->b_ffname);
6993 if (eap->addr_count > 0)
6994 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006995 // Repeat finding the file "count" times. This matters when it
6996 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 count = eap->line2;
6998 while (fname != NULL && --count > 0)
6999 {
7000 vim_free(fname);
7001 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7002 FALSE, curbuf->b_ffname);
7003 }
7004 }
7005
7006 if (fname != NULL)
7007 {
7008 eap->arg = fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 do_exedit(eap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 vim_free(fname);
7011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012}
7013
7014/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007015 * ":open" simulation: for now just work like ":visual".
7016 */
7017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007018ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007019{
7020 regmatch_T regmatch;
7021 char_u *p;
7022
Bram Moolenaar65088802021-03-13 21:07:21 +01007023#ifdef FEAT_EVAL
7024 if (not_in_vim9(eap) == FAIL)
7025 return;
7026#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007027 curwin->w_cursor.lnum = eap->line2;
7028 beginline(BL_SOL | BL_FIX);
7029 if (*eap->arg == '/')
7030 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007031 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007032 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007033 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007034 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007035 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007036 if (regmatch.regprog != NULL)
7037 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007038 // make a copy of the line, when searching for a mark it might be
7039 // flushed
7040 char_u *line = vim_strsave(ml_get_curline());
7041
Bram Moolenaardf177f62005-02-22 08:39:57 +00007042 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007043 if (vim_regexec(&regmatch, line, (colnr_T)0))
7044 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007045 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007046 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007047 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007048 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007049 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007050 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007051 eap->arg += STRLEN(eap->arg);
7052 }
7053 check_cursor();
7054
7055 eap->cmdidx = CMD_visual;
7056 do_exedit(eap, NULL);
7057}
7058
7059/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007060 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 */
7062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007063ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064{
7065 do_exedit(eap, NULL);
7066}
7067
7068/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007069 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007072do_exedit(
7073 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007074 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007078 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007080 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7081 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007082 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 /*
7084 * ":vi" command ends Ex mode.
7085 */
7086 if (exmode_active && (eap->cmdidx == CMD_visual
7087 || eap->cmdidx == CMD_view))
7088 {
7089 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007090 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007092 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007093 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007094 if (global_busy)
7095 {
7096 int rd = RedrawingDisabled;
7097 int nwr = no_wait_return;
7098 int ms = msg_scroll;
7099#ifdef FEAT_GUI
7100 int he = hold_gui_events;
7101#endif
7102
7103 if (eap->nextcmd != NULL)
7104 {
7105 stuffReadbuff(eap->nextcmd);
7106 eap->nextcmd = NULL;
7107 }
7108
7109 if (exmode_was != EXMODE_VIM)
7110 settmode(TMODE_RAW);
7111 RedrawingDisabled = 0;
7112 no_wait_return = 0;
7113 need_wait_return = FALSE;
7114 msg_scroll = 0;
7115#ifdef FEAT_GUI
7116 hold_gui_events = 0;
7117#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007118 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007119 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007120
7121 main_loop(FALSE, TRUE);
7122
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007123 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007124 RedrawingDisabled = rd;
7125 no_wait_return = nwr;
7126 msg_scroll = ms;
7127#ifdef FEAT_GUI
7128 hold_gui_events = he;
7129#endif
7130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 }
7134
7135 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007136 || eap->cmdidx == CMD_tabnew
7137 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007138 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007140 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 setpcmark();
7142 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007143 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7144 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007146 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 || *eap->arg != NUL
7148#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007149 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150#endif
7151 )
7152 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007153 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7154 // Only ":edit" or ":script" can bring us here, others are stopped
7155 // earlier.
7156 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007157 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007158
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 n = readonlymode;
7160 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7161 readonlymode = TRUE;
7162 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007163 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7164 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007165 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7166 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7168 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007169 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7171 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7172 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007173 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007175 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007176 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007177 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7178 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007179 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007181 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 if (old_curwin != NULL)
7183 {
7184 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007185 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007187#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007188 cleanup_T cs;
7189
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007190 // Reset the error/interrupt/exception state here so that
7191 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007192 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007193#endif
7194#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007196#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007197 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007198
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007199#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007200 // Restore the error/interrupt/exception state if not
7201 // discarded by a new aborting error, interrupt, or
7202 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007203 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 }
7206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 }
7208 else if (readonlymode && curbuf->b_nwindows == 1)
7209 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007210 // When editing an already visited buffer, 'readonly' won't be set
7211 // but the previous value is kept. With ":view" and ":sview" we
7212 // want the file to be readonly, except when another window is
7213 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 curbuf->b_p_ro = TRUE;
7215 }
7216 readonlymode = n;
7217 }
7218 else
7219 {
7220 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007221 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 if (n != curwin->w_arg_idx_invalid)
7225 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
7227
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 /*
7229 * if ":split file" worked, set alternate file name in old window to new
7230 * file
7231 */
7232 if (old_curwin != NULL
7233 && *eap->arg != NUL
7234 && curwin != old_curwin
7235 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007236 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007237 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240 ex_no_reprint = TRUE;
7241}
7242
7243#ifndef FEAT_GUI
7244/*
7245 * ":gui" and ":gvim" when there is no GUI.
7246 */
7247 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007248ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007250 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251}
7252#endif
7253
Bram Moolenaar4f974752019-02-17 17:44:42 +01007254#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007256ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
7258 gui_make_tearoff(eap->arg);
7259}
7260#endif
7261
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007262#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7263 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007265ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007267# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7268 if (gui.in_use)
7269 gui_make_popup(eap->arg, eap->forceit);
7270# ifdef FEAT_TERM_POPUP_MENU
7271 else
7272# endif
7273# endif
7274# ifdef FEAT_TERM_POPUP_MENU
7275 pum_make_popup(eap->arg, eap->forceit);
7276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277}
7278#endif
7279
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007281ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282{
7283 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007284 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007286 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287}
7288
7289/*
7290 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7291 * offset.
7292 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007295ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007298 win_T *save_curwin = curwin;
7299 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 long topline;
7301 long y;
7302 linenr_T old_linenr = curwin->w_cursor.lnum;
7303
7304 setpcmark();
7305
7306 /*
7307 * determine max topline
7308 */
7309 if (curwin->w_p_scb)
7310 {
7311 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007312 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 {
7314 if (wp->w_p_scb && wp->w_buffer)
7315 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007316 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 if (topline > y)
7318 topline = y;
7319 }
7320 }
7321 if (topline < 1)
7322 topline = 1;
7323 }
7324 else
7325 {
7326 topline = 1;
7327 }
7328
7329
7330 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007331 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007333 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 {
7335 if (curwin->w_p_scb)
7336 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007337 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 y = topline - curwin->w_topline;
7339 if (y > 0)
7340 scrollup(y, TRUE);
7341 else
7342 scrolldown(-y, TRUE);
7343 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007344 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 }
7348 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007349 curwin = save_curwin;
7350 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 if (curwin->w_p_scb)
7352 {
7353 did_syncbind = TRUE;
7354 checkpcmark();
7355 if (old_linenr != curwin->w_cursor.lnum)
7356 {
7357 char_u ctrl_o[2];
7358
7359 ctrl_o[0] = Ctrl_O;
7360 ctrl_o[1] = 0;
7361 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7362 }
7363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364}
7365
7366
7367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007368ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007370 int i;
7371 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7372 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007374 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 do_bang(1, eap, FALSE, FALSE, TRUE);
7376 else
7377 {
7378 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7379 return;
7380
7381#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007382 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 {
7384 char_u *browseFile;
7385
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007386 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 NULL, NULL, NULL, curbuf);
7388 if (browseFile != NULL)
7389 {
7390 i = readfile(browseFile, NULL,
7391 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7392 vim_free(browseFile);
7393 }
7394 else
7395 i = OK;
7396 }
7397 else
7398#endif
7399 if (*eap->arg == NUL)
7400 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007401 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 return;
7403 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7404 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7405 }
7406 else
7407 {
7408 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7409 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7410 i = readfile(eap->arg, NULL,
7411 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7412
7413 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007414 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007416#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 if (!aborting())
7418#endif
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007419 semsg(_(e_cant_open_file_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 }
7421 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007422 {
7423 if (empty && exmode_active)
7424 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007425 // Delete the empty line that remains. Historically ex does
7426 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007427 if (eap->line2 == 0)
7428 lnum = curbuf->b_ml.ml_line_count;
7429 else
7430 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007431 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007432 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007433 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007434 if (curwin->w_cursor.lnum > 1
7435 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007436 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007437 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007438 }
7439 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007440 redraw_curbuf_later(UPD_VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 }
7443}
7444
Bram Moolenaarea408852005-06-25 22:49:46 +00007445static char_u *prev_dir = NULL;
7446
7447#if defined(EXITFREE) || defined(PROTO)
7448 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007449free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007450{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007451 VIM_CLEAR(prev_dir);
7452 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007453}
7454#endif
7455
Bram Moolenaarf4258302013-06-02 18:20:17 +02007456/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007457 * Get the previous directory for the given chdir scope.
7458 */
7459 static char_u *
7460get_prevdir(cdscope_T scope)
7461{
7462 if (scope == CDSCOPE_WINDOW)
7463 return curwin->w_prevdir;
7464 else if (scope == CDSCOPE_TABPAGE)
7465 return curtab->tp_prevdir;
7466 return prev_dir;
7467}
7468
7469/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007470 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007471 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7472 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007473 */
7474 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007475post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007476{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007477 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007478 // Clear tab local directory for both :cd and :tcd
7479 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007480 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007481 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007482 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007483 char_u *pdir = get_prevdir(scope);
7484
7485 // If still in the global directory, need to remember current
7486 // directory as the global directory.
7487 if (globaldir == NULL && pdir != NULL)
7488 globaldir = vim_strsave(pdir);
7489
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007490 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007491 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007492 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007493 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007494 curtab->tp_localdir = vim_strsave(NameBuff);
7495 else
7496 curwin->w_localdir = vim_strsave(NameBuff);
7497 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007498 }
7499 else
7500 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007501 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007502 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007503 }
7504
zeertzjq64be6aa2021-11-19 11:59:08 +00007505 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007506 shorten_fnames(TRUE);
7507}
7508
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007509/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007510 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7511 */
7512 void
7513trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7514{
7515#ifdef FEAT_EVAL
7516 dict_T *v_event;
7517 save_v_event_T save_v_event;
7518
7519 v_event = get_v_event(&save_v_event);
7520 (void)dict_add_string(v_event, "directory", new_dir);
7521 dict_set_items_ro(v_event);
7522#endif
7523 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7524#ifdef FEAT_EVAL
7525 restore_v_event(v_event, &save_v_event);
7526#endif
7527}
7528
7529/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007530 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007531 * chdir() function.
7532 * scope == CDSCOPE_WINDOW: changes the window-local directory
7533 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7534 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007535 * Returns TRUE if the directory is successfully changed.
7536 */
7537 int
7538changedir_func(
7539 char_u *new_dir,
7540 int forceit,
7541 cdscope_T scope)
7542{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007543 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007544 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007545 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007546 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007547 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007548
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007549 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007550 return FALSE;
7551
7552 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7553 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007554 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007555 return FALSE;
7556 }
7557
7558 // ":cd -": Change to previous directory
7559 if (STRCMP(new_dir, "-") == 0)
7560 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007561 pdir = get_prevdir(scope);
7562 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007563 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007564 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007565 return FALSE;
7566 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007567 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007568 }
7569
7570 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007571 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007572 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007573 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007574 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007575
Bakudankun29f3a452021-12-11 12:28:08 +00007576 // For UNIX ":cd" means: go to home directory.
7577 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007578#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007579 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007580#else
7581 if (*new_dir == NUL && p_cdh)
7582#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007583 {
7584 // use NameBuff for home directory name
7585# ifdef VMS
7586 char_u *p;
7587
7588 p = mch_getenv((char_u *)"SYS$LOGIN");
7589 if (p == NULL || *p == NUL) // empty is the same as not set
7590 NameBuff[0] = NUL;
7591 else
7592 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7593# else
7594 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7595# endif
7596 new_dir = NameBuff;
7597 }
zeertzjqf38aad82021-12-30 13:45:57 +00007598 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007599 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7600 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007601 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007602 if (scope == CDSCOPE_WINDOW)
7603 acmd_fname = (char_u *)"window";
7604 else if (scope == CDSCOPE_TABPAGE)
7605 acmd_fname = (char_u *)"tabpage";
7606 else
7607 acmd_fname = (char_u *)"global";
7608 trigger_DirChangedPre(acmd_fname, new_dir);
7609
7610 if (vim_chdir(new_dir))
7611 {
7612 emsg(_(e_command_failed));
7613 vim_free(pdir);
7614 return FALSE;
7615 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007616 }
zeertzjq73257142022-02-02 13:16:37 +00007617
7618 if (scope == CDSCOPE_WINDOW)
7619 pp = &curwin->w_prevdir;
7620 else if (scope == CDSCOPE_TABPAGE)
7621 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007622 else
zeertzjq73257142022-02-02 13:16:37 +00007623 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007624 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007625 *pp = pdir;
7626
7627 post_chdir(scope);
7628
7629 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007630 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007631 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007632 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007633}
Bram Moolenaarea408852005-06-25 22:49:46 +00007634
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007636 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007638 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007639ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007641 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642
7643 new_dir = eap->arg;
7644#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007645 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7646 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 ex_pwd(NULL);
7648 else
7649#endif
7650 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007651 cdscope_T scope = CDSCOPE_GLOBAL;
7652
7653 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7654 scope = CDSCOPE_WINDOW;
7655 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7656 scope = CDSCOPE_TABPAGE;
7657
7658 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007659 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007660 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007661 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 ex_pwd(eap);
7663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
7665}
7666
7667/*
7668 * ":pwd".
7669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007671ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
7673 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7674 {
7675#ifdef BACKSLASH_IN_FILENAME
7676 slash_adjust(NameBuff);
7677#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007678 if (p_verbose > 0)
7679 {
7680 char *context = "global";
7681
Bram Moolenaar05268152021-11-18 18:53:45 +00007682 if (last_chdir_reason != NULL)
7683 context = last_chdir_reason;
7684 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007685 context = "window";
7686 else if (curtab->tp_localdir != NULL)
7687 context = "tabpage";
7688 smsg("[%s] %s", context, (char *)NameBuff);
7689 }
7690 else
7691 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 }
7693 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007694 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695}
7696
7697/*
7698 * ":=".
7699 */
7700 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007701ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007703 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007704 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705}
7706
7707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007708ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007710 int n;
7711 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712
7713 if (cursor_valid())
7714 {
7715 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7716 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007717 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007719
7720 len = eap->line2;
7721 switch (*eap->arg)
7722 {
7723 case 'm': break;
7724 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007725 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007726 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007727
7728 // Hide the cursor if invoked with !
7729 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730}
7731
7732/*
7733 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007734 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 */
7736 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007737do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
Bram Moolenaar52953192019-08-16 10:27:13 +02007739 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007740 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007741# ifdef ELAPSED_FUNC
7742 elapsed_T start_tv;
7743
7744 // Remember at what time we started, so that we know how much longer we
7745 // should wait after waiting for a bit.
7746 ELAPSED_INIT(start_tv);
7747# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007749 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007750 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007751 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007752 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007753
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007754 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007755 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007757 wait_now = msec - done > 1000L ? 1000L : msec - done;
7758#ifdef FEAT_TIMERS
7759 {
7760 long due_time = check_due_timer();
7761
7762 if (due_time > 0 && due_time < wait_now)
7763 wait_now = due_time;
7764 }
7765#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007766#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007767 if (has_any_channel() && wait_now > 20L)
7768 wait_now = 20L;
7769#endif
7770#ifdef FEAT_SOUND
7771 if (has_any_sound_callback() && wait_now > 20L)
7772 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007773#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007774 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007775
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007776#ifdef FEAT_JOB_CHANNEL
7777 if (has_any_channel())
7778 ui_breakcheck_force(TRUE);
7779 else
7780#endif
7781 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007782#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007783 // Process the netbeans and clientserver messages that may have been
7784 // received in the call to ui_breakcheck() when the GUI is in use. This
7785 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007786 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007787#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007788
7789# ifdef ELAPSED_FUNC
7790 // actual time passed
7791 done = ELAPSED_FUNC(start_tv);
7792# else
7793 // guestimate time passed (will actually be more)
7794 done += wait_now;
7795# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007797
7798 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7799 // input buffer, otherwise a following call to input() fails.
7800 if (got_int)
7801 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007802
7803 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007804 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805}
7806
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807/*
7808 * ":winsize" command (obsolete).
7809 */
7810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007811ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 int w, h;
7814 char_u *arg = eap->arg;
7815 char_u *p;
7816
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007817 if (!isdigit(*arg))
7818 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007819 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007820 return;
7821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 w = getdigits(&arg);
7823 arg = skipwhite(arg);
7824 p = arg;
7825 h = getdigits(&arg);
7826 if (*p != NUL && *arg == NUL)
7827 set_shellsize(w, h, TRUE);
7828 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007829 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830}
7831
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007833ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
7835 int xchar = NUL;
7836 char_u *p;
7837
7838 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7839 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007840 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 if (eap->arg[1] == NUL)
7842 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007843 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 return;
7845 }
7846 xchar = eap->arg[1];
7847 p = eap->arg + 2;
7848 }
7849 else
7850 p = eap->arg + 1;
7851
Bram Moolenaar63b91732021-08-05 20:40:03 +02007852 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007854 if (*p != NUL && *p != (
7855#ifdef FEAT_EVAL
7856 in_vim9script() ? '#' :
7857#endif
7858 '"')
7859 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007860 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007861 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007863 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007864 postponed_split_flags = cmdmod.cmod_split;
7865 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7867 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007868 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 }
7870}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871
Bram Moolenaar843ee412004-06-30 16:16:41 +00007872#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873/*
7874 * ":winpos".
7875 */
7876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007877ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878{
7879 int x, y;
7880 char_u *arg = eap->arg;
7881 char_u *p;
7882
7883 if (*arg == NUL)
7884 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007885# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007886# ifdef VIMDLL
7887 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7888 mch_get_winpos(&x, &y) != FAIL)
7889# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007891# else
7892 if (mch_get_winpos(&x, &y) != FAIL)
7893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {
7895 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007896 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 }
7898 else
7899# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007900 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 }
7902 else
7903 {
7904 x = getdigits(&arg);
7905 arg = skipwhite(arg);
7906 p = arg;
7907 y = getdigits(&arg);
7908 if (*p == NUL || *arg != NUL)
7909 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007910 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 return;
7912 }
7913# ifdef FEAT_GUI
7914 if (gui.in_use)
7915 gui_mch_set_winpos(x, y);
7916 else if (gui.starting)
7917 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007918 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 gui_win_x = x;
7920 gui_win_y = y;
7921 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007922# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 else
7924# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007925# endif
7926# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007927 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928# endif
7929# ifdef HAVE_TGETENT
7930 if (*T_CWP)
7931 term_set_winpos(x, y);
7932# endif
7933 }
7934}
7935#endif
7936
7937/*
7938 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7939 */
7940 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007941ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
7943 oparg_T oa;
7944
7945 clear_oparg(&oa);
7946 oa.regname = eap->regname;
7947 oa.start.lnum = eap->line1;
7948 oa.end.lnum = eap->line2;
7949 oa.line_count = eap->line2 - eap->line1 + 1;
7950 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007952 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {
7954 setpcmark();
7955 curwin->w_cursor.lnum = eap->line1;
7956 beginline(BL_SOL | BL_FIX);
7957 }
7958
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007959 if (VIsual_active)
7960 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007961
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 switch (eap->cmdidx)
7963 {
7964 case CMD_delete:
7965 oa.op_type = OP_DELETE;
7966 op_delete(&oa);
7967 break;
7968
7969 case CMD_yank:
7970 oa.op_type = OP_YANK;
7971 (void)op_yank(&oa, FALSE, TRUE);
7972 break;
7973
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007974 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007975 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007977 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7978#else
7979 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007981 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 oa.op_type = OP_RSHIFT;
7983 else
7984 oa.op_type = OP_LSHIFT;
7985 op_shift(&oa, FALSE, eap->amount);
7986 break;
7987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007989 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990}
7991
7992/*
7993 * ":put".
7994 */
7995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007996ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007998 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 if (eap->line2 == 0)
8000 {
8001 eap->line2 = 1;
8002 eap->forceit = TRUE;
8003 }
8004 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008005 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008006 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008007 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008}
8009
8010/*
8011 * Handle ":copy" and ":move".
8012 */
8013 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008014ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015{
8016 long n;
8017
Bram Moolenaar491799b2020-08-01 19:23:43 +02008018#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008019 if (not_in_vim9(eap) == FAIL)
8020 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008021#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008022 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008023 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 {
8025 eap->nextcmd = NULL;
8026 return;
8027 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008028 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029
8030 /*
8031 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8032 */
8033 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8034 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008035 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 return;
8037 }
8038
8039 if (eap->cmdidx == CMD_move)
8040 {
8041 if (do_move(eap->line1, eap->line2, n) == FAIL)
8042 return;
8043 }
8044 else
8045 ex_copy(eap->line1, eap->line2, n);
8046 u_clearline();
8047 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008048 ex_may_print(eap);
8049}
8050
8051/*
8052 * Print the current line if flags were given to the Ex command.
8053 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008054 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008055ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008056{
8057 if (eap->flags != 0)
8058 {
8059 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8060 (eap->flags & EXFLAG_LIST));
8061 ex_no_reprint = TRUE;
8062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063}
8064
8065/*
8066 * ":smagic" and ":snomagic".
8067 */
8068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008069ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008071 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008073 magic_overruled = eap->cmdidx == CMD_smagic
8074 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008075 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008076 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077}
8078
8079/*
8080 * ":join".
8081 */
8082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008083ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 curwin->w_cursor.lnum = eap->line1;
8086 if (eap->line1 == eap->line2)
8087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008088 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 return;
8090 if (eap->line2 == curbuf->b_ml.ml_line_count)
8091 {
8092 beep_flush();
8093 return;
8094 }
8095 ++eap->line2;
8096 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008097 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008099 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100}
8101
8102/*
8103 * ":[addr]@r" or ":[addr]*r": execute register
8104 */
8105 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008106ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008109 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110
8111 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008112 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113
8114#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008115 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116#endif
8117
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008118 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 c = *eap->arg;
8120 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8121 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008122 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008123 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8124 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008125 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 else
8129 {
8130 int save_efr = exec_from_reg;
8131
8132 exec_from_reg = TRUE;
8133
8134 /*
8135 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008136 * Continue until the stuff buffer is empty and all added characters
8137 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008139 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8141
8142 exec_from_reg = save_efr;
8143 }
8144}
8145
8146/*
8147 * ":!".
8148 */
8149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008150ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
8152 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8153}
8154
8155/*
8156 * ":undo".
8157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008159ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008161 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008162 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008163 else
8164 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165}
8166
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008167#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008169ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008170{
8171 char_u hash[UNDO_HASH_SIZE];
8172
8173 u_compute_hash(hash);
8174 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8175}
8176
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008177 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008178ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008179{
8180 char_u hash[UNDO_HASH_SIZE];
8181
8182 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008183 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008184}
8185#endif
8186
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187/*
8188 * ":redo".
8189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008191ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
8193 u_redo(1);
8194}
8195
8196/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008197 * ":earlier" and ":later".
8198 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008200ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008201{
8202 long count = 0;
8203 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008204 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008205 char_u *p = eap->arg;
8206
8207 if (*p == NUL)
8208 count = 1;
8209 else if (isdigit(*p))
8210 {
8211 count = getdigits(&p);
8212 switch (*p)
8213 {
8214 case 's': ++p; sec = TRUE; break;
8215 case 'm': ++p; sec = TRUE; count *= 60; break;
8216 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008217 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8218 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008219 }
8220 }
8221
8222 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008223 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008224 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008225 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8226 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008227}
8228
8229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 * ":redir": start/stop redirection.
8231 */
8232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008233ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234{
8235 char *mode;
8236 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008237 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238
Bram Moolenaarba768492016-07-08 15:32:54 +02008239#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008240 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008241 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008242 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008243 return;
8244 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008245#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008246
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 if (STRICMP(eap->arg, "END") == 0)
8248 close_redir();
8249 else
8250 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008251 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008253 ++arg;
8254 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008256 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 mode = "a";
8258 }
8259 else
8260 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008261 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262
8263 close_redir();
8264
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008265 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008266 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 if (fname == NULL)
8268 return;
8269#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008270 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {
8272 char_u *browseFile;
8273
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008274 browseFile = do_browse(BROWSE_SAVE,
8275 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008276 fname, NULL, NULL,
8277 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008279 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 vim_free(fname);
8281 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008282 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 }
8284#endif
8285
8286 redir_fd = open_exfile(fname, eap->forceit, mode);
8287 vim_free(fname);
8288 }
8289#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008290 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008292 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008294 ++arg;
8295 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008297 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008298 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008300 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008302 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008303 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008304 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008305 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008307 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008308 if (*arg == '>')
8309 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008310 // Make register empty when not using @A-@Z and the
8311 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008312 if (*arg == NUL && !isupper(redir_reg))
8313 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008315 }
8316 if (*arg != NUL)
8317 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008318 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008319 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008320 }
8321 }
8322 else if (*arg == '=' && arg[1] == '>')
8323 {
8324 int append;
8325
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008326 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008327 close_redir();
8328 arg += 2;
8329
8330 if (*arg == '>')
8331 {
8332 ++arg;
8333 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 }
8335 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008336 append = FALSE;
8337
8338 if (var_redir_start(skipwhite(arg), append) == OK)
8339 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 }
8341#endif
8342
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008343 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008346 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008348
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008349 // Make sure redirection is not off. Can happen for cmdline completion
8350 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008351 if (redir_fd != NULL
8352#ifdef FEAT_EVAL
8353 || redir_reg || redir_vname
8354#endif
8355 )
8356 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357}
8358
8359/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008360 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008362 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008363ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008365 redraw_cmd(eap->forceit);
8366}
8367
8368/*
8369 * ":redraw": force redraw, with clear if "clear" is TRUE.
8370 */
8371 void
8372redraw_cmd(int clear)
8373{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 int r = RedrawingDisabled;
8375 int p = p_lz;
8376
8377 RedrawingDisabled = 0;
8378 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008379 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008381 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 if (need_maketitle)
8383 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008384#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8385# ifdef VIMDLL
8386 if (!gui.in_use)
8387# endif
8388 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 RedrawingDisabled = r;
8391 p_lz = p;
8392
Bram Moolenaar5017c662022-04-07 18:06:08 +01008393 // After drawing the statusline screen_attr may still be set.
8394 screen_stop_highlight();
8395
Bram Moolenaara2a89732022-08-31 14:46:18 +01008396 // Reset msg_didout, so that a message that's there is overwritten.
8397 msg_didout = FALSE;
8398 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008400 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008401 need_wait_return = FALSE;
8402
Bram Moolenaara653e532022-04-19 11:38:24 +01008403 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008404 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008405 redrawcmdline();
8406
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 out_flush();
8408}
8409
8410/*
8411 * ":redrawstatus": force redraw of status line(s)
8412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008414ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 int r = RedrawingDisabled;
8417 int p = p_lz;
8418
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 if (eap->forceit)
8420 status_redraw_all();
8421 else
8422 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008423 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008424 return; // redraw later
8425
8426 RedrawingDisabled = 0;
8427 p_lz = FALSE;
zeertzjq320d9102022-09-20 17:12:13 +01008428 if (State & MODE_CMDLINE)
8429 redraw_statuslines();
8430 else
8431 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 RedrawingDisabled = r;
8433 p_lz = p;
8434 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435}
8436
Bram Moolenaare12bab32019-01-08 22:02:56 +01008437/*
8438 * ":redrawtabline": force redraw of the tabline
8439 */
8440 static void
8441ex_redrawtabline(exarg_T *eap UNUSED)
8442{
8443 int r = RedrawingDisabled;
8444 int p = p_lz;
8445
8446 RedrawingDisabled = 0;
8447 p_lz = FALSE;
8448
8449 draw_tabline();
8450
8451 RedrawingDisabled = r;
8452 p_lz = p;
8453 out_flush();
8454}
8455
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008457close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458{
8459 if (redir_fd != NULL)
8460 {
8461 fclose(redir_fd);
8462 redir_fd = NULL;
8463 }
8464#ifdef FEAT_EVAL
8465 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008466 if (redir_vname)
8467 {
8468 var_redir_stop();
8469 redir_vname = 0;
8470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471#endif
8472}
8473
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008474#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008475 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008476vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008477{
8478 if (vim_mkdir(name, prot) != 0)
8479 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008480 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008481 return FAIL;
8482 }
8483 return OK;
8484}
8485#endif
8486
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487/*
8488 * Open a file for writing for an Ex command, with some checks.
8489 * Return file descriptor, or NULL on failure.
8490 */
8491 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008492open_exfile(
8493 char_u *fname,
8494 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008495 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496{
8497 FILE *fd;
8498
8499#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008500 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 if (mch_isdir(fname))
8502 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008503 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 return NULL;
8505 }
8506#endif
8507 if (!forceit && *mode != 'a' && vim_fexists(fname))
8508 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008509 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 return NULL;
8511 }
8512
8513 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008514 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515
8516 return fd;
8517}
8518
8519/*
8520 * ":mark" and ":k".
8521 */
8522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008523ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524{
8525 pos_T pos;
8526
Bram Moolenaar10b94212021-02-19 21:42:57 +01008527#ifdef FEAT_EVAL
8528 if (not_in_vim9(eap) == FAIL)
8529 return;
8530#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008531 if (*eap->arg == NUL) // No argument?
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008532 emsg(_(e_argument_required));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008533 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar74409f62022-01-01 15:58:22 +00008534 semsg(_(e_trailing_characters_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 else
8536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008537 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 curwin->w_cursor.lnum = eap->line2;
8539 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008540 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaar6d057012021-12-31 18:49:43 +00008541 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008542 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 }
8544}
8545
8546/*
8547 * Update w_topline, w_leftcol and the cursor position.
8548 */
8549 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008550update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008552 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 update_topline();
8554 if (!curwin->w_p_wrap)
8555 validate_cursor();
8556 update_curswant();
8557}
8558
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008560 * Save the current State and go to Normal mode.
8561 * Return TRUE if the typeahead could be saved.
8562 */
8563 int
8564save_current_state(save_state_T *sst)
8565{
8566 sst->save_msg_scroll = msg_scroll;
8567 sst->save_restart_edit = restart_edit;
8568 sst->save_msg_didout = msg_didout;
8569 sst->save_State = State;
8570 sst->save_insertmode = p_im;
8571 sst->save_finish_op = finish_op;
8572 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008573 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008574 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008575
Bram Moolenaar4324d872020-12-01 20:12:24 +01008576 msg_scroll = FALSE; // no msg scrolling in Normal mode
8577 restart_edit = 0; // don't go to Insert mode
8578 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008579
Bram Moolenaara452b802020-12-01 21:47:59 +01008580 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008581 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008582
8583 /*
8584 * Save the current typeahead. This is required to allow using ":normal"
8585 * from an event handler and makes sure we don't hang when the argument
8586 * ends with half a command.
8587 */
8588 save_typeahead(&sst->tabuf);
8589 return sst->tabuf.typebuf_valid;
8590}
8591
8592 void
8593restore_current_state(save_state_T *sst)
8594{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008595 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008596 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008597
8598 msg_scroll = sst->save_msg_scroll;
8599 restart_edit = sst->save_restart_edit;
8600 p_im = sst->save_insertmode;
8601 finish_op = sst->save_finish_op;
8602 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008603 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008604 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008605 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008606 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008607
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008608 // Restore the state (needed when called from a function executed for
8609 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008610 State = sst->save_State;
8611#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008612 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008613#endif
8614}
8615
8616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 * ":normal[!] {commands}": Execute normal mode commands.
8618 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008619 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008620ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008622 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 char_u *arg = NULL;
8624 int l;
8625 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008627 if (ex_normal_lock > 0)
8628 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008629 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008630 return;
8631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 if (ex_normal_busy >= p_mmd)
8633 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008634 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 return;
8636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 /*
8639 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8640 * this for the K_SPECIAL leading byte, otherwise special keys will not
8641 * work.
8642 */
8643 if (has_mbyte)
8644 {
8645 int len = 0;
8646
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008647 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 for (p = eap->arg; *p != NUL; ++p)
8649 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008650#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008651 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008653#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008654 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008655 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008656#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 )
8660 len += 2;
8661 }
8662 if (len > 0)
8663 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008664 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 if (arg != NULL)
8666 {
8667 len = 0;
8668 for (p = eap->arg; *p != NUL; ++p)
8669 {
8670 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008671#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 if (*p == CSI)
8673 {
8674 arg[len++] = KS_EXTRA;
8675 arg[len++] = (int)KE_CSI;
8676 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008677#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008678 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 {
8680 arg[len++] = *++p;
8681 if (*p == K_SPECIAL)
8682 {
8683 arg[len++] = KS_SPECIAL;
8684 arg[len++] = KE_FILLER;
8685 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008686#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 else if (*p == CSI)
8688 {
8689 arg[len++] = KS_EXTRA;
8690 arg[len++] = (int)KE_CSI;
8691 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 }
8694 arg[len] = NUL;
8695 }
8696 }
8697 }
8698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008700 ++ex_normal_busy;
8701 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 {
8703 /*
8704 * Repeat the :normal command for each line in the range. When no
8705 * range given, execute it just once, without positioning the cursor
8706 * first.
8707 */
8708 do
8709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 if (eap->addr_count != 0)
8711 {
8712 curwin->w_cursor.lnum = eap->line1++;
8713 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008714 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 }
8716
Bram Moolenaar13505972019-01-24 15:04:48 +01008717 exec_normal_cmd(arg != NULL
8718 ? arg
8719 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 }
8721 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8722 }
8723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008724 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 update_topline_cursor();
8726
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008727 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008729 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008730#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008731 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008732#endif
8733
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735}
8736
8737/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008738 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 */
8740 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008741ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008743 if (eap->forceit)
8744 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008745 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008746 if (!curwin->w_cursor.lnum)
8747 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008748 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008749 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008750#ifdef FEAT_TERMINAL
8751 // Ignore this when running in an active terminal.
8752 if (term_job_running(curbuf->b_term))
8753 return;
8754#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008755
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008756 // Ignore the command when already in Insert mode. Inserting an
8757 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01008758 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00008759 return;
8760
Bram Moolenaar64969662005-12-14 21:59:55 +00008761 if (eap->cmdidx == CMD_startinsert)
8762 restart_edit = 'a';
8763 else if (eap->cmdidx == CMD_startreplace)
8764 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008766 restart_edit = 'V';
8767
8768 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008770 if (eap->cmdidx == CMD_startinsert)
8771 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008772 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008774
8775 if (VIsual_active)
8776 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777}
8778
8779/*
8780 * ":stopinsert"
8781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008783ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784{
8785 restart_edit = 0;
8786 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008787 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008790/*
8791 * Execute normal mode command "cmd".
8792 * "remap" can be REMAP_NONE or REMAP_YES.
8793 */
8794 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008795exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008796{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008797 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008798 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008799 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008800}
Bram Moolenaar25281632016-01-21 23:32:32 +01008801
Bram Moolenaar25281632016-01-21 23:32:32 +01008802/*
8803 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008804 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008805 */
8806 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008807exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008808{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008809 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008810 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008811
Bram Moolenaar905dd902019-04-07 14:21:47 +02008812 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8813 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008814 clear_oparg(&oa);
8815 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008816 while ((!stuff_empty()
8817 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008818 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008819 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008820 {
8821 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008822#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008823 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008824 && oa.op_type == OP_NOP && oa.regname == NUL
8825 && !VIsual_active)
8826 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008827 // If terminal_loop() returns OK we got a key that is handled
8828 // in Normal model. With FAIL we first need to position the
8829 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008830 if (terminal_loop(TRUE) == OK)
8831 normal_cmd(&oa, TRUE);
8832 }
8833 else
8834#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008835 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008836 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008837 }
8838}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008839
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840#ifdef FEAT_FIND_ID
8841 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008842ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843{
8844 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8845 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8846 (linenr_T)1, (linenr_T)MAXLNUM);
8847}
8848
Bram Moolenaar4033c552017-09-16 20:54:51 +02008849#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850/*
8851 * ":psearch"
8852 */
8853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008854ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
8856 g_do_tagpreview = p_pvh;
8857 ex_findpat(eap);
8858 g_do_tagpreview = 0;
8859}
8860#endif
8861
8862 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008863ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864{
8865 int whole = TRUE;
8866 long n;
8867 char_u *p;
8868 int action;
8869
8870 switch (cmdnames[eap->cmdidx].cmd_name[2])
8871 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008872 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8874 action = ACTION_GOTO;
8875 else
8876 action = ACTION_SHOW;
8877 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008878 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 action = ACTION_SHOW_ALL;
8880 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008881 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 action = ACTION_GOTO;
8883 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008884 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 action = ACTION_SPLIT;
8886 break;
8887 }
8888
8889 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008890 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 {
8892 n = getdigits(&eap->arg);
8893 eap->arg = skipwhite(eap->arg);
8894 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008895 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 {
8897 whole = FALSE;
8898 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008899 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 if (*p)
8901 {
8902 *p++ = NUL;
8903 p = skipwhite(p);
8904
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008905 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008906 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008907 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008909 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 }
8911 }
8912 if (!eap->skip)
8913 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8914 whole, !eap->forceit,
8915 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8916 n, action, eap->line1, eap->line2);
8917}
8918#endif
8919
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
Bram Moolenaar4033c552017-09-16 20:54:51 +02008921#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922/*
8923 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8924 */
8925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008926ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008928 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8930}
8931
8932/*
8933 * ":pedit"
8934 */
8935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008936ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937{
8938 win_T *curwin_save = curwin;
8939
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008940 if (ERROR_IF_ANY_POPUP_WINDOW)
8941 return;
8942
Bram Moolenaar026587b2019-08-17 15:08:00 +02008943 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008945 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008946
Bram Moolenaar026587b2019-08-17 15:08:00 +02008947 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008949
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 if (curwin != curwin_save && win_valid(curwin_save))
8951 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008952 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01008954 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 win_enter(curwin_save, TRUE);
8956 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008957# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008958 else if (WIN_IS_POPUP(curwin))
8959 {
8960 // can't keep focus in popup window
8961 win_enter(firstwin, TRUE);
8962 }
8963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 g_do_tagpreview = 0;
8965}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967
8968/*
8969 * ":stag", ":stselect" and ":stjump".
8970 */
8971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008972ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973{
8974 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008975 postponed_split_flags = cmdmod.cmod_split;
8976 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8978 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008979 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981
8982/*
8983 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8984 */
8985 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008986ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
8988 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8989}
8990
8991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008992ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
8994 int cmd;
8995
8996 switch (name[1])
8997 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008998 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009000 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009002 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009004 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009006 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009008 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009010 case 'f': // ":tfirst"
9011 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009013 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009015 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009017 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009019 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 return;
9021 }
9022#endif
9023 cmd = DT_TAG;
9024 break;
9025 }
9026
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009027 if (name[0] == 'l')
9028 {
9029#ifndef FEAT_QUICKFIX
9030 ex_ni(eap);
9031 return;
9032#else
9033 cmd = DT_LTAG;
9034#endif
9035 }
9036
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9038 eap->forceit, TRUE);
9039}
9040
9041/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009042 * Check "str" for starting with a special cmdline variable.
9043 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9044 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9045 */
9046 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009047find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009048{
9049 int len;
9050 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009051 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009052 "%",
9053#define SPEC_PERC 0
9054 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009055#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009056 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02009057#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009058 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02009059#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009060 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02009061#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009062 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009063#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009064 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009065#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009066 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02009067#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02009068 "<stack>", // call stack
9069#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01009070 "<script>", // script file name
9071#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009072 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01009073#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009074 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009075#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009076 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009077#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009078 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009079#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02009080 "<SID>", // script ID: <SNR>123_
9081#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009082#ifdef FEAT_CLIENTSERVER
9083 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02009084# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009085#endif
9086 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009087
K.Takataeeec2542021-06-02 13:28:16 +02009088 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009089 {
9090 len = (int)STRLEN(spec_str[i]);
9091 if (STRNCMP(src, spec_str[i], len) == 0)
9092 {
9093 *usedlen = len;
9094 return i;
9095 }
9096 }
9097 return -1;
9098}
9099
9100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 * Evaluate cmdline variables.
9102 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009103 * change "%" to curbuf->b_ffname
9104 * "#" to curwin->w_alt_fnum
9105 * "%%" to curwin->w_alt_fnum in Vim9 script
9106 * "<cword>" to word under the cursor
9107 * "<cWORD>" to WORD under the cursor
9108 * "<cexpr>" to C-expression under the cursor
9109 * "<cfile>" to path name under the cursor
9110 * "<sfile>" to sourced file name
9111 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009112 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009113 * "<slnum>" to sourced file line number
9114 * "<afile>" to file name for autocommand
9115 * "<abuf>" to buffer number for autocommand
9116 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 *
9118 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9119 * "" for error without a message) and NULL is returned.
9120 * Returns an allocated string if a valid match was found.
9121 * Returns NULL if no match was found. "usedlen" then still contains the
9122 * number of characters to skip.
9123 */
9124 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009125eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009126 char_u *src, // pointer into commandline
9127 char_u *srcstart, // beginning of valid memory for src
9128 int *usedlen, // characters after src that are used
9129 linenr_T *lnump, // line number for :e command, or NULL
9130 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009131 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009132 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009133 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
9135 int i;
9136 char_u *s;
9137 char_u *result;
9138 char_u *resultbuf = NULL;
9139 int resultlen;
9140 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009141 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009143 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146
9147 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009148 if (escaped != NULL)
9149 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150
9151 /*
9152 * Check if there is something to do.
9153 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009154 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009155 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 {
9157 *usedlen = 1;
9158 return NULL;
9159 }
9160
9161 /*
9162 * Skip when preceded with a backslash "\%" and "\#".
9163 * Note: In "\\%" the % is also not recognized!
9164 */
9165 if (src > srcstart && src[-1] == '\\')
9166 {
9167 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009168 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 return NULL;
9170 }
9171
9172 /*
9173 * word or WORD under cursor
9174 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009175 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9176 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009178 resultlen = find_ident_under_cursor(&result,
9179 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9180 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9181 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 if (resultlen == 0)
9183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009184 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 return NULL;
9186 }
9187 }
9188
9189 /*
9190 * '#': Alternate file name
9191 * '%': Current file name
9192 * File name under the cursor
9193 * File name for autocommand
9194 * and following modifiers
9195 */
9196 else
9197 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009198 int off = 0;
9199
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 switch (spec_idx)
9201 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009202 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009203#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009204 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009207 // '%': current file
9208 if (curbuf->b_fname == NULL)
9209 {
9210 result = (char_u *)"";
9211 valid = 0; // Must have ":p:h" to be valid
9212 }
9213 else
9214 {
9215 result = curbuf->b_fname;
9216 tilde_file = STRCMP(result, "~") == 0;
9217 }
9218 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009220#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009221 // "%%" alternate file
9222 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009223#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009224 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009225 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009226 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009228 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 result = arg_all();
9230 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009231 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009232 if (escaped != NULL)
9233 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 break;
9236 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009237 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009238 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009239 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009241 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009242 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009243 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009244 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009246 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009248 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009249 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009250 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009251 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009252 return NULL;
9253 }
9254#ifdef FEAT_EVAL
9255 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9256 (long)i);
9257 if (result == NULL)
9258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009259 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009260 return NULL;
9261 }
9262#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009263 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 }
9267 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009268 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009269 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9270 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009271 buf = buflist_findnr(i);
9272 if (buf == NULL)
9273 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009274 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009275 return NULL;
9276 }
9277 if (lnump != NULL)
9278 *lnump = ECMD_LAST;
9279 if (buf->b_fname == NULL)
9280 {
9281 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009282 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009283 }
9284 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009285 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009286 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009287 tilde_file = STRCMP(result, "~") == 0;
9288 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 break;
9291
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009292 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009293 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 if (result == NULL)
9295 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009296 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 return NULL;
9298 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009299 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009302 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009304 if (result != NULL && !autocmd_fname_full)
9305 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009306 // Still need to turn the fname into a full path. It is
9307 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009308 autocmd_fname_full = TRUE;
9309 result = FullName_save(autocmd_fname, FALSE);
9310 vim_free(autocmd_fname);
9311 autocmd_fname = result;
9312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 if (result == NULL)
9314 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009315 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 return NULL;
9317 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009318 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 break;
9320
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009321 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 if (autocmd_bufnr <= 0)
9323 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009324 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 return NULL;
9326 }
9327 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9328 result = strbuf;
9329 break;
9330
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009331 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 result = autocmd_match;
9333 if (result == NULL)
9334 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009335 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 return NULL;
9337 }
9338 break;
9339
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009340 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009341 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 if (result == NULL)
9343 {
LemonBoy6013d002022-04-09 21:42:10 +01009344 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9345 return NULL;
9346 }
9347 resultbuf = result; // remember allocated string
9348 break;
9349 case SPEC_STACK: // call stack
9350 result = estack_sfile(ESTACK_STACK);
9351 if (result == NULL)
9352 {
9353 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9354 return NULL;
9355 }
9356 resultbuf = result; // remember allocated string
9357 break;
9358 case SPEC_SCRIPT: // script file name
9359 result = estack_sfile(ESTACK_SCRIPT);
9360 if (result == NULL)
9361 {
9362 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 return NULL;
9364 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009365 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009367
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009368 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009369 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009370 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009371 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009372 return NULL;
9373 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009374 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009375 result = strbuf;
9376 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009377
9378#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009379 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009380 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009381 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009382 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009383 return NULL;
9384 }
9385 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009386 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009387 result = strbuf;
9388 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009389
Bram Moolenaar90944302020-08-01 20:45:11 +02009390 case SPEC_SID:
9391 if (current_sctx.sc_sid <= 0)
9392 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009393 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009394 return NULL;
9395 }
9396 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9397 result = strbuf;
9398 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009399#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009400
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009401#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009402 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009403 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9404 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 result = strbuf;
9406 break;
9407#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009408
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009409 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009410 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009411 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 }
9413
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009414 resultlen = (int)STRLEN(result); // length of new string
9415 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 {
9417 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 resultlen = (int)(s - result);
9420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 else if (!skip_mod)
9422 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009423 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 &resultlen);
9425 if (result == NULL)
9426 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009427 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 return NULL;
9429 }
9430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 }
9432
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009433 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009435 if (empty_is_error)
9436 {
9437 if (valid != VALID_HEAD + VALID_PATH)
9438 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9439 else
9440 *errormsg = _(e_evaluates_to_an_empty_string);
9441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 result = NULL;
9443 }
9444 else
9445 result = vim_strnsave(result, resultlen);
9446 vim_free(resultbuf);
9447 return result;
9448}
9449
9450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 * Expand the <sfile> string in "arg".
9452 *
9453 * Returns an allocated string, or NULL for any error.
9454 */
9455 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009456expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009458 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 int len;
9460 char_u *result;
9461 char_u *newres;
9462 char_u *repl;
9463 int srclen;
9464 char_u *p;
9465
9466 result = vim_strsave(arg);
9467 if (result == NULL)
9468 return NULL;
9469
9470 for (p = result; *p; )
9471 {
9472 if (STRNCMP(p, "<sfile>", 7) != 0)
9473 ++p;
9474 else
9475 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009476 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009477 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 if (errormsg != NULL)
9479 {
9480 if (*errormsg)
9481 emsg(errormsg);
9482 vim_free(result);
9483 return NULL;
9484 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009485 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 {
9487 p += srclen;
9488 continue;
9489 }
9490 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9491 newres = alloc(len);
9492 if (newres == NULL)
9493 {
9494 vim_free(repl);
9495 vim_free(result);
9496 return NULL;
9497 }
9498 mch_memmove(newres, result, (size_t)(p - result));
9499 STRCPY(newres + (p - result), repl);
9500 len = (int)STRLEN(newres);
9501 STRCAT(newres, p + srclen);
9502 vim_free(repl);
9503 vim_free(result);
9504 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009505 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 }
9507 }
9508
9509 return result;
9510}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009513/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009514 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009515 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009518dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 if (fname == NULL)
9521 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009522 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523}
9524#endif
9525
9526/*
9527 * ":behave {mswin,xterm}"
9528 */
9529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009530ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531{
9532 if (STRCMP(eap->arg, "mswin") == 0)
9533 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009534 set_option_value_give_err((char_u *)"selection",
9535 0L, (char_u *)"exclusive", 0);
9536 set_option_value_give_err((char_u *)"selectmode",
9537 0L, (char_u *)"mouse,key", 0);
9538 set_option_value_give_err((char_u *)"mousemodel",
9539 0L, (char_u *)"popup", 0);
9540 set_option_value_give_err((char_u *)"keymodel",
9541 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 }
9543 else if (STRCMP(eap->arg, "xterm") == 0)
9544 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009545 set_option_value_give_err((char_u *)"selection",
9546 0L, (char_u *)"inclusive", 0);
9547 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9548 set_option_value_give_err((char_u *)"mousemodel",
9549 0L, (char_u *)"extend", 0);
9550 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 }
9552 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009553 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554}
9555
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556static int filetype_detect = FALSE;
9557static int filetype_plugin = FALSE;
9558static int filetype_indent = FALSE;
9559
9560/*
9561 * ":filetype [plugin] [indent] {on,off,detect}"
9562 * on: Load the filetype.vim file to install autocommands for file types.
9563 * off: Load the ftoff.vim file to remove all autocommands for file types.
9564 * plugin on: load filetype.vim and ftplugin.vim
9565 * plugin off: load ftplugof.vim
9566 * indent on: load filetype.vim and indent.vim
9567 * indent off: load indoff.vim
9568 */
9569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009570ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571{
9572 char_u *arg = eap->arg;
9573 int plugin = FALSE;
9574 int indent = FALSE;
9575
9576 if (*eap->arg == NUL)
9577 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009578 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009579 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 filetype_detect ? "ON" : "OFF",
9581 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9582 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9583 return;
9584 }
9585
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009586 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 for (;;)
9588 {
9589 if (STRNCMP(arg, "plugin", 6) == 0)
9590 {
9591 plugin = TRUE;
9592 arg = skipwhite(arg + 6);
9593 continue;
9594 }
9595 if (STRNCMP(arg, "indent", 6) == 0)
9596 {
9597 indent = TRUE;
9598 arg = skipwhite(arg + 6);
9599 continue;
9600 }
9601 break;
9602 }
9603 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9604 {
9605 if (*arg == 'o' || !filetype_detect)
9606 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009607 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 filetype_detect = TRUE;
9609 if (plugin)
9610 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009611 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 filetype_plugin = TRUE;
9613 }
9614 if (indent)
9615 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009616 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 filetype_indent = TRUE;
9618 }
9619 }
9620 if (*arg == 'd')
9621 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009622 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009623 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 }
9625 }
9626 else if (STRCMP(arg, "off") == 0)
9627 {
9628 if (plugin || indent)
9629 {
9630 if (plugin)
9631 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009632 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 filetype_plugin = FALSE;
9634 }
9635 if (indent)
9636 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009637 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 filetype_indent = FALSE;
9639 }
9640 }
9641 else
9642 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009643 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 filetype_detect = FALSE;
9645 }
9646 }
9647 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009648 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649}
9650
9651/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009652 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 */
9654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009655ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009658 {
9659 char_u *arg = eap->arg;
9660
9661 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9662 arg += 9;
9663
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009664 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
Bram Moolenaar3e545692017-06-04 19:00:32 +02009665 if (arg != eap->arg)
9666 did_filetype = FALSE;
9667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009671ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672{
9673#ifdef FEAT_DIGRAPHS
9674 if (*eap->arg != NUL)
9675 putdigraph(eap->arg);
9676 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009677 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678#else
mityu61065042021-07-19 20:07:21 +02009679 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680#endif
9681}
9682
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009683#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9684 void
9685set_no_hlsearch(int flag)
9686{
9687 no_hlsearch = flag;
9688# ifdef FEAT_EVAL
9689 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9690# endif
9691}
9692
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693/*
9694 * ":nohlsearch"
9695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009697ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009699 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009700 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702#endif
9703
9704#ifdef FEAT_CRYPT
9705/*
9706 * ":X": Get crypt key
9707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009709ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009711 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009712 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713}
9714#endif
9715
9716#ifdef FEAT_FOLDING
9717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009718ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719{
9720 if (foldManualAllowed(TRUE))
9721 foldCreate(eap->line1, eap->line2);
9722}
9723
9724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009725ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726{
9727 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9728 eap->forceit, FALSE);
9729}
9730
9731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009732ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733{
9734 linenr_T lnum;
9735
Bram Moolenaar4facea32019-10-12 20:17:40 +02009736# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009737 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009738# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009740 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9742 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9743 ml_setmarked(lnum);
9744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009745 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009747 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009748# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009749 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009750# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751}
9752#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009754#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009755/*
9756 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9757 * instead of a quickfix command.
9758 */
9759 int
9760is_loclist_cmd(int cmdidx)
9761{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009762 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009763 return FALSE;
9764 return cmdnames[cmdidx].cmd_name[0] == 'l';
9765}
9766#endif
9767
Bram Moolenaar4facea32019-10-12 20:17:40 +02009768#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009769 int
9770get_pressedreturn(void)
9771{
9772 return ex_pressedreturn;
9773}
9774
9775 void
9776set_pressedreturn(int val)
9777{
9778 ex_pressedreturn = val;
9779}
9780#endif