blob: 4531794ffd76e5373fe6f8d154cf348fab6eba0e [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 Moolenaare88c8e82020-11-01 17:03:37 +01001823 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001824 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001825 if (ea.cmd == cmd + 1 && *cmd == '$')
1826 // should be "$VAR = val"
1827 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001828#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001829 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001830#else
1831 p = find_ex_command(&ea, NULL, NULL, NULL);
1832#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001833 if (ea.cmdidx == CMD_SIZE)
1834 {
1835 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1836
1837 // If a ':' before the range is missing, give a clearer error
1838 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001839 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001840 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001841 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001842 goto doend;
1843 }
1844 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001845 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001848
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001849#ifdef FEAT_EVAL
1850# ifdef FEAT_PROFILE
1851 // Count this line for profiling if skip is TRUE.
1852 if (do_profiling == PROF_YES
1853 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1854 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1855 {
1856 int skip = did_emsg || got_int || did_throw;
1857
1858 if (ea.cmdidx == CMD_catch)
1859 skip = !skip && !(cstack->cs_idx >= 0
1860 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1861 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1862 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1863 skip = skip || !(cstack->cs_idx >= 0
1864 && !(cstack->cs_flags[cstack->cs_idx]
1865 & (CSF_ACTIVE | CSF_TRUE)));
1866 else if (ea.cmdidx == CMD_finally)
1867 skip = FALSE;
1868 else if (ea.cmdidx != CMD_endif
1869 && ea.cmdidx != CMD_endfor
1870 && ea.cmdidx != CMD_endtry
1871 && ea.cmdidx != CMD_endwhile)
1872 skip = ea.skip;
1873
1874 if (!skip)
1875 {
1876 if (getline_equal(fgetline, cookie, get_func_line))
1877 func_line_exec(getline_cookie(fgetline, cookie));
1878 else if (getline_equal(fgetline, cookie, getsourceline))
1879 script_line_exec();
1880 }
1881 }
1882# endif
1883
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001884 // May go to debug mode. If this happens and the ">quit" debug command is
1885 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001886 dbg_check_breakpoint(&ea);
1887 if (!ea.skip && got_int)
1888 {
1889 ea.skip = TRUE;
1890 (void)do_intthrow(cstack);
1891 }
1892#endif
1893
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001894/*
1895 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 *
1897 * where 'addr' is:
1898 *
1899 * % (entire file)
1900 * $ [+-NUM]
1901 * 'x [+-NUM] (where x denotes a currently defined mark)
1902 * . [+-NUM]
1903 * [+-NUM]..
1904 * NUM
1905 *
1906 * The ea.cmd pointer is updated to point to the first character following the
1907 * range spec. If an initial address is found, but no second, the upper bound
1908 * is equal to the lower.
1909 */
1910
Bram Moolenaar25190db2019-05-04 15:05:28 +02001911 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001912 if (!IS_USER_CMDIDX(ea.cmdidx))
1913 {
1914 if (ea.cmdidx != CMD_SIZE)
1915 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1916 else
1917 ea.addr_type = ADDR_LINES;
1918
Bram Moolenaar25190db2019-05-04 15:05:28 +02001919 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001920 if (ea.cmdidx == CMD_wincmd && p != NULL)
1921 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001922#ifdef FEAT_QUICKFIX
1923 // :.cc in quickfix window uses line number
1924 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1925 ea.addr_type = ADDR_OTHER;
1926#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001927 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001928
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001929 ea.cmd = cmd;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001930 if (!may_have_range)
1931 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001932 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1933 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001936 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 */
1938
1939 /*
1940 * Skip ':' and any white space
1941 */
1942 ea.cmd = skipwhite(ea.cmd);
1943 while (*ea.cmd == ':')
1944 ea.cmd = skipwhite(ea.cmd + 1);
1945
1946 /*
1947 * If we got a line, but no command, then go to the line.
1948 * If we find a '|' or '\n' we set ea.nextcmd.
1949 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001950 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1951 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 /*
1954 * strange vi behaviour:
1955 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001956 * ":3|..." prints line 3 (not in Vim9 script)
1957 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001959 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001961 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 goto doend;
1963 }
1964
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001965 // If this looks like an undefined user command and there are CmdUndefined
1966 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001967 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1968 && ASCII_ISUPPER(*ea.cmd)
1969 && has_cmdundefined())
1970 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001971 int ret;
1972
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001973 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001974 while (ASCII_ISALNUM(*p))
1975 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001976 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001977 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1978 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001979 // If the autocommands did something and didn't cause an error, try
1980 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001981 p = (ret
1982#ifdef FEAT_EVAL
1983 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001984#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001986 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (p == NULL)
1989 {
1990 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001991 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 goto doend;
1993 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001994 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001995 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1996 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
1998 errormsg = uc_fun_cmd();
1999 goto doend;
2000 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002001
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (ea.cmdidx == CMD_SIZE)
2003 {
2004 if (!ea.skip)
2005 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002006 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002008 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002009 // If the modifier was parsed OK the error must be in the
2010 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002011 if (after_modifier != NULL)
2012 append_command(after_modifier);
2013 else
2014 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002015 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002016 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002017 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002018 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020 goto doend;
2021 }
2022
Bram Moolenaar958636c2014-10-21 20:01:58 +02002023 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2024 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002025#ifdef HAVE_EX_SCRIPT_NI
2026 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2027#endif
2028 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
2030#ifndef FEAT_EVAL
2031 /*
2032 * When the expression evaluation is disabled, recognize the ":if" and
2033 * ":endif" commands and ignore everything in between it.
2034 */
2035 if (ea.cmdidx == CMD_if)
2036 ++if_level;
2037 if (if_level)
2038 {
2039 if (ea.cmdidx == CMD_endif)
2040 --if_level;
2041 goto doend;
2042 }
2043
2044#endif
2045
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002046 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002047 if (*p == '!' && ea.cmdidx != CMD_substitute
2048 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
2050 ++p;
2051 ea.forceit = TRUE;
2052 }
2053 else
2054 ea.forceit = FALSE;
2055
2056/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002057 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002059 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002060 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062 if (!ea.skip)
2063 {
2064#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002065 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002067 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002068 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 goto doend;
2070 }
2071#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002072 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002073 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002074 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002075 goto doend;
2076 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002077 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002079 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002080 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 goto doend;
2082 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002083
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002084 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002086#ifdef FEAT_CMDWIN
2087 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2088 {
2089 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002090 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002091 goto doend;
2092 }
2093#endif
2094 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2095 {
2096 // Command not allowed when text is locked
2097 errormsg = _(get_text_locked_msg());
2098 goto doend;
2099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002102 // Disallow editing another buffer when "curbuf_lock" is set.
2103 // Do allow ":checktime" (it is postponed).
2104 // Do allow ":edit" (check for an argument later).
2105 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002106 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002107 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002108 && ea.cmdidx != CMD_edit
2109 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002110 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002111 && curbuf_locked())
2112 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002114 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002116 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 goto doend;
2118 }
2119 }
2120
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002121 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002123 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 goto doend;
2125 }
2126
2127 /*
2128 * Don't complain about the range if it is not used
2129 * (could happen if line_count is accidentally set to 0).
2130 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002131 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
2133 /*
2134 * If the range is backwards, ask for confirmation and, if given, swap
2135 * ea.line1 & ea.line2 so it's forwards again.
2136 * When global command is busy, don't ask, will fail below.
2137 */
2138 if (!global_busy && ea.line1 > ea.line2)
2139 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002140 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002142 if (sourcing || exmode_active)
2143 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002144 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002145 goto doend;
2146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (ask_yesno((char_u *)
2148 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002149 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151 lnum = ea.line1;
2152 ea.line1 = ea.line2;
2153 ea.line2 = lnum;
2154 }
2155 if ((errormsg = invalid_range(&ea)) != NULL)
2156 goto doend;
2157 }
2158
Bram Moolenaarb7316892019-05-01 18:08:42 +02002159 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2160 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 ea.line2 = 1;
2162
2163 correct_range(&ea);
2164
2165#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002166 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002167 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002169 // Put the first line at the start of a closed fold, put the last line
2170 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 (void)hasFolding(ea.line1, &ea.line1, NULL);
2172 (void)hasFolding(ea.line2, NULL, &ea.line2);
2173 }
2174#endif
2175
2176#ifdef FEAT_QUICKFIX
2177 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002178 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 * option here, so things like % get expanded.
2180 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002181 p = replace_makeprg(&ea, p, cmdlinep);
2182 if (p == NULL)
2183 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184#endif
2185
2186 /*
2187 * Skip to start of argument.
2188 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2189 */
2190 if (ea.cmdidx == CMD_bang)
2191 ea.arg = p;
2192 else
2193 ea.arg = skipwhite(p);
2194
Bram Moolenaar379fb762018-08-30 15:58:28 +02002195 // ":file" cannot be run with an argument when "curbuf_lock" is set
2196 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2197 goto doend;
2198
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 /*
2200 * Check for "++opt=val" argument.
2201 * Must be first, allow ":w ++enc=utf8 !cmd"
2202 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002203 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2205 if (getargopt(&ea) == FAIL && !ni)
2206 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002207 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 goto doend;
2209 }
2210
2211 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2212 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002213 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002215 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002217 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 goto doend;
2219 }
2220 ea.arg = skipwhite(ea.arg + 1);
2221 ea.append = TRUE;
2222 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002223 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
2225 ++ea.arg;
2226 ea.usefilter = TRUE;
2227 }
2228 }
2229
2230 if (ea.cmdidx == CMD_read)
2231 {
2232 if (ea.forceit)
2233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002234 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 ea.forceit = FALSE;
2236 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002237 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
2239 ++ea.arg;
2240 ea.usefilter = TRUE;
2241 }
2242 }
2243
2244 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2245 {
2246 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002247 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
2249 ++ea.arg;
2250 ++ea.amount;
2251 }
2252 ea.arg = skipwhite(ea.arg);
2253 }
2254
2255 /*
2256 * Check for "+command" argument, before checking for next command.
2257 * Don't do this for ":read !cmd" and ":write !cmd".
2258 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002259 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2261
2262 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002263 * For commands that do not use '|' inside their argument: Check for '|' to
2264 * separate commands and '"' or '#' to start comments.
2265 *
2266 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002267 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002268 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002269 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002271 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2272 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002273 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002274 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002275 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002276 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002277 || ea.cmdidx == CMD_global
2278 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002279 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002280#ifdef FEAT_EVAL
2281 || inside_block(&ea)
2282#endif
2283 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 for (p = ea.arg; *p; ++p)
2286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002287 // Remove one backslash before a newline, so that it's possible to
2288 // pass a newline to the shell and also a newline that is preceded
2289 // with a backslash. This makes it impossible to end a shell
2290 // command in a backslash, but that doesn't appear useful.
2291 // Halving the number of backslashes is incompatible with previous
2292 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002294 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002295 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 ea.nextcmd = p + 1;
2298 *p = NUL;
2299 break;
2300 }
2301 }
2302 }
2303
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002304 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002305 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002307 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002308 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002310 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002311 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002312 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002314#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002315 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002316 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002318 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002319 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002322 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2323 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002324 {
2325 ea.regname = *ea.arg++;
2326#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002327 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002328 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2329 {
kuuote08d7b1c2021-10-04 22:17:36 +01002330 if (!ea.skip)
2331 {
2332 set_expr_line(vim_strsave(ea.arg), &ea);
2333 did_set_expr_line = TRUE;
2334 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002335 ea.arg += STRLEN(ea.arg);
2336 }
2337#endif
2338 ea.arg = skipwhite(ea.arg);
2339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341
2342 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002343 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 * count, it's a buffer name.
2345 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002346 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002347 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002348 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002350 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002352 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002354 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 goto doend;
2356 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002357 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 {
2359 ea.line2 = n;
2360 if (ea.addr_count == 0)
2361 ea.addr_count = 1;
2362 }
2363 else
2364 {
2365 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002366 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002367 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002368 else
2369 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 ++ea.addr_count;
2371 /*
2372 * Be vi compatible: no error message for out of range.
2373 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002374 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 ea.line2 = curbuf->b_ml.ml_line_count;
2376 }
2377 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002378
2379 /*
2380 * Check for flags: 'l', 'p' and '#'.
2381 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002382 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002383 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002384 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2385 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002387 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002388 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 goto doend;
2390 }
2391
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002392 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002394 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 goto doend;
2396 }
2397
2398#ifdef FEAT_EVAL
2399 /*
2400 * Skip the command when it's not going to be executed.
2401 * The commands like :if, :endif, etc. always need to be executed.
2402 * Also make an exception for commands that handle a trailing command
2403 * themselves.
2404 */
2405 if (ea.skip)
2406 {
2407 switch (ea.cmdidx)
2408 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002409 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CMD_while:
2411 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002412 case CMD_for:
2413 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 case CMD_if:
2415 case CMD_elseif:
2416 case CMD_else:
2417 case CMD_endif:
2418 case CMD_try:
2419 case CMD_catch:
2420 case CMD_finally:
2421 case CMD_endtry:
2422 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002423 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 break;
2425
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002426 // Commands that handle '|' themselves. Check: A command should
2427 // either have the EX_TRLBAR flag, appear in this list or appear in
2428 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 case CMD_aboveleft:
2430 case CMD_and:
2431 case CMD_belowright:
2432 case CMD_botright:
2433 case CMD_browse:
2434 case CMD_call:
2435 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002436 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 case CMD_delfunction:
2438 case CMD_djump:
2439 case CMD_dlist:
2440 case CMD_dsearch:
2441 case CMD_dsplit:
2442 case CMD_echo:
2443 case CMD_echoerr:
2444 case CMD_echomsg:
2445 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002446 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002448 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002449 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 case CMD_help:
2451 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002452 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 case CMD_ijump:
2454 case CMD_ilist:
2455 case CMD_isearch:
2456 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002457 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_keepjumps:
2459 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002460 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 case CMD_leftabove:
2462 case CMD_let:
2463 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002464 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002465 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002467 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002468 case CMD_noautocmd:
2469 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_perl:
2471 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002472 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002473 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002474 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 case CMD_return:
2476 case CMD_rightbelow:
2477 case CMD_ruby:
2478 case CMD_silent:
2479 case CMD_smagic:
2480 case CMD_snomagic:
2481 case CMD_substitute:
2482 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002483 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 case CMD_tcl:
2485 case CMD_throw:
2486 case CMD_tilde:
2487 case CMD_topleft:
2488 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002489 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002490 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 case CMD_verbose:
2492 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002493 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 break;
2495
2496 default: goto doend;
2497 }
2498 }
2499#endif
2500
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002501 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
2503 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2504 goto doend;
2505 }
2506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 /*
2508 * Accept buffer name. Cannot be used at the same time with a buffer
2509 * number. Don't do this for a user command.
2510 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002511 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002512 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
2514 /*
2515 * :bdelete, :bwipeout and :bunload take several arguments, separated
2516 * by spaces: find next space (skipping over escaped characters).
2517 * The others take one argument: ignore trailing spaces.
2518 */
2519 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2520 || ea.cmdidx == CMD_bunload)
2521 p = skiptowhite_esc(ea.arg);
2522 else
2523 {
2524 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002525 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 --p;
2527 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002528 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002529 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002530 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 goto doend;
2532 ea.addr_count = 1;
2533 ea.arg = skipwhite(p);
2534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002536 // The :try command saves the emsg_silent flag, reset it here when
2537 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002538 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002539 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002540 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002541 if (emsg_silent < 0)
2542 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002543 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002544 }
2545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002547 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
Bram Moolenaar958636c2014-10-21 20:01:58 +02002550 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
2552 /*
2553 * Execute a user-defined command.
2554 */
2555 do_ucmd(&ea);
2556 }
2557 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002560 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 */
2562 ea.errmsg = NULL;
2563 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2564 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002565 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567
2568#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002570 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002571 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002572 && current_sctx.sc_sid > 0
2573 && ea.cmdidx != CMD_endif
2574 && (cstack->cs_idx < 0
2575 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002576 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 /*
2579 * If the command just executed called do_cmdline(), any throw or ":return"
2580 * or ":finish" encountered there must also check the cstack of the still
2581 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2582 * exception, or reanimate a returned function or finished script file and
2583 * return or finish it again.
2584 */
2585 if (need_rethrow)
2586 do_throw(cstack);
2587 else if (check_cstack)
2588 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002589 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002591 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 && current_func_returned())
2593 do_return(&ea, TRUE, FALSE, NULL);
2594 }
2595 need_rethrow = check_cstack = FALSE;
2596#endif
2597
2598doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002599 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002602 curwin->w_cursor.col = 0;
2603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
2605 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2606 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002607 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002609 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 {
2611 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002612 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002614 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 }
2616 emsg(errormsg);
2617 }
2618#ifdef FEAT_EVAL
2619 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002620 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2621 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002622
2623 if (did_set_expr_line)
2624 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625#endif
2626
Bram Moolenaare1004402020-10-24 20:49:43 +02002627 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002629 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002630 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002632 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 ea.nextcmd = NULL;
2634
2635#ifdef FEAT_EVAL
2636 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002637 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638#endif
2639
2640 return ea.nextcmd;
2641}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002643static char ex_error_buf[MSG_BUF_LEN];
2644
2645/*
2646 * Return an error message with argument included.
2647 * Uses a static buffer, only the last error will be kept.
2648 * "msg" will be translated, caller should use N_().
2649 */
2650 char *
2651ex_errmsg(char *msg, char_u *arg)
2652{
2653 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2654 return ex_error_buf;
2655}
2656
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002658 * Handle a range without a command.
2659 * Returns an error message on failure.
2660 */
2661 char *
2662ex_range_without_command(exarg_T *eap)
2663{
2664 char *errormsg = NULL;
2665
2666 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2667#ifdef FEAT_EVAL
2668 && !in_vim9script()
2669#endif
2670 )
2671 {
2672 eap->cmdidx = CMD_print;
2673 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2674 if ((errormsg = invalid_range(eap)) == NULL)
2675 {
2676 correct_range(eap);
2677 ex_print(eap);
2678 }
2679 }
2680 else if (eap->addr_count != 0)
2681 {
2682 if (eap->line2 > curbuf->b_ml.ml_line_count)
2683 {
2684 // With '-' in 'cpoptions' a line number past the file is an
2685 // error, otherwise put it at the end of the file.
2686 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2687 eap->line2 = -1;
2688 else
2689 eap->line2 = curbuf->b_ml.ml_line_count;
2690 }
2691
2692 if (eap->line2 < 0)
2693 errormsg = _(e_invalid_range);
2694 else
2695 {
2696 if (eap->line2 == 0)
2697 curwin->w_cursor.lnum = 1;
2698 else
2699 curwin->w_cursor.lnum = eap->line2;
2700 beginline(BL_SOL | BL_FIX);
2701 }
2702 }
2703 return errormsg;
2704}
2705
2706/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002707 * Check for an Ex command with optional tail.
2708 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002709 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002710 */
2711 static int
2712checkforcmd_opt(
2713 char_u **pp, // start of command
2714 char *cmd, // name of command
2715 int len, // required length
2716 int noparen)
2717{
2718 int i;
2719
2720 for (i = 0; cmd[i] != NUL; ++i)
2721 if (((char_u *)cmd)[i] != (*pp)[i])
2722 break;
Bram Moolenaar68854a82022-01-31 18:59:13 +00002723 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_'
2724 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002725 {
2726 *pp = skipwhite(*pp + i);
2727 return TRUE;
2728 }
2729 return FALSE;
2730}
2731
2732/*
2733 * Check for an Ex command with optional tail.
2734 * If there is a match advance "pp" to the argument and return TRUE.
2735 */
2736 int
2737checkforcmd(
2738 char_u **pp, // start of command
2739 char *cmd, // name of command
2740 int len) // required length
2741{
2742 return checkforcmd_opt(pp, cmd, len, FALSE);
2743}
2744
2745/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002746 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002747 * If there is a match advance "pp" to the argument and return TRUE.
2748 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002749 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002750checkforcmd_noparen(
2751 char_u **pp, // start of command
2752 char *cmd, // name of command
2753 int len) // required length
2754{
2755 return checkforcmd_opt(pp, cmd, len, TRUE);
2756}
2757
2758/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002759 * Parse and skip over command modifiers:
2760 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002761 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002762 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002763 * When "skip_only" is TRUE the global variables are not changed, except for
2764 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002765 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2766 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002767 * Call apply_cmdmod() to get the side effects of the modifiers:
2768 * - Increment "sandbox" for ":sandbox"
2769 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002770 * - set msg_silent for ":silent"
2771 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 * Return FAIL when the command is not to be executed.
2773 * May set "errormsg" to an error message.
2774 */
2775 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002776parse_command_modifiers(
2777 exarg_T *eap,
2778 char **errormsg,
2779 cmdmod_T *cmod,
2780 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002781{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002782 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002783 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002784 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002785 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002786 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002787 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002788
Bram Moolenaare1004402020-10-24 20:49:43 +02002789 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002790 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002791
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002792 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2793 {
2794 // The automatically inserted Visual area range is skipped, so that
2795 // typing ":cmdmod cmd" in Visual mode works without having to move the
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002796 // range to after the modififiers. The command will be
2797 // "'<,'>cmdmod cmd", parse "cmdmod cmd" and then put back "'<,'>"
2798 // before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002799 eap->cmd += 5;
2800 cmd_start = eap->cmd;
2801 has_visual_range = TRUE;
2802 }
2803
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002804 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002805 for (;;)
2806 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002807 char_u *p;
2808
Bram Moolenaareffed932018-08-14 13:38:17 +02002809 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002810 {
2811 if (*eap->cmd == ':')
2812 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002813 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002814 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002815
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002816 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002817 if (*eap->cmd == NUL && exmode_active
2818 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2819 || getline_equal(eap->getline, eap->cookie, getexline))
2820 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2821 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002822 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002823 if (!skip_only)
2824 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002825 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002826 }
2827
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002828 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002829 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002830 {
2831 // a comment ends at a NL
2832 if (eap->nextcmd == NULL)
2833 {
2834 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2835 if (eap->nextcmd != NULL)
2836 ++eap->nextcmd;
2837 }
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002838 if (vim9script)
2839 {
2840 if (has_cmdmod(cmod, FALSE))
2841 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002842#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002843 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2844 && eap->cmd[2] != '{')
2845 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002846#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002847 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002848 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002849 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002850 if (*eap->cmd == NUL)
2851 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002852 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002853 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002854 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002855 if (vim9script && has_cmdmod(cmod, FALSE))
2856 *errormsg = _(e_command_modifier_without_command);
2857 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002858 return FAIL;
2859 }
2860
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002861 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002862
2863 // In Vim9 script a variable can shadow a command modifier:
2864 // verbose = 123
2865 // verbose += 123
2866 // silent! verbose = func()
2867 // verbose.member = 2
2868 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002869 // But not:
2870 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002871 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002872 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002873 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002874
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002875 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002876 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002877 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002878 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2879 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002880 break;
2881 }
2882
Bram Moolenaareffed932018-08-14 13:38:17 +02002883 switch (*p)
2884 {
Bram Moolenaar91324262022-09-09 13:27:59 +01002885 // When adding an entry, also modify cmdmods[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002886 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002887 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002888 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 continue;
2890
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002891 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002892 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002893 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 continue;
2895 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002896 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 {
2898#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002899 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002900#endif
2901 continue;
2902 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002903 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002904 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002905 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002906 continue;
2907
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002908 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 break;
2910#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002911 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002912#endif
2913 continue;
2914
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002915 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002916 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002917 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002918 continue;
2919 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002920 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002922 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002923 continue;
2924 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002925 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002926 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002927 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002928 continue;
2929 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002930 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002931 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002932 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002933 continue;
2934
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002935 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002936 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002937 char_u *reg_pat;
2938 char_u *nulp = NULL;
2939 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002940
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002941 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002942 || *p == NUL
2943 || (ends_excmd(*p)
2944#ifdef FEAT_EVAL
2945 // in ":filter #pat# cmd" # does not
2946 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002947 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002948#endif
2949 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002950 break;
2951 if (*p == '!')
2952 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002953 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002954 p = skipwhite(p + 1);
2955 if (*p == NUL || ends_excmd(*p))
2956 break;
2957 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002958#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002959 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002960 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002961 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002962#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002963 if (skip_only)
2964 p = skip_vimgrep_pat(p, NULL, NULL);
2965 else
2966 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002967 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2968 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002969 if (p == NULL || *p == NUL)
2970 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002971 if (!skip_only)
2972 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002973 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002974 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002975 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002976 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002977 // restore the character overwritten by NUL
2978 if (nulp != NULL)
2979 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002980 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002981 eap->cmd = p;
2982 continue;
2983 }
2984
Bram Moolenaar21c3a802022-08-31 17:49:14 +01002985 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
2986 {
2987 cmod->cmod_split |= WSP_HOR;
2988 continue;
2989 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002990 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01002991 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002992 || *p == NUL || ends_excmd(*p))
2993 break;
2994 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002995 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002996 continue;
2997
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002998 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003000 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003001 continue;
3002 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003003 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3004 {
3005 if (ends_excmd2(p, eap->cmd))
3006 {
3007 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003008 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003009 return FAIL;
3010 }
3011 cmod->cmod_flags |= CMOD_LEGACY;
3012 continue;
3013 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003014
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003015 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003016 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003017 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003018 continue;
3019
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003020 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003022 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003023 continue;
3024 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003025 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003026 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003027 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003028 continue;
3029
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003030 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003031 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003032 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003033 continue;
3034
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003035 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003036 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003037 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003038 continue;
3039 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003040 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003041 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003042 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003043 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3044 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003045 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003046 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003047 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003048 }
3049 continue;
3050
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003051 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003052 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003053 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003054 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003055 long tabnr = get_address(eap, &eap->cmd,
3056 ADDR_TABS, eap->skip,
3057 skip_only, FALSE, 1);
3058 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003059 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003060 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003061 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003062 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3063 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003064 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003065 return FAIL;
3066 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003067 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003068 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003069 }
3070 eap->cmd = p;
3071 continue;
3072 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003073 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003074 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003075 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003076 continue;
3077
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003078 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003079 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003080 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003081 continue;
3082
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003083 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003084 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003085 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003086 continue;
3087 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003088 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003089 {
3090 if (ends_excmd2(p, eap->cmd))
3091 {
3092 *errormsg =
3093 _(e_vim9cmd_must_be_followed_by_command);
3094 return FAIL;
3095 }
3096 cmod->cmod_flags |= CMOD_VIM9CMD;
3097 continue;
3098 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003099 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003100 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003101 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003102 {
zeertzjqcd749632022-06-14 13:30:35 +01003103 // zero means not set, one is verbose == 0, etc.
3104 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003105 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003106 else
zeertzjqcd749632022-06-14 13:30:35 +01003107 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003108 eap->cmd = p;
3109 continue;
3110 }
3111 break;
3112 }
3113
Bram Moolenaar1501b632022-03-27 16:56:21 +01003114 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003115 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003116 if (eap->cmd > cmd_start)
3117 {
3118 // Move the '<,'> range to after the modifiers and insert a colon.
3119 // Since the modifiers have been parsed put the colon on top of the
3120 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3121 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003122 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003123 {
3124 size_t len = STRLEN(cmd_start);
3125
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003126 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003127 // "'<,'>mods" -> "mods *+
3128 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003129 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003130 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003131 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003132 }
3133 else
3134 {
3135 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3136 eap->cmd -= 5;
3137 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3138 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003139 }
3140 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003141 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003142 // Special case: change empty command to "+".
3143 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003144 eap->cmd = (char_u *)"'<,'>+";
3145 else
3146 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003147 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003148 else if (use_plus_cmd)
3149 eap->cmd = (char_u *)"+";
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003150
Bram Moolenaareffed932018-08-14 13:38:17 +02003151 return OK;
3152}
3153
3154/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003155 * Return TRUE if "cmod" has anything set.
3156 */
3157 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003158has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003159{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003160 return (cmod->cmod_flags != 0 && (!ignore_silent
3161 || (cmod->cmod_flags
3162 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003163 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003164 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003165 || cmod->cmod_tab != 0
3166 || cmod->cmod_filter_regmatch.regprog != NULL;
3167}
3168
Bram Moolenaar8991be22022-02-14 21:51:46 +00003169#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003170/*
3171 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3172 */
3173 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003174cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003175{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003176 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003177 {
3178 emsg(_(e_misplaced_command_modifier));
3179 return TRUE;
3180 }
3181 return FALSE;
3182}
Dominique Pelle748b3082022-01-08 12:41:16 +00003183#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003184
3185/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003186 * Apply the command modifiers. Saves current state in "cmdmod", call
3187 * undo_cmdmod() later.
3188 */
3189 void
3190apply_cmdmod(cmdmod_T *cmod)
3191{
3192#ifdef HAVE_SANDBOX
3193 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3194 {
3195 ++sandbox;
3196 cmod->cmod_did_sandbox = TRUE;
3197 }
3198#endif
zeertzjqcd749632022-06-14 13:30:35 +01003199 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003200 {
3201 if (cmod->cmod_verbose_save == 0)
3202 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003203 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003204 }
3205
3206 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3207 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003208 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003209 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003210 cmod->cmod_save_msg_scroll = msg_scroll;
3211 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003212 if (cmod->cmod_flags & CMOD_SILENT)
3213 ++msg_silent;
3214 if (cmod->cmod_flags & CMOD_UNSILENT)
3215 msg_silent = 0;
3216
3217 if (cmod->cmod_flags & CMOD_ERRSILENT)
3218 {
3219 ++emsg_silent;
3220 ++cmod->cmod_did_esilent;
3221 }
3222
Bram Moolenaare1004402020-10-24 20:49:43 +02003223 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003224 {
3225 // Set 'eventignore' to "all".
3226 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003227 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003228 set_string_option_direct((char_u *)"ei", -1,
3229 (char_u *)"all", OPT_FREE, SID_NONE);
3230 }
3231}
3232
3233/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003234 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003235 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003236 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003237undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003238{
Bram Moolenaare1004402020-10-24 20:49:43 +02003239 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003240 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003241 p_verbose = cmod->cmod_verbose_save - 1;
3242 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003243 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003244
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003245#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003246 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003247 {
3248 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003249 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003250 }
3251#endif
3252
Bram Moolenaare1004402020-10-24 20:49:43 +02003253 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003254 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003255 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003256 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3257 OPT_FREE, SID_NONE);
3258 free_string_option(cmod->cmod_save_ei);
3259 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003260 }
3261
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003262 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003263
Bram Moolenaare1004402020-10-24 20:49:43 +02003264 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003265 {
3266 // messages could be enabled for a serious error, need to check if the
3267 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003268 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3269 msg_silent = cmod->cmod_save_msg_silent - 1;
3270 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003271 if (emsg_silent < 0)
3272 emsg_silent = 0;
3273 // Restore msg_scroll, it's set by file I/O commands, even when no
3274 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003275 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003276
3277 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3278 // somewhere in the line. Put it back in the first column.
3279 if (redirecting())
3280 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003281
Bram Moolenaare1004402020-10-24 20:49:43 +02003282 cmod->cmod_save_msg_silent = 0;
3283 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003284 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003285}
3286
3287/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003288 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003289 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003290 * Return FAIL and set "errormsg" or return OK.
3291 */
3292 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003293parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003294{
3295 int address_count = 1;
3296 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003297 int need_check_cursor = FALSE;
3298 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003299
3300 // Repeat for all ',' or ';' separated addresses.
3301 for (;;)
3302 {
3303 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003304 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003305 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003306 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003307 eap->addr_count == 0, address_count++);
3308 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003309 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003310 if (lnum == MAXLNUM)
3311 {
3312 if (*eap->cmd == '%') // '%' - all lines
3313 {
3314 ++eap->cmd;
3315 switch (eap->addr_type)
3316 {
3317 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003318 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003319 eap->line1 = 1;
3320 eap->line2 = curbuf->b_ml.ml_line_count;
3321 break;
3322 case ADDR_LOADED_BUFFERS:
3323 {
3324 buf_T *buf = firstbuf;
3325
3326 while (buf->b_next != NULL
3327 && buf->b_ml.ml_mfp == NULL)
3328 buf = buf->b_next;
3329 eap->line1 = buf->b_fnum;
3330 buf = lastbuf;
3331 while (buf->b_prev != NULL
3332 && buf->b_ml.ml_mfp == NULL)
3333 buf = buf->b_prev;
3334 eap->line2 = buf->b_fnum;
3335 break;
3336 }
3337 case ADDR_BUFFERS:
3338 eap->line1 = firstbuf->b_fnum;
3339 eap->line2 = lastbuf->b_fnum;
3340 break;
3341 case ADDR_WINDOWS:
3342 case ADDR_TABS:
3343 if (IS_USER_CMDIDX(eap->cmdidx))
3344 {
3345 eap->line1 = 1;
3346 eap->line2 = eap->addr_type == ADDR_WINDOWS
3347 ? LAST_WIN_NR : LAST_TAB_NR;
3348 }
3349 else
3350 {
3351 // there is no Vim command which uses '%' and
3352 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003353 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003354 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003355 }
3356 break;
3357 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003358 case ADDR_UNSIGNED:
3359 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003360 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003361 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003362 case ADDR_ARGUMENTS:
3363 if (ARGCOUNT == 0)
3364 eap->line1 = eap->line2 = 0;
3365 else
3366 {
3367 eap->line1 = 1;
3368 eap->line2 = ARGCOUNT;
3369 }
3370 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003371 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003372#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003373 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003374 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003375 if (eap->line2 == 0)
3376 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003377#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003378 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003379 case ADDR_NONE:
3380 // Will give an error later if a range is found.
3381 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003382 }
3383 ++eap->addr_count;
3384 }
3385 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3386 {
3387 pos_T *fp;
3388
3389 // '*' - visual area
3390 if (eap->addr_type != ADDR_LINES)
3391 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003392 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003393 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003394 }
3395
3396 ++eap->cmd;
3397 if (!eap->skip)
3398 {
3399 fp = getmark('<', FALSE);
3400 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003401 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003402 eap->line1 = fp->lnum;
3403 fp = getmark('>', FALSE);
3404 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003405 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003406 eap->line2 = fp->lnum;
3407 ++eap->addr_count;
3408 }
3409 }
3410 }
3411 else
3412 eap->line2 = lnum;
3413 eap->addr_count++;
3414
3415 if (*eap->cmd == ';')
3416 {
3417 if (!eap->skip)
3418 {
3419 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003420
Bram Moolenaar0e717042020-04-27 19:29:01 +02003421 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003422 // accept zero as address, so 0;/PATTERN/ works correctly
3423 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003424 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003425 if (eap->line2 > 0)
3426 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003427 else
3428 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003429 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003430 }
3431 }
3432 else if (*eap->cmd != ',')
3433 break;
3434 ++eap->cmd;
3435 }
3436
3437 // One address given: set start and end lines.
3438 if (eap->addr_count == 1)
3439 {
3440 eap->line1 = eap->line2;
3441 // ... but only implicit: really no address given
3442 if (lnum == MAXLNUM)
3443 eap->addr_count = 0;
3444 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003445 ret = OK;
3446
3447theend:
3448 if (need_check_cursor)
3449 check_cursor();
3450 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003451}
3452
3453/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003454 * Append "cmd" to the error message in IObuff.
3455 * Takes care of limiting the length and handling 0xa0, which would be
3456 * invisible otherwise.
3457 */
3458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003459append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003460{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003461 size_t len = STRLEN(IObuff);
3462 char_u *s = cmd;
3463 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003464
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003465 if (len > IOSIZE - 100)
3466 {
3467 // Not enough space, truncate and put in "...".
3468 d = IObuff + IOSIZE - 100;
3469 d -= mb_head_off(IObuff, d);
3470 STRCPY(d, "...");
3471 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003472 STRCAT(IObuff, ": ");
3473 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003474 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003475 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003476 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003477 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003478 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003479 STRCPY(d, "<a0>");
3480 d += 4;
3481 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003482 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3483 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003484 else
3485 MB_COPY_CHAR(s, d);
3486 }
3487 *d = NUL;
3488}
3489
Dominique Pelle748b3082022-01-08 12:41:16 +00003490#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003491/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003492 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3493 * the name. Otherwise just return "start".
3494 */
3495 char_u *
3496skip_option_env_lead(char_u *start)
3497{
3498 char_u *name = start;
3499
3500 if (*start == '&')
3501 {
3502 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3503 name += 3;
3504 else
3505 name += 1;
3506 }
3507 else if (*start == '$')
3508 name += 1;
3509 return name;
3510}
Dominique Pelle748b3082022-01-08 12:41:16 +00003511#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003512
3513/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003514 * Return TRUE and set "*idx" if "p" points to a one letter command.
3515 * If not in Vim9 script:
3516 * - The 'k' command can directly be followed by any character.
3517 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3518 * but :sre[wind] is another command, as are :scr[iptnames],
3519 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3520 */
3521 static int
3522one_letter_cmd(char_u *p, cmdidx_T *idx)
3523{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003524 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003525 return FALSE;
3526 if (*p == 'k')
3527 {
3528 *idx = CMD_k;
3529 return TRUE;
3530 }
3531 if (p[0] == 's'
3532 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3533 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3534 || p[1] == 'g'
3535 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3536 || p[1] == 'I'
3537 || (p[1] == 'r' && p[2] != 'e')))
3538 {
3539 *idx = CMD_substitute;
3540 return TRUE;
3541 }
3542 return FALSE;
3543}
3544
3545/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003546 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3547 * call.
3548 */
3549 int
3550number_method(char_u *cmd)
3551{
3552 char_u *p = skipdigits(cmd);
3553
3554 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3555}
3556
3557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003559 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003560 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003561 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 *
3563 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3564 * assignment without "let". Sets eap->cmdidx to the command while returning
3565 * "eap->cmd".
3566 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 * Returns NULL for an ambiguous user command.
3568 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 char_u *
3570find_ex_command(
3571 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003572 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003573 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003574 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
3576 int len;
3577 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003578 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003579#ifndef FEAT_EVAL
3580 int vim9 = FALSE;
3581#else
3582 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 /*
3585 * Recognize a Vim9 script function/method call and assignment:
3586 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3587 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003588 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003589 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003591 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003593 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003594 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3595 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003597 int oplen;
3598 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003599 char_u *swp;
3600
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003601 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003602 || (eap->cmd[0] == '$'
3603 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003604 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003605 && (valid_yank_reg(eap->cmd[1], FALSE)
3606 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003607 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003608 if (*eap->cmd == '&')
3609 {
3610 p = eap->cmd + 1;
3611 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3612 p += 2;
3613 p = to_name_end(p, FALSE);
3614 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003615 else if (*eap->cmd == '$')
3616 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003617 else
3618 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003619 if (ends_excmd(*skipwhite(p)))
3620 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003621 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3622 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003623 eap->cmdidx = CMD_eval;
3624 return eap->cmd;
3625 }
3626 // "&option" can be followed by "->" or "=", check below
3627 }
3628
3629 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003631 if (
3632 // "(..." is an expression.
3633 // "funcname(" is always a function call.
3634 *p == '('
3635 || (p == eap->cmd
3636 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003637 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003638 *eap->cmd == '{'
3639 // "'string'->func()" is an expression.
3640 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003641 // '"string"->func()' is an expression.
3642 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003643 // '$"string"->func()' is an expression.
3644 // "$'string'->func()" is an expression.
3645 || (eap->cmd[0] == '$'
3646 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3647 // '0z1234->func()' is an expression.
3648 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003649 // "g:varname" is an expression.
3650 || eap->cmd[1] == ':'
3651 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003652 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003653 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003654 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003655 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3656 {
3657 // "{" by itself is the start of a block.
3658 eap->cmdidx = CMD_block;
3659 return eap->cmd + 1;
3660 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003661 eap->cmdidx = CMD_eval;
3662 return eap->cmd;
3663 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003664
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003665 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003666 // "varname[]" is an expression.
3667 *p == '['
3668 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003669 || (*p == '.'
3670 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3671 // g:[key] is an expression
3672 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003673 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003674 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003675
3676 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003677 // Skip over the whole thing, it can be:
3678 // name.member = val
3679 // name[a : b] = val
3680 // name[idx] = val
3681 // name[idx].member = val
3682 // etc.
3683 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003684 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003685 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003686 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003687 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003688 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003689 || (after[0] == '.' && after[1] == '.'
3690 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003691 eap->cmdidx = CMD_var;
3692 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003693 --emsg_silent;
3694 return eap->cmd;
3695 }
3696
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003697 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3698 // an assignment.
3699 // If there is no line break inside the "[...]" then "p" is
3700 // advanced to after the "]" by to_name_const_end(): check if a "="
3701 // follows.
3702 // If "[...]" has a line break "p" still points at the "[" and it
3703 // can't be an assignment.
3704 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003705 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003706 char_u *eq;
3707
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003708 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003709 if (p == eap->cmd && *p == '[')
3710 {
3711 int count = 0;
3712 int semicolon = FALSE;
3713
3714 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3715 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003716 eq = p;
3717 if (eq != NULL)
3718 {
3719 eq = skipwhite(eq);
3720 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3721 ++eq;
3722 }
3723 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003724 {
3725 eap->cmdidx = CMD_eval;
3726 return eap->cmd;
3727 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003728 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003729 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003730 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003731 return eap->cmd;
3732 }
3733 }
3734
3735 // Recognize an assignment if we recognize the variable name:
3736 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003737 // "@r = expr"
3738 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003739 // "var = expr" where "var" is a variable name or we are skipping
3740 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003741 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003742 oplen = assignment_len(skipwhite(p), &heredoc);
3743 if (oplen > 0)
3744 {
3745 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3746 || *eap->cmd == '&'
3747 || *eap->cmd == '$'
3748 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003749 || (eap->skip && IS_WHITE_OR_NUL(
3750 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003751 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003752 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003753 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003754 return eap->cmd;
3755 }
3756 }
3757
Bram Moolenaar9510d222022-09-11 15:14:05 +01003758 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003759 // "w:varname: number = 123".
3760 if (eap->cmd[1] == ':' && *p == ':')
3761 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003762 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003763 return eap->cmd;
3764 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003765 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003766
Bram Moolenaar31d99482022-05-26 22:24:43 +01003767 // 1234->func() is a method call
3768 if (number_method(eap->cmd))
3769 {
3770 eap->cmdidx = CMD_eval;
3771 return eap->cmd;
3772 }
3773
Bram Moolenaar7b829262021-10-13 15:04:34 +01003774 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3775 // an expression. A global/substitute/list command needs to use a
3776 // longer name.
3777 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3778 {
3779 eap->cmdidx = CMD_eval;
3780 return eap->cmd;
3781 }
3782
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003783 // If it is an ID it might be a variable with an operator on the next
3784 // line, if the variable exists it can't be an Ex command.
3785 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003786 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003787 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3788 {
3789 eap->cmdidx = CMD_eval;
3790 return eap->cmd;
3791 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003792
3793 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003794 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3795 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003796 {
3797 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3798 return eap->cmd + 2;
3799 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 }
3801#endif
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 /*
3804 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 */
3806 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003807 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 ++p;
3810 }
3811 else
3812 {
3813 while (ASCII_ISALPHA(*p))
3814 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003815 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003816 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003818 while (ASCII_ISALNUM(*p))
3819 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 }
3821 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3822 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003823 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 ++p;
3825 while (ASCII_ISALPHA(*p))
3826 ++p;
3827 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003828
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003829 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003830 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 ++p;
3832 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003833 // The "d" command can directly be followed by 'l' or 'p' flag, when
3834 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003835 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003837 // Check for ":dl", ":dell", etc. to ":deletel": that's
3838 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003839 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003840 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003841 break;
3842 if (i == len - 1)
3843 {
3844 --len;
3845 if (p[-1] == 'l')
3846 eap->flags |= EXFLAG_LIST;
3847 else
3848 eap->flags |= EXFLAG_PRINT;
3849 }
3850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003852 if (ASCII_ISLOWER(eap->cmd[0]))
3853 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003854 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003855 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003856
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003857 if (command_count != (int)CMD_SIZE)
3858 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003859 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003860 getout(1);
3861 }
3862
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003863 // Use a precomputed index for fast look-up in cmdnames[]
3864 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003865 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3866 if (ASCII_ISLOWER(c2))
3867 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3868 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003869 else if (ASCII_ISUPPER(eap->cmd[0]))
3870 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003872 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873
3874 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3875 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3876 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3877 (size_t)len) == 0)
3878 {
3879#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003880 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 *full = TRUE;
3882#endif
3883 break;
3884 }
3885
Bram Moolenaar204852a2022-03-05 12:56:44 +00003886 // :Print and :mode are not supported in Vim9 script.
3887 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003888 if (vim9 && eap->cmdidx != CMD_SIZE)
3889 {
3890 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3891 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003892 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003893 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003894 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003895 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003896 eap->cmdidx = CMD_SIZE;
3897 }
3898 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003899
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003900 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003901 // 'cpoptions'.
3902 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3903 p = eap->cmd;
3904
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003905 // Look for a user defined command as a last resort. Let ":Print" be
3906 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003907 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3908 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003910 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 while (ASCII_ISALNUM(*p))
3912 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003913 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003915 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 eap->cmdidx = CMD_SIZE;
3917 }
3918
Bram Moolenaar204852a2022-03-05 12:56:44 +00003919 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3920 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003921 eap->cmdidx = CMD_finally;
3922
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003923#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003924 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003925 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003926 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003927 && (eap->cmdidx < 0 ||
3928 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003929 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003930 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3931
3932 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003933 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003934 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003935 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003936#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003937
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 return p;
3939}
3940
3941#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003942static struct cmdmod
3943{
3944 char *name;
3945 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003946 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003947} cmdmods[] = {
3948 {"aboveleft", 3, FALSE},
3949 {"belowright", 3, FALSE},
3950 {"botright", 2, FALSE},
3951 {"browse", 3, FALSE},
3952 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003953 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003954 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003955 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003956 {"keepalt", 5, FALSE},
3957 {"keepjumps", 5, FALSE},
3958 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003959 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003960 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003961 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003962 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003963 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003964 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003965 {"rightbelow", 6, FALSE},
3966 {"sandbox", 3, FALSE},
3967 {"silent", 3, FALSE},
3968 {"tab", 3, TRUE},
3969 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003970 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003971 {"verbose", 4, TRUE},
3972 {"vertical", 4, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003973 {"vim9cmd", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003974};
3975
3976/*
3977 * Return length of a command modifier (including optional count).
3978 * Return zero when it's not a modifier.
3979 */
3980 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003981modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003982{
3983 int i, j;
3984 char_u *p = cmd;
3985
3986 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003987 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003988 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003989 {
3990 for (j = 0; p[j] != NUL; ++j)
3991 if (p[j] != cmdmods[i].name[j])
3992 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003993 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003994 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003995 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003996 }
3997 return 0;
3998}
3999
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000/*
4001 * Return > 0 if an Ex command "name" exists.
4002 * Return 2 if there is an exact match.
4003 * Return 3 if there is an ambiguous match.
4004 */
4005 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004006cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007{
4008 exarg_T ea;
4009 int full = FALSE;
4010 int i;
4011 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004012 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004014 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02004015 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
4017 for (j = 0; name[j] != NUL; ++j)
4018 if (name[j] != cmdmods[i].name[j])
4019 break;
4020 if (name[j] == NUL && j >= cmdmods[i].minlen)
4021 return (cmdmods[i].name[j] == NUL ? 2 : 1);
4022 }
4023
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004024 // Check built-in commands and user defined commands.
4025 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004026 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004028 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004030 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004032 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4033 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004034 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004035 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4037}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004038
4039/*
4040 * "fullcommand" function
4041 */
4042 void
4043f_fullcommand(typval_T *argvars, typval_T *rettv)
4044{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004045 exarg_T ea;
4046 char_u *name;
4047 char_u *p;
4048 int vim9script = in_vim9script();
4049 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004050
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004051 rettv->v_type = VAR_STRING;
4052 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004053
Bram Moolenaaraa534142022-09-15 21:46:02 +01004054 if (in_vim9script()
4055 && (check_for_string_arg(argvars, 0) == FAIL
4056 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004057 return;
4058
4059 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004060 if (name == NULL)
4061 return;
4062
Bram Moolenaaraa534142022-09-15 21:46:02 +01004063 if (argvars[1].v_type != VAR_UNKNOWN)
4064 {
4065 vim9script = tv_get_bool(&argvars[1]);
4066 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4067 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4068 }
4069
zeertzjqc024ed92022-01-04 16:22:52 +00004070 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004071 name++;
4072 name = skip_range(name, TRUE, NULL);
4073
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004074 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4075 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004076 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004077 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004078 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004079 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004080 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004081 goto theend;
4082
4083 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004084 {
4085 int res;
4086
4087 ++emsg_silent;
4088 res = not_in_vim9(&ea);
4089 --emsg_silent;
4090
4091 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004092 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004093 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004094
4095 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004096 ? get_user_command_name(ea.useridx, ea.cmdidx)
4097 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004098theend:
4099 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004100}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101#endif
4102
Bram Moolenaard0190392019-08-23 21:17:35 +02004103 cmdidx_T
4104excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105{
Bram Moolenaard0190392019-08-23 21:17:35 +02004106 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004108 if (!one_letter_cmd(cmd, &idx))
4109 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4110 idx = (cmdidx_T)((int)idx + 1))
4111 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4112 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
Bram Moolenaard0190392019-08-23 21:17:35 +02004114 return idx;
4115}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116
Bram Moolenaard0190392019-08-23 21:17:35 +02004117 long
4118excmd_get_argt(cmdidx_T idx)
4119{
4120 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121}
4122
4123/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004124 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 *
4126 * Backslashed delimiters after / or ? will be skipped, and commands will
4127 * not be expanded between /'s and ?'s or after "'".
4128 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004129 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 * Returns the "cmd" pointer advanced to beyond the range.
4131 */
4132 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004133skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004134 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004135 int skip_star, // skip "*" used for Visual range
4136 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004138 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004139 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004141 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004143 if (*cmd == '\\')
4144 {
4145 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4146 ++cmd;
4147 else
4148 break;
4149 }
4150 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004152 char_u *p = cmd;
4153
4154 // a quote is only valid at the start or after a separator
4155 while (p > cmd_start)
4156 {
4157 --p;
4158 if (!VIM_ISWHITE(*p))
4159 break;
4160 }
4161 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4162 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 if (*++cmd == NUL && ctx != NULL)
4164 *ctx = EXPAND_NOTHING;
4165 }
4166 else if (*cmd == '/' || *cmd == '?')
4167 {
4168 delim = *cmd++;
4169 while (*cmd != NUL && *cmd != delim)
4170 if (*cmd++ == '\\' && *cmd != NUL)
4171 ++cmd;
4172 if (*cmd == NUL && ctx != NULL)
4173 *ctx = EXPAND_NOTHING;
4174 }
4175 if (*cmd != NUL)
4176 ++cmd;
4177 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004178
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004179 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004180 while (*cmd == ':')
4181 cmd = skipwhite(cmd + 1);
4182
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004183 // Skip "*" used for Visual range.
4184 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4185 cmd = skipwhite(cmd + 1);
4186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 return cmd;
4188}
4189
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004190 static void
4191addr_error(cmd_addr_T addr_type)
4192{
4193 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004194 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004195 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004196 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004197}
4198
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004200 * Return the default address for an address type.
4201 */
4202 static linenr_T
4203default_address(exarg_T *eap)
4204{
4205 linenr_T lnum = 0;
4206
4207 switch (eap->addr_type)
4208 {
4209 case ADDR_LINES:
4210 case ADDR_OTHER:
4211 // Default is the cursor line number. Avoid using an invalid
4212 // line number though.
4213 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4214 lnum = curbuf->b_ml.ml_line_count;
4215 else
4216 lnum = curwin->w_cursor.lnum;
4217 break;
4218 case ADDR_WINDOWS:
4219 lnum = CURRENT_WIN_NR;
4220 break;
4221 case ADDR_ARGUMENTS:
4222 lnum = curwin->w_arg_idx + 1;
4223 if (lnum > ARGCOUNT)
4224 lnum = ARGCOUNT;
4225 break;
4226 case ADDR_LOADED_BUFFERS:
4227 case ADDR_BUFFERS:
4228 lnum = curbuf->b_fnum;
4229 break;
4230 case ADDR_TABS:
4231 lnum = CURRENT_TAB_NR;
4232 break;
4233 case ADDR_TABS_RELATIVE:
4234 case ADDR_UNSIGNED:
4235 lnum = 1;
4236 break;
4237 case ADDR_QUICKFIX:
4238#ifdef FEAT_QUICKFIX
4239 lnum = qf_get_cur_idx(eap);
4240#endif
4241 break;
4242 case ADDR_QUICKFIX_VALID:
4243#ifdef FEAT_QUICKFIX
4244 lnum = qf_get_cur_valid_idx(eap);
4245#endif
4246 break;
4247 case ADDR_NONE:
4248 // Will give an error later if a range is found.
4249 break;
4250 }
4251 return lnum;
4252}
4253
4254/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004255 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 *
4257 * Set ptr to the next character after the part that was interpreted.
4258 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004259 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 *
4261 * Return MAXLNUM when no Ex address was found.
4262 */
4263 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004264get_address(
4265 exarg_T *eap UNUSED,
4266 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004267 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004268 int skip, // only skip the address, don't use it
4269 int silent, // no errors or side effects
4270 int to_other_file, // flag: may jump to other file
4271 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272{
4273 int c;
4274 int i;
4275 long n;
4276 char_u *cmd;
4277 pos_T pos;
4278 pos_T *fp;
4279 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004280 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
4282 cmd = skipwhite(*ptr);
4283 lnum = MAXLNUM;
4284 do
4285 {
4286 switch (*cmd)
4287 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004288 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004289 ++cmd;
4290 switch (addr_type)
4291 {
4292 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004293 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 lnum = curwin->w_cursor.lnum;
4295 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004296 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004297 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004298 break;
4299 case ADDR_ARGUMENTS:
4300 lnum = curwin->w_arg_idx + 1;
4301 break;
4302 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004303 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004304 lnum = curbuf->b_fnum;
4305 break;
4306 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004307 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004308 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004309 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004310 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004311 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004312 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004313 cmd = NULL;
4314 goto error;
4315 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004316 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004317#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004318 lnum = qf_get_cur_idx(eap);
4319#endif
4320 break;
4321 case ADDR_QUICKFIX_VALID:
4322#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004323 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004324#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004325 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004326 }
4327 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004329 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004330 ++cmd;
4331 switch (addr_type)
4332 {
4333 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004334 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 lnum = curbuf->b_ml.ml_line_count;
4336 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004337 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004338 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004339 break;
4340 case ADDR_ARGUMENTS:
4341 lnum = ARGCOUNT;
4342 break;
4343 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004344 buf = lastbuf;
4345 while (buf->b_ml.ml_mfp == NULL)
4346 {
4347 if (buf->b_prev == NULL)
4348 break;
4349 buf = buf->b_prev;
4350 }
4351 lnum = buf->b_fnum;
4352 break;
4353 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004354 lnum = lastbuf->b_fnum;
4355 break;
4356 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004357 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004359 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004360 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004361 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004362 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004363 cmd = NULL;
4364 goto error;
4365 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004366 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004367#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004368 lnum = qf_get_size(eap);
4369 if (lnum == 0)
4370 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004371#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004372 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004373 case ADDR_QUICKFIX_VALID:
4374#ifdef FEAT_QUICKFIX
4375 lnum = qf_get_valid_size(eap);
4376 if (lnum == 0)
4377 lnum = 1;
4378#endif
4379 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004380 }
4381 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004383 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004384 if (*++cmd == NUL)
4385 {
4386 cmd = NULL;
4387 goto error;
4388 }
4389 if (addr_type != ADDR_LINES)
4390 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004391 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004392 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004393 goto error;
4394 }
4395 if (skip)
4396 ++cmd;
4397 else
4398 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004399 // Only accept a mark in another file when it is
4400 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004401 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4402 ++cmd;
4403 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004404 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004405 lnum = curwin->w_cursor.lnum;
4406 else
4407 {
4408 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 {
4410 cmd = NULL;
4411 goto error;
4412 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004413 lnum = fp->lnum;
4414 }
4415 }
4416 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004419 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 c = *cmd++;
4421 if (addr_type != ADDR_LINES)
4422 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004423 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004424 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004425 goto error;
4426 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004427 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004428 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004429 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004430 if (*cmd == c)
4431 ++cmd;
4432 }
4433 else
4434 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004435 int flags;
4436
4437 pos = curwin->w_cursor; // save curwin->w_cursor
4438
4439 // When '/' or '?' follows another address, start from
4440 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004441 if (lnum > 0 && lnum != MAXLNUM)
4442 curwin->w_cursor.lnum =
4443 lnum > curbuf->b_ml.ml_line_count
4444 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004445
4446 // Start a forward search at the end of the line (unless
4447 // before the first line).
4448 // Start a backward search at the start of the line.
4449 // This makes sure we never match in the current
4450 // line, and can match anywhere in the
4451 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004452 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 curwin->w_cursor.col = MAXCOL;
4454 else
4455 curwin->w_cursor.col = 0;
4456 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004457 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004458 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 {
4460 curwin->w_cursor = pos;
4461 cmd = NULL;
4462 goto error;
4463 }
4464 lnum = curwin->w_cursor.lnum;
4465 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004466 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004467 cmd += searchcmdlen;
4468 }
4469 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004471 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 ++cmd;
4473 if (addr_type != ADDR_LINES)
4474 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004475 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004476 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004477 goto error;
4478 }
4479 if (*cmd == '&')
4480 i = RE_SUBST;
4481 else if (*cmd == '?' || *cmd == '/')
4482 i = RE_SEARCH;
4483 else
4484 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004485 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 cmd = NULL;
4487 goto error;
4488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 if (!skip)
4491 {
4492 /*
4493 * When search follows another address, start from
4494 * there.
4495 */
4496 if (lnum != MAXLNUM)
4497 pos.lnum = lnum;
4498 else
4499 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004501 /*
4502 * Start the search just like for the above
4503 * do_search().
4504 */
4505 if (*cmd != '?')
4506 pos.col = MAXCOL;
4507 else
4508 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004509 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004510 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004511 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004512 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004513 lnum = pos.lnum;
4514 else
4515 {
4516 cmd = NULL;
4517 goto error;
4518 }
4519 }
4520 ++cmd;
4521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
4523 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004524 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004525 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527
4528 for (;;)
4529 {
4530 cmd = skipwhite(cmd);
4531 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4532 break;
4533
4534 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004535 {
4536 switch (addr_type)
4537 {
4538 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004539 case ADDR_OTHER:
4540 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004541 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004542 break;
4543 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004544 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 break;
4546 case ADDR_ARGUMENTS:
4547 lnum = curwin->w_arg_idx + 1;
4548 break;
4549 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004550 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004551 lnum = curbuf->b_fnum;
4552 break;
4553 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004554 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004555 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004556 case ADDR_TABS_RELATIVE:
4557 lnum = 1;
4558 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004559 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004560#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004561 lnum = qf_get_cur_idx(eap);
4562#endif
4563 break;
4564 case ADDR_QUICKFIX_VALID:
4565#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004566 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004567#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004568 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004569 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004570 case ADDR_UNSIGNED:
4571 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004572 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004573 }
4574 }
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004577 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 else
4579 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004580 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 n = 1;
4582 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004583 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004585 if (n == MAXLNUM)
4586 {
4587 emsg(_(e_line_number_out_of_range));
4588 goto error;
4589 }
4590 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004591
4592 if (addr_type == ADDR_TABS_RELATIVE)
4593 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004594 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004595 cmd = NULL;
4596 goto error;
4597 }
4598 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004599 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004600 lnum = compute_buffer_local_count(
4601 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004603 {
4604#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004605 // Relative line addressing, need to adjust for folded lines
4606 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004607 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004608 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004609 (void)hasFolding(lnum, NULL, &lnum);
4610#endif
4611 if (i == '-')
4612 lnum -= n;
4613 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004614 {
4615 if (n >= LONG_MAX - lnum)
4616 {
4617 emsg(_(e_line_number_out_of_range));
4618 goto error;
4619 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004620 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004621 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
4624 } while (*cmd == '/' || *cmd == '?');
4625
4626error:
4627 *ptr = cmd;
4628 return lnum;
4629}
4630
4631/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004632 * Set eap->line1 and eap->line2 to the whole range.
4633 * Used for commands with the EX_DFLALL flag and no range given.
4634 */
4635 static void
4636address_default_all(exarg_T *eap)
4637{
4638 eap->line1 = 1;
4639 switch (eap->addr_type)
4640 {
4641 case ADDR_LINES:
4642 case ADDR_OTHER:
4643 eap->line2 = curbuf->b_ml.ml_line_count;
4644 break;
4645 case ADDR_LOADED_BUFFERS:
4646 {
4647 buf_T *buf = firstbuf;
4648
4649 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4650 buf = buf->b_next;
4651 eap->line1 = buf->b_fnum;
4652 buf = lastbuf;
4653 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4654 buf = buf->b_prev;
4655 eap->line2 = buf->b_fnum;
4656 }
4657 break;
4658 case ADDR_BUFFERS:
4659 eap->line1 = firstbuf->b_fnum;
4660 eap->line2 = lastbuf->b_fnum;
4661 break;
4662 case ADDR_WINDOWS:
4663 eap->line2 = LAST_WIN_NR;
4664 break;
4665 case ADDR_TABS:
4666 eap->line2 = LAST_TAB_NR;
4667 break;
4668 case ADDR_TABS_RELATIVE:
4669 eap->line2 = 1;
4670 break;
4671 case ADDR_ARGUMENTS:
4672 if (ARGCOUNT == 0)
4673 eap->line1 = eap->line2 = 0;
4674 else
4675 eap->line2 = ARGCOUNT;
4676 break;
4677 case ADDR_QUICKFIX_VALID:
4678#ifdef FEAT_QUICKFIX
4679 eap->line2 = qf_get_valid_size(eap);
4680 if (eap->line2 == 0)
4681 eap->line2 = 1;
4682#endif
4683 break;
4684 case ADDR_NONE:
4685 case ADDR_UNSIGNED:
4686 case ADDR_QUICKFIX:
4687 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4688 break;
4689 }
4690}
4691
4692
4693/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004694 * Get flags from an Ex command argument.
4695 */
4696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004697get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004698{
4699 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4700 {
4701 if (*eap->arg == 'l')
4702 eap->flags |= EXFLAG_LIST;
4703 else if (*eap->arg == 'p')
4704 eap->flags |= EXFLAG_PRINT;
4705 else
4706 eap->flags |= EXFLAG_NR;
4707 eap->arg = skipwhite(eap->arg + 1);
4708 }
4709}
4710
4711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 * Function called for command which is Not Implemented. NI!
4713 */
4714 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004715ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004718 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004719 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720}
4721
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004722#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723/*
4724 * Function called for script command which is Not Implemented. NI!
4725 * Skips over ":perl <<EOF" constructs.
4726 */
4727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004728ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
4730 if (!eap->skip)
4731 ex_ni(eap);
4732 else
4733 vim_free(script_get(eap, eap->arg));
4734}
4735#endif
4736
4737/*
4738 * Check range in Ex command for validity.
4739 * Return NULL when valid, error message when invalid.
4740 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004741 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004742invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004744 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004745
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 if ( eap->line1 < 0
4747 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004748 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004749 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004750
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004751 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004752 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004753 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004754 {
4755 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004756 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004757#ifdef FEAT_DIFF
4758 + (eap->cmdidx == CMD_diffget)
4759#endif
4760 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004761 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004762 break;
4763 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004764 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004765 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004766 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004767 break;
4768 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004769 // Only a boundary check, not whether the buffers actually
4770 // exist.
4771 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004772 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004773 break;
4774 case ADDR_LOADED_BUFFERS:
4775 buf = firstbuf;
4776 while (buf->b_ml.ml_mfp == NULL)
4777 {
4778 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004779 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004780 buf = buf->b_next;
4781 }
4782 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004783 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004784 buf = lastbuf;
4785 while (buf->b_ml.ml_mfp == NULL)
4786 {
4787 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004788 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004789 buf = buf->b_prev;
4790 }
4791 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004792 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004793 break;
4794 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004795 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004796 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004797 break;
4798 case ADDR_TABS:
4799 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004800 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004801 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004802 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004803 case ADDR_OTHER:
4804 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004805 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004806 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004807#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004808 // No error for value that is too big, will use the last entry.
4809 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004810 {
4811 if (eap->addr_count == 0)
4812 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004813 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004814 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004815#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004816 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004817 case ADDR_QUICKFIX_VALID:
4818#ifdef FEAT_QUICKFIX
4819 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4820 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004821 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004822#endif
4823 break;
4824 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004825 case ADDR_NONE:
4826 // Will give an error elsewhere.
4827 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004828 }
4829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 return NULL;
4831}
4832
4833/*
4834 * Correct the range for zero line number, if required.
4835 */
4836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004837correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004839 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
4841 if (eap->line1 == 0)
4842 eap->line1 = 1;
4843 if (eap->line2 == 0)
4844 eap->line2 = 1;
4845 }
4846}
4847
Bram Moolenaar748bf032005-02-02 23:04:36 +00004848#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004849/*
4850 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4851 * pattern. Otherwise return eap->arg.
4852 */
4853 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004854skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004855{
4856 char_u *p = eap->arg;
4857
Bram Moolenaara37420f2006-02-04 22:37:47 +00004858 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4859 || eap->cmdidx == CMD_vimgrepadd
4860 || eap->cmdidx == CMD_lvimgrepadd
4861 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004862 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004863 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004864 if (p == NULL)
4865 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004866 }
4867 return p;
4868}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004869
4870/*
4871 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4872 * in the command line, so that things like % get expanded.
4873 */
4874 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004875replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004876{
4877 char_u *new_cmdline;
4878 char_u *program;
4879 char_u *pos;
4880 char_u *ptr;
4881 int len;
4882 int i;
4883
4884 /*
4885 * Don't do it when ":vimgrep" is used for ":grep".
4886 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004887 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4888 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4889 || eap->cmdidx == CMD_grepadd
4890 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004891 && !grep_internal(eap->cmdidx))
4892 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004893 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4894 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004895 {
4896 if (*curbuf->b_p_gp == NUL)
4897 program = p_gp;
4898 else
4899 program = curbuf->b_p_gp;
4900 }
4901 else
4902 {
4903 if (*curbuf->b_p_mp == NUL)
4904 program = p_mp;
4905 else
4906 program = curbuf->b_p_mp;
4907 }
4908
4909 p = skipwhite(p);
4910
4911 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4912 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004913 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004914 i = 1;
4915 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4916 ++i;
4917 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004918 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004919 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004920 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004921 ptr = new_cmdline;
4922 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4923 {
4924 i = (int)(pos - program);
4925 STRNCPY(ptr, program, i);
4926 STRCPY(ptr += i, p);
4927 ptr += len;
4928 program = pos + 2;
4929 }
4930 STRCPY(ptr, program);
4931 }
4932 else
4933 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004934 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004935 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004936 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004937 STRCPY(new_cmdline, program);
4938 STRCAT(new_cmdline, " ");
4939 STRCAT(new_cmdline, p);
4940 }
4941 msg_make(p);
4942
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004943 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004944 vim_free(*cmdlinep);
4945 *cmdlinep = new_cmdline;
4946 p = new_cmdline;
4947 }
4948 return p;
4949}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004950#endif
4951
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952/*
4953 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004954 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 * Return FAIL for failure, OK otherwise.
4956 */
4957 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004958expand_filename(
4959 exarg_T *eap,
4960 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004961 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004963 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 char_u *repl;
4965 int srclen;
4966 char_u *p;
4967 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004968 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
Bram Moolenaar748bf032005-02-02 23:04:36 +00004970#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004971 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004972 p = skip_grep_pat(eap);
4973#else
4974 p = eap->arg;
4975#endif
4976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 /*
4978 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4979 * the file name contains a wildcard it should not cause expanding.
4980 * (it will be expanded anyway if there is a wildcard before replacing).
4981 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004982 has_wildcards = mch_has_wildcard(p);
4983 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004985#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004986 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004987 if (p[0] == '`' && p[1] == '=')
4988 {
4989 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004990 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004991 if (*p == '`')
4992 ++p;
4993 continue;
4994 }
4995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 /*
4997 * Quick check if this cannot be the start of a special string.
4998 * Also removes backslash before '%', '#' and '<'.
4999 */
5000 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5001 {
5002 ++p;
5003 continue;
5004 }
5005
5006 /*
5007 * Try to find a match at this position.
5008 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005009 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005010 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005011 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005013 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 {
5015 p += srclen;
5016 continue;
5017 }
5018
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005019 // Wildcards won't be expanded below, the replacement is taken
5020 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005021 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5022 {
5023 char_u *l = repl;
5024
5025 repl = expand_env_save(repl);
5026 vim_free(l);
5027 }
5028
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005029 // Need to escape white space et al. with a backslash.
5030 // Don't do this for:
5031 // - replacement that already has been escaped: "##"
5032 // - shell commands (may have to use quotes instead).
5033 // - non-unix systems when there is a single argument (spaces don't
5034 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005036 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 && eap->cmdidx != CMD_grep
5039 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005040 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005041 && eap->cmdidx != CMD_lgrep
5042 && eap->cmdidx != CMD_lgrepadd
5043 && eap->cmdidx != CMD_lmake
5044 && eap->cmdidx != CMD_make
5045 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005047 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048#endif
5049 )
5050 {
5051 char_u *l;
5052#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005053 // Don't escape a backslash here, because rem_backslash() doesn't
5054 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 static char_u *nobslash = (char_u *)" \t\"|";
5056# define ESCAPE_CHARS nobslash
5057#else
5058# define ESCAPE_CHARS escape_chars
5059#endif
5060
5061 for (l = repl; *l; ++l)
5062 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5063 {
5064 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5065 if (l != NULL)
5066 {
5067 vim_free(repl);
5068 repl = l;
5069 }
5070 break;
5071 }
5072 }
5073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005074 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005075 if ((eap->usefilter || eap->cmdidx == CMD_bang
5076 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005077 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
5079 char_u *l;
5080
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005081 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 if (l != NULL)
5083 {
5084 vim_free(repl);
5085 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087 }
5088
5089 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5090 vim_free(repl);
5091 if (p == NULL)
5092 return FAIL;
5093 }
5094
5095 /*
5096 * One file argument: Expand wildcards.
5097 * Don't do this with ":r !command" or ":w !command".
5098 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005099 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
5101 /*
5102 * May do this twice:
5103 * 1. Replace environment variables.
5104 * 2. Replace any other wildcards, remove backslashes.
5105 */
5106 for (n = 1; n <= 2; ++n)
5107 {
5108 if (n == 2)
5109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 /*
5111 * Halve the number of backslashes (this is Vi compatible).
5112 * For Unix and OS/2, when wildcards are expanded, this is
5113 * done by ExpandOne() below.
5114 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005115#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 if (!has_wildcards)
5117#endif
5118 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120
5121 if (has_wildcards)
5122 {
5123 if (n == 1)
5124 {
5125 /*
5126 * First loop: May expand environment variables. This
5127 * can be done much faster with expand_env() than with
5128 * something else (e.g., calling a shell).
5129 * After expanding environment variables, check again
5130 * if there are still wildcards present.
5131 */
5132 if (vim_strchr(eap->arg, '$') != NULL
5133 || vim_strchr(eap->arg, '~') != NULL)
5134 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005135 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005136 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 has_wildcards = mch_has_wildcard(NameBuff);
5138 p = NameBuff;
5139 }
5140 else
5141 p = NULL;
5142 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005143 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005146 int options = WILD_LIST_NOTFOUND
5147 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149 ExpandInit(&xpc);
5150 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005151 if (p_wic)
5152 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005154 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 if (p == NULL)
5156 return FAIL;
5157 }
5158 if (p != NULL)
5159 {
5160 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5161 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005162 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 vim_free(p);
5164 }
5165 }
5166 }
5167 }
5168 return OK;
5169}
5170
5171/*
5172 * Replace part of the command line, keeping eap->cmd, eap->arg and
5173 * eap->nextcmd correct.
5174 * "src" points to the part that is to be replaced, of length "srclen".
5175 * "repl" is the replacement string.
5176 * Returns a pointer to the character after the replaced string.
5177 * Returns NULL for failure.
5178 */
5179 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005180repl_cmdline(
5181 exarg_T *eap,
5182 char_u *src,
5183 int srclen,
5184 char_u *repl,
5185 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
5187 int len;
5188 int i;
5189 char_u *new_cmdline;
5190
5191 /*
5192 * The new command line is build in new_cmdline[].
5193 * First allocate it.
5194 * Careful: a "+cmd" argument may have been NUL terminated.
5195 */
5196 len = (int)STRLEN(repl);
5197 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005198 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005199 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005200 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005201 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
5203 /*
5204 * Copy the stuff before the expanded part.
5205 * Copy the expanded stuff.
5206 * Copy what came after the expanded part.
5207 * Copy the next commands, if there are any.
5208 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005209 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005211
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005213 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005215 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005217 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
5219 i = (int)STRLEN(new_cmdline) + 1;
5220 STRCPY(new_cmdline + i, eap->nextcmd);
5221 eap->nextcmd = new_cmdline + i;
5222 }
5223 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5224 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5225 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5226 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5227 vim_free(*cmdlinep);
5228 *cmdlinep = new_cmdline;
5229
5230 return src;
5231}
5232
5233/*
5234 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005235 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 */
5237 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005238separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240 char_u *p;
5241
Bram Moolenaar86b68352004-12-27 21:59:20 +00005242#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005243 p = skip_grep_pat(eap);
5244#else
5245 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005246#endif
5247
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005248 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 {
5250 if (*p == Ctrl_V)
5251 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005252 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005253 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005255 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005256 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005257 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 break;
5259 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005260
5261#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005262 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005263 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005264 {
5265 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005266 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005267 if (*p == NUL) // stop at NUL after CTRL-V
5268 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005269 }
5270#endif
5271
Bram Moolenaareda29c92022-10-02 12:59:00 +01005272 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005273 // :@" and :*" do not start a comment!
5274 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005275 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005276 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005277 && !(eap->argt & EX_NOTRLCOM)
5278 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5279 || p != eap->arg)
5280 && (eap->cmdidx != CMD_redir
5281 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005282 || (*p == '#'
5283 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005284 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005285 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 || *p == '|' || *p == '\n')
5287 {
5288 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005289 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 * AND 'b' is present in 'cpoptions'.
5291 */
5292 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005293 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005295 if (!keep_backslash)
5296 {
5297 STRMOVE(p - 1, p); // remove the '\'
5298 --p;
5299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
5301 else
5302 {
5303 eap->nextcmd = check_nextcmd(p);
5304 *p = NUL;
5305 break;
5306 }
5307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005310 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 del_trailing_spaces(eap->arg);
5312}
5313
5314/*
5315 * get + command from ex argument
5316 */
5317 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005318getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
5320 char_u *arg = *argp;
5321 char_u *command = NULL;
5322
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005323 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
5325 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005326 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 command = dollar_command;
5328 else
5329 {
5330 command = arg;
5331 arg = skip_cmd_arg(command, TRUE);
5332 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005333 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005336 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 *argp = arg;
5338 }
5339 return command;
5340}
5341
5342/*
5343 * Find end of "+command" argument. Skip over "\ " and "\\".
5344 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005345 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005346skip_cmd_arg(
5347 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005348 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
5350 while (*p && !vim_isspace(*p))
5351 {
5352 if (*p == '\\' && p[1] != NUL)
5353 {
5354 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005355 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 else
5357 ++p;
5358 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005359 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361 return p;
5362}
5363
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005364 int
5365get_bad_opt(char_u *p, exarg_T *eap)
5366{
5367 if (STRICMP(p, "keep") == 0)
5368 eap->bad_char = BAD_KEEP;
5369 else if (STRICMP(p, "drop") == 0)
5370 eap->bad_char = BAD_DROP;
5371 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5372 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005373 else
5374 return FAIL;
5375 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005376}
5377
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378/*
5379 * Get "++opt=arg" argument.
5380 * Return FAIL or OK.
5381 */
5382 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005383getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384{
5385 char_u *arg = eap->arg + 2;
5386 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005387 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005390 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5392 {
5393 if (*arg == 'n')
5394 {
5395 arg += 2;
5396 eap->force_bin = FORCE_NOBIN;
5397 }
5398 else
5399 eap->force_bin = FORCE_BIN;
5400 if (!checkforcmd(&arg, "binary", 3))
5401 return FAIL;
5402 eap->arg = skipwhite(arg);
5403 return OK;
5404 }
5405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005406 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005407 if (STRNCMP(arg, "edit", 4) == 0)
5408 {
5409 eap->read_edit = TRUE;
5410 eap->arg = skipwhite(arg + 4);
5411 return OK;
5412 }
5413
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 if (STRNCMP(arg, "ff", 2) == 0)
5415 {
5416 arg += 2;
5417 pp = &eap->force_ff;
5418 }
5419 else if (STRNCMP(arg, "fileformat", 10) == 0)
5420 {
5421 arg += 10;
5422 pp = &eap->force_ff;
5423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 else if (STRNCMP(arg, "enc", 3) == 0)
5425 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005426 if (STRNCMP(arg, "encoding", 8) == 0)
5427 arg += 8;
5428 else
5429 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 pp = &eap->force_enc;
5431 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005432 else if (STRNCMP(arg, "bad", 3) == 0)
5433 {
5434 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005435 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437
5438 if (pp == NULL || *arg != '=')
5439 return FAIL;
5440
5441 ++arg;
5442 *pp = (int)(arg - eap->cmd);
5443 arg = skip_cmd_arg(arg, FALSE);
5444 eap->arg = skipwhite(arg);
5445 *arg = NUL;
5446
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (pp == &eap->force_ff)
5448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5450 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005451 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005453 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005455 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5457 *p = TOLOWER_ASC(*p);
5458 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005459 else
5460 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005461 // Check ++bad= argument. Must be a single-byte character, "keep" or
5462 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005463 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005464 return FAIL;
5465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
5467 return OK;
5468}
5469
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005471ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
5473 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005474 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 * directory for security reasons.
5476 */
5477 if (secure)
5478 {
5479 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005480 eap->errmsg =
5481 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
5483 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005484 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 else
5486 do_augroup(eap->arg, eap->forceit);
5487}
5488
5489/*
5490 * ":doautocmd": Apply the automatic commands to the current buffer.
5491 */
5492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005493ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005495 char_u *arg = eap->arg;
5496 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005497 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005498
Bram Moolenaar1610d052016-06-09 22:53:01 +02005499 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005500 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005501 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005502 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505/*
5506 * :[N]bunload[!] [N] [bufname] unload buffer
5507 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5508 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5509 */
5510 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005511ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005513 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005514 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 eap->errmsg = do_bufdel(
5516 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5517 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5518 : DOBUF_UNLOAD, eap->arg,
5519 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5520}
5521
5522/*
5523 * :[N]buffer [N] to buffer N
5524 * :[N]sbuffer [N] to buffer N
5525 */
5526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005527ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005529 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005530 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005532 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 else
5534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005535 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5537 else
5538 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005539 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005540 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
5542}
5543
5544/*
5545 * :[N]bmodified [N] to next mod. buffer
5546 * :[N]sbmodified [N] to next mod. buffer
5547 */
5548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005549ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
5551 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005552 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005553 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554}
5555
5556/*
5557 * :[N]bnext [N] to next buffer
5558 * :[N]sbnext [N] split and to next buffer
5559 */
5560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005561ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005563 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005564 return;
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005567 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005568 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569}
5570
5571/*
5572 * :[N]bNext [N] to previous buffer
5573 * :[N]bprevious [N] to previous buffer
5574 * :[N]sbNext [N] split and to previous buffer
5575 * :[N]sbprevious [N] split and to previous buffer
5576 */
5577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005580 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005581 return;
5582
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005584 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005585 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586}
5587
5588/*
5589 * :brewind to first buffer
5590 * :bfirst to first buffer
5591 * :sbrewind split and to first buffer
5592 * :sbfirst split and to first buffer
5593 */
5594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005595ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005597 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005598 return;
5599
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005601 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005602 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603}
5604
5605/*
5606 * :blast to last buffer
5607 * :sblast split and to last buffer
5608 */
5609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005610ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005612 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005613 return;
5614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005616 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005617 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
Bram Moolenaar7a092242020-04-16 22:10:49 +02005620/*
5621 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005622 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005627 int comment_char = '"';
5628
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005629 if (in_vim9script())
5630 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005631 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005632}
5633
5634/*
5635 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5636 * to "cmd_start" or has a white space character before it.
5637 */
5638 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005639ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005640{
5641 int c = *cmd;
5642
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005643 if (c == NUL || c == '|' || c == '\n')
5644 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005645 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005646 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5647 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005648 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005649 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650}
5651
5652#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5653 || defined(PROTO)
5654/*
5655 * Return the next command, after the first '|' or '\n'.
5656 * Return NULL if not found.
5657 */
5658 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
5661 while (*p != '|' && *p != '\n')
5662 {
5663 if (*p == NUL)
5664 return NULL;
5665 ++p;
5666 }
5667 return (p + 1);
5668}
5669#endif
5670
5671/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005672 * Check if *p is a separator between Ex commands, skipping over white space.
5673 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 */
5675 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005676check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005678 char_u *s = skipwhite(p);
5679
5680 if (*s == '|' || *s == '\n')
5681 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 else
5683 return NULL;
5684}
5685
5686/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005687 * If "eap->nextcmd" is not set, check for a next command at "p".
5688 */
5689 void
5690set_nextcmd(exarg_T *eap, char_u *arg)
5691{
5692 char_u *p = check_nextcmd(arg);
5693
5694 if (eap->nextcmd == NULL)
5695 eap->nextcmd = p;
5696 else if (p != NULL)
5697 // cannot use "| command" inside a {} block
5698 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5699}
5700
5701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 * - if there are more files to edit
5703 * - and this is the last window
5704 * - and forceit not used
5705 * - and not repeated twice on a row
5706 * return FAIL and give error message if 'message' TRUE
5707 * return OK otherwise
5708 */
5709 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005710check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005711 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005712 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
5714 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5715
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005716 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005717 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 if (message)
5720 {
5721#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005722 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5723 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005725 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005727 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5728 NGETTEXT("%d more file to edit. Quit anyway?",
5729 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5731 return OK;
5732 return FAIL;
5733 }
5734#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005735 semsg(NGETTEXT(e_nr_more_file_to_edit,
5736 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005737 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739 return FAIL;
5740 }
5741 return OK;
5742}
5743
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744/*
5745 * Function given to ExpandGeneric() to obtain the list of command names.
5746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005748get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005751 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 return cmdnames[idx].cmd_name;
5753}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005756ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757{
Bram Moolenaare6850792010-05-14 15:28:44 +02005758 if (*eap->arg == NUL)
5759 {
5760#ifdef FEAT_EVAL
5761 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5762 char_u *p = NULL;
5763
5764 if (expr != NULL)
5765 {
5766 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005767 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005768 --emsg_off;
5769 vim_free(expr);
5770 }
5771 if (p != NULL)
5772 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005773 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005774 vim_free(p);
5775 }
5776 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005777 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005778#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005779 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005780#endif
5781 }
5782 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005783 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005784
5785#ifdef FEAT_VTP
5786 else if (has_vtp_working())
5787 {
5788 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005789 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005790 redrawcmd();
5791 }
5792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793}
5794
5795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005796ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797{
5798 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005799 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 do_highlight(eap->arg, eap->forceit, FALSE);
5801}
5802
5803
5804/*
5805 * Call this function if we thought we were going to exit, but we won't
5806 * (because of an error). May need to restore the terminal mode.
5807 */
5808 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005809not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
5811 exiting = FALSE;
5812 settmode(TMODE_RAW);
5813}
5814
Bram Moolenaar3552e742021-05-29 12:21:58 +02005815 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005816before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5817{
5818 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5819
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005820 // Bail out when autocommands closed the window.
5821 // Refuse to quit when the buffer in the last window is being closed (can
5822 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005823 if (!win_valid(wp)
5824 || curbuf_locked()
5825 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5826 return TRUE;
5827
5828 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5829 {
5830 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005831 // Refuse to quit when locked or when the window was closed or the
5832 // buffer in the last window is being closed (can only happen in
5833 // autocommands).
5834 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005835 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5836 return TRUE;
5837 }
5838
5839 return FALSE;
5840}
5841
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005843 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005844 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005845 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005847 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005848ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005850 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005851
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852#ifdef FEAT_CMDWIN
5853 if (cmdwin_type != 0)
5854 {
5855 cmdwin_result = Ctrl_C;
5856 return;
5857 }
5858#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005859 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005860 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005861 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005862 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005863 return;
5864 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005865 if (eap->addr_count > 0)
5866 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005867 int wnr = eap->line2;
5868
5869 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5870 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005871 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005872 }
5873 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005874 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005875
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005876 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005877 if (curbuf_locked())
5878 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005879
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005880 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005881 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005882 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
5884#ifdef FEAT_NETBEANS_INTG
5885 netbeansForcedQuit = eap->forceit;
5886#endif
5887
5888 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005889 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 */
5891 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5892 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005893 if ((!buf_hide(wp->w_buffer)
5894 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005895 | (eap->forceit ? CCGD_FORCEIT : 0)
5896 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005898 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {
5900 not_exiting();
5901 }
5902 else
5903 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005904 // quit last window
5905 // Note: only_one_window() returns true, even so a help window is
5906 // still open. In that case only quit, if no address has been
5907 // specified. Example:
5908 // :h|wincmd w|1q - don't quit
5909 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005910 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005912 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005913#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005915#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005916 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005917 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
5919}
5920
5921/*
5922 * ":cquit".
5923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005925ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005927 // this does not always pass on the exit code to the Manx compiler. why?
5928 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929}
5930
5931/*
5932 * ":qall": try to quit all windows
5933 */
5934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005935ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937# ifdef FEAT_CMDWIN
5938 if (cmdwin_type != 0)
5939 {
5940 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005941 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 else
5943 cmdwin_result = K_XF2;
5944 return;
5945 }
5946# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005947
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005948 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005949 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005950 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005951 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005952 return;
5953 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005954
5955 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005956 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005957
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005959 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 getout(0);
5961 not_exiting();
5962}
5963
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964/*
5965 * ":close": close current window, unless it is the last one
5966 */
5967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005968ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005970 win_T *win;
5971 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005972#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005974 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005976#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005977 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005978 {
5979 if (eap->addr_count == 0)
5980 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005981 else
5982 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005983 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005984 {
5985 winnr++;
5986 if (winnr == eap->line2)
5987 break;
5988 }
5989 if (win == NULL)
5990 win = lastwin;
5991 ex_win_close(eap->forceit, win, NULL);
5992 }
5993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994}
5995
Bram Moolenaar4033c552017-09-16 20:54:51 +02005996#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005997/*
5998 * ":pclose": Close any preview window.
5999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006001ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006002{
6003 win_T *win;
6004
Bram Moolenaar79648732019-07-18 21:43:07 +02006005 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006006 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006007 if (win->w_p_pvw)
6008 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006009 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006010 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006011 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006012# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006013 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006014 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006015# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006016}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006017#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006018
Bram Moolenaarf740b292006-02-16 22:11:02 +00006019/*
6020 * Close window "win" and take care of handling closing the last window for a
6021 * modified buffer.
6022 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006024ex_win_close(
6025 int forceit,
6026 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006027 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
6029 int need_hide;
6030 buf_T *buf = win->w_buffer;
6031
Bram Moolenaarcf844172020-06-26 19:44:06 +02006032 // Never close the autocommand window.
6033 if (win == aucmd_win)
6034 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006035 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006036 return;
6037 }
6038
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006040 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006042#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006043 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006045 bufref_T bufref;
6046
6047 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006049 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 return;
6051 need_hide = FALSE;
6052 }
6053 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006056 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 return;
6058 }
6059 }
6060
Bram Moolenaar4033c552017-09-16 20:54:51 +02006061#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006063#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006064
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006065 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006066 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006067 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006068 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006069 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070}
6071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006073 * Handle the argument for a tabpage related ex command.
6074 * Returns a tabpage number.
6075 * When an error is encountered then eap->errmsg is set.
6076 */
6077 static int
6078get_tabpage_arg(exarg_T *eap)
6079{
6080 int tab_number;
6081 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6082
6083 if (eap->arg && *eap->arg != NUL)
6084 {
6085 char_u *p = eap->arg;
6086 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006087 int relative = 0; // argument +N/-N means: go to N places to the
6088 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006089
6090 if (*p == '-')
6091 {
6092 relative = -1;
6093 p++;
6094 }
6095 else if (*p == '+')
6096 {
6097 relative = 1;
6098 p++;
6099 }
6100
6101 p_save = p;
6102 tab_number = getdigits(&p);
6103
6104 if (relative == 0)
6105 {
6106 if (STRCMP(p, "$") == 0)
6107 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006108 else if (STRCMP(p, "#") == 0)
6109 if (valid_tabpage(lastused_tabpage))
6110 tab_number = tabpage_index(lastused_tabpage);
6111 else
6112 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006113 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006114 tab_number = 0;
6115 goto theend;
6116 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006117 else if (p == p_save || *p_save == '-' || *p != NUL
6118 || tab_number > LAST_TAB_NR)
6119 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006120 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006121 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006122 goto theend;
6123 }
6124 }
6125 else
6126 {
6127 if (*p_save == NUL)
6128 tab_number = 1;
6129 else if (p == p_save || *p_save == '-' || *p != NUL
6130 || tab_number == 0)
6131 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006132 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006133 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006134 goto theend;
6135 }
6136 tab_number = tab_number * relative + tabpage_index(curtab);
6137 if (!unaccept_arg0 && relative == -1)
6138 --tab_number;
6139 }
6140 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006141 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006142 }
6143 else if (eap->addr_count > 0)
6144 {
6145 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006146 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006147 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006148 tab_number = 0;
6149 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006150 else
6151 {
6152 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006153 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006154 {
6155 --tab_number;
6156 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006157 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006158 }
6159 }
6160 }
6161 else
6162 {
6163 switch (eap->cmdidx)
6164 {
6165 case CMD_tabnext:
6166 tab_number = tabpage_index(curtab) + 1;
6167 if (tab_number > LAST_TAB_NR)
6168 tab_number = 1;
6169 break;
6170 case CMD_tabmove:
6171 tab_number = LAST_TAB_NR;
6172 break;
6173 default:
6174 tab_number = tabpage_index(curtab);
6175 }
6176 }
6177
6178theend:
6179 return tab_number;
6180}
6181
6182/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006183 * ":tabclose": close current tab page, unless it is the last one.
6184 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 */
6186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006187ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006189 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006190 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006191
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006192# ifdef FEAT_CMDWIN
6193 if (cmdwin_type != 0)
6194 cmdwin_result = K_IGNORE;
6195 else
6196# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006197 if (first_tabpage->tp_next == NULL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00006198 emsg(_(e_cannot_close_last_tab_page));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006201 tab_number = get_tabpage_arg(eap);
6202 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006203 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006204 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006205 if (tp == NULL)
6206 {
6207 beep_flush();
6208 return;
6209 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006210 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006211 {
6212 tabpage_close_other(tp, eap->forceit);
6213 return;
6214 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006215 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006216 tabpage_close(eap->forceit);
6217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006219}
6220
6221/*
6222 * ":tabonly": close all tab pages except the current one
6223 */
6224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006225ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006226{
6227 tabpage_T *tp;
6228 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006229 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006230
6231# ifdef FEAT_CMDWIN
6232 if (cmdwin_type != 0)
6233 cmdwin_result = K_IGNORE;
6234 else
6235# endif
6236 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006237 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006238 else
6239 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006240 tab_number = get_tabpage_arg(eap);
6241 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006242 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006243 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006244 // Repeat this up to a 1000 times, because autocommands may
6245 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006246 for (done = 0; done < 1000; ++done)
6247 {
6248 FOR_ALL_TABPAGES(tp)
6249 if (tp->tp_topframe != topframe)
6250 {
6251 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006252 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006253 if (valid_tabpage(tp))
6254 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006255 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006256 break;
6257 }
6258 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006259 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006260 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006261 }
6262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264
6265/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006266 * Close the current tab page.
6267 */
6268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006269tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006270{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006271 // First close all the windows but the current one. If that worked then
6272 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006273 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006274 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006275 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006276 ex_win_close(forceit, curwin, NULL);
6277# ifdef FEAT_GUI
6278 need_mouse_correct = TRUE;
6279# endif
6280}
6281
6282/*
6283 * Close tab page "tp", which is not the current tab page.
6284 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006285 * Also takes care of the tab pages line disappearing when closing the
6286 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006287 */
6288 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006289tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006290{
6291 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006292 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006293
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006294 // Limit to 1000 windows, autocommands may add a window while we close
6295 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006296 while (++done < 1000)
6297 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006298 wp = tp->tp_firstwin;
6299 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006300
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006301 // Autocommands may delete the tab page under our fingers and we may
6302 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006303 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006304 break;
6305 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006306
Bram Moolenaar29323592016-07-24 22:04:11 +02006307 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006308}
6309
6310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 * ":only".
6312 */
6313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006314ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006316 win_T *wp;
6317 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318# ifdef FEAT_GUI
6319 need_mouse_correct = TRUE;
6320# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006321 if (eap->addr_count > 0)
6322 {
6323 wnr = eap->line2;
6324 for (wp = firstwin; --wnr > 0; )
6325 {
6326 if (wp->w_next == NULL)
6327 break;
6328 else
6329 wp = wp->w_next;
6330 }
6331 win_goto(wp);
6332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 close_others(TRUE, eap->forceit);
6334}
6335
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006337ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006339 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006340 if (!eap->skip)
6341 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006342#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006343 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006344#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006345 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006346 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006347 else
6348 {
6349 int winnr = 0;
6350 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006351
Bram Moolenaar2256c992016-11-15 21:17:07 +01006352 FOR_ALL_WINDOWS(win)
6353 {
6354 winnr++;
6355 if (winnr == eap->line2)
6356 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006357 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006358 if (win == NULL)
6359 win = lastwin;
6360 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 }
6363}
6364
6365/*
6366 * ":stop" and ":suspend": Suspend Vim.
6367 */
dbivolaruab16ad32021-12-29 19:41:47 +00006368 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006369ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
6371 /*
6372 * Disallow suspending for "rvim".
6373 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006374 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
6376 if (!eap->forceit)
6377 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006378 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 windgoto((int)Rows - 1, 0);
6380 out_char('\n');
6381 out_flush();
6382 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006383 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006384 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006385 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006387 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006389 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006391 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006392 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 }
6394}
6395
6396/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006397 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 */
6399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006400ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006402#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006403 if (not_in_vim9(eap) == FAIL)
6404 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406#ifdef FEAT_CMDWIN
6407 if (cmdwin_type != 0)
6408 {
6409 cmdwin_result = Ctrl_C;
6410 return;
6411 }
6412#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006413 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006414 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006415 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006416 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006417 return;
6418 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006419
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006421 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 */
6423 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6424 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006425
6426 // Write the buffer for ":wq" or when it was changed.
6427 // Trigger QuitPre and ExitPre.
6428 // Check if we can exit now, after autocommands have changed things.
6429 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6430 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006432 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 {
6434 not_exiting();
6435 }
6436 else
6437 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006438 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006440 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441# ifdef FEAT_GUI
6442 need_mouse_correct = TRUE;
6443# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006444 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006445 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 }
6447}
6448
6449/*
6450 * ":print", ":list", ":number".
6451 */
6452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006453ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006455 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006456 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006457 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006459 for ( ;!got_int; ui_breakcheck())
6460 {
6461 print_line(eap->line1,
6462 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6463 || (eap->flags & EXFLAG_NR)),
6464 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6465 if (++eap->line1 > eap->line2)
6466 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006467 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006468 }
6469 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006470 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006471 curwin->w_cursor.lnum = eap->line2;
6472 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 }
6474
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476}
6477
6478#ifdef FEAT_BYTEOFF
6479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006480ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
6482 goto_byte(eap->line2);
6483}
6484#endif
6485
6486/*
6487 * ":shell".
6488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006490ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491{
6492 do_shell(NULL, 0);
6493}
6494
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006495#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006497static int drop_busy = FALSE;
6498static int drop_filec;
6499static char_u **drop_filev = NULL;
6500static int drop_split;
6501static void (*drop_callback)(void *);
6502static void *drop_cookie;
6503
6504 static void
6505handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
6507 exarg_T ea;
6508 int save_msg_scroll = msg_scroll;
6509
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006510 // Setting the argument list may cause screen updates and being called
6511 // recursively. Avoid that by setting drop_busy.
6512 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006514 // Check whether the current buffer is changed. If so, we will need
6515 // to split the current window or data could be lost.
6516 // We don't need to check if the 'hidden' option is set, as in this
6517 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006518 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
6520 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006521 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 --emsg_off;
6523 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006524 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 if (win_split(0, 0) == FAIL)
6527 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006528 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006530 // When splitting the window, create a new alist. Otherwise the
6531 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 alist_unlink(curwin->w_alist);
6533 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 }
6535
6536 /*
6537 * Set up the new argument list.
6538 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006539 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540
6541 /*
6542 * Move to the first file.
6543 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006544 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006545 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 ea.cmd = (char_u *)"next";
6547 do_argfile(&ea, 0);
6548
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006549 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6550 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 need_start_insertmode = FALSE;
6552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006553 // Restore msg_scroll, otherwise a following command may cause scrolling
6554 // unexpectedly. The screen will be redrawn by the caller, thus
6555 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006557
6558 if (drop_callback != NULL)
6559 drop_callback(drop_cookie);
6560
6561 drop_filev = NULL;
6562 drop_busy = FALSE;
6563}
6564
6565/*
6566 * Handle a file drop. The code is here because a drop is *nearly* like an
6567 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006568 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006569 * code is very similar to :args and hence needs access to a lot of the static
6570 * functions in this file.
6571 *
6572 * The "filev" list must have been allocated using alloc(), as should each item
6573 * in the list. This function takes over responsibility for freeing the "filev"
6574 * list.
6575 */
6576 void
6577handle_drop(
6578 int filec, // the number of files dropped
6579 char_u **filev, // the list of files dropped
6580 int split, // force splitting the window
6581 void (*callback)(void *), // to be called after setting the argument
6582 // list
6583 void *cookie) // argument for "callback" (allocated)
6584{
6585 // Cannot handle recursive drops, finish the pending one.
6586 if (drop_busy)
6587 {
6588 FreeWild(filec, filev);
6589 vim_free(cookie);
6590 return;
6591 }
6592
6593 // When calling handle_drop() more than once in a row we only use the last
6594 // one.
6595 if (drop_filev != NULL)
6596 {
6597 FreeWild(drop_filec, drop_filev);
6598 vim_free(drop_cookie);
6599 }
6600
6601 drop_filec = filec;
6602 drop_filev = filev;
6603 drop_split = split;
6604 drop_callback = callback;
6605 drop_cookie = cookie;
6606
6607 // Postpone this when:
6608 // - editing the command line
6609 // - not possible to change the current buffer
6610 // - updating the screen
6611 // As it may change buffers and window structures that are in use and cause
6612 // freed memory to be used.
6613 if (text_locked() || curbuf_locked() || updating_screen)
6614 return;
6615
6616 handle_drop_internal();
6617}
6618
6619/*
6620 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6621 * reset: Handle a postponed drop.
6622 */
6623 void
6624handle_any_postponed_drop(void)
6625{
6626 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006627 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006628 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629}
6630#endif
6631
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 * ":preserve".
6634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006636ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006638 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 ml_preserve(curbuf, TRUE);
6640}
6641
6642/*
6643 * ":recover".
6644 */
6645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006646ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006648 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006650 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6651 | CCGD_MULTWIN
6652 | (eap->forceit ? CCGD_FORCEIT : 0)
6653 | CCGD_EXCMD)
6654
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 && (*eap->arg == NUL
6656 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006657 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 recoverymode = FALSE;
6659}
6660
6661/*
6662 * Command modifier used in a wrong way.
6663 */
6664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006665ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006667 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668}
6669
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670/*
6671 * :sview [+command] file split window with new file, read-only
6672 * :split [[+command] file] split window with current or new file
6673 * :vsplit [[+command] file] split window vertically with current or new file
6674 * :new [[+command] file] split window with no or new file
6675 * :vnew [[+command] file] split vertically window with no or new file
6676 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006677 *
6678 * :tabedit open new Tab page with empty window
6679 * :tabedit [+command] file open new Tab page and edit "file"
6680 * :tabnew [[+command] file] just like :tabedit
6681 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 */
6683 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006684ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006686 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006688#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006689 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006690 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006691#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006692 int use_tab = eap->cmdidx == CMD_tabedit
6693 || eap->cmdidx == CMD_tabfind
6694 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006696 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006697 return;
6698
Bram Moolenaar4033c552017-09-16 20:54:51 +02006699#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702
Bram Moolenaar4033c552017-09-16 20:54:51 +02006703#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006704 // A ":split" in the quickfix window works like ":new". Don't want two
6705 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006706 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {
6708 if (eap->cmdidx == CMD_split)
6709 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 if (eap->cmdidx == CMD_vsplit)
6711 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006715 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 {
6717 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6718 FNAME_MESS, TRUE, curbuf->b_ffname);
6719 if (fname == NULL)
6720 goto theend;
6721 eap->arg = fname;
6722 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006723# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006724 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 && eap->cmdidx != CMD_new)
6727 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006728 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006729# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006730 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006731# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006732 au_has_group((char_u *)"FileExplorer"))
6733 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006734 // No browsing supported but we do have the file explorer:
6735 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006736 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006737 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006738 }
6739 else
6740 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006741 fname = do_browse(0, (char_u *)(use_tab
6742 ? _("Edit File in new tab page")
6743 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006745 if (fname == NULL)
6746 goto theend;
6747 eap->arg = fname;
6748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006750 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006753 /*
6754 * Either open new tab page or split the window.
6755 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006756 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006757 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006758 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006759 : eap->addr_count == 0 ? 0
6760 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006761 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006762 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006763
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006764 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006765 if (curwin != old_curwin
6766 && win_valid(old_curwin)
6767 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006768 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006769 old_curwin->w_alt_fnum = curbuf->b_fnum;
6770 }
6771 }
6772 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6774 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006775 // Reset 'scrollbind' when editing another file, but keep it when
6776 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006777 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006778 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 else
6780 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 do_exedit(eap, old_curwin);
6782 }
6783
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006784# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006785 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788theend:
6789 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006791
6792/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006793 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006794 */
6795 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006796tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006797{
6798 exarg_T ea;
6799
Bram Moolenaara80faa82020-04-12 19:37:17 +02006800 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006801 ea.cmdidx = CMD_tabnew;
6802 ea.cmd = (char_u *)"tabn";
6803 ea.arg = (char_u *)"";
6804 ex_splitview(&ea);
6805}
6806
6807/*
6808 * :tabnext command
6809 */
6810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006811ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006812{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006813 int tab_number;
6814
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006815 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006816 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006817 switch (eap->cmdidx)
6818 {
6819 case CMD_tabfirst:
6820 case CMD_tabrewind:
6821 goto_tabpage(1);
6822 break;
6823 case CMD_tablast:
6824 goto_tabpage(9999);
6825 break;
6826 case CMD_tabprevious:
6827 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006828 if (eap->arg && *eap->arg != NUL)
6829 {
6830 char_u *p = eap->arg;
6831 char_u *p_save = p;
6832
6833 tab_number = getdigits(&p);
6834 if (p == p_save || *p_save == '-' || *p != NUL
6835 || tab_number == 0)
6836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006837 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006838 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006839 return;
6840 }
6841 }
6842 else
6843 {
6844 if (eap->addr_count == 0)
6845 tab_number = 1;
6846 else
6847 {
6848 tab_number = eap->line2;
6849 if (tab_number < 1)
6850 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006851 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006852 return;
6853 }
6854 }
6855 }
6856 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006857 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006858 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006859 tab_number = get_tabpage_arg(eap);
6860 if (eap->errmsg == NULL)
6861 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006862 break;
6863 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006864}
6865
6866/*
6867 * :tabmove command
6868 */
6869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006870ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006871{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006872 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006873
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006874 tab_number = get_tabpage_arg(eap);
6875 if (eap->errmsg == NULL)
6876 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006877}
6878
6879/*
6880 * :tabs command: List tabs and their contents.
6881 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006882 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006883ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006884{
6885 tabpage_T *tp;
6886 win_T *wp;
6887 int tabcount = 1;
6888
6889 msg_start();
6890 msg_scroll = TRUE;
6891 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6892 {
6893 msg_putchar('\n');
6894 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006895 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006896 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006897 ui_breakcheck();
6898
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006899 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006900 wp = firstwin;
6901 else
6902 wp = tp->tp_firstwin;
6903 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6904 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006905 msg_putchar('\n');
6906 msg_putchar(wp == curwin ? '>' : ' ');
6907 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006908 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6909 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006910 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006911 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006912 else
6913 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6914 IObuff, IOSIZE, TRUE);
6915 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006916 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006917 ui_breakcheck();
6918 }
6919 }
6920}
6921
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922/*
6923 * ":mode": Set screen mode.
6924 * If no argument given, just get the screen size and redraw.
6925 */
6926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006927ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928{
6929 if (*eap->arg == NUL)
6930 shell_resized();
6931 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006932 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933}
6934
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935/*
6936 * ":resize".
6937 * set, increment or decrement current window height
6938 */
6939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006940ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941{
6942 int n;
6943 win_T *wp = curwin;
6944
6945 if (eap->addr_count > 0)
6946 {
6947 n = eap->line2;
6948 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6949 ;
6950 }
6951
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006952# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006956 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {
6958 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006959 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006960 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006962 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 }
6964 else
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_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006968 else if (n == 0 && eap->arg[0] == NUL) // default is very high
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_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 }
6972}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
6974/*
6975 * ":find [+command] <file>" command.
6976 */
6977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006978ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 char_u *fname;
6981 int count;
6982
6983 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6984 TRUE, curbuf->b_ffname);
6985 if (eap->addr_count > 0)
6986 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006987 // Repeat finding the file "count" times. This matters when it
6988 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 count = eap->line2;
6990 while (fname != NULL && --count > 0)
6991 {
6992 vim_free(fname);
6993 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6994 FALSE, curbuf->b_ffname);
6995 }
6996 }
6997
6998 if (fname != NULL)
6999 {
7000 eap->arg = fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 do_exedit(eap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 vim_free(fname);
7003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004}
7005
7006/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007007 * ":open" simulation: for now just work like ":visual".
7008 */
7009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007010ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007011{
7012 regmatch_T regmatch;
7013 char_u *p;
7014
Bram Moolenaar65088802021-03-13 21:07:21 +01007015#ifdef FEAT_EVAL
7016 if (not_in_vim9(eap) == FAIL)
7017 return;
7018#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007019 curwin->w_cursor.lnum = eap->line2;
7020 beginline(BL_SOL | BL_FIX);
7021 if (*eap->arg == '/')
7022 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007023 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007024 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007025 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007026 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007027 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007028 if (regmatch.regprog != NULL)
7029 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007030 // make a copy of the line, when searching for a mark it might be
7031 // flushed
7032 char_u *line = vim_strsave(ml_get_curline());
7033
Bram Moolenaardf177f62005-02-22 08:39:57 +00007034 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007035 if (vim_regexec(&regmatch, line, (colnr_T)0))
7036 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007037 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007038 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007039 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007040 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007041 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007042 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007043 eap->arg += STRLEN(eap->arg);
7044 }
7045 check_cursor();
7046
7047 eap->cmdidx = CMD_visual;
7048 do_exedit(eap, NULL);
7049}
7050
7051/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007052 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 */
7054 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007055ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
7057 do_exedit(eap, NULL);
7058}
7059
7060/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007061 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007064do_exedit(
7065 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007066 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067{
7068 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007070 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007072 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7073 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007074 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 /*
7076 * ":vi" command ends Ex mode.
7077 */
7078 if (exmode_active && (eap->cmdidx == CMD_visual
7079 || eap->cmdidx == CMD_view))
7080 {
7081 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007082 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007085 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007086 if (global_busy)
7087 {
7088 int rd = RedrawingDisabled;
7089 int nwr = no_wait_return;
7090 int ms = msg_scroll;
7091#ifdef FEAT_GUI
7092 int he = hold_gui_events;
7093#endif
7094
7095 if (eap->nextcmd != NULL)
7096 {
7097 stuffReadbuff(eap->nextcmd);
7098 eap->nextcmd = NULL;
7099 }
7100
7101 if (exmode_was != EXMODE_VIM)
7102 settmode(TMODE_RAW);
7103 RedrawingDisabled = 0;
7104 no_wait_return = 0;
7105 need_wait_return = FALSE;
7106 msg_scroll = 0;
7107#ifdef FEAT_GUI
7108 hold_gui_events = 0;
7109#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007110 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007111 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007112
7113 main_loop(FALSE, TRUE);
7114
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007115 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007116 RedrawingDisabled = rd;
7117 no_wait_return = nwr;
7118 msg_scroll = ms;
7119#ifdef FEAT_GUI
7120 hold_gui_events = he;
7121#endif
7122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 }
7126
7127 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007128 || eap->cmdidx == CMD_tabnew
7129 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007130 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007132 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 setpcmark();
7134 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007135 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7136 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007138 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 || *eap->arg != NUL
7140#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007141 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142#endif
7143 )
7144 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007145 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7146 // Only ":edit" or ":script" can bring us here, others are stopped
7147 // earlier.
7148 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007149 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007150
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 n = readonlymode;
7152 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7153 readonlymode = TRUE;
7154 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007155 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7156 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007157 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7158 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7160 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007161 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7163 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7164 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007165 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007167 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007168 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007169 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7170 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007171 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007173 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 if (old_curwin != NULL)
7175 {
7176 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007177 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007179#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007180 cleanup_T cs;
7181
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007182 // Reset the error/interrupt/exception state here so that
7183 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007184 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007185#endif
7186#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007188#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007189 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007190
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007191#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007192 // Restore the error/interrupt/exception state if not
7193 // discarded by a new aborting error, interrupt, or
7194 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007195 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 }
7198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 }
7200 else if (readonlymode && curbuf->b_nwindows == 1)
7201 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007202 // When editing an already visited buffer, 'readonly' won't be set
7203 // but the previous value is kept. With ":view" and ":sview" we
7204 // want the file to be readonly, except when another window is
7205 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 curbuf->b_p_ro = TRUE;
7207 }
7208 readonlymode = n;
7209 }
7210 else
7211 {
7212 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007213 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (n != curwin->w_arg_idx_invalid)
7217 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 }
7219
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 /*
7221 * if ":split file" worked, set alternate file name in old window to new
7222 * file
7223 */
7224 if (old_curwin != NULL
7225 && *eap->arg != NUL
7226 && curwin != old_curwin
7227 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007228 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007229 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
7232 ex_no_reprint = TRUE;
7233}
7234
7235#ifndef FEAT_GUI
7236/*
7237 * ":gui" and ":gvim" when there is no GUI.
7238 */
7239 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007240ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007242 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243}
7244#endif
7245
Bram Moolenaar4f974752019-02-17 17:44:42 +01007246#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007248ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249{
7250 gui_make_tearoff(eap->arg);
7251}
7252#endif
7253
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007254#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7255 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007257ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007259# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7260 if (gui.in_use)
7261 gui_make_popup(eap->arg, eap->forceit);
7262# ifdef FEAT_TERM_POPUP_MENU
7263 else
7264# endif
7265# endif
7266# ifdef FEAT_TERM_POPUP_MENU
7267 pum_make_popup(eap->arg, eap->forceit);
7268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269}
7270#endif
7271
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007273ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
7275 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007276 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007278 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279}
7280
7281/*
7282 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7283 * offset.
7284 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007287ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007290 win_T *save_curwin = curwin;
7291 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 long topline;
7293 long y;
7294 linenr_T old_linenr = curwin->w_cursor.lnum;
7295
7296 setpcmark();
7297
7298 /*
7299 * determine max topline
7300 */
7301 if (curwin->w_p_scb)
7302 {
7303 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007304 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 {
7306 if (wp->w_p_scb && wp->w_buffer)
7307 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007308 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 if (topline > y)
7310 topline = y;
7311 }
7312 }
7313 if (topline < 1)
7314 topline = 1;
7315 }
7316 else
7317 {
7318 topline = 1;
7319 }
7320
7321
7322 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007323 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007325 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 {
7327 if (curwin->w_p_scb)
7328 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007329 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 y = topline - curwin->w_topline;
7331 if (y > 0)
7332 scrollup(y, TRUE);
7333 else
7334 scrolldown(-y, TRUE);
7335 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007336 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 }
7340 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007341 curwin = save_curwin;
7342 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 if (curwin->w_p_scb)
7344 {
7345 did_syncbind = TRUE;
7346 checkpcmark();
7347 if (old_linenr != curwin->w_cursor.lnum)
7348 {
7349 char_u ctrl_o[2];
7350
7351 ctrl_o[0] = Ctrl_O;
7352 ctrl_o[1] = 0;
7353 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7354 }
7355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356}
7357
7358
7359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007360ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007362 int i;
7363 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7364 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007366 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 do_bang(1, eap, FALSE, FALSE, TRUE);
7368 else
7369 {
7370 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7371 return;
7372
7373#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007374 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 {
7376 char_u *browseFile;
7377
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007378 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 NULL, NULL, NULL, curbuf);
7380 if (browseFile != NULL)
7381 {
7382 i = readfile(browseFile, NULL,
7383 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7384 vim_free(browseFile);
7385 }
7386 else
7387 i = OK;
7388 }
7389 else
7390#endif
7391 if (*eap->arg == NUL)
7392 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007393 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 return;
7395 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7396 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7397 }
7398 else
7399 {
7400 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7401 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7402 i = readfile(eap->arg, NULL,
7403 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7404
7405 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007406 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007408#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 if (!aborting())
7410#endif
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007411 semsg(_(e_cant_open_file_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
7413 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007414 {
7415 if (empty && exmode_active)
7416 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007417 // Delete the empty line that remains. Historically ex does
7418 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007419 if (eap->line2 == 0)
7420 lnum = curbuf->b_ml.ml_line_count;
7421 else
7422 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007423 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007424 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007425 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007426 if (curwin->w_cursor.lnum > 1
7427 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007428 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007429 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007430 }
7431 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007432 redraw_curbuf_later(UPD_VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 }
7435}
7436
Bram Moolenaarea408852005-06-25 22:49:46 +00007437static char_u *prev_dir = NULL;
7438
7439#if defined(EXITFREE) || defined(PROTO)
7440 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007441free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007442{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007443 VIM_CLEAR(prev_dir);
7444 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007445}
7446#endif
7447
Bram Moolenaarf4258302013-06-02 18:20:17 +02007448/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007449 * Get the previous directory for the given chdir scope.
7450 */
7451 static char_u *
7452get_prevdir(cdscope_T scope)
7453{
7454 if (scope == CDSCOPE_WINDOW)
7455 return curwin->w_prevdir;
7456 else if (scope == CDSCOPE_TABPAGE)
7457 return curtab->tp_prevdir;
7458 return prev_dir;
7459}
7460
7461/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007462 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007463 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7464 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007465 */
7466 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007467post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007468{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007469 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007470 // Clear tab local directory for both :cd and :tcd
7471 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007472 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007473 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007474 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007475 char_u *pdir = get_prevdir(scope);
7476
7477 // If still in the global directory, need to remember current
7478 // directory as the global directory.
7479 if (globaldir == NULL && pdir != NULL)
7480 globaldir = vim_strsave(pdir);
7481
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007482 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007483 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007484 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007485 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007486 curtab->tp_localdir = vim_strsave(NameBuff);
7487 else
7488 curwin->w_localdir = vim_strsave(NameBuff);
7489 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007490 }
7491 else
7492 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007493 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007494 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007495 }
7496
zeertzjq64be6aa2021-11-19 11:59:08 +00007497 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007498 shorten_fnames(TRUE);
7499}
7500
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007501/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007502 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7503 */
7504 void
7505trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7506{
7507#ifdef FEAT_EVAL
7508 dict_T *v_event;
7509 save_v_event_T save_v_event;
7510
7511 v_event = get_v_event(&save_v_event);
7512 (void)dict_add_string(v_event, "directory", new_dir);
7513 dict_set_items_ro(v_event);
7514#endif
7515 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7516#ifdef FEAT_EVAL
7517 restore_v_event(v_event, &save_v_event);
7518#endif
7519}
7520
7521/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007522 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007523 * chdir() function.
7524 * scope == CDSCOPE_WINDOW: changes the window-local directory
7525 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7526 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007527 * Returns TRUE if the directory is successfully changed.
7528 */
7529 int
7530changedir_func(
7531 char_u *new_dir,
7532 int forceit,
7533 cdscope_T scope)
7534{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007535 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007536 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007537 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007538 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007539 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007540
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007541 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007542 return FALSE;
7543
7544 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7545 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007546 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007547 return FALSE;
7548 }
7549
7550 // ":cd -": Change to previous directory
7551 if (STRCMP(new_dir, "-") == 0)
7552 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007553 pdir = get_prevdir(scope);
7554 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007555 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007556 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007557 return FALSE;
7558 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007559 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007560 }
7561
7562 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007563 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007564 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007565 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007566 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007567
Bakudankun29f3a452021-12-11 12:28:08 +00007568 // For UNIX ":cd" means: go to home directory.
7569 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007570#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007571 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007572#else
7573 if (*new_dir == NUL && p_cdh)
7574#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007575 {
7576 // use NameBuff for home directory name
7577# ifdef VMS
7578 char_u *p;
7579
7580 p = mch_getenv((char_u *)"SYS$LOGIN");
7581 if (p == NULL || *p == NUL) // empty is the same as not set
7582 NameBuff[0] = NUL;
7583 else
7584 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7585# else
7586 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7587# endif
7588 new_dir = NameBuff;
7589 }
zeertzjqf38aad82021-12-30 13:45:57 +00007590 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007591 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7592 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007593 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007594 if (scope == CDSCOPE_WINDOW)
7595 acmd_fname = (char_u *)"window";
7596 else if (scope == CDSCOPE_TABPAGE)
7597 acmd_fname = (char_u *)"tabpage";
7598 else
7599 acmd_fname = (char_u *)"global";
7600 trigger_DirChangedPre(acmd_fname, new_dir);
7601
7602 if (vim_chdir(new_dir))
7603 {
7604 emsg(_(e_command_failed));
7605 vim_free(pdir);
7606 return FALSE;
7607 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007608 }
zeertzjq73257142022-02-02 13:16:37 +00007609
7610 if (scope == CDSCOPE_WINDOW)
7611 pp = &curwin->w_prevdir;
7612 else if (scope == CDSCOPE_TABPAGE)
7613 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007614 else
zeertzjq73257142022-02-02 13:16:37 +00007615 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007616 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007617 *pp = pdir;
7618
7619 post_chdir(scope);
7620
7621 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007622 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007623 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007624 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007625}
Bram Moolenaarea408852005-06-25 22:49:46 +00007626
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007628 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007630 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007631ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007633 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634
7635 new_dir = eap->arg;
7636#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007637 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7638 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 ex_pwd(NULL);
7640 else
7641#endif
7642 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007643 cdscope_T scope = CDSCOPE_GLOBAL;
7644
7645 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7646 scope = CDSCOPE_WINDOW;
7647 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7648 scope = CDSCOPE_TABPAGE;
7649
7650 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007651 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007652 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007653 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 ex_pwd(eap);
7655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 }
7657}
7658
7659/*
7660 * ":pwd".
7661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007663ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664{
7665 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7666 {
7667#ifdef BACKSLASH_IN_FILENAME
7668 slash_adjust(NameBuff);
7669#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007670 if (p_verbose > 0)
7671 {
7672 char *context = "global";
7673
Bram Moolenaar05268152021-11-18 18:53:45 +00007674 if (last_chdir_reason != NULL)
7675 context = last_chdir_reason;
7676 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007677 context = "window";
7678 else if (curtab->tp_localdir != NULL)
7679 context = "tabpage";
7680 smsg("[%s] %s", context, (char *)NameBuff);
7681 }
7682 else
7683 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 }
7685 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007686 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687}
7688
7689/*
7690 * ":=".
7691 */
7692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007693ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007695 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007696 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697}
7698
7699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007700ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007702 int n;
7703 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704
7705 if (cursor_valid())
7706 {
7707 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7708 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007709 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007711
7712 len = eap->line2;
7713 switch (*eap->arg)
7714 {
7715 case 'm': break;
7716 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007717 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007718 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007719
7720 // Hide the cursor if invoked with !
7721 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722}
7723
7724/*
7725 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007726 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 */
7728 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007729do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730{
Bram Moolenaar52953192019-08-16 10:27:13 +02007731 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007732 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007733# ifdef ELAPSED_FUNC
7734 elapsed_T start_tv;
7735
7736 // Remember at what time we started, so that we know how much longer we
7737 // should wait after waiting for a bit.
7738 ELAPSED_INIT(start_tv);
7739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007741 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007742 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007743 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007744 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007745
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007746 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007747 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007749 wait_now = msec - done > 1000L ? 1000L : msec - done;
7750#ifdef FEAT_TIMERS
7751 {
7752 long due_time = check_due_timer();
7753
7754 if (due_time > 0 && due_time < wait_now)
7755 wait_now = due_time;
7756 }
7757#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007758#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007759 if (has_any_channel() && wait_now > 20L)
7760 wait_now = 20L;
7761#endif
7762#ifdef FEAT_SOUND
7763 if (has_any_sound_callback() && wait_now > 20L)
7764 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007765#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007766 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007767
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007768#ifdef FEAT_JOB_CHANNEL
7769 if (has_any_channel())
7770 ui_breakcheck_force(TRUE);
7771 else
7772#endif
7773 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007774#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007775 // Process the netbeans and clientserver messages that may have been
7776 // received in the call to ui_breakcheck() when the GUI is in use. This
7777 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007778 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007779#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007780
7781# ifdef ELAPSED_FUNC
7782 // actual time passed
7783 done = ELAPSED_FUNC(start_tv);
7784# else
7785 // guestimate time passed (will actually be more)
7786 done += wait_now;
7787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007789
7790 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7791 // input buffer, otherwise a following call to input() fails.
7792 if (got_int)
7793 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007794
7795 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007796 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797}
7798
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799/*
7800 * ":winsize" command (obsolete).
7801 */
7802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007803ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
7805 int w, h;
7806 char_u *arg = eap->arg;
7807 char_u *p;
7808
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007809 if (!isdigit(*arg))
7810 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007811 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007812 return;
7813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 w = getdigits(&arg);
7815 arg = skipwhite(arg);
7816 p = arg;
7817 h = getdigits(&arg);
7818 if (*p != NUL && *arg == NUL)
7819 set_shellsize(w, h, TRUE);
7820 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007821 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822}
7823
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007825ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826{
7827 int xchar = NUL;
7828 char_u *p;
7829
7830 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007832 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 if (eap->arg[1] == NUL)
7834 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007835 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 return;
7837 }
7838 xchar = eap->arg[1];
7839 p = eap->arg + 2;
7840 }
7841 else
7842 p = eap->arg + 1;
7843
Bram Moolenaar63b91732021-08-05 20:40:03 +02007844 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007846 if (*p != NUL && *p != (
7847#ifdef FEAT_EVAL
7848 in_vim9script() ? '#' :
7849#endif
7850 '"')
7851 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007852 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007853 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007855 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007856 postponed_split_flags = cmdmod.cmod_split;
7857 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7859 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007860 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 }
7862}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863
Bram Moolenaar843ee412004-06-30 16:16:41 +00007864#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865/*
7866 * ":winpos".
7867 */
7868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007869ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870{
7871 int x, y;
7872 char_u *arg = eap->arg;
7873 char_u *p;
7874
7875 if (*arg == NUL)
7876 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007877# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007878# ifdef VIMDLL
7879 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7880 mch_get_winpos(&x, &y) != FAIL)
7881# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007883# else
7884 if (mch_get_winpos(&x, &y) != FAIL)
7885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
7887 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007888 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890 else
7891# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007892 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 }
7894 else
7895 {
7896 x = getdigits(&arg);
7897 arg = skipwhite(arg);
7898 p = arg;
7899 y = getdigits(&arg);
7900 if (*p == NUL || *arg != NUL)
7901 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007902 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 return;
7904 }
7905# ifdef FEAT_GUI
7906 if (gui.in_use)
7907 gui_mch_set_winpos(x, y);
7908 else if (gui.starting)
7909 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007910 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 gui_win_x = x;
7912 gui_win_y = y;
7913 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007914# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 else
7916# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007917# endif
7918# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007919 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920# endif
7921# ifdef HAVE_TGETENT
7922 if (*T_CWP)
7923 term_set_winpos(x, y);
7924# endif
7925 }
7926}
7927#endif
7928
7929/*
7930 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7931 */
7932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007933ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
7935 oparg_T oa;
7936
7937 clear_oparg(&oa);
7938 oa.regname = eap->regname;
7939 oa.start.lnum = eap->line1;
7940 oa.end.lnum = eap->line2;
7941 oa.line_count = eap->line2 - eap->line1 + 1;
7942 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007944 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
7946 setpcmark();
7947 curwin->w_cursor.lnum = eap->line1;
7948 beginline(BL_SOL | BL_FIX);
7949 }
7950
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007951 if (VIsual_active)
7952 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007953
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 switch (eap->cmdidx)
7955 {
7956 case CMD_delete:
7957 oa.op_type = OP_DELETE;
7958 op_delete(&oa);
7959 break;
7960
7961 case CMD_yank:
7962 oa.op_type = OP_YANK;
7963 (void)op_yank(&oa, FALSE, TRUE);
7964 break;
7965
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007966 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007967 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007969 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7970#else
7971 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007973 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 oa.op_type = OP_RSHIFT;
7975 else
7976 oa.op_type = OP_LSHIFT;
7977 op_shift(&oa, FALSE, eap->amount);
7978 break;
7979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007981 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982}
7983
7984/*
7985 * ":put".
7986 */
7987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007988ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007990 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 if (eap->line2 == 0)
7992 {
7993 eap->line2 = 1;
7994 eap->forceit = TRUE;
7995 }
7996 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00007997 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007998 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007999 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000}
8001
8002/*
8003 * Handle ":copy" and ":move".
8004 */
8005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008006ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007{
8008 long n;
8009
Bram Moolenaar491799b2020-08-01 19:23:43 +02008010#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008011 if (not_in_vim9(eap) == FAIL)
8012 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008013#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008014 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008015 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {
8017 eap->nextcmd = NULL;
8018 return;
8019 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008020 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021
8022 /*
8023 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8024 */
8025 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8026 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008027 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 return;
8029 }
8030
8031 if (eap->cmdidx == CMD_move)
8032 {
8033 if (do_move(eap->line1, eap->line2, n) == FAIL)
8034 return;
8035 }
8036 else
8037 ex_copy(eap->line1, eap->line2, n);
8038 u_clearline();
8039 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008040 ex_may_print(eap);
8041}
8042
8043/*
8044 * Print the current line if flags were given to the Ex command.
8045 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008046 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008047ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008048{
8049 if (eap->flags != 0)
8050 {
8051 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8052 (eap->flags & EXFLAG_LIST));
8053 ex_no_reprint = TRUE;
8054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055}
8056
8057/*
8058 * ":smagic" and ":snomagic".
8059 */
8060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008061ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008063 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008065 magic_overruled = eap->cmdidx == CMD_smagic
8066 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008067 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008068 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069}
8070
8071/*
8072 * ":join".
8073 */
8074 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008075ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
8077 curwin->w_cursor.lnum = eap->line1;
8078 if (eap->line1 == eap->line2)
8079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008080 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 return;
8082 if (eap->line2 == curbuf->b_ml.ml_line_count)
8083 {
8084 beep_flush();
8085 return;
8086 }
8087 ++eap->line2;
8088 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008089 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008091 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092}
8093
8094/*
8095 * ":[addr]@r" or ":[addr]*r": execute register
8096 */
8097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008098ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
8100 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008101 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102
8103 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008104 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105
8106#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008107 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108#endif
8109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008110 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 c = *eap->arg;
8112 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8113 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008114 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008115 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8116 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008117 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 else
8121 {
8122 int save_efr = exec_from_reg;
8123
8124 exec_from_reg = TRUE;
8125
8126 /*
8127 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008128 * Continue until the stuff buffer is empty and all added characters
8129 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008131 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8133
8134 exec_from_reg = save_efr;
8135 }
8136}
8137
8138/*
8139 * ":!".
8140 */
8141 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008142ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143{
8144 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8145}
8146
8147/*
8148 * ":undo".
8149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008151ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008153 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008154 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008155 else
8156 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157}
8158
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008159#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008161ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008162{
8163 char_u hash[UNDO_HASH_SIZE];
8164
8165 u_compute_hash(hash);
8166 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8167}
8168
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008170ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008171{
8172 char_u hash[UNDO_HASH_SIZE];
8173
8174 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008175 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008176}
8177#endif
8178
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179/*
8180 * ":redo".
8181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008183ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184{
8185 u_redo(1);
8186}
8187
8188/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008189 * ":earlier" and ":later".
8190 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008191 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008192ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008193{
8194 long count = 0;
8195 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008196 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008197 char_u *p = eap->arg;
8198
8199 if (*p == NUL)
8200 count = 1;
8201 else if (isdigit(*p))
8202 {
8203 count = getdigits(&p);
8204 switch (*p)
8205 {
8206 case 's': ++p; sec = TRUE; break;
8207 case 'm': ++p; sec = TRUE; count *= 60; break;
8208 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008209 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8210 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008211 }
8212 }
8213
8214 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008215 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008216 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008217 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8218 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008219}
8220
8221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 * ":redir": start/stop redirection.
8223 */
8224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008225ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226{
8227 char *mode;
8228 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008229 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230
Bram Moolenaarba768492016-07-08 15:32:54 +02008231#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008232 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008233 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008234 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008235 return;
8236 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008237#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008238
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 if (STRICMP(eap->arg, "END") == 0)
8240 close_redir();
8241 else
8242 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008243 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008245 ++arg;
8246 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008248 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 mode = "a";
8250 }
8251 else
8252 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008253 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254
8255 close_redir();
8256
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008257 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008258 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 if (fname == NULL)
8260 return;
8261#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008262 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {
8264 char_u *browseFile;
8265
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008266 browseFile = do_browse(BROWSE_SAVE,
8267 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008268 fname, NULL, NULL,
8269 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008271 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 vim_free(fname);
8273 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008274 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 }
8276#endif
8277
8278 redir_fd = open_exfile(fname, eap->forceit, mode);
8279 vim_free(fname);
8280 }
8281#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008282 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008284 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008286 ++arg;
8287 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008289 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008290 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008292 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008294 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008295 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008296 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008297 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008299 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008300 if (*arg == '>')
8301 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008302 // Make register empty when not using @A-@Z and the
8303 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008304 if (*arg == NUL && !isupper(redir_reg))
8305 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008307 }
8308 if (*arg != NUL)
8309 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008310 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008311 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008312 }
8313 }
8314 else if (*arg == '=' && arg[1] == '>')
8315 {
8316 int append;
8317
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008318 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008319 close_redir();
8320 arg += 2;
8321
8322 if (*arg == '>')
8323 {
8324 ++arg;
8325 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 }
8327 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008328 append = FALSE;
8329
8330 if (var_redir_start(skipwhite(arg), append) == OK)
8331 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 }
8333#endif
8334
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008335 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008338 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008340
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008341 // Make sure redirection is not off. Can happen for cmdline completion
8342 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008343 if (redir_fd != NULL
8344#ifdef FEAT_EVAL
8345 || redir_reg || redir_vname
8346#endif
8347 )
8348 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349}
8350
8351/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008352 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008354 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008355ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008357 redraw_cmd(eap->forceit);
8358}
8359
8360/*
8361 * ":redraw": force redraw, with clear if "clear" is TRUE.
8362 */
8363 void
8364redraw_cmd(int clear)
8365{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 int r = RedrawingDisabled;
8367 int p = p_lz;
8368
8369 RedrawingDisabled = 0;
8370 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008371 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008373 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 if (need_maketitle)
8375 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008376#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8377# ifdef VIMDLL
8378 if (!gui.in_use)
8379# endif
8380 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 RedrawingDisabled = r;
8383 p_lz = p;
8384
Bram Moolenaar5017c662022-04-07 18:06:08 +01008385 // After drawing the statusline screen_attr may still be set.
8386 screen_stop_highlight();
8387
Bram Moolenaara2a89732022-08-31 14:46:18 +01008388 // Reset msg_didout, so that a message that's there is overwritten.
8389 msg_didout = FALSE;
8390 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008392 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008393 need_wait_return = FALSE;
8394
Bram Moolenaara653e532022-04-19 11:38:24 +01008395 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008396 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008397 redrawcmdline();
8398
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 out_flush();
8400}
8401
8402/*
8403 * ":redrawstatus": force redraw of status line(s)
8404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008406ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 int r = RedrawingDisabled;
8409 int p = p_lz;
8410
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 if (eap->forceit)
8412 status_redraw_all();
8413 else
8414 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008415 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008416 return; // redraw later
8417
8418 RedrawingDisabled = 0;
8419 p_lz = FALSE;
zeertzjq320d9102022-09-20 17:12:13 +01008420 if (State & MODE_CMDLINE)
8421 redraw_statuslines();
8422 else
8423 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 RedrawingDisabled = r;
8425 p_lz = p;
8426 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427}
8428
Bram Moolenaare12bab32019-01-08 22:02:56 +01008429/*
8430 * ":redrawtabline": force redraw of the tabline
8431 */
8432 static void
8433ex_redrawtabline(exarg_T *eap UNUSED)
8434{
8435 int r = RedrawingDisabled;
8436 int p = p_lz;
8437
8438 RedrawingDisabled = 0;
8439 p_lz = FALSE;
8440
8441 draw_tabline();
8442
8443 RedrawingDisabled = r;
8444 p_lz = p;
8445 out_flush();
8446}
8447
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008449close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
8451 if (redir_fd != NULL)
8452 {
8453 fclose(redir_fd);
8454 redir_fd = NULL;
8455 }
8456#ifdef FEAT_EVAL
8457 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008458 if (redir_vname)
8459 {
8460 var_redir_stop();
8461 redir_vname = 0;
8462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463#endif
8464}
8465
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008466#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008467 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008468vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008469{
8470 if (vim_mkdir(name, prot) != 0)
8471 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008472 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008473 return FAIL;
8474 }
8475 return OK;
8476}
8477#endif
8478
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479/*
8480 * Open a file for writing for an Ex command, with some checks.
8481 * Return file descriptor, or NULL on failure.
8482 */
8483 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008484open_exfile(
8485 char_u *fname,
8486 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008487 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488{
8489 FILE *fd;
8490
8491#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008492 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 if (mch_isdir(fname))
8494 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008495 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 return NULL;
8497 }
8498#endif
8499 if (!forceit && *mode != 'a' && vim_fexists(fname))
8500 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008501 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 return NULL;
8503 }
8504
8505 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008506 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507
8508 return fd;
8509}
8510
8511/*
8512 * ":mark" and ":k".
8513 */
8514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008515ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516{
8517 pos_T pos;
8518
Bram Moolenaar10b94212021-02-19 21:42:57 +01008519#ifdef FEAT_EVAL
8520 if (not_in_vim9(eap) == FAIL)
8521 return;
8522#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008523 if (*eap->arg == NUL) // No argument?
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008524 emsg(_(e_argument_required));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008525 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar74409f62022-01-01 15:58:22 +00008526 semsg(_(e_trailing_characters_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 else
8528 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008529 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 curwin->w_cursor.lnum = eap->line2;
8531 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008532 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaar6d057012021-12-31 18:49:43 +00008533 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008534 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 }
8536}
8537
8538/*
8539 * Update w_topline, w_leftcol and the cursor position.
8540 */
8541 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008542update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008544 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 update_topline();
8546 if (!curwin->w_p_wrap)
8547 validate_cursor();
8548 update_curswant();
8549}
8550
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008552 * Save the current State and go to Normal mode.
8553 * Return TRUE if the typeahead could be saved.
8554 */
8555 int
8556save_current_state(save_state_T *sst)
8557{
8558 sst->save_msg_scroll = msg_scroll;
8559 sst->save_restart_edit = restart_edit;
8560 sst->save_msg_didout = msg_didout;
8561 sst->save_State = State;
8562 sst->save_insertmode = p_im;
8563 sst->save_finish_op = finish_op;
8564 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008565 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008566 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008567
Bram Moolenaar4324d872020-12-01 20:12:24 +01008568 msg_scroll = FALSE; // no msg scrolling in Normal mode
8569 restart_edit = 0; // don't go to Insert mode
8570 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008571
Bram Moolenaara452b802020-12-01 21:47:59 +01008572 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008573 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008574
8575 /*
8576 * Save the current typeahead. This is required to allow using ":normal"
8577 * from an event handler and makes sure we don't hang when the argument
8578 * ends with half a command.
8579 */
8580 save_typeahead(&sst->tabuf);
8581 return sst->tabuf.typebuf_valid;
8582}
8583
8584 void
8585restore_current_state(save_state_T *sst)
8586{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008587 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008588 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008589
8590 msg_scroll = sst->save_msg_scroll;
8591 restart_edit = sst->save_restart_edit;
8592 p_im = sst->save_insertmode;
8593 finish_op = sst->save_finish_op;
8594 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008595 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008596 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008597 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008598 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008599
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008600 // Restore the state (needed when called from a function executed for
8601 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008602 State = sst->save_State;
8603#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008604 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008605#endif
8606}
8607
8608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 * ":normal[!] {commands}": Execute normal mode commands.
8610 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008611 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008612ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008614 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 char_u *arg = NULL;
8616 int l;
8617 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008619 if (ex_normal_lock > 0)
8620 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008621 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008622 return;
8623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 if (ex_normal_busy >= p_mmd)
8625 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008626 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 return;
8628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 /*
8631 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8632 * this for the K_SPECIAL leading byte, otherwise special keys will not
8633 * work.
8634 */
8635 if (has_mbyte)
8636 {
8637 int len = 0;
8638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008639 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 for (p = eap->arg; *p != NUL; ++p)
8641 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008642#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008643 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008645#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008646 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008647 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008648#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 )
8652 len += 2;
8653 }
8654 if (len > 0)
8655 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008656 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 if (arg != NULL)
8658 {
8659 len = 0;
8660 for (p = eap->arg; *p != NUL; ++p)
8661 {
8662 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008663#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 if (*p == CSI)
8665 {
8666 arg[len++] = KS_EXTRA;
8667 arg[len++] = (int)KE_CSI;
8668 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008669#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008670 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 {
8672 arg[len++] = *++p;
8673 if (*p == K_SPECIAL)
8674 {
8675 arg[len++] = KS_SPECIAL;
8676 arg[len++] = KE_FILLER;
8677 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008678#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 else if (*p == CSI)
8680 {
8681 arg[len++] = KS_EXTRA;
8682 arg[len++] = (int)KE_CSI;
8683 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 }
8686 arg[len] = NUL;
8687 }
8688 }
8689 }
8690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008692 ++ex_normal_busy;
8693 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 {
8695 /*
8696 * Repeat the :normal command for each line in the range. When no
8697 * range given, execute it just once, without positioning the cursor
8698 * first.
8699 */
8700 do
8701 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 if (eap->addr_count != 0)
8703 {
8704 curwin->w_cursor.lnum = eap->line1++;
8705 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008706 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 }
8708
Bram Moolenaar13505972019-01-24 15:04:48 +01008709 exec_normal_cmd(arg != NULL
8710 ? arg
8711 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 }
8713 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8714 }
8715
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008716 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 update_topline_cursor();
8718
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008719 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008721 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008722#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008723 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008724#endif
8725
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727}
8728
8729/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008730 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 */
8732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008733ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008735 if (eap->forceit)
8736 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008737 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008738 if (!curwin->w_cursor.lnum)
8739 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008740 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008741 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008742#ifdef FEAT_TERMINAL
8743 // Ignore this when running in an active terminal.
8744 if (term_job_running(curbuf->b_term))
8745 return;
8746#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008747
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008748 // Ignore the command when already in Insert mode. Inserting an
8749 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01008750 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00008751 return;
8752
Bram Moolenaar64969662005-12-14 21:59:55 +00008753 if (eap->cmdidx == CMD_startinsert)
8754 restart_edit = 'a';
8755 else if (eap->cmdidx == CMD_startreplace)
8756 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008758 restart_edit = 'V';
8759
8760 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008762 if (eap->cmdidx == CMD_startinsert)
8763 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008764 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008766
8767 if (VIsual_active)
8768 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769}
8770
8771/*
8772 * ":stopinsert"
8773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008775ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
8777 restart_edit = 0;
8778 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008779 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008782/*
8783 * Execute normal mode command "cmd".
8784 * "remap" can be REMAP_NONE or REMAP_YES.
8785 */
8786 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008787exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008788{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008789 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008790 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008791 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008792}
Bram Moolenaar25281632016-01-21 23:32:32 +01008793
Bram Moolenaar25281632016-01-21 23:32:32 +01008794/*
8795 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008796 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008797 */
8798 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008799exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008800{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008801 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008802 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008803
Bram Moolenaar905dd902019-04-07 14:21:47 +02008804 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8805 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008806 clear_oparg(&oa);
8807 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008808 while ((!stuff_empty()
8809 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008810 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008811 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008812 {
8813 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008814#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008815 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008816 && oa.op_type == OP_NOP && oa.regname == NUL
8817 && !VIsual_active)
8818 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008819 // If terminal_loop() returns OK we got a key that is handled
8820 // in Normal model. With FAIL we first need to position the
8821 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008822 if (terminal_loop(TRUE) == OK)
8823 normal_cmd(&oa, TRUE);
8824 }
8825 else
8826#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008827 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008828 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008829 }
8830}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008831
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832#ifdef FEAT_FIND_ID
8833 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008834ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
8836 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8837 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8838 (linenr_T)1, (linenr_T)MAXLNUM);
8839}
8840
Bram Moolenaar4033c552017-09-16 20:54:51 +02008841#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842/*
8843 * ":psearch"
8844 */
8845 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008846ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847{
8848 g_do_tagpreview = p_pvh;
8849 ex_findpat(eap);
8850 g_do_tagpreview = 0;
8851}
8852#endif
8853
8854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008855ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856{
8857 int whole = TRUE;
8858 long n;
8859 char_u *p;
8860 int action;
8861
8862 switch (cmdnames[eap->cmdidx].cmd_name[2])
8863 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008864 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8866 action = ACTION_GOTO;
8867 else
8868 action = ACTION_SHOW;
8869 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008870 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 action = ACTION_SHOW_ALL;
8872 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008873 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 action = ACTION_GOTO;
8875 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008876 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 action = ACTION_SPLIT;
8878 break;
8879 }
8880
8881 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008882 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 {
8884 n = getdigits(&eap->arg);
8885 eap->arg = skipwhite(eap->arg);
8886 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008887 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 {
8889 whole = FALSE;
8890 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008891 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 if (*p)
8893 {
8894 *p++ = NUL;
8895 p = skipwhite(p);
8896
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008897 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008898 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008899 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008901 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 }
8903 }
8904 if (!eap->skip)
8905 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8906 whole, !eap->forceit,
8907 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8908 n, action, eap->line1, eap->line2);
8909}
8910#endif
8911
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912
Bram Moolenaar4033c552017-09-16 20:54:51 +02008913#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914/*
8915 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8916 */
8917 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008918ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008920 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8922}
8923
8924/*
8925 * ":pedit"
8926 */
8927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008928ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929{
8930 win_T *curwin_save = curwin;
8931
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008932 if (ERROR_IF_ANY_POPUP_WINDOW)
8933 return;
8934
Bram Moolenaar026587b2019-08-17 15:08:00 +02008935 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008937 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008938
Bram Moolenaar026587b2019-08-17 15:08:00 +02008939 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008941
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 if (curwin != curwin_save && win_valid(curwin_save))
8943 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008944 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01008946 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 win_enter(curwin_save, TRUE);
8948 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008949# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008950 else if (WIN_IS_POPUP(curwin))
8951 {
8952 // can't keep focus in popup window
8953 win_enter(firstwin, TRUE);
8954 }
8955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 g_do_tagpreview = 0;
8957}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
8960/*
8961 * ":stag", ":stselect" and ":stjump".
8962 */
8963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008964ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965{
8966 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008967 postponed_split_flags = cmdmod.cmod_split;
8968 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8970 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008971 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973
8974/*
8975 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8976 */
8977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008978ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
8980 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8981}
8982
8983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008984ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985{
8986 int cmd;
8987
8988 switch (name[1])
8989 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008990 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008992 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008994 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008996 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008998 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009000 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009002 case 'f': // ":tfirst"
9003 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009005 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009007 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009009 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009011 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 return;
9013 }
9014#endif
9015 cmd = DT_TAG;
9016 break;
9017 }
9018
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009019 if (name[0] == 'l')
9020 {
9021#ifndef FEAT_QUICKFIX
9022 ex_ni(eap);
9023 return;
9024#else
9025 cmd = DT_LTAG;
9026#endif
9027 }
9028
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9030 eap->forceit, TRUE);
9031}
9032
9033/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009034 * Check "str" for starting with a special cmdline variable.
9035 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9036 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9037 */
9038 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009039find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009040{
9041 int len;
9042 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009043 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009044 "%",
9045#define SPEC_PERC 0
9046 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009047#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009048 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02009049#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009050 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02009051#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009052 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02009053#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009054 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009055#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009056 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009057#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009058 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02009059#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02009060 "<stack>", // call stack
9061#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01009062 "<script>", // script file name
9063#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009064 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01009065#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009066 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009067#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009068 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009069#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009070 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009071#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02009072 "<SID>", // script ID: <SNR>123_
9073#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009074#ifdef FEAT_CLIENTSERVER
9075 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02009076# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009077#endif
9078 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009079
K.Takataeeec2542021-06-02 13:28:16 +02009080 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009081 {
9082 len = (int)STRLEN(spec_str[i]);
9083 if (STRNCMP(src, spec_str[i], len) == 0)
9084 {
9085 *usedlen = len;
9086 return i;
9087 }
9088 }
9089 return -1;
9090}
9091
9092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 * Evaluate cmdline variables.
9094 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009095 * change "%" to curbuf->b_ffname
9096 * "#" to curwin->w_alt_fnum
9097 * "%%" to curwin->w_alt_fnum in Vim9 script
9098 * "<cword>" to word under the cursor
9099 * "<cWORD>" to WORD under the cursor
9100 * "<cexpr>" to C-expression under the cursor
9101 * "<cfile>" to path name under the cursor
9102 * "<sfile>" to sourced file name
9103 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009104 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009105 * "<slnum>" to sourced file line number
9106 * "<afile>" to file name for autocommand
9107 * "<abuf>" to buffer number for autocommand
9108 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 *
9110 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9111 * "" for error without a message) and NULL is returned.
9112 * Returns an allocated string if a valid match was found.
9113 * Returns NULL if no match was found. "usedlen" then still contains the
9114 * number of characters to skip.
9115 */
9116 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009117eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009118 char_u *src, // pointer into commandline
9119 char_u *srcstart, // beginning of valid memory for src
9120 int *usedlen, // characters after src that are used
9121 linenr_T *lnump, // line number for :e command, or NULL
9122 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009123 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009124 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009125 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126{
9127 int i;
9128 char_u *s;
9129 char_u *result;
9130 char_u *resultbuf = NULL;
9131 int resultlen;
9132 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009133 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009135 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138
9139 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009140 if (escaped != NULL)
9141 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142
9143 /*
9144 * Check if there is something to do.
9145 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009146 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009147 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 {
9149 *usedlen = 1;
9150 return NULL;
9151 }
9152
9153 /*
9154 * Skip when preceded with a backslash "\%" and "\#".
9155 * Note: In "\\%" the % is also not recognized!
9156 */
9157 if (src > srcstart && src[-1] == '\\')
9158 {
9159 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009160 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 return NULL;
9162 }
9163
9164 /*
9165 * word or WORD under cursor
9166 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009167 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9168 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009170 resultlen = find_ident_under_cursor(&result,
9171 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9172 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9173 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 if (resultlen == 0)
9175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009176 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 return NULL;
9178 }
9179 }
9180
9181 /*
9182 * '#': Alternate file name
9183 * '%': Current file name
9184 * File name under the cursor
9185 * File name for autocommand
9186 * and following modifiers
9187 */
9188 else
9189 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009190 int off = 0;
9191
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 switch (spec_idx)
9193 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009194 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009195#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009196 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009199 // '%': current file
9200 if (curbuf->b_fname == NULL)
9201 {
9202 result = (char_u *)"";
9203 valid = 0; // Must have ":p:h" to be valid
9204 }
9205 else
9206 {
9207 result = curbuf->b_fname;
9208 tilde_file = STRCMP(result, "~") == 0;
9209 }
9210 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009212#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009213 // "%%" alternate file
9214 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009215#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009216 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009217 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009218 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009220 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 result = arg_all();
9222 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009223 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009224 if (escaped != NULL)
9225 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 break;
9228 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009229 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009230 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009231 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009233 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009234 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009235 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009236 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009238 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009240 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009241 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009242 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009243 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009244 return NULL;
9245 }
9246#ifdef FEAT_EVAL
9247 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9248 (long)i);
9249 if (result == NULL)
9250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009251 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009252 return NULL;
9253 }
9254#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009255 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 }
9259 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009260 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009261 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9262 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009263 buf = buflist_findnr(i);
9264 if (buf == NULL)
9265 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009266 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009267 return NULL;
9268 }
9269 if (lnump != NULL)
9270 *lnump = ECMD_LAST;
9271 if (buf->b_fname == NULL)
9272 {
9273 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009274 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009275 }
9276 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009277 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009278 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009279 tilde_file = STRCMP(result, "~") == 0;
9280 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 break;
9283
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009284 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009285 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 if (result == NULL)
9287 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009288 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 return NULL;
9290 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009291 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009294 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009296 if (result != NULL && !autocmd_fname_full)
9297 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009298 // Still need to turn the fname into a full path. It is
9299 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009300 autocmd_fname_full = TRUE;
9301 result = FullName_save(autocmd_fname, FALSE);
9302 vim_free(autocmd_fname);
9303 autocmd_fname = result;
9304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 if (result == NULL)
9306 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009307 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 return NULL;
9309 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009310 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 break;
9312
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009313 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 if (autocmd_bufnr <= 0)
9315 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009316 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 return NULL;
9318 }
9319 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9320 result = strbuf;
9321 break;
9322
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009323 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 result = autocmd_match;
9325 if (result == NULL)
9326 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009327 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 return NULL;
9329 }
9330 break;
9331
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009332 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009333 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 if (result == NULL)
9335 {
LemonBoy6013d002022-04-09 21:42:10 +01009336 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9337 return NULL;
9338 }
9339 resultbuf = result; // remember allocated string
9340 break;
9341 case SPEC_STACK: // call stack
9342 result = estack_sfile(ESTACK_STACK);
9343 if (result == NULL)
9344 {
9345 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9346 return NULL;
9347 }
9348 resultbuf = result; // remember allocated string
9349 break;
9350 case SPEC_SCRIPT: // script file name
9351 result = estack_sfile(ESTACK_SCRIPT);
9352 if (result == NULL)
9353 {
9354 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 return NULL;
9356 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009357 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009359
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009360 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009361 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009362 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009363 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009364 return NULL;
9365 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009366 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009367 result = strbuf;
9368 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009369
9370#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009371 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009372 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009373 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009374 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009375 return NULL;
9376 }
9377 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009378 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009379 result = strbuf;
9380 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009381
Bram Moolenaar90944302020-08-01 20:45:11 +02009382 case SPEC_SID:
9383 if (current_sctx.sc_sid <= 0)
9384 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009385 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009386 return NULL;
9387 }
9388 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9389 result = strbuf;
9390 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009391#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009392
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009393#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009394 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009395 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9396 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 result = strbuf;
9398 break;
9399#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009400
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009401 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009402 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009403 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 }
9405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009406 resultlen = (int)STRLEN(result); // length of new string
9407 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 {
9409 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 resultlen = (int)(s - result);
9412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 else if (!skip_mod)
9414 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009415 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 &resultlen);
9417 if (result == NULL)
9418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009419 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 return NULL;
9421 }
9422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 }
9424
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009425 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009427 if (empty_is_error)
9428 {
9429 if (valid != VALID_HEAD + VALID_PATH)
9430 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9431 else
9432 *errormsg = _(e_evaluates_to_an_empty_string);
9433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 result = NULL;
9435 }
9436 else
9437 result = vim_strnsave(result, resultlen);
9438 vim_free(resultbuf);
9439 return result;
9440}
9441
9442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 * Expand the <sfile> string in "arg".
9444 *
9445 * Returns an allocated string, or NULL for any error.
9446 */
9447 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009448expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009450 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 int len;
9452 char_u *result;
9453 char_u *newres;
9454 char_u *repl;
9455 int srclen;
9456 char_u *p;
9457
9458 result = vim_strsave(arg);
9459 if (result == NULL)
9460 return NULL;
9461
9462 for (p = result; *p; )
9463 {
9464 if (STRNCMP(p, "<sfile>", 7) != 0)
9465 ++p;
9466 else
9467 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009468 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009469 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 if (errormsg != NULL)
9471 {
9472 if (*errormsg)
9473 emsg(errormsg);
9474 vim_free(result);
9475 return NULL;
9476 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009477 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 {
9479 p += srclen;
9480 continue;
9481 }
9482 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9483 newres = alloc(len);
9484 if (newres == NULL)
9485 {
9486 vim_free(repl);
9487 vim_free(result);
9488 return NULL;
9489 }
9490 mch_memmove(newres, result, (size_t)(p - result));
9491 STRCPY(newres + (p - result), repl);
9492 len = (int)STRLEN(newres);
9493 STRCAT(newres, p + srclen);
9494 vim_free(repl);
9495 vim_free(result);
9496 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009497 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 }
9499 }
9500
9501 return result;
9502}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009505/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009506 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009507 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009510dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 if (fname == NULL)
9513 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009514 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515}
9516#endif
9517
9518/*
9519 * ":behave {mswin,xterm}"
9520 */
9521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009522ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
9524 if (STRCMP(eap->arg, "mswin") == 0)
9525 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009526 set_option_value_give_err((char_u *)"selection",
9527 0L, (char_u *)"exclusive", 0);
9528 set_option_value_give_err((char_u *)"selectmode",
9529 0L, (char_u *)"mouse,key", 0);
9530 set_option_value_give_err((char_u *)"mousemodel",
9531 0L, (char_u *)"popup", 0);
9532 set_option_value_give_err((char_u *)"keymodel",
9533 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 }
9535 else if (STRCMP(eap->arg, "xterm") == 0)
9536 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009537 set_option_value_give_err((char_u *)"selection",
9538 0L, (char_u *)"inclusive", 0);
9539 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9540 set_option_value_give_err((char_u *)"mousemodel",
9541 0L, (char_u *)"extend", 0);
9542 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 }
9544 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009545 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546}
9547
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548static int filetype_detect = FALSE;
9549static int filetype_plugin = FALSE;
9550static int filetype_indent = FALSE;
9551
9552/*
9553 * ":filetype [plugin] [indent] {on,off,detect}"
9554 * on: Load the filetype.vim file to install autocommands for file types.
9555 * off: Load the ftoff.vim file to remove all autocommands for file types.
9556 * plugin on: load filetype.vim and ftplugin.vim
9557 * plugin off: load ftplugof.vim
9558 * indent on: load filetype.vim and indent.vim
9559 * indent off: load indoff.vim
9560 */
9561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009562ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 char_u *arg = eap->arg;
9565 int plugin = FALSE;
9566 int indent = FALSE;
9567
9568 if (*eap->arg == NUL)
9569 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009570 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009571 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 filetype_detect ? "ON" : "OFF",
9573 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9574 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9575 return;
9576 }
9577
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009578 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 for (;;)
9580 {
9581 if (STRNCMP(arg, "plugin", 6) == 0)
9582 {
9583 plugin = TRUE;
9584 arg = skipwhite(arg + 6);
9585 continue;
9586 }
9587 if (STRNCMP(arg, "indent", 6) == 0)
9588 {
9589 indent = TRUE;
9590 arg = skipwhite(arg + 6);
9591 continue;
9592 }
9593 break;
9594 }
9595 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9596 {
9597 if (*arg == 'o' || !filetype_detect)
9598 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009599 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 filetype_detect = TRUE;
9601 if (plugin)
9602 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009603 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 filetype_plugin = TRUE;
9605 }
9606 if (indent)
9607 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009608 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 filetype_indent = TRUE;
9610 }
9611 }
9612 if (*arg == 'd')
9613 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009614 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009615 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 }
9617 }
9618 else if (STRCMP(arg, "off") == 0)
9619 {
9620 if (plugin || indent)
9621 {
9622 if (plugin)
9623 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009624 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 filetype_plugin = FALSE;
9626 }
9627 if (indent)
9628 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009629 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 filetype_indent = FALSE;
9631 }
9632 }
9633 else
9634 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009635 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 filetype_detect = FALSE;
9637 }
9638 }
9639 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009640 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641}
9642
9643/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009644 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 */
9646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009647ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648{
9649 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009650 {
9651 char_u *arg = eap->arg;
9652
9653 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9654 arg += 9;
9655
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009656 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
Bram Moolenaar3e545692017-06-04 19:00:32 +02009657 if (arg != eap->arg)
9658 did_filetype = FALSE;
9659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009663ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665#ifdef FEAT_DIGRAPHS
9666 if (*eap->arg != NUL)
9667 putdigraph(eap->arg);
9668 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009669 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670#else
mityu61065042021-07-19 20:07:21 +02009671 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672#endif
9673}
9674
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009675#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9676 void
9677set_no_hlsearch(int flag)
9678{
9679 no_hlsearch = flag;
9680# ifdef FEAT_EVAL
9681 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9682# endif
9683}
9684
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685/*
9686 * ":nohlsearch"
9687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009689ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009691 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009692 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694#endif
9695
9696#ifdef FEAT_CRYPT
9697/*
9698 * ":X": Get crypt key
9699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009701ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009703 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009704 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705}
9706#endif
9707
9708#ifdef FEAT_FOLDING
9709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009710ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711{
9712 if (foldManualAllowed(TRUE))
9713 foldCreate(eap->line1, eap->line2);
9714}
9715
9716 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009717ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
9719 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9720 eap->forceit, FALSE);
9721}
9722
9723 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009724ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725{
9726 linenr_T lnum;
9727
Bram Moolenaar4facea32019-10-12 20:17:40 +02009728# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009729 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009730# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009731
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009732 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9734 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9735 ml_setmarked(lnum);
9736
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009737 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009739 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009740# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009741 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009742# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743}
9744#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009746#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009747/*
9748 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9749 * instead of a quickfix command.
9750 */
9751 int
9752is_loclist_cmd(int cmdidx)
9753{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009754 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009755 return FALSE;
9756 return cmdnames[cmdidx].cmd_name[0] == 'l';
9757}
9758#endif
9759
Bram Moolenaar4facea32019-10-12 20:17:40 +02009760#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009761 int
9762get_pressedreturn(void)
9763{
9764 return ex_pressedreturn;
9765}
9766
9767 void
9768set_pressedreturn(int val)
9769{
9770 ex_pressedreturn = val;
9771}
9772#endif