blob: be0e3465d1b83bb268a188ee528d5feaa0e3093f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058# define ex_cclose ex_ni
59# define ex_copen ex_ni
60# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020061# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000063#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
64# define ex_cexpr ex_ni
65#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020067static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020068static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010070static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020071#if !defined(FEAT_PERL) \
72 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
73 || !defined(FEAT_TCL) \
74 || !defined(FEAT_RUBY) \
75 || !defined(FEAT_LUA) \
76 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000077# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010080static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000082#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
86static void ex_highlight(exarg_T *eap);
87static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void ex_cquit(exarg_T *eap);
89static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_close(exarg_T *eap);
91static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
92static void ex_only(exarg_T *eap);
93static void ex_resize(exarg_T *eap);
94static void ex_stag(exarg_T *eap);
95static void ex_tabclose(exarg_T *eap);
96static void ex_tabonly(exarg_T *eap);
97static void ex_tabnext(exarg_T *eap);
98static void ex_tabmove(exarg_T *eap);
99static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100101static void ex_pclose(exarg_T *eap);
102static void ex_ptag(exarg_T *eap);
103static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#else
105# define ex_pclose ex_ni
106# define ex_ptag ex_ni
107# define ex_pedit ex_ni
108#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_hide(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100110static void ex_exit(exarg_T *eap);
111static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100113static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#else
115# define ex_goto ex_ni
116#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100117static void ex_shell(exarg_T *eap);
118static void ex_preserve(exarg_T *eap);
119static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_mode(exarg_T *eap);
121static void ex_wrongmodifier(exarg_T *eap);
122static void ex_find(exarg_T *eap);
123static void ex_open(exarg_T *eap);
124static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#ifndef FEAT_GUI
126# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100127static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100129#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#else
132# define ex_tearoff ex_ni
133#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100134#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
135 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100136static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#else
138# define ex_popup ex_ni
139#endif
140#ifndef FEAT_GUI_MSWIN
141# define ex_simalt ex_ni
142#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000143#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144# define gui_mch_find_dialog ex_ni
145# define gui_mch_replace_dialog ex_ni
146#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000147#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148# define ex_helpfind ex_ni
149#endif
150#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100151# define ex_cscope ex_ni
152# define ex_scscope ex_ni
153# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#endif
155#ifndef FEAT_SYN_HL
156# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200157# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000158#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200159#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200160# define ex_syntime ex_ni
161#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000162#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000163# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000164# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000165# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000166# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000167# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000168#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200169#ifndef FEAT_PERSISTENT_UNDO
170# define ex_rundo ex_ni
171# define ex_wundo ex_ni
172#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200173#ifndef FEAT_LUA
174# define ex_lua ex_script_ni
175# define ex_luado ex_ni
176# define ex_luafile ex_ni
177#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000178#ifndef FEAT_MZSCHEME
179# define ex_mzscheme ex_script_ni
180# define ex_mzfile ex_ni
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#ifndef FEAT_PERL
183# define ex_perl ex_script_ni
184# define ex_perldo ex_ni
185#endif
186#ifndef FEAT_PYTHON
187# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200188# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189# define ex_pyfile ex_ni
190#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200191#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200192# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200193# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194# define ex_py3file ex_ni
195#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100196#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
197# define ex_pyx ex_script_ni
198# define ex_pyxdo ex_ni
199# define ex_pyxfile ex_ni
200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifndef FEAT_TCL
202# define ex_tcl ex_script_ni
203# define ex_tcldo ex_ni
204# define ex_tclfile ex_ni
205#endif
206#ifndef FEAT_RUBY
207# define ex_ruby ex_script_ni
208# define ex_rubydo ex_ni
209# define ex_rubyfile ex_ni
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211#ifndef FEAT_KEYMAP
212# define ex_loadkeymap ex_ni
213#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100214static void ex_swapname(exarg_T *eap);
215static void ex_syncbind(exarg_T *eap);
216static void ex_read(exarg_T *eap);
217static void ex_pwd(exarg_T *eap);
218static void ex_equal(exarg_T *eap);
219static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100220static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000222#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#else
225# define ex_winpos ex_ni
226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ex_operators(exarg_T *eap);
228static void ex_put(exarg_T *eap);
229static void ex_copymove(exarg_T *eap);
230static void ex_submagic(exarg_T *eap);
231static void ex_join(exarg_T *eap);
232static void ex_at(exarg_T *eap);
233static void ex_bang(exarg_T *eap);
234static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200235#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ex_wundo(exarg_T *eap);
237static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_redo(exarg_T *eap);
240static void ex_later(exarg_T *eap);
241static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100243static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_startinsert(exarg_T *eap);
247static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_checkpath(exarg_T *eap);
250static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#else
252# define ex_findpat ex_ni
253# define ex_checkpath ex_ni
254#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200255#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#else
258# define ex_psearch ex_ni
259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static void ex_tag(exarg_T *eap);
261static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200263# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200264# define ex_break ex_ni
265# define ex_breakadd ex_ni
266# define ex_breakdel ex_ni
267# define ex_breaklist ex_ni
268# define ex_call ex_ni
269# define ex_catch ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000270# define ex_class ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200272# define ex_continue ex_ni
273# define ex_debug ex_ni
274# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200275# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200281# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200282# define ex_endfunction ex_ni
283# define ex_endif ex_ni
284# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285# define ex_endwhile ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000286# define ex_enum ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
289# define ex_finally ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000290# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200291# define ex_finish ex_ni
292# define ex_function ex_ni
293# define ex_if ex_ni
294# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200295# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200296# define ex_lockvar ex_ni
297# define ex_oldfiles ex_ni
298# define ex_options ex_ni
299# define ex_packadd ex_ni
300# define ex_packloadall ex_ni
301# define ex_return ex_ni
302# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_throw ex_ni
304# define ex_try ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000305# define ex_type ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200307# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_import ex_ni
309# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200311#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_loadview ex_ni
313#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200314#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315# define ex_viminfo ex_ni
316#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_filetype(exarg_T *eap);
319static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000321# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322# define ex_diffpatch ex_ni
323# define ex_diffgetput ex_ni
324# define ex_diffsplit ex_ni
325# define ex_diffthis ex_ni
326# define ex_diffupdate ex_ni
327#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#else
332# define ex_nohlsearch ex_ni
333# define ex_match ex_ni
334#endif
335#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_X ex_ni
339#endif
340#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_fold(exarg_T *eap);
342static void ex_foldopen(exarg_T *eap);
343static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#else
345# define ex_fold ex_ni
346# define ex_foldopen ex_ni
347# define ex_folddo ex_ni
348#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100349#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350# define ex_language ex_ni
351#endif
352#ifndef FEAT_SIGNS
353# define ex_sign ex_ni
354#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar05159a02005-02-26 23:04:13 +0000361#ifndef FEAT_PROFILE
362# define ex_profile ex_ni
363#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200364#ifndef FEAT_TERMINAL
365# define ex_terminal ex_ni
366#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200367#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
368# define ex_xrestore ex_ni
369#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100370#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200371# define ex_popupclear ex_ni
372#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374/*
375 * Declare cmdnames[].
376 */
377#define DO_DECLARE_EXCMD
378#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200379#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381static char_u dollar_command[2] = {'$', 0};
382
383
384#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100385// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386typedef struct
387{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388 char_u *line; // command line
389 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390} wcmd_T;
391
392/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000393 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000395 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000397struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100399 garray_T *lines_gap; // growarray with line info
400 int current_line; // last read line from growarray
401 int repeating; // TRUE when looping a second time
402 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200403 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 void *cookie;
405};
406
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200407static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static int store_loop_line(garray_T *gap, char_u *line);
409static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000410
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100411// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000412struct dbg_stuff
413{
414 int trylevel;
415 int force_abort;
416 except_T *caught_stack;
417 char_u *vv_exception;
418 char_u *vv_throwpoint;
419 int did_emsg;
420 int got_int;
421 int did_throw;
422 int need_rethrow;
423 int check_cstack;
424 except_T *current_exception;
425};
426
Bram Moolenaared203462004-06-16 11:19:22 +0000427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100428save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000429{
430 dsp->trylevel = trylevel; trylevel = 0;
431 dsp->force_abort = force_abort; force_abort = FALSE;
432 dsp->caught_stack = caught_stack; caught_stack = NULL;
433 dsp->vv_exception = v_exception(NULL);
434 dsp->vv_throwpoint = v_throwpoint(NULL);
435
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100436 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000437 dsp->did_emsg = did_emsg; did_emsg = FALSE;
438 dsp->got_int = got_int; got_int = FALSE;
439 dsp->did_throw = did_throw; did_throw = FALSE;
440 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
441 dsp->check_cstack = check_cstack; check_cstack = FALSE;
442 dsp->current_exception = current_exception; current_exception = NULL;
443}
444
445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100446restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000447{
448 suppress_errthrow = FALSE;
449 trylevel = dsp->trylevel;
450 force_abort = dsp->force_abort;
451 caught_stack = dsp->caught_stack;
452 (void)v_exception(dsp->vv_exception);
453 (void)v_throwpoint(dsp->vv_throwpoint);
454 did_emsg = dsp->did_emsg;
455 got_int = dsp->got_int;
456 did_throw = dsp->did_throw;
457 need_rethrow = dsp->need_rethrow;
458 check_cstack = dsp->check_cstack;
459 current_exception = dsp->current_exception;
460}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461#endif
462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463/*
464 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
465 * command is given.
466 */
467 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100468do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100469 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 int save_msg_scroll;
472 int prev_msg_row;
473 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100474 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000475
476 if (improved)
477 exmode_active = EXMODE_VIM;
478 else
479 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100480 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100481 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100483 // When using ":global /pat/ visual" and then "Q" we return to continue
484 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000485 if (global_busy)
486 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
488 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100489 ++RedrawingDisabled; // don't redisplay the window
490 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100492 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 ++hold_gui_events;
494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495
Bram Moolenaar32526b32019-01-19 17:43:09 +0100496 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 while (exmode_active)
498 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100499 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000500 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
501 {
502 exmode_active = FALSE;
503 break;
504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 msg_scroll = TRUE;
506 need_wait_return = FALSE;
507 ex_pressedreturn = FALSE;
508 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100509 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 prev_msg_row = msg_row;
511 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 if (improved)
513 {
514 cmdline_row = msg_row;
515 do_cmdline(NULL, getexline, NULL, 0);
516 }
517 else
518 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
519 lines_left = Rows - 1;
520
Bram Moolenaardf177f62005-02-22 08:39:57 +0000521 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100522 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000524 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000525 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if (ex_pressedreturn)
529 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100530 // go up one line, to overwrite the ":<CR>" line, so the
531 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 msg_row = prev_msg_row;
533 if (prev_msg_row == Rows - 1)
534 msg_row--;
535 }
536 msg_col = 0;
537 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
538 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100541 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000542 {
543 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000544 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000545 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000546 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
549
550#ifdef FEAT_GUI
551 --hold_gui_events;
552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 --RedrawingDisabled;
554 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100555 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 need_wait_return = FALSE;
557 msg_scroll = save_msg_scroll;
558}
559
560/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200561 * Print the executed command for when 'verbose' is set.
562 * When "lnum" is 0 only print the command.
563 */
564 static void
565msg_verbose_cmd(linenr_T lnum, char_u *cmd)
566{
567 ++no_wait_return;
568 verbose_enter_scroll();
569
570 if (lnum == 0)
571 smsg(_("Executing: %s"), cmd);
572 else
573 smsg(_("line %ld: %s"), (long)lnum, cmd);
574 if (msg_silent == 0)
575 msg_puts("\n"); // don't overwrite this
576
577 verbose_leave_scroll();
578 --no_wait_return;
579}
580
581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 * Execute a simple command line. Used for translated commands like "*".
583 */
584 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100585do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
587 return do_cmdline(cmd, NULL, NULL,
588 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
589}
590
591/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100592 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
593 * This allows for using a range without ":" in Vim9 script.
594 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200595 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100596do_cmd_argument(char_u *cmd)
597{
598 return do_cmdline(cmd, NULL, NULL,
599 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
600}
601
602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * do_cmdline(): execute one Ex command line
604 *
605 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100606 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * 2. Split up in parts separated with '|'.
608 *
609 * This function can be called recursively!
610 *
611 * flags:
612 * DOCMD_VERBOSE - The command will be included in the error message.
613 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100614 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 * DOCMD_KEYTYPED - Don't reset KeyTyped.
616 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
617 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
618 *
619 * return FAIL if cmdline could not be executed, OK otherwise
620 */
621 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100622do_cmdline(
623 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200624 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100625 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100626 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100628 char_u *next_cmdline; // next cmd to execute
629 char_u *cmdline_copy = NULL; // copy of cmd line
630 int used_getline = FALSE; // used "fgetline" to obtain command
631 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100633 int count = 0; // line number count
634 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 int retval = OK;
636#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100637 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100638 garray_T lines_ga; // keep lines for ":while"/":for"
639 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200640 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100641 char_u *fname = NULL; // function or script name
642 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
643 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
644 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200646 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200647 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100649 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200650 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000652 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000654 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100656# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657# define cmd_cookie cookie
658#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100659 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200660#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100661 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
662 // location for storing error messages to be converted to an exception.
663 // This ensures that the do_errthrow() call in do_one_cmd() does not
664 // combine the messages stored by an earlier invocation of do_one_cmd()
665 // with the command name of the later one. This would happen when
666 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 saved_msg_list = msg_list;
668 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669#endif
670
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100671 // It's possible to create an endless loop with ":execute", catch that
672 // here. The value of 200 allows nested function calls, ":source", etc.
673 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100674 if (call_depth >= 200
675#ifdef FEAT_EVAL
676 && call_depth >= p_mfd
677#endif
678 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000680 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100682 // When converting to an exception, we do not include the command name
683 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100684 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 msg_list = saved_msg_list;
686#endif
687 return FAIL;
688 }
689 ++call_depth;
690
691#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100692 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000694 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100698 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100699 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000700 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 ++ex_nesting_level;
702
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100703 // Get the function or script name and the address where the next breakpoint
704 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000705 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
707 fname = func_name(real_cookie);
708 breakpoint = func_breakpoint(real_cookie);
709 dbg_tick = func_dbg_tick(real_cookie);
710 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100711 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100713 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 breakpoint = source_breakpoint(real_cookie);
715 dbg_tick = source_dbg_tick(real_cookie);
716 }
717
718 /*
719 * Initialize "force_abort" and "suppress_errthrow" at the top level.
720 */
721 if (!recursive)
722 {
723 force_abort = FALSE;
724 suppress_errthrow = FALSE;
725 }
726
727 /*
728 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000729 * exception handling (used when debugging). Otherwise clear it to avoid
730 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000732 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000733 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000734 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200735 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
737 initial_trylevel = trylevel;
738
739 /*
740 * "did_throw" will be set to TRUE when an exception is being thrown.
741 */
742 did_throw = FALSE;
743#endif
744 /*
745 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000746 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 * If force_abort is set, we cancel everything.
748 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100749#ifdef FEAT_EVAL
750 did_emsg_cumul += did_emsg;
751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 did_emsg = FALSE;
753
754 /*
755 * KeyTyped is only set when calling vgetc(). Reset it here when not
756 * calling vgetc() (sourced command lines).
757 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100758 if (!(flags & DOCMD_KEYTYPED)
759 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 KeyTyped = FALSE;
761
762 /*
763 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000764 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 * - for multiple commands on one line, separated with '|'
766 * - when repeating until there are no more lines (for ":source")
767 */
768 next_cmdline = cmdline;
769 do
770 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000771#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100772 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000773#endif
774
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100775 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 if (next_cmdline == NULL
777#ifdef FEAT_EVAL
778 && !force_abort
779 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000780 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781#endif
782 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100783 {
784#ifdef FEAT_EVAL
785 did_emsg_cumul += did_emsg;
786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000791 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100792 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 * 3. If a line is given: Make a copy, so we can mess with it.
794 */
795
796#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100797 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000798 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100800 // Each '|' separated command is stored separately in lines_ga, to
801 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100802 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100804 // Check if a function has returned or, unless it has an unclosed
805 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000809 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 func_line_end(real_cookie);
811# endif
812 if (func_has_ended(real_cookie))
813 {
814 retval = FAIL;
815 break;
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000819 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100820 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 script_line_end();
822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100824 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
827 retval = FAIL;
828 break;
829 }
830
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100831 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 if (breakpoint != NULL && dbg_tick != NULL
833 && *dbg_tick != debug_tick)
834 {
835 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100836 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 *dbg_tick = debug_tick;
839 }
840
841 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100842 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100844 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100846 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100848 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100849 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100851 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100852 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 *dbg_tick = debug_tick;
854 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000855# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000856 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000857 {
858 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100859 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100860 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000861 script_line_start();
862 }
863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865#endif
866
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100867 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (next_cmdline == NULL)
869 {
870 /*
871 * Need to set msg_didout for the first line after an ":if",
872 * otherwise the ":if" will be overwritten.
873 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100874 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100876 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#ifdef FEAT_EVAL
878 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
879#else
880 0
881#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100882 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
883 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100885 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100886 // returned for the end of a sourced file or executed function
887 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (KeyTyped && !(flags & DOCMD_REPEAT))
889 need_wait_return = FALSE;
890 retval = FAIL;
891 break;
892 }
893 used_getline = TRUE;
894
895 /*
896 * Keep the first typed line. Clear it when more lines are typed.
897 */
898 if (flags & DOCMD_KEEPLINE)
899 {
900 vim_free(repeat_cmdline);
901 if (count == 0)
902 repeat_cmdline = vim_strsave(next_cmdline);
903 else
904 repeat_cmdline = NULL;
905 }
906 }
907
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100908 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 else if (cmdline_copy == NULL)
910 {
911 next_cmdline = vim_strsave(next_cmdline);
912 if (next_cmdline == NULL)
913 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200914 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 retval = FAIL;
916 break;
917 }
918 }
919 cmdline_copy = next_cmdline;
920
921#ifdef FEAT_EVAL
922 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200923 * Inside a while/for loop, and when the command looks like a ":while"
924 * or ":for", the line is stored, because we may need it later when
925 * looping.
926 *
927 * When there is a '|' and another command, it is stored separately,
928 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000929 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200930 *
931 * Pass a different "fgetline" function to do_one_cmd() below,
932 * that it stores lines in or reads them from "lines_ga". Makes it
933 * possible to define a function inside a while/for loop and handles
934 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200936 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200938 cmd_getline = get_loop_line;
939 cmd_cookie = (void *)&cmd_loop_cookie;
940 cmd_loop_cookie.lines_gap = &lines_ga;
941 cmd_loop_cookie.current_line = current_line;
942 cmd_loop_cookie.getline = fgetline;
943 cmd_loop_cookie.cookie = cookie;
944 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
945
946 // Save the current line when encountering it the first time.
947 if (current_line == lines_ga.ga_len
948 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 {
950 retval = FAIL;
951 break;
952 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200953 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200955 else
956 {
957 cmd_getline = fgetline;
958 cmd_cookie = cookie;
959 }
960
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 did_endif = FALSE;
962#endif
963
964 if (count++ == 0)
965 {
966 /*
967 * All output from the commands is put below each other, without
968 * waiting for a return. Don't do this when executing commands
969 * from a script or when being called recursive (e.g. for ":e
970 * +command file").
971 */
972 if (!(flags & DOCMD_NOWAIT) && !recursive)
973 {
974 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100975 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100977 msg_scroll = TRUE; // put messages below each other
978 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 ++RedrawingDisabled;
980 did_inc = TRUE;
981 }
982 }
983
Bram Moolenaar823654b2020-05-29 23:03:09 +0200984 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100985 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 /*
988 * 2. Execute one '|' separated command.
989 * do_one_cmd() will return NULL if there is no trailing '|'.
990 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
991 */
992 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100993 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#ifdef FEAT_EVAL
995 &cstack,
996#endif
997 cmd_getline, cmd_cookie);
998 --recursive;
999
1000#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001001 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001002 // Use "current_line" from "cmd_loop_cookie", it may have been
1003 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001004 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005#endif
1006
1007 if (next_cmdline == NULL)
1008 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001009 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001010
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 /*
1012 * If the command was typed, remember it for the ':' register.
1013 * Do this AFTER executing the command to make :@: work.
1014 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001015 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 && new_last_cmdline != NULL)
1017 {
1018 vim_free(last_cmdline);
1019 last_cmdline = new_last_cmdline;
1020 new_last_cmdline = NULL;
1021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023 else
1024 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001025 // need to copy the command after the '|' to cmdline_copy, for the
1026 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001027 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 next_cmdline = cmdline_copy;
1029 }
1030
1031
1032#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001033 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001035 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001037 {
1038 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 ++current_line;
1045
1046 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 * An ":endwhile", ":endfor" and ":continue" is handled here.
1048 * If we were executing commands, jump back to the ":while" or
1049 * ":for".
1050 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001056 // Jump back to the matching ":while" or ":for". Be careful
1057 // not to use a cs_line[] from an entry that isn't a ":while"
1058 // or ":for": It would make "current_line" invalid and can
1059 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (!did_emsg && !got_int && !did_throw
1061 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 && (cstack.cs_flags[cstack.cs_idx]
1063 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 && cstack.cs_line[cstack.cs_idx] >= 0
1065 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1066 {
1067 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001068 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001070 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001072 // Check for the next breakpoint at or after the ":while"
1073 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001074 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
1076 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001077 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 fname,
1079 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1080 *dbg_tick = debug_tick;
1081 }
1082 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001085 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001087 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1088 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091
1092 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001098 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
1100 }
1101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001102 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001103 if (breakpoint != NULL && has_watchexpr())
1104 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001105 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001106 *dbg_tick = debug_tick;
1107 }
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 /*
1110 * When not inside any ":while" loop, clear remembered lines.
1111 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {
1114 if (lines_ga.ga_len > 0)
1115 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001116 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1118 free_cmdlines(&lines_ga);
1119 }
1120 current_line = 0;
1121 }
1122
1123 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1125 * being restored at the ":endtry". Reset them here and set the
1126 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1127 * This includes the case where a missing ":endif", ":endwhile" or
1128 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 cstack.cs_lflags &= ~CSL_HAD_FINA;
1133 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1134 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 did_throw ? (void *)current_exception : NULL);
1136 did_emsg = got_int = did_throw = FALSE;
1137 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1138 }
1139
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001140 // Update global "trylevel" for recursive calls to do_cmdline() from
1141 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 trylevel = initial_trylevel + cstack.cs_trylevel;
1143
1144 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001145 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * files) is aborted because of an error, an interrupt, or an uncaught
1147 * exception, cancel everything. If it is left normally, reset
1148 * force_abort to get the non-EH compatible abortion behavior for
1149 * the rest of the script.
1150 */
1151 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1152 force_abort = FALSE;
1153
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001154 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001156#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158 }
1159 /*
1160 * Continue executing command lines when:
1161 * - no CTRL-C typed, no aborting error, no exception thrown or try
1162 * conditionals need to be checked for executing finally clauses or
1163 * catching an interrupt exception
1164 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 * looping for ":source" command or function call.
1167 */
1168 while (!((got_int
1169#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001170 || (did_emsg && (force_abort || in_vim9script()))
1171 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172#endif
1173 )
1174#ifdef FEAT_EVAL
1175 && cstack.cs_trylevel == 0
1176#endif
1177 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001178 && !(did_emsg
1179#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001180 // Keep going when inside try/catch, so that the error can be
1181 // deal with, except when it is a syntax error, it may cause
1182 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001183 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1184#endif
1185 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001186 && (getline_equal(fgetline, cookie, getexmodeline)
1187 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 && (next_cmdline != NULL
1189#ifdef FEAT_EVAL
1190 || cstack.cs_idx >= 0
1191#endif
1192 || (flags & DOCMD_REPEAT)));
1193
1194 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001195 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196#ifdef FEAT_EVAL
1197 free_cmdlines(&lines_ga);
1198 ga_clear(&lines_ga);
1199
1200 if (cstack.cs_idx >= 0)
1201 {
1202 /*
1203 * If a sourced file or executed function ran to its end, report the
1204 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001205 * In Vim9 script do not give a second error, executing aborts after
1206 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001208 if (!got_int && !did_throw && !aborting()
1209 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001210 && ((getline_equal(fgetline, cookie, getsourceline)
1211 && !source_finished(fgetline, cookie))
1212 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 && !func_has_ended(real_cookie))))
1214 {
1215 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001216 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001218 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001220 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001222 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224
1225 /*
1226 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1227 * ":endtry" in a sourced file or executed function. If the try
1228 * conditional is in its finally clause, ignore anything pending.
1229 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001230 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 */
1232 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001233 {
1234 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1235
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001236 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001237 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001238 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1239 &cstack.cs_looplevel);
1240 }
1241 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 trylevel = initial_trylevel;
1243 }
1244
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001245 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1246 // lack was reported above and the error message is to be converted to an
1247 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001248 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 ? (char_u *)"endfunction" : (char_u *)NULL);
1250
1251 if (trylevel == 0)
1252 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001253 // Just in case did_throw got set but current_exception wasn't.
1254 if (current_exception == NULL)
1255 did_throw = FALSE;
1256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 /*
1258 * When an exception is being thrown out of the outermost try
1259 * conditional, discard the uncaught exception, disable the conversion
1260 * of interrupts or errors to exceptions, and ensure that no more
1261 * commands are executed.
1262 */
1263 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001264 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266 /*
1267 * On an interrupt or an aborting error not converted to an exception,
1268 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001269 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 * when force_abort is set and did_emsg unset in case of an interrupt
1271 * from a finally clause after an error.
1272 */
1273 else if (got_int || (did_emsg && force_abort))
1274 suppress_errthrow = TRUE;
1275 }
1276
1277 /*
1278 * The current cstack will be freed when do_cmdline() returns. An uncaught
1279 * exception will have to be rethrown in the previous cstack. If a function
1280 * has just returned or a script file was just finished and the previous
1281 * cstack belongs to the same function or, respectively, script file, it
1282 * will have to be checked for finally clauses to be executed due to the
1283 * ":return" or ":finish". This is done in do_one_cmd().
1284 */
1285 if (did_throw)
1286 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001287 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001289 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 && ex_nesting_level > func_level(real_cookie) + 1))
1291 {
1292 if (!did_throw)
1293 check_cstack = TRUE;
1294 }
1295 else
1296 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001297 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001298 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 --ex_nesting_level;
1300 /*
1301 * Go to debug mode when returning from a function in which we are
1302 * single-stepping.
1303 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001304 if ((getline_equal(fgetline, cookie, getsourceline)
1305 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001307 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 ? (char_u *)_("End of sourced file")
1309 : (char_u *)_("End of function"));
1310 }
1311
1312 /*
1313 * Restore the exception environment (done after returning from the
1314 * debugger).
1315 */
1316 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001317 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
1319 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001320
1321 // Cleanup if "cs_emsg_silent_list" remains.
1322 if (cstack.cs_emsg_silent_list != NULL)
1323 {
1324 eslist_T *elem, *temp;
1325
1326 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1327 {
1328 temp = elem->next;
1329 vim_free(elem);
1330 }
1331 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001332#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
1334 /*
1335 * If there was too much output to fit on the command line, ask the user to
1336 * hit return before redrawing the screen. With the ":global" command we do
1337 * this only once after the command is finished.
1338 */
1339 if (did_inc)
1340 {
1341 --RedrawingDisabled;
1342 --no_wait_return;
1343 msg_scroll = FALSE;
1344
1345 /*
1346 * When just finished an ":if"-":else" which was typed, no need to
1347 * wait for hit-return. Also for an error situation.
1348 */
1349 if (retval == FAIL
1350#ifdef FEAT_EVAL
1351 || (did_endif && KeyTyped && !did_emsg)
1352#endif
1353 )
1354 {
1355 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001356 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358 else if (need_wait_return)
1359 {
1360 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001361 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 * here should not overwrite the command that may be shown before
1363 * doing that.
1364 */
1365 msg_didout |= msg_didout_before_start;
1366 wait_return(FALSE);
1367 }
1368 }
1369
Bram Moolenaar2a942252012-11-28 23:03:07 +01001370#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001371 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001372#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 /*
1374 * Reset if_level, in case a sourced script file contains more ":if" than
1375 * ":endif" (could be ":if x | foo | endif").
1376 */
1377 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001378#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 --call_depth;
1381 return retval;
1382}
1383
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001384#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001385/*
1386 * Handle when "did_throw" is set after executing commands.
1387 */
1388 void
1389handle_did_throw()
1390{
1391 char *p = NULL;
1392 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001393 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001394
1395 /*
1396 * If the uncaught exception is a user exception, report it as an
1397 * error. If it is an error exception, display the saved error
1398 * message now. For an interrupt exception, do nothing; the
1399 * interrupt message is given elsewhere.
1400 */
1401 switch (current_exception->type)
1402 {
1403 case ET_USER:
1404 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001405 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001406 current_exception->value);
1407 p = (char *)vim_strsave(IObuff);
1408 break;
1409 case ET_ERROR:
1410 messages = current_exception->messages;
1411 current_exception->messages = NULL;
1412 break;
1413 case ET_INTERRUPT:
1414 break;
1415 }
1416
1417 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1418 current_exception->throw_lnum);
1419 ESTACK_CHECK_SETUP
1420 current_exception->throw_name = NULL;
1421
1422 discard_current_exception(); // uses IObuff if 'verbose'
1423 suppress_errthrow = TRUE;
1424 force_abort = TRUE;
1425
1426 if (messages != NULL)
1427 {
1428 do
1429 {
1430 msglist_T *next = messages->next;
1431 int save_compiling = estack_compiling;
1432
1433 estack_compiling = messages->msg_compiling;
1434 emsg(messages->msg);
1435 vim_free(messages->msg);
1436 vim_free(messages->sfile);
1437 vim_free(messages);
1438 messages = next;
1439 estack_compiling = save_compiling;
1440 }
1441 while (messages != NULL);
1442 }
1443 else if (p != NULL)
1444 {
1445 emsg(p);
1446 vim_free(p);
1447 }
1448 vim_free(SOURCING_NAME);
1449 ESTACK_CHECK_NOW
1450 estack_pop();
1451}
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001454 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 */
1456 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001457get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001459 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 wcmd_T *wp;
1461 char_u *line;
1462
1463 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1464 {
1465 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001466 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001468 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (cp->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001470 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001472 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001473 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 ++cp->current_line;
1475
1476 return line;
1477 }
1478
1479 KeyTyped = FALSE;
1480 ++cp->current_line;
1481 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001482 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 return vim_strsave(wp->line);
1484}
1485
1486/*
1487 * Store a line in "gap" so that a ":while" loop can execute it again.
1488 */
1489 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001490store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491{
1492 if (ga_grow(gap, 1) == FAIL)
1493 return FAIL;
1494 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001495 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 return OK;
1498}
1499
1500/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001501 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 */
1503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001504free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
1506 while (gap->ga_len > 0)
1507 {
1508 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1509 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511}
1512#endif
1513
1514/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001515 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1516 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001519getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001520 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001521 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001522 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001525 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001528 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1529 // function that's originally used to obtain the lines. This may be
1530 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001531 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001532 cp = (struct loop_cookie *)cookie;
1533 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 {
1535 gp = cp->getline;
1536 cp = cp->cookie;
1537 }
1538 return gp == func;
1539#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001540 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541#endif
1542}
1543
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001545 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 * getline function. Otherwise return "cookie".
1547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001549getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001550 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001551 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
Bram Moolenaar13505972019-01-24 15:04:48 +01001553#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001554 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001555 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001557 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1558 // cookie that's originally used to obtain the lines. This may be nested
1559 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001560 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001561 cp = (struct loop_cookie *)cookie;
1562 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 {
1564 gp = cp->getline;
1565 cp = cp->cookie;
1566 }
1567 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001568#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001571}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001573#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001574/*
1575 * Get the next line source line without advancing.
1576 */
1577 char_u *
1578getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001579 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001580 void *cookie) // argument for fgetline()
1581{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001582 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001583 struct loop_cookie *cp;
1584 wcmd_T *wp;
1585
1586 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1587 // cookie that's originally used to obtain the lines. This may be nested
1588 // several levels.
1589 gp = fgetline;
1590 cp = (struct loop_cookie *)cookie;
1591 while (gp == get_loop_line)
1592 {
1593 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1594 {
1595 // executing lines a second time, use the stored copy
1596 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1597 return wp->line;
1598 }
1599 gp = cp->getline;
1600 cp = cp->cookie;
1601 }
1602 if (gp == getsourceline)
1603 return source_nextline(cp);
1604 return NULL;
1605}
1606#endif
1607
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001608
1609/*
1610 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1611 * ":bwipeout", etc.
1612 * Returns the buffer number.
1613 */
1614 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001615compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001616{
1617 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001618 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001619 int count = offset;
1620
1621 buf = firstbuf;
1622 while (buf->b_next != NULL && buf->b_fnum < lnum)
1623 buf = buf->b_next;
1624 while (count != 0)
1625 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001626 count += (offset < 0) ? 1 : -1;
1627 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1628 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001629 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001630 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001631 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001632 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001633 while (buf->b_ml.ml_mfp == NULL)
1634 {
1635 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1636 if (nextbuf == NULL)
1637 break;
1638 buf = nextbuf;
1639 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001640 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001641 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001642 if (addr_type == ADDR_LOADED_BUFFERS)
1643 while (buf->b_ml.ml_mfp == NULL)
1644 {
1645 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1646 if (nextbuf == NULL)
1647 break;
1648 buf = nextbuf;
1649 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001650 return buf->b_fnum;
1651}
1652
Bram Moolenaarb7316892019-05-01 18:08:42 +02001653/*
1654 * Return the window number of "win".
1655 * When "win" is NULL return the number of windows.
1656 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001657 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001658current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001659{
1660 win_T *wp;
1661 int nr = 0;
1662
Bram Moolenaar29323592016-07-24 22:04:11 +02001663 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001664 {
1665 ++nr;
1666 if (wp == win)
1667 break;
1668 }
1669 return nr;
1670}
1671
1672 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001673current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001674{
1675 tabpage_T *tp;
1676 int nr = 0;
1677
Bram Moolenaar29323592016-07-24 22:04:11 +02001678 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001679 {
1680 ++nr;
1681 if (tp == tab)
1682 break;
1683 }
1684 return nr;
1685}
1686
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001687 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001688comment_start(char_u *p, int starts_with_colon UNUSED)
1689{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001690 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001691 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001692 return *p == '"';
1693}
1694
Bram Moolenaarf240e182014-11-27 18:33:02 +01001695# define CURRENT_WIN_NR current_win_nr(curwin)
1696# define LAST_WIN_NR current_win_nr(NULL)
1697# define CURRENT_TAB_NR current_tab_nr(curtab)
1698# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700/*
1701 * Execute one Ex command.
1702 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001703 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1704 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 *
1706 * 1. skip comment lines and leading space
1707 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001708 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001709 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001710 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001711 * 6. parse arguments
1712 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001714 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 *
1716 * This function may be called recursively!
1717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001719do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001720 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001721 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001723 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001725 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001726 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001728 char_u *p;
1729 linenr_T lnum;
1730 long n;
1731 char *errormsg = NULL; // error message
1732 char_u *after_modifier = NULL;
1733 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001734 cmdmod_T save_cmdmod;
1735 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001736 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001737 int ni; // set when Not Implemented
1738 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001739 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001740 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001741#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001742 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001743#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001744 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001745 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaara80faa82020-04-12 19:37:17 +02001747 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 ea.line1 = 1;
1749 ea.line2 = 1;
1750#ifdef FEAT_EVAL
1751 ++ex_nesting_level;
1752#endif
1753
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001754 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (quitmore
1756#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001757 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001759#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001760 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001761 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 --quitmore;
1763
1764 /*
1765 * Reset browse, confirm, etc.. They are restored when returning, for
1766 * recursive calls.
1767 */
1768 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001770 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001771 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1772 goto doend;
1773
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001774/*
1775 * 1. Skip comment lines and leading white space and colons.
1776 * 2. Handle command modifiers.
1777 */
1778 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001780 ea.cmdlinep = cmdlinep;
1781 ea.getline = fgetline;
1782 ea.cookie = cookie;
1783#ifdef FEAT_EVAL
1784 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001785 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001787 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001788 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001789 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001790 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
1792#ifdef FEAT_EVAL
1793 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1794 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1795#else
1796 ea.skip = (if_level > 0);
1797#endif
1798
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001800 * 3. Skip over the range to find the command. Let "p" point to after it.
1801 *
1802 * We need the command to know what kind of range it uses.
1803 */
1804 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001805
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001806 // In Vim9 script a colon is required before the range. This may also be
1807 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001808 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001809 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001810 {
1811 may_have_range = FALSE;
1812 for (p = ea.cmd; p >= *cmdlinep; --p)
1813 {
1814 if (*p == ':')
1815 may_have_range = TRUE;
1816 if (p < ea.cmd && !VIM_ISWHITE(*p))
1817 break;
1818 }
1819 }
1820 else
1821 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001822 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001823 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001825#ifdef FEAT_EVAL
1826 // Handle ":export" - it functions almost like a command modifier.
1827 // ":export var Name: type"
1828 // ":export def Name(..."
1829 // etc.
1830 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1831 is_export = TRUE;
1832#endif
1833
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001834 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001835 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001836 if (ea.cmd == cmd + 1 && *cmd == '$')
1837 // should be "$VAR = val"
1838 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001839#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001840 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001841#else
1842 p = find_ex_command(&ea, NULL, NULL, NULL);
1843#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001844 if (ea.cmdidx == CMD_SIZE)
1845 {
1846 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1847
1848 // If a ':' before the range is missing, give a clearer error
1849 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001850 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001851 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001852 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001853 goto doend;
1854 }
1855 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001856 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001859
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001860#ifdef FEAT_EVAL
1861# ifdef FEAT_PROFILE
1862 // Count this line for profiling if skip is TRUE.
1863 if (do_profiling == PROF_YES
1864 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1865 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1866 {
1867 int skip = did_emsg || got_int || did_throw;
1868
1869 if (ea.cmdidx == CMD_catch)
1870 skip = !skip && !(cstack->cs_idx >= 0
1871 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1872 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1873 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1874 skip = skip || !(cstack->cs_idx >= 0
1875 && !(cstack->cs_flags[cstack->cs_idx]
1876 & (CSF_ACTIVE | CSF_TRUE)));
1877 else if (ea.cmdidx == CMD_finally)
1878 skip = FALSE;
1879 else if (ea.cmdidx != CMD_endif
1880 && ea.cmdidx != CMD_endfor
1881 && ea.cmdidx != CMD_endtry
1882 && ea.cmdidx != CMD_endwhile)
1883 skip = ea.skip;
1884
1885 if (!skip)
1886 {
1887 if (getline_equal(fgetline, cookie, get_func_line))
1888 func_line_exec(getline_cookie(fgetline, cookie));
1889 else if (getline_equal(fgetline, cookie, getsourceline))
1890 script_line_exec();
1891 }
1892 }
1893# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001894#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001895
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001896 ea.cmd = cmd;
1897
1898#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001899 // May go to debug mode. If this happens and the ">quit" debug command is
1900 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001901 dbg_check_breakpoint(&ea);
1902 if (!ea.skip && got_int)
1903 {
1904 ea.skip = TRUE;
1905 (void)do_intthrow(cstack);
1906 }
1907#endif
1908
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001909/*
1910 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 *
1912 * where 'addr' is:
1913 *
1914 * % (entire file)
1915 * $ [+-NUM]
1916 * 'x [+-NUM] (where x denotes a currently defined mark)
1917 * . [+-NUM]
1918 * [+-NUM]..
1919 * NUM
1920 *
1921 * The ea.cmd pointer is updated to point to the first character following the
1922 * range spec. If an initial address is found, but no second, the upper bound
1923 * is equal to the lower.
1924 */
1925
Bram Moolenaar25190db2019-05-04 15:05:28 +02001926 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001927 if (!IS_USER_CMDIDX(ea.cmdidx))
1928 {
1929 if (ea.cmdidx != CMD_SIZE)
1930 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1931 else
1932 ea.addr_type = ADDR_LINES;
1933
Bram Moolenaar25190db2019-05-04 15:05:28 +02001934 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001935 if (ea.cmdidx == CMD_wincmd && p != NULL)
1936 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001937#ifdef FEAT_QUICKFIX
1938 // :.cc in quickfix window uses line number
1939 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1940 ea.addr_type = ADDR_OTHER;
1941#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001942 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001943
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001944 if (!may_have_range)
1945 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001946 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1947 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001950 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 */
1952
1953 /*
1954 * Skip ':' and any white space
1955 */
1956 ea.cmd = skipwhite(ea.cmd);
1957 while (*ea.cmd == ':')
1958 ea.cmd = skipwhite(ea.cmd + 1);
1959
1960 /*
1961 * If we got a line, but no command, then go to the line.
1962 * If we find a '|' or '\n' we set ea.nextcmd.
1963 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001964 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1965 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
1967 /*
1968 * strange vi behaviour:
1969 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001970 * ":3|..." prints line 3 (not in Vim9 script)
1971 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001973 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001975 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 goto doend;
1977 }
1978
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001979 // If this looks like an undefined user command and there are CmdUndefined
1980 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001981 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1982 && ASCII_ISUPPER(*ea.cmd)
1983 && has_cmdundefined())
1984 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001985 int ret;
1986
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001987 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001988 while (ASCII_ISALNUM(*p))
1989 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001990 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001991 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1992 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001993 // If the autocommands did something and didn't cause an error, try
1994 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001995 p = (ret
1996#ifdef FEAT_EVAL
1997 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001998#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002000 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002001
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (p == NULL)
2003 {
2004 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002005 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 goto doend;
2007 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002008 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002009 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2010 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
2012 errormsg = uc_fun_cmd();
2013 goto doend;
2014 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (ea.cmdidx == CMD_SIZE)
2017 {
2018 if (!ea.skip)
2019 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002020 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002022 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002023 // If the modifier was parsed OK the error must be in the
2024 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002025 if (after_modifier != NULL)
2026 append_command(after_modifier);
2027 else
2028 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002029 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002030 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002031 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002032 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 }
2034 goto doend;
2035 }
2036
Bram Moolenaar958636c2014-10-21 20:01:58 +02002037 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2038 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002039#ifdef HAVE_EX_SCRIPT_NI
2040 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2041#endif
2042 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
2044#ifndef FEAT_EVAL
2045 /*
2046 * When the expression evaluation is disabled, recognize the ":if" and
2047 * ":endif" commands and ignore everything in between it.
2048 */
2049 if (ea.cmdidx == CMD_if)
2050 ++if_level;
2051 if (if_level)
2052 {
2053 if (ea.cmdidx == CMD_endif)
2054 --if_level;
2055 goto doend;
2056 }
2057
2058#endif
2059
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002060 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002061 if (*p == '!' && ea.cmdidx != CMD_substitute
2062 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
2064 ++p;
2065 ea.forceit = TRUE;
2066 }
2067 else
2068 ea.forceit = FALSE;
2069
2070/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002071 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002073 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002074 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075
2076 if (!ea.skip)
2077 {
2078#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002079 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002081 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002082 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 goto doend;
2084 }
2085#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002086 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002087 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002088 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002089 goto doend;
2090 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002091 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002093 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002094 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 goto doend;
2096 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002097
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002098 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002100 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2101 {
2102 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002103 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002104 goto doend;
2105 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002106 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2107 {
2108 // Command not allowed when text is locked
2109 errormsg = _(get_text_locked_msg());
2110 goto doend;
2111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002114 // Disallow editing another buffer when "curbuf_lock" is set.
2115 // Do allow ":checktime" (it is postponed).
2116 // Do allow ":edit" (check for an argument later).
2117 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002118 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002119 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002120 && ea.cmdidx != CMD_edit
2121 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002122 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002123 && curbuf_locked())
2124 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002126 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002128 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 goto doend;
2130 }
2131 }
2132
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002133 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002135 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 goto doend;
2137 }
2138
2139 /*
2140 * Don't complain about the range if it is not used
2141 * (could happen if line_count is accidentally set to 0).
2142 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002143 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 {
2145 /*
2146 * If the range is backwards, ask for confirmation and, if given, swap
2147 * ea.line1 & ea.line2 so it's forwards again.
2148 * When global command is busy, don't ask, will fail below.
2149 */
2150 if (!global_busy && ea.line1 > ea.line2)
2151 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002152 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002154 if (sourcing || exmode_active)
2155 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002156 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002157 goto doend;
2158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 if (ask_yesno((char_u *)
2160 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002161 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 }
2163 lnum = ea.line1;
2164 ea.line1 = ea.line2;
2165 ea.line2 = lnum;
2166 }
2167 if ((errormsg = invalid_range(&ea)) != NULL)
2168 goto doend;
2169 }
2170
Bram Moolenaarb7316892019-05-01 18:08:42 +02002171 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2172 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 ea.line2 = 1;
2174
2175 correct_range(&ea);
2176
2177#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002178 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002179 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002181 // Put the first line at the start of a closed fold, put the last line
2182 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 (void)hasFolding(ea.line1, &ea.line1, NULL);
2184 (void)hasFolding(ea.line2, NULL, &ea.line2);
2185 }
2186#endif
2187
2188#ifdef FEAT_QUICKFIX
2189 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002190 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 * option here, so things like % get expanded.
2192 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002193 p = replace_makeprg(&ea, p, cmdlinep);
2194 if (p == NULL)
2195 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196#endif
2197
2198 /*
2199 * Skip to start of argument.
2200 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2201 */
2202 if (ea.cmdidx == CMD_bang)
2203 ea.arg = p;
2204 else
2205 ea.arg = skipwhite(p);
2206
Bram Moolenaar379fb762018-08-30 15:58:28 +02002207 // ":file" cannot be run with an argument when "curbuf_lock" is set
2208 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2209 goto doend;
2210
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 /*
2212 * Check for "++opt=val" argument.
2213 * Must be first, allow ":w ++enc=utf8 !cmd"
2214 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002215 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2217 if (getargopt(&ea) == FAIL && !ni)
2218 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002219 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 goto doend;
2221 }
2222
2223 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2224 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002225 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002227 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002229 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 goto doend;
2231 }
2232 ea.arg = skipwhite(ea.arg + 1);
2233 ea.append = TRUE;
2234 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002235 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 ++ea.arg;
2238 ea.usefilter = TRUE;
2239 }
2240 }
2241
2242 if (ea.cmdidx == CMD_read)
2243 {
2244 if (ea.forceit)
2245 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002246 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 ea.forceit = FALSE;
2248 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002249 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
2251 ++ea.arg;
2252 ea.usefilter = TRUE;
2253 }
2254 }
2255
2256 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2257 {
2258 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002259 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
2261 ++ea.arg;
2262 ++ea.amount;
2263 }
2264 ea.arg = skipwhite(ea.arg);
2265 }
2266
2267 /*
2268 * Check for "+command" argument, before checking for next command.
2269 * Don't do this for ":read !cmd" and ":write !cmd".
2270 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002271 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2273
2274 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002275 * For commands that do not use '|' inside their argument: Check for '|' to
2276 * separate commands and '"' or '#' to start comments.
2277 *
2278 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002279 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002280 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002281 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002283 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2284 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002285 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002286 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002287 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002288 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002289 || ea.cmdidx == CMD_global
2290 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002291 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002292#ifdef FEAT_EVAL
2293 || inside_block(&ea)
2294#endif
2295 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 for (p = ea.arg; *p; ++p)
2298 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002299 // Remove one backslash before a newline, so that it's possible to
2300 // pass a newline to the shell and also a newline that is preceded
2301 // with a backslash. This makes it impossible to end a shell
2302 // command in a backslash, but that doesn't appear useful.
2303 // Halving the number of backslashes is incompatible with previous
2304 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002306 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002307 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
2309 ea.nextcmd = p + 1;
2310 *p = NUL;
2311 break;
2312 }
2313 }
2314 }
2315
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002316 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002317 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002319 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002320 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002322 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002323 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002324 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002326#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002327 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002328 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002330 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002331 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002334 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2335 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002336 {
2337 ea.regname = *ea.arg++;
2338#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002339 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002340 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2341 {
kuuote08d7b1c2021-10-04 22:17:36 +01002342 if (!ea.skip)
2343 {
2344 set_expr_line(vim_strsave(ea.arg), &ea);
2345 did_set_expr_line = TRUE;
2346 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002347 ea.arg += STRLEN(ea.arg);
2348 }
2349#endif
2350 ea.arg = skipwhite(ea.arg);
2351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353
2354 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002355 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 * count, it's a buffer name.
2357 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002358 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002359 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002360 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002362 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002364 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002366 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 goto doend;
2368 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002369 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
2371 ea.line2 = n;
2372 if (ea.addr_count == 0)
2373 ea.addr_count = 1;
2374 }
2375 else
2376 {
2377 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002378 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002379 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002380 else
2381 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 ++ea.addr_count;
2383 /*
2384 * Be vi compatible: no error message for out of range.
2385 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002386 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 ea.line2 = curbuf->b_ml.ml_line_count;
2388 }
2389 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002390
2391 /*
2392 * Check for flags: 'l', 'p' and '#'.
2393 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002394 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002395 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002396 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2397 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002399 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002400 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 goto doend;
2402 }
2403
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002404 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002406 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 goto doend;
2408 }
2409
2410#ifdef FEAT_EVAL
2411 /*
2412 * Skip the command when it's not going to be executed.
2413 * The commands like :if, :endif, etc. always need to be executed.
2414 * Also make an exception for commands that handle a trailing command
2415 * themselves.
2416 */
2417 if (ea.skip)
2418 {
2419 switch (ea.cmdidx)
2420 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002421 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_while:
2423 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002424 case CMD_for:
2425 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 case CMD_if:
2427 case CMD_elseif:
2428 case CMD_else:
2429 case CMD_endif:
2430 case CMD_try:
2431 case CMD_catch:
2432 case CMD_finally:
2433 case CMD_endtry:
2434 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002435 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 break;
2437
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002438 // Commands that handle '|' themselves. Check: A command should
2439 // either have the EX_TRLBAR flag, appear in this list or appear in
2440 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 case CMD_aboveleft:
2442 case CMD_and:
2443 case CMD_belowright:
2444 case CMD_botright:
2445 case CMD_browse:
2446 case CMD_call:
2447 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002448 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 case CMD_delfunction:
2450 case CMD_djump:
2451 case CMD_dlist:
2452 case CMD_dsearch:
2453 case CMD_dsplit:
2454 case CMD_echo:
2455 case CMD_echoerr:
2456 case CMD_echomsg:
2457 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002458 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002460 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002461 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_help:
2463 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002464 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_ijump:
2466 case CMD_ilist:
2467 case CMD_isearch:
2468 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002469 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_keepjumps:
2471 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002472 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 case CMD_leftabove:
2474 case CMD_let:
2475 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002476 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002477 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002479 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002480 case CMD_noautocmd:
2481 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 case CMD_perl:
2483 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002484 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002485 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002486 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 case CMD_return:
2488 case CMD_rightbelow:
2489 case CMD_ruby:
2490 case CMD_silent:
2491 case CMD_smagic:
2492 case CMD_snomagic:
2493 case CMD_substitute:
2494 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002495 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 case CMD_tcl:
2497 case CMD_throw:
2498 case CMD_tilde:
2499 case CMD_topleft:
2500 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002501 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002502 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 case CMD_verbose:
2504 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002505 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 break;
2507
2508 default: goto doend;
2509 }
2510 }
2511#endif
2512
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002513 if ((ea.argt & EX_XFILE)
2514 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2515 goto doend;
2516
2517#ifdef FEAT_EVAL
2518 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002520 emsg(_(e_invalid_command_after_export));
2521 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 /*
2526 * Accept buffer name. Cannot be used at the same time with a buffer
2527 * number. Don't do this for a user command.
2528 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002529 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002530 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
2532 /*
2533 * :bdelete, :bwipeout and :bunload take several arguments, separated
2534 * by spaces: find next space (skipping over escaped characters).
2535 * The others take one argument: ignore trailing spaces.
2536 */
2537 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2538 || ea.cmdidx == CMD_bunload)
2539 p = skiptowhite_esc(ea.arg);
2540 else
2541 {
2542 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002543 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 --p;
2545 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002546 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002547 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002548 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 goto doend;
2550 ea.addr_count = 1;
2551 ea.arg = skipwhite(p);
2552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002554 // The :try command saves the emsg_silent flag, reset it here when
2555 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002556 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002557 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002558 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002559 if (emsg_silent < 0)
2560 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002561 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002562 }
2563
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002565 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
Bram Moolenaar958636c2014-10-21 20:01:58 +02002568 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
2570 /*
2571 * Execute a user-defined command.
2572 */
2573 do_ucmd(&ea);
2574 }
2575 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
2577 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002578 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2581 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002582 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
2584
2585#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002586 // A command will reset "is_export" when exporting an item. If it is still
2587 // set something went wrong.
2588 if (is_export)
2589 {
2590 if (errormsg == NULL)
2591 errormsg = _(e_export_with_invalid_argument);
2592 is_export = FALSE;
2593 }
2594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002596 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002597 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002598 && current_sctx.sc_sid > 0
2599 && ea.cmdidx != CMD_endif
2600 && (cstack->cs_idx < 0
2601 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002602 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 /*
2605 * If the command just executed called do_cmdline(), any throw or ":return"
2606 * or ":finish" encountered there must also check the cstack of the still
2607 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2608 * exception, or reanimate a returned function or finished script file and
2609 * return or finish it again.
2610 */
2611 if (need_rethrow)
2612 do_throw(cstack);
2613 else if (check_cstack)
2614 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002615 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002617 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 && current_func_returned())
2619 do_return(&ea, TRUE, FALSE, NULL);
2620 }
2621 need_rethrow = check_cstack = FALSE;
2622#endif
2623
2624doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002625 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002628 curwin->w_cursor.col = 0;
2629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630
2631 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2632 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002633 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002635 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
2637 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002638 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002640 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
2642 emsg(errormsg);
2643 }
2644#ifdef FEAT_EVAL
2645 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002646 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2647 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002648
2649 if (did_set_expr_line)
2650 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002651 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652#endif
2653
Bram Moolenaare1004402020-10-24 20:49:43 +02002654 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002656 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002657 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002659 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 ea.nextcmd = NULL;
2661
2662#ifdef FEAT_EVAL
2663 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002664 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665#endif
2666
2667 return ea.nextcmd;
2668}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002670static char ex_error_buf[MSG_BUF_LEN];
2671
2672/*
2673 * Return an error message with argument included.
2674 * Uses a static buffer, only the last error will be kept.
2675 * "msg" will be translated, caller should use N_().
2676 */
2677 char *
2678ex_errmsg(char *msg, char_u *arg)
2679{
2680 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2681 return ex_error_buf;
2682}
2683
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002685 * Handle a range without a command.
2686 * Returns an error message on failure.
2687 */
2688 char *
2689ex_range_without_command(exarg_T *eap)
2690{
2691 char *errormsg = NULL;
2692
2693 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2694#ifdef FEAT_EVAL
2695 && !in_vim9script()
2696#endif
2697 )
2698 {
2699 eap->cmdidx = CMD_print;
2700 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2701 if ((errormsg = invalid_range(eap)) == NULL)
2702 {
2703 correct_range(eap);
2704 ex_print(eap);
2705 }
2706 }
2707 else if (eap->addr_count != 0)
2708 {
2709 if (eap->line2 > curbuf->b_ml.ml_line_count)
2710 {
2711 // With '-' in 'cpoptions' a line number past the file is an
2712 // error, otherwise put it at the end of the file.
2713 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2714 eap->line2 = -1;
2715 else
2716 eap->line2 = curbuf->b_ml.ml_line_count;
2717 }
2718
2719 if (eap->line2 < 0)
2720 errormsg = _(e_invalid_range);
2721 else
2722 {
2723 if (eap->line2 == 0)
2724 curwin->w_cursor.lnum = 1;
2725 else
2726 curwin->w_cursor.lnum = eap->line2;
2727 beginline(BL_SOL | BL_FIX);
2728 }
2729 }
2730 return errormsg;
2731}
2732
2733/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002734 * Check for an Ex command with optional tail.
2735 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002736 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002737 */
2738 static int
2739checkforcmd_opt(
2740 char_u **pp, // start of command
2741 char *cmd, // name of command
2742 int len, // required length
2743 int noparen)
2744{
2745 int i;
2746
2747 for (i = 0; cmd[i] != NUL; ++i)
2748 if (((char_u *)cmd)[i] != (*pp)[i])
2749 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002750 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002751 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002752 {
2753 *pp = skipwhite(*pp + i);
2754 return TRUE;
2755 }
2756 return FALSE;
2757}
2758
2759/*
2760 * Check for an Ex command with optional tail.
2761 * If there is a match advance "pp" to the argument and return TRUE.
2762 */
2763 int
2764checkforcmd(
2765 char_u **pp, // start of command
2766 char *cmd, // name of command
2767 int len) // required length
2768{
2769 return checkforcmd_opt(pp, cmd, len, FALSE);
2770}
2771
2772/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002773 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002774 * If there is a match advance "pp" to the argument and return TRUE.
2775 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002776 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002777checkforcmd_noparen(
2778 char_u **pp, // start of command
2779 char *cmd, // name of command
2780 int len) // required length
2781{
2782 return checkforcmd_opt(pp, cmd, len, TRUE);
2783}
2784
2785/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002786 * Parse and skip over command modifiers:
2787 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002788 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002789 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002790 * When "skip_only" is TRUE the global variables are not changed, except for
2791 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002792 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2793 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002794 * Call apply_cmdmod() to get the side effects of the modifiers:
2795 * - Increment "sandbox" for ":sandbox"
2796 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002797 * - set msg_silent for ":silent"
2798 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 * Return FAIL when the command is not to be executed.
2800 * May set "errormsg" to an error message.
2801 */
2802 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002803parse_command_modifiers(
2804 exarg_T *eap,
2805 char **errormsg,
2806 cmdmod_T *cmod,
2807 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002808{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002809 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002810 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002811 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002812 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002813 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002814 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002815
Bram Moolenaare1004402020-10-24 20:49:43 +02002816 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002817 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002818
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002819 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2820 {
2821 // The automatically inserted Visual area range is skipped, so that
2822 // typing ":cmdmod cmd" in Visual mode works without having to move the
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002823 // range to after the modififiers. The command will be
2824 // "'<,'>cmdmod cmd", parse "cmdmod cmd" and then put back "'<,'>"
2825 // before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002826 eap->cmd += 5;
2827 cmd_start = eap->cmd;
2828 has_visual_range = TRUE;
2829 }
2830
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002831 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 for (;;)
2833 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002834 char_u *p;
2835
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002837 {
2838 if (*eap->cmd == ':')
2839 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002841 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002842
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002843 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002844 if (*eap->cmd == NUL && exmode_active
2845 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2846 || getline_equal(eap->getline, eap->cookie, getexline))
2847 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2848 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002849 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002850 if (!skip_only)
2851 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002852 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002853 }
2854
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002855 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002856 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002857 {
2858 // a comment ends at a NL
2859 if (eap->nextcmd == NULL)
2860 {
2861 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2862 if (eap->nextcmd != NULL)
2863 ++eap->nextcmd;
2864 }
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002865 if (vim9script)
2866 {
2867 if (has_cmdmod(cmod, FALSE))
2868 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002869#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002870 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2871 && eap->cmd[2] != '{')
2872 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002873#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002874 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002876 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 if (*eap->cmd == NUL)
2878 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002879 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002880 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002881 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002882 if (vim9script && has_cmdmod(cmod, FALSE))
2883 *errormsg = _(e_command_modifier_without_command);
2884 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 return FAIL;
2886 }
2887
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002888 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002889
2890 // In Vim9 script a variable can shadow a command modifier:
2891 // verbose = 123
2892 // verbose += 123
2893 // silent! verbose = func()
2894 // verbose.member = 2
2895 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002896 // But not:
2897 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002898 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002899 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002900 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002901
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002902 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002903 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002904 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002905 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2906 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002907 break;
2908 }
2909
Bram Moolenaareffed932018-08-14 13:38:17 +02002910 switch (*p)
2911 {
Bram Moolenaar91324262022-09-09 13:27:59 +01002912 // When adding an entry, also modify cmdmods[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002913 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002915 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002916 continue;
2917
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002918 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002919 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002920 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 continue;
2922 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002923 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002924 {
2925#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002926 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002927#endif
2928 continue;
2929 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002930 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002931 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002932 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002933 continue;
2934
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002935 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002936 break;
2937#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002938 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002939#endif
2940 continue;
2941
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002942 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002943 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002944 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002945 continue;
2946 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002947 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002948 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002949 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002950 continue;
2951 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002952 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002953 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002954 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002955 continue;
2956 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002957 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002959 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002960 continue;
2961
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002962 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002964 char_u *reg_pat;
2965 char_u *nulp = NULL;
2966 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002967
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002968 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002969 || *p == NUL
2970 || (ends_excmd(*p)
2971#ifdef FEAT_EVAL
2972 // in ":filter #pat# cmd" # does not
2973 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002974 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002975#endif
2976 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002977 break;
2978 if (*p == '!')
2979 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002980 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002981 p = skipwhite(p + 1);
2982 if (*p == NUL || ends_excmd(*p))
2983 break;
2984 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002985#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002986 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002987 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002988 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002989#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002990 if (skip_only)
2991 p = skip_vimgrep_pat(p, NULL, NULL);
2992 else
2993 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002994 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2995 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002996 if (p == NULL || *p == NUL)
2997 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002998 if (!skip_only)
2999 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003000 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003001 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003002 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003003 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003004 // restore the character overwritten by NUL
3005 if (nulp != NULL)
3006 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003007 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003008 eap->cmd = p;
3009 continue;
3010 }
3011
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003012 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3013 {
3014 cmod->cmod_split |= WSP_HOR;
3015 continue;
3016 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003017 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003018 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003019 || *p == NUL || ends_excmd(*p))
3020 break;
3021 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003022 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003023 continue;
3024
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003025 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003026 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003027 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003028 continue;
3029 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003030 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3031 {
3032 if (ends_excmd2(p, eap->cmd))
3033 {
3034 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003035 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003036 return FAIL;
3037 }
3038 cmod->cmod_flags |= CMOD_LEGACY;
3039 continue;
3040 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003041
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003042 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003043 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003044 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003045 continue;
3046
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003047 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003048 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003049 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003050 continue;
3051 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003052 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003053 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003054 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003055 continue;
3056
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003057 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003058 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003059 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003060 continue;
3061
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003062 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003063 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003064 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003065 continue;
3066 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003067 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003068 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003069 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003070 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003072 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003073 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003074 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003075 }
3076 continue;
3077
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003078 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003079 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003080 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003081 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003082 long tabnr = get_address(eap, &eap->cmd,
3083 ADDR_TABS, eap->skip,
3084 skip_only, FALSE, 1);
3085 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003086 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003087 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003088 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003089 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3090 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003091 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003092 return FAIL;
3093 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003094 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003095 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003096 }
3097 eap->cmd = p;
3098 continue;
3099 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003100 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003101 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003102 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003103 continue;
3104
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003105 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003106 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003107 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003108 continue;
3109
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003110 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003111 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003112 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003113 continue;
3114 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003115 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003116 {
3117 if (ends_excmd2(p, eap->cmd))
3118 {
3119 *errormsg =
3120 _(e_vim9cmd_must_be_followed_by_command);
3121 return FAIL;
3122 }
3123 cmod->cmod_flags |= CMOD_VIM9CMD;
3124 continue;
3125 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003126 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003127 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003128 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003129 {
zeertzjqcd749632022-06-14 13:30:35 +01003130 // zero means not set, one is verbose == 0, etc.
3131 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003132 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003133 else
zeertzjqcd749632022-06-14 13:30:35 +01003134 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003135 eap->cmd = p;
3136 continue;
3137 }
3138 break;
3139 }
3140
Bram Moolenaar1501b632022-03-27 16:56:21 +01003141 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003142 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003143 if (eap->cmd > cmd_start)
3144 {
3145 // Move the '<,'> range to after the modifiers and insert a colon.
3146 // Since the modifiers have been parsed put the colon on top of the
3147 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3148 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003149 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003150 {
3151 size_t len = STRLEN(cmd_start);
3152
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003153 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003154 // "'<,'>mods" -> "mods *+
3155 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003156 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003157 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003158 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003159 }
3160 else
3161 {
3162 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3163 eap->cmd -= 5;
3164 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3165 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003166 }
3167 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003168 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003169 // Special case: change empty command to "+".
3170 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003171 eap->cmd = (char_u *)"'<,'>+";
3172 else
3173 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003174 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003175 else if (use_plus_cmd)
3176 eap->cmd = (char_u *)"+";
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003177
Bram Moolenaareffed932018-08-14 13:38:17 +02003178 return OK;
3179}
3180
3181/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003182 * Return TRUE if "cmod" has anything set.
3183 */
3184 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003185has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003186{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003187 return (cmod->cmod_flags != 0 && (!ignore_silent
3188 || (cmod->cmod_flags
3189 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003190 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003191 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003192 || cmod->cmod_tab != 0
3193 || cmod->cmod_filter_regmatch.regprog != NULL;
3194}
3195
Bram Moolenaar8991be22022-02-14 21:51:46 +00003196#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003197/*
3198 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3199 */
3200 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003201cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003202{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003203 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003204 {
3205 emsg(_(e_misplaced_command_modifier));
3206 return TRUE;
3207 }
3208 return FALSE;
3209}
Dominique Pelle748b3082022-01-08 12:41:16 +00003210#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003211
3212/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003213 * Apply the command modifiers. Saves current state in "cmdmod", call
3214 * undo_cmdmod() later.
3215 */
3216 void
3217apply_cmdmod(cmdmod_T *cmod)
3218{
3219#ifdef HAVE_SANDBOX
3220 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3221 {
3222 ++sandbox;
3223 cmod->cmod_did_sandbox = TRUE;
3224 }
3225#endif
zeertzjqcd749632022-06-14 13:30:35 +01003226 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003227 {
3228 if (cmod->cmod_verbose_save == 0)
3229 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003230 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003231 }
3232
3233 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3234 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003235 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003236 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003237 cmod->cmod_save_msg_scroll = msg_scroll;
3238 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003239 if (cmod->cmod_flags & CMOD_SILENT)
3240 ++msg_silent;
3241 if (cmod->cmod_flags & CMOD_UNSILENT)
3242 msg_silent = 0;
3243
3244 if (cmod->cmod_flags & CMOD_ERRSILENT)
3245 {
3246 ++emsg_silent;
3247 ++cmod->cmod_did_esilent;
3248 }
3249
Bram Moolenaare1004402020-10-24 20:49:43 +02003250 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003251 {
3252 // Set 'eventignore' to "all".
3253 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003254 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003255 set_string_option_direct((char_u *)"ei", -1,
3256 (char_u *)"all", OPT_FREE, SID_NONE);
3257 }
3258}
3259
3260/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003261 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003262 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003263 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003264undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003265{
Bram Moolenaare1004402020-10-24 20:49:43 +02003266 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003267 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003268 p_verbose = cmod->cmod_verbose_save - 1;
3269 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003270 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003271
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003272#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003273 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003274 {
3275 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003276 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003277 }
3278#endif
3279
Bram Moolenaare1004402020-10-24 20:49:43 +02003280 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003281 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003282 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003283 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3284 OPT_FREE, SID_NONE);
3285 free_string_option(cmod->cmod_save_ei);
3286 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003287 }
3288
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003289 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003290
Bram Moolenaare1004402020-10-24 20:49:43 +02003291 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003292 {
3293 // messages could be enabled for a serious error, need to check if the
3294 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003295 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3296 msg_silent = cmod->cmod_save_msg_silent - 1;
3297 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003298 if (emsg_silent < 0)
3299 emsg_silent = 0;
3300 // Restore msg_scroll, it's set by file I/O commands, even when no
3301 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003302 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003303
3304 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3305 // somewhere in the line. Put it back in the first column.
3306 if (redirecting())
3307 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003308
Bram Moolenaare1004402020-10-24 20:49:43 +02003309 cmod->cmod_save_msg_silent = 0;
3310 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003311 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003312}
3313
3314/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003315 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003316 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003317 * Return FAIL and set "errormsg" or return OK.
3318 */
3319 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003320parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003321{
3322 int address_count = 1;
3323 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003324 int need_check_cursor = FALSE;
3325 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003326
3327 // Repeat for all ',' or ';' separated addresses.
3328 for (;;)
3329 {
3330 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003331 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003332 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003333 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003334 eap->addr_count == 0, address_count++);
3335 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003336 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003337 if (lnum == MAXLNUM)
3338 {
3339 if (*eap->cmd == '%') // '%' - all lines
3340 {
3341 ++eap->cmd;
3342 switch (eap->addr_type)
3343 {
3344 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003345 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003346 eap->line1 = 1;
3347 eap->line2 = curbuf->b_ml.ml_line_count;
3348 break;
3349 case ADDR_LOADED_BUFFERS:
3350 {
3351 buf_T *buf = firstbuf;
3352
3353 while (buf->b_next != NULL
3354 && buf->b_ml.ml_mfp == NULL)
3355 buf = buf->b_next;
3356 eap->line1 = buf->b_fnum;
3357 buf = lastbuf;
3358 while (buf->b_prev != NULL
3359 && buf->b_ml.ml_mfp == NULL)
3360 buf = buf->b_prev;
3361 eap->line2 = buf->b_fnum;
3362 break;
3363 }
3364 case ADDR_BUFFERS:
3365 eap->line1 = firstbuf->b_fnum;
3366 eap->line2 = lastbuf->b_fnum;
3367 break;
3368 case ADDR_WINDOWS:
3369 case ADDR_TABS:
3370 if (IS_USER_CMDIDX(eap->cmdidx))
3371 {
3372 eap->line1 = 1;
3373 eap->line2 = eap->addr_type == ADDR_WINDOWS
3374 ? LAST_WIN_NR : LAST_TAB_NR;
3375 }
3376 else
3377 {
3378 // there is no Vim command which uses '%' and
3379 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003380 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003381 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003382 }
3383 break;
3384 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003385 case ADDR_UNSIGNED:
3386 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003387 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003388 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003389 case ADDR_ARGUMENTS:
3390 if (ARGCOUNT == 0)
3391 eap->line1 = eap->line2 = 0;
3392 else
3393 {
3394 eap->line1 = 1;
3395 eap->line2 = ARGCOUNT;
3396 }
3397 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003398 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003399#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003400 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003401 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003402 if (eap->line2 == 0)
3403 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003404#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003405 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003406 case ADDR_NONE:
3407 // Will give an error later if a range is found.
3408 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003409 }
3410 ++eap->addr_count;
3411 }
3412 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3413 {
3414 pos_T *fp;
3415
3416 // '*' - visual area
3417 if (eap->addr_type != ADDR_LINES)
3418 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003419 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003420 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003421 }
3422
3423 ++eap->cmd;
3424 if (!eap->skip)
3425 {
3426 fp = getmark('<', FALSE);
3427 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003428 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003429 eap->line1 = fp->lnum;
3430 fp = getmark('>', FALSE);
3431 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003432 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003433 eap->line2 = fp->lnum;
3434 ++eap->addr_count;
3435 }
3436 }
3437 }
3438 else
3439 eap->line2 = lnum;
3440 eap->addr_count++;
3441
3442 if (*eap->cmd == ';')
3443 {
3444 if (!eap->skip)
3445 {
3446 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003447
Bram Moolenaar0e717042020-04-27 19:29:01 +02003448 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003449 // accept zero as address, so 0;/PATTERN/ works correctly
3450 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003451 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003452 if (eap->line2 > 0)
3453 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003454 else
3455 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003456 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003457 }
3458 }
3459 else if (*eap->cmd != ',')
3460 break;
3461 ++eap->cmd;
3462 }
3463
3464 // One address given: set start and end lines.
3465 if (eap->addr_count == 1)
3466 {
3467 eap->line1 = eap->line2;
3468 // ... but only implicit: really no address given
3469 if (lnum == MAXLNUM)
3470 eap->addr_count = 0;
3471 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003472 ret = OK;
3473
3474theend:
3475 if (need_check_cursor)
3476 check_cursor();
3477 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003478}
3479
3480/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003481 * Append "cmd" to the error message in IObuff.
3482 * Takes care of limiting the length and handling 0xa0, which would be
3483 * invisible otherwise.
3484 */
3485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003486append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003487{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003488 size_t len = STRLEN(IObuff);
3489 char_u *s = cmd;
3490 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003491
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003492 if (len > IOSIZE - 100)
3493 {
3494 // Not enough space, truncate and put in "...".
3495 d = IObuff + IOSIZE - 100;
3496 d -= mb_head_off(IObuff, d);
3497 STRCPY(d, "...");
3498 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003499 STRCAT(IObuff, ": ");
3500 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003501 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003502 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003503 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003504 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003505 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003506 STRCPY(d, "<a0>");
3507 d += 4;
3508 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003509 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3510 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003511 else
3512 MB_COPY_CHAR(s, d);
3513 }
3514 *d = NUL;
3515}
3516
Dominique Pelle748b3082022-01-08 12:41:16 +00003517#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003518/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003519 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3520 * the name. Otherwise just return "start".
3521 */
3522 char_u *
3523skip_option_env_lead(char_u *start)
3524{
3525 char_u *name = start;
3526
3527 if (*start == '&')
3528 {
3529 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3530 name += 3;
3531 else
3532 name += 1;
3533 }
3534 else if (*start == '$')
3535 name += 1;
3536 return name;
3537}
Dominique Pelle748b3082022-01-08 12:41:16 +00003538#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003539
3540/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003541 * Return TRUE and set "*idx" if "p" points to a one letter command.
3542 * If not in Vim9 script:
3543 * - The 'k' command can directly be followed by any character.
3544 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3545 * but :sre[wind] is another command, as are :scr[iptnames],
3546 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3547 */
3548 static int
3549one_letter_cmd(char_u *p, cmdidx_T *idx)
3550{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003551 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003552 return FALSE;
3553 if (*p == 'k')
3554 {
3555 *idx = CMD_k;
3556 return TRUE;
3557 }
3558 if (p[0] == 's'
3559 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3560 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3561 || p[1] == 'g'
3562 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3563 || p[1] == 'I'
3564 || (p[1] == 'r' && p[2] != 'e')))
3565 {
3566 *idx = CMD_substitute;
3567 return TRUE;
3568 }
3569 return FALSE;
3570}
3571
3572/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003573 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3574 * call.
3575 */
3576 int
3577number_method(char_u *cmd)
3578{
3579 char_u *p = skipdigits(cmd);
3580
3581 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3582}
3583
3584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003586 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003587 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003588 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 *
3590 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3591 * assignment without "let". Sets eap->cmdidx to the command while returning
3592 * "eap->cmd".
3593 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 * Returns NULL for an ambiguous user command.
3595 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 char_u *
3597find_ex_command(
3598 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003599 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003600 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003601 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
3603 int len;
3604 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003605 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003606#ifndef FEAT_EVAL
3607 int vim9 = FALSE;
3608#else
3609 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 /*
3612 * Recognize a Vim9 script function/method call and assignment:
3613 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3614 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003615 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003616 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003618 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003620 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003621 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3622 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003624 int oplen;
3625 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003626 char_u *swp;
3627
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003628 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003629 || (eap->cmd[0] == '$'
3630 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003631 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003632 && (valid_yank_reg(eap->cmd[1], FALSE)
3633 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003634 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003635 if (*eap->cmd == '&')
3636 {
3637 p = eap->cmd + 1;
3638 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3639 p += 2;
3640 p = to_name_end(p, FALSE);
3641 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003642 else if (*eap->cmd == '$')
3643 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003644 else
3645 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003646 if (ends_excmd(*skipwhite(p)))
3647 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003648 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3649 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003650 eap->cmdidx = CMD_eval;
3651 return eap->cmd;
3652 }
3653 // "&option" can be followed by "->" or "=", check below
3654 }
3655
3656 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003658 if (
3659 // "(..." is an expression.
3660 // "funcname(" is always a function call.
3661 *p == '('
3662 || (p == eap->cmd
3663 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003664 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003665 *eap->cmd == '{'
3666 // "'string'->func()" is an expression.
3667 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003668 // '"string"->func()' is an expression.
3669 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003670 // '$"string"->func()' is an expression.
3671 // "$'string'->func()" is an expression.
3672 || (eap->cmd[0] == '$'
3673 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3674 // '0z1234->func()' is an expression.
3675 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003676 // "g:varname" is an expression.
3677 || eap->cmd[1] == ':'
3678 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003679 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003680 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003681 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003682 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3683 {
3684 // "{" by itself is the start of a block.
3685 eap->cmdidx = CMD_block;
3686 return eap->cmd + 1;
3687 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003688 eap->cmdidx = CMD_eval;
3689 return eap->cmd;
3690 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003691
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003692 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003693 // "varname[]" is an expression.
3694 *p == '['
3695 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003696 || (*p == '.'
3697 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3698 // g:[key] is an expression
3699 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003700 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003701 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003702
3703 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003704 // Skip over the whole thing, it can be:
3705 // name.member = val
3706 // name[a : b] = val
3707 // name[idx] = val
3708 // name[idx].member = val
3709 // etc.
3710 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003711 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003712 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003713 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003714 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003715 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003716 || (after[0] == '.' && after[1] == '.'
3717 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003718 eap->cmdidx = CMD_var;
3719 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003720 --emsg_silent;
3721 return eap->cmd;
3722 }
3723
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003724 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3725 // an assignment.
3726 // If there is no line break inside the "[...]" then "p" is
3727 // advanced to after the "]" by to_name_const_end(): check if a "="
3728 // follows.
3729 // If "[...]" has a line break "p" still points at the "[" and it
3730 // can't be an assignment.
3731 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003732 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003733 char_u *eq;
3734
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003735 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003736 if (p == eap->cmd && *p == '[')
3737 {
3738 int count = 0;
3739 int semicolon = FALSE;
3740
3741 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3742 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003743 eq = p;
3744 if (eq != NULL)
3745 {
3746 eq = skipwhite(eq);
3747 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3748 ++eq;
3749 }
3750 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003751 {
3752 eap->cmdidx = CMD_eval;
3753 return eap->cmd;
3754 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003755 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003756 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003757 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003758 return eap->cmd;
3759 }
3760 }
3761
3762 // Recognize an assignment if we recognize the variable name:
3763 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003764 // "@r = expr"
3765 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003766 // "var = expr" where "var" is a variable name or we are skipping
3767 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003768 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003769 oplen = assignment_len(skipwhite(p), &heredoc);
3770 if (oplen > 0)
3771 {
3772 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3773 || *eap->cmd == '&'
3774 || *eap->cmd == '$'
3775 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003776 || (eap->skip && IS_WHITE_OR_NUL(
3777 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003778 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003779 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003780 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003781 return eap->cmd;
3782 }
3783 }
3784
Bram Moolenaar9510d222022-09-11 15:14:05 +01003785 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003786 // "w:varname: number = 123".
3787 if (eap->cmd[1] == ':' && *p == ':')
3788 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003789 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003790 return eap->cmd;
3791 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003792 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003793
Bram Moolenaar31d99482022-05-26 22:24:43 +01003794 // 1234->func() is a method call
3795 if (number_method(eap->cmd))
3796 {
3797 eap->cmdidx = CMD_eval;
3798 return eap->cmd;
3799 }
3800
Bram Moolenaar7b829262021-10-13 15:04:34 +01003801 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3802 // an expression. A global/substitute/list command needs to use a
3803 // longer name.
3804 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3805 {
3806 eap->cmdidx = CMD_eval;
3807 return eap->cmd;
3808 }
3809
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003810 // If it is an ID it might be a variable with an operator on the next
3811 // line, if the variable exists it can't be an Ex command.
3812 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003813 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003814 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3815 {
3816 eap->cmdidx = CMD_eval;
3817 return eap->cmd;
3818 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003819
3820 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003821 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3822 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003823 {
3824 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3825 return eap->cmd + 2;
3826 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 }
3828#endif
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 /*
3831 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 */
3833 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003834 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 ++p;
3837 }
3838 else
3839 {
3840 while (ASCII_ISALPHA(*p))
3841 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003842 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003843 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003845 while (ASCII_ISALNUM(*p))
3846 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 }
3848 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3849 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003850 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 ++p;
3852 while (ASCII_ISALPHA(*p))
3853 ++p;
3854 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003855
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003856 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003857 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 ++p;
3859 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003860 // The "d" command can directly be followed by 'l' or 'p' flag, when
3861 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003862 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003863 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003864 // Check for ":dl", ":dell", etc. to ":deletel": that's
3865 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003866 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003867 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003868 break;
3869 if (i == len - 1)
3870 {
3871 --len;
3872 if (p[-1] == 'l')
3873 eap->flags |= EXFLAG_LIST;
3874 else
3875 eap->flags |= EXFLAG_PRINT;
3876 }
3877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003879 if (ASCII_ISLOWER(eap->cmd[0]))
3880 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003881 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003882 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003883
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003884 if (command_count != (int)CMD_SIZE)
3885 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003886 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003887 getout(1);
3888 }
3889
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003890 // Use a precomputed index for fast look-up in cmdnames[]
3891 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003892 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3893 if (ASCII_ISLOWER(c2))
3894 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3895 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003896 else if (ASCII_ISUPPER(eap->cmd[0]))
3897 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003899 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
3901 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3902 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3903 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3904 (size_t)len) == 0)
3905 {
3906#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003907 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 *full = TRUE;
3909#endif
3910 break;
3911 }
3912
Bram Moolenaar204852a2022-03-05 12:56:44 +00003913 // :Print and :mode are not supported in Vim9 script.
3914 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003915 if (vim9 && eap->cmdidx != CMD_SIZE)
3916 {
3917 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3918 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003919 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003920 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003921 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003922 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003923 eap->cmdidx = CMD_SIZE;
3924 }
3925 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003926
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003927 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003928 // 'cpoptions'.
3929 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3930 p = eap->cmd;
3931
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003932 // Look for a user defined command as a last resort. Let ":Print" be
3933 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003934 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3935 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003937 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 while (ASCII_ISALNUM(*p))
3939 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003940 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003942 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 eap->cmdidx = CMD_SIZE;
3944 }
3945
Bram Moolenaar204852a2022-03-05 12:56:44 +00003946 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3947 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003948 eap->cmdidx = CMD_finally;
3949
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003950#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003951 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003952 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003953 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003954 && (eap->cmdidx < 0 ||
3955 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003956 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003957 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3958
3959 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003960 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003961 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003962 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003963#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003964
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 return p;
3966}
3967
3968#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003969static struct cmdmod
3970{
3971 char *name;
3972 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003973 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003974} cmdmods[] = {
3975 {"aboveleft", 3, FALSE},
3976 {"belowright", 3, FALSE},
3977 {"botright", 2, FALSE},
3978 {"browse", 3, FALSE},
3979 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003980 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003981 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003982 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003983 {"keepalt", 5, FALSE},
3984 {"keepjumps", 5, FALSE},
3985 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003986 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003987 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003988 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003989 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003990 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003991 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003992 {"rightbelow", 6, FALSE},
3993 {"sandbox", 3, FALSE},
3994 {"silent", 3, FALSE},
3995 {"tab", 3, TRUE},
3996 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003997 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003998 {"verbose", 4, TRUE},
3999 {"vertical", 4, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004000 {"vim9cmd", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004001};
4002
4003/*
4004 * Return length of a command modifier (including optional count).
4005 * Return zero when it's not a modifier.
4006 */
4007 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004008modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004009{
4010 int i, j;
4011 char_u *p = cmd;
4012
4013 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004014 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02004015 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004016 {
4017 for (j = 0; p[j] != NUL; ++j)
4018 if (p[j] != cmdmods[i].name[j])
4019 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02004020 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00004021 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00004022 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004023 }
4024 return 0;
4025}
4026
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027/*
4028 * Return > 0 if an Ex command "name" exists.
4029 * Return 2 if there is an exact match.
4030 * Return 3 if there is an ambiguous match.
4031 */
4032 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004033cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034{
4035 exarg_T ea;
4036 int full = FALSE;
4037 int i;
4038 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004039 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004041 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02004042 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
4044 for (j = 0; name[j] != NUL; ++j)
4045 if (name[j] != cmdmods[i].name[j])
4046 break;
4047 if (name[j] == NUL && j >= cmdmods[i].minlen)
4048 return (cmdmods[i].name[j] == NUL ? 2 : 1);
4049 }
4050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004051 // Check built-in commands and user defined commands.
4052 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004053 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004055 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004057 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004059 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4060 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004061 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004062 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4064}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004065
4066/*
4067 * "fullcommand" function
4068 */
4069 void
4070f_fullcommand(typval_T *argvars, typval_T *rettv)
4071{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004072 exarg_T ea;
4073 char_u *name;
4074 char_u *p;
4075 int vim9script = in_vim9script();
4076 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004077
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004078 rettv->v_type = VAR_STRING;
4079 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004080
Bram Moolenaaraa534142022-09-15 21:46:02 +01004081 if (in_vim9script()
4082 && (check_for_string_arg(argvars, 0) == FAIL
4083 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004084 return;
4085
4086 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004087 if (name == NULL)
4088 return;
4089
Bram Moolenaaraa534142022-09-15 21:46:02 +01004090 if (argvars[1].v_type != VAR_UNKNOWN)
4091 {
4092 vim9script = tv_get_bool(&argvars[1]);
4093 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4094 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4095 }
4096
zeertzjqc024ed92022-01-04 16:22:52 +00004097 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004098 name++;
4099 name = skip_range(name, TRUE, NULL);
4100
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004101 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4102 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004103 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004104 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004105 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004106 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004107 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004108 goto theend;
4109
4110 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004111 {
4112 int res;
4113
4114 ++emsg_silent;
4115 res = not_in_vim9(&ea);
4116 --emsg_silent;
4117
4118 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004119 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004120 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004121
4122 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004123 ? get_user_command_name(ea.useridx, ea.cmdidx)
4124 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004125theend:
4126 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004127}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128#endif
4129
Bram Moolenaard0190392019-08-23 21:17:35 +02004130 cmdidx_T
4131excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
Bram Moolenaard0190392019-08-23 21:17:35 +02004133 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004135 if (!one_letter_cmd(cmd, &idx))
4136 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4137 idx = (cmdidx_T)((int)idx + 1))
4138 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4139 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140
Bram Moolenaard0190392019-08-23 21:17:35 +02004141 return idx;
4142}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
Bram Moolenaard0190392019-08-23 21:17:35 +02004144 long
4145excmd_get_argt(cmdidx_T idx)
4146{
4147 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148}
4149
4150/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004151 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 *
4153 * Backslashed delimiters after / or ? will be skipped, and commands will
4154 * not be expanded between /'s and ?'s or after "'".
4155 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004156 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 * Returns the "cmd" pointer advanced to beyond the range.
4158 */
4159 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004160skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004161 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004162 int skip_star, // skip "*" used for Visual range
4163 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004165 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004166 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004168 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004170 if (*cmd == '\\')
4171 {
4172 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4173 ++cmd;
4174 else
4175 break;
4176 }
4177 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004179 char_u *p = cmd;
4180
4181 // a quote is only valid at the start or after a separator
4182 while (p > cmd_start)
4183 {
4184 --p;
4185 if (!VIM_ISWHITE(*p))
4186 break;
4187 }
4188 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4189 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 if (*++cmd == NUL && ctx != NULL)
4191 *ctx = EXPAND_NOTHING;
4192 }
4193 else if (*cmd == '/' || *cmd == '?')
4194 {
4195 delim = *cmd++;
4196 while (*cmd != NUL && *cmd != delim)
4197 if (*cmd++ == '\\' && *cmd != NUL)
4198 ++cmd;
4199 if (*cmd == NUL && ctx != NULL)
4200 *ctx = EXPAND_NOTHING;
4201 }
4202 if (*cmd != NUL)
4203 ++cmd;
4204 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004205
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004206 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004207 while (*cmd == ':')
4208 cmd = skipwhite(cmd + 1);
4209
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004210 // Skip "*" used for Visual range.
4211 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4212 cmd = skipwhite(cmd + 1);
4213
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return cmd;
4215}
4216
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004217 static void
4218addr_error(cmd_addr_T addr_type)
4219{
4220 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004221 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004222 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004223 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004224}
4225
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004227 * Return the default address for an address type.
4228 */
4229 static linenr_T
4230default_address(exarg_T *eap)
4231{
4232 linenr_T lnum = 0;
4233
4234 switch (eap->addr_type)
4235 {
4236 case ADDR_LINES:
4237 case ADDR_OTHER:
4238 // Default is the cursor line number. Avoid using an invalid
4239 // line number though.
4240 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4241 lnum = curbuf->b_ml.ml_line_count;
4242 else
4243 lnum = curwin->w_cursor.lnum;
4244 break;
4245 case ADDR_WINDOWS:
4246 lnum = CURRENT_WIN_NR;
4247 break;
4248 case ADDR_ARGUMENTS:
4249 lnum = curwin->w_arg_idx + 1;
4250 if (lnum > ARGCOUNT)
4251 lnum = ARGCOUNT;
4252 break;
4253 case ADDR_LOADED_BUFFERS:
4254 case ADDR_BUFFERS:
4255 lnum = curbuf->b_fnum;
4256 break;
4257 case ADDR_TABS:
4258 lnum = CURRENT_TAB_NR;
4259 break;
4260 case ADDR_TABS_RELATIVE:
4261 case ADDR_UNSIGNED:
4262 lnum = 1;
4263 break;
4264 case ADDR_QUICKFIX:
4265#ifdef FEAT_QUICKFIX
4266 lnum = qf_get_cur_idx(eap);
4267#endif
4268 break;
4269 case ADDR_QUICKFIX_VALID:
4270#ifdef FEAT_QUICKFIX
4271 lnum = qf_get_cur_valid_idx(eap);
4272#endif
4273 break;
4274 case ADDR_NONE:
4275 // Will give an error later if a range is found.
4276 break;
4277 }
4278 return lnum;
4279}
4280
4281/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004282 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 *
4284 * Set ptr to the next character after the part that was interpreted.
4285 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004286 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 *
4288 * Return MAXLNUM when no Ex address was found.
4289 */
4290 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004291get_address(
4292 exarg_T *eap UNUSED,
4293 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004294 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004295 int skip, // only skip the address, don't use it
4296 int silent, // no errors or side effects
4297 int to_other_file, // flag: may jump to other file
4298 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299{
4300 int c;
4301 int i;
4302 long n;
4303 char_u *cmd;
4304 pos_T pos;
4305 pos_T *fp;
4306 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004307 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
4309 cmd = skipwhite(*ptr);
4310 lnum = MAXLNUM;
4311 do
4312 {
4313 switch (*cmd)
4314 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004315 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004316 ++cmd;
4317 switch (addr_type)
4318 {
4319 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004320 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 lnum = curwin->w_cursor.lnum;
4322 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004323 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004324 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004325 break;
4326 case ADDR_ARGUMENTS:
4327 lnum = curwin->w_arg_idx + 1;
4328 break;
4329 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004330 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004331 lnum = curbuf->b_fnum;
4332 break;
4333 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004334 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004335 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004336 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004337 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004338 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004339 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004340 cmd = NULL;
4341 goto error;
4342 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004343 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004344#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004345 lnum = qf_get_cur_idx(eap);
4346#endif
4347 break;
4348 case ADDR_QUICKFIX_VALID:
4349#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004350 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004351#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004352 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004353 }
4354 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004356 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004357 ++cmd;
4358 switch (addr_type)
4359 {
4360 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004361 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 lnum = curbuf->b_ml.ml_line_count;
4363 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004364 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004365 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004366 break;
4367 case ADDR_ARGUMENTS:
4368 lnum = ARGCOUNT;
4369 break;
4370 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004371 buf = lastbuf;
4372 while (buf->b_ml.ml_mfp == NULL)
4373 {
4374 if (buf->b_prev == NULL)
4375 break;
4376 buf = buf->b_prev;
4377 }
4378 lnum = buf->b_fnum;
4379 break;
4380 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004381 lnum = lastbuf->b_fnum;
4382 break;
4383 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004384 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004385 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004386 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004387 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004388 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004389 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004390 cmd = NULL;
4391 goto error;
4392 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004393 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004394#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004395 lnum = qf_get_size(eap);
4396 if (lnum == 0)
4397 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004398#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004399 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004400 case ADDR_QUICKFIX_VALID:
4401#ifdef FEAT_QUICKFIX
4402 lnum = qf_get_valid_size(eap);
4403 if (lnum == 0)
4404 lnum = 1;
4405#endif
4406 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004407 }
4408 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004410 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004411 if (*++cmd == NUL)
4412 {
4413 cmd = NULL;
4414 goto error;
4415 }
4416 if (addr_type != ADDR_LINES)
4417 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004418 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004419 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 goto error;
4421 }
4422 if (skip)
4423 ++cmd;
4424 else
4425 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004426 // Only accept a mark in another file when it is
4427 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004428 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4429 ++cmd;
4430 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004431 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004432 lnum = curwin->w_cursor.lnum;
4433 else
4434 {
4435 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 {
4437 cmd = NULL;
4438 goto error;
4439 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004440 lnum = fp->lnum;
4441 }
4442 }
4443 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
4445 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004446 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004447 c = *cmd++;
4448 if (addr_type != ADDR_LINES)
4449 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004450 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004451 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004452 goto error;
4453 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004454 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004455 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004456 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 if (*cmd == c)
4458 ++cmd;
4459 }
4460 else
4461 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004462 int flags;
4463
4464 pos = curwin->w_cursor; // save curwin->w_cursor
4465
4466 // When '/' or '?' follows another address, start from
4467 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004468 if (lnum > 0 && lnum != MAXLNUM)
4469 curwin->w_cursor.lnum =
4470 lnum > curbuf->b_ml.ml_line_count
4471 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004472
4473 // Start a forward search at the end of the line (unless
4474 // before the first line).
4475 // Start a backward search at the start of the line.
4476 // This makes sure we never match in the current
4477 // line, and can match anywhere in the
4478 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004479 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004480 curwin->w_cursor.col = MAXCOL;
4481 else
4482 curwin->w_cursor.col = 0;
4483 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004484 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004485 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 {
4487 curwin->w_cursor = pos;
4488 cmd = NULL;
4489 goto error;
4490 }
4491 lnum = curwin->w_cursor.lnum;
4492 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004493 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004494 cmd += searchcmdlen;
4495 }
4496 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004498 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 ++cmd;
4500 if (addr_type != ADDR_LINES)
4501 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004502 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004503 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004504 goto error;
4505 }
4506 if (*cmd == '&')
4507 i = RE_SUBST;
4508 else if (*cmd == '?' || *cmd == '/')
4509 i = RE_SEARCH;
4510 else
4511 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004512 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004513 cmd = NULL;
4514 goto error;
4515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004517 if (!skip)
4518 {
4519 /*
4520 * When search follows another address, start from
4521 * there.
4522 */
4523 if (lnum != MAXLNUM)
4524 pos.lnum = lnum;
4525 else
4526 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004528 /*
4529 * Start the search just like for the above
4530 * do_search().
4531 */
4532 if (*cmd != '?')
4533 pos.col = MAXCOL;
4534 else
4535 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004536 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004537 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004538 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004539 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004540 lnum = pos.lnum;
4541 else
4542 {
4543 cmd = NULL;
4544 goto error;
4545 }
4546 }
4547 ++cmd;
4548 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
4550 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004551 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004552 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
4554
4555 for (;;)
4556 {
4557 cmd = skipwhite(cmd);
4558 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4559 break;
4560
4561 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004562 {
4563 switch (addr_type)
4564 {
4565 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004566 case ADDR_OTHER:
4567 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004568 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004569 break;
4570 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004571 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004572 break;
4573 case ADDR_ARGUMENTS:
4574 lnum = curwin->w_arg_idx + 1;
4575 break;
4576 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004577 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004578 lnum = curbuf->b_fnum;
4579 break;
4580 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004581 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004582 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004583 case ADDR_TABS_RELATIVE:
4584 lnum = 1;
4585 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004586 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004587#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004588 lnum = qf_get_cur_idx(eap);
4589#endif
4590 break;
4591 case ADDR_QUICKFIX_VALID:
4592#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004593 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004594#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004595 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004596 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004597 case ADDR_UNSIGNED:
4598 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004599 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004600 }
4601 }
4602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004604 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 else
4606 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004607 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 n = 1;
4609 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004610 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004611 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004613 if (n == MAXLNUM)
4614 {
4615 emsg(_(e_line_number_out_of_range));
4616 goto error;
4617 }
4618 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004619
4620 if (addr_type == ADDR_TABS_RELATIVE)
4621 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004622 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004623 cmd = NULL;
4624 goto error;
4625 }
4626 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004627 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004628 lnum = compute_buffer_local_count(
4629 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004631 {
4632#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004633 // Relative line addressing: need to adjust for lines in a
4634 // closed fold after the first address.
4635 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4636 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004637 (void)hasFolding(lnum, NULL, &lnum);
4638#endif
4639 if (i == '-')
4640 lnum -= n;
4641 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004642 {
4643 if (n >= LONG_MAX - lnum)
4644 {
4645 emsg(_(e_line_number_out_of_range));
4646 goto error;
4647 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004648 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004649 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652 } while (*cmd == '/' || *cmd == '?');
4653
4654error:
4655 *ptr = cmd;
4656 return lnum;
4657}
4658
4659/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004660 * Set eap->line1 and eap->line2 to the whole range.
4661 * Used for commands with the EX_DFLALL flag and no range given.
4662 */
4663 static void
4664address_default_all(exarg_T *eap)
4665{
4666 eap->line1 = 1;
4667 switch (eap->addr_type)
4668 {
4669 case ADDR_LINES:
4670 case ADDR_OTHER:
4671 eap->line2 = curbuf->b_ml.ml_line_count;
4672 break;
4673 case ADDR_LOADED_BUFFERS:
4674 {
4675 buf_T *buf = firstbuf;
4676
4677 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4678 buf = buf->b_next;
4679 eap->line1 = buf->b_fnum;
4680 buf = lastbuf;
4681 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4682 buf = buf->b_prev;
4683 eap->line2 = buf->b_fnum;
4684 }
4685 break;
4686 case ADDR_BUFFERS:
4687 eap->line1 = firstbuf->b_fnum;
4688 eap->line2 = lastbuf->b_fnum;
4689 break;
4690 case ADDR_WINDOWS:
4691 eap->line2 = LAST_WIN_NR;
4692 break;
4693 case ADDR_TABS:
4694 eap->line2 = LAST_TAB_NR;
4695 break;
4696 case ADDR_TABS_RELATIVE:
4697 eap->line2 = 1;
4698 break;
4699 case ADDR_ARGUMENTS:
4700 if (ARGCOUNT == 0)
4701 eap->line1 = eap->line2 = 0;
4702 else
4703 eap->line2 = ARGCOUNT;
4704 break;
4705 case ADDR_QUICKFIX_VALID:
4706#ifdef FEAT_QUICKFIX
4707 eap->line2 = qf_get_valid_size(eap);
4708 if (eap->line2 == 0)
4709 eap->line2 = 1;
4710#endif
4711 break;
4712 case ADDR_NONE:
4713 case ADDR_UNSIGNED:
4714 case ADDR_QUICKFIX:
4715 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4716 break;
4717 }
4718}
4719
4720
4721/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004722 * Get flags from an Ex command argument.
4723 */
4724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004725get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004726{
4727 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4728 {
4729 if (*eap->arg == 'l')
4730 eap->flags |= EXFLAG_LIST;
4731 else if (*eap->arg == 'p')
4732 eap->flags |= EXFLAG_PRINT;
4733 else
4734 eap->flags |= EXFLAG_NR;
4735 eap->arg = skipwhite(eap->arg + 1);
4736 }
4737}
4738
4739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 * Function called for command which is Not Implemented. NI!
4741 */
4742 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004743ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744{
4745 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004746 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004747 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748}
4749
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004750#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751/*
4752 * Function called for script command which is Not Implemented. NI!
4753 * Skips over ":perl <<EOF" constructs.
4754 */
4755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004756ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758 if (!eap->skip)
4759 ex_ni(eap);
4760 else
4761 vim_free(script_get(eap, eap->arg));
4762}
4763#endif
4764
4765/*
4766 * Check range in Ex command for validity.
4767 * Return NULL when valid, error message when invalid.
4768 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004769 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004770invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004772 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004773
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if ( eap->line1 < 0
4775 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004776 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004777 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004778
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004779 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004780 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004781 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004782 {
4783 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004784 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004785#ifdef FEAT_DIFF
4786 + (eap->cmdidx == CMD_diffget)
4787#endif
4788 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004789 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004790 break;
4791 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004792 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004793 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004794 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004795 break;
4796 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004797 // Only a boundary check, not whether the buffers actually
4798 // exist.
4799 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004800 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004801 break;
4802 case ADDR_LOADED_BUFFERS:
4803 buf = firstbuf;
4804 while (buf->b_ml.ml_mfp == NULL)
4805 {
4806 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004807 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004808 buf = buf->b_next;
4809 }
4810 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004811 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004812 buf = lastbuf;
4813 while (buf->b_ml.ml_mfp == NULL)
4814 {
4815 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004816 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004817 buf = buf->b_prev;
4818 }
4819 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004820 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004821 break;
4822 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004823 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004824 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004825 break;
4826 case ADDR_TABS:
4827 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004828 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004829 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004830 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004831 case ADDR_OTHER:
4832 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004833 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004834 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004835#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004836 // No error for value that is too big, will use the last entry.
4837 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004838 {
4839 if (eap->addr_count == 0)
4840 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004841 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004842 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004843#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004844 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004845 case ADDR_QUICKFIX_VALID:
4846#ifdef FEAT_QUICKFIX
4847 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4848 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004849 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004850#endif
4851 break;
4852 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004853 case ADDR_NONE:
4854 // Will give an error elsewhere.
4855 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004856 }
4857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 return NULL;
4859}
4860
4861/*
4862 * Correct the range for zero line number, if required.
4863 */
4864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004865correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004867 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
4869 if (eap->line1 == 0)
4870 eap->line1 = 1;
4871 if (eap->line2 == 0)
4872 eap->line2 = 1;
4873 }
4874}
4875
Bram Moolenaar748bf032005-02-02 23:04:36 +00004876#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004877/*
4878 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4879 * pattern. Otherwise return eap->arg.
4880 */
4881 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004882skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004883{
4884 char_u *p = eap->arg;
4885
Bram Moolenaara37420f2006-02-04 22:37:47 +00004886 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4887 || eap->cmdidx == CMD_vimgrepadd
4888 || eap->cmdidx == CMD_lvimgrepadd
4889 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004890 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004891 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004892 if (p == NULL)
4893 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004894 }
4895 return p;
4896}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004897
4898/*
4899 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4900 * in the command line, so that things like % get expanded.
4901 */
4902 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004903replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004904{
4905 char_u *new_cmdline;
4906 char_u *program;
4907 char_u *pos;
4908 char_u *ptr;
4909 int len;
4910 int i;
4911
4912 /*
4913 * Don't do it when ":vimgrep" is used for ":grep".
4914 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004915 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4916 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4917 || eap->cmdidx == CMD_grepadd
4918 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004919 && !grep_internal(eap->cmdidx))
4920 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004921 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4922 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004923 {
4924 if (*curbuf->b_p_gp == NUL)
4925 program = p_gp;
4926 else
4927 program = curbuf->b_p_gp;
4928 }
4929 else
4930 {
4931 if (*curbuf->b_p_mp == NUL)
4932 program = p_mp;
4933 else
4934 program = curbuf->b_p_mp;
4935 }
4936
4937 p = skipwhite(p);
4938
4939 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4940 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004941 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004942 i = 1;
4943 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4944 ++i;
4945 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004946 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004947 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004948 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004949 ptr = new_cmdline;
4950 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4951 {
4952 i = (int)(pos - program);
4953 STRNCPY(ptr, program, i);
4954 STRCPY(ptr += i, p);
4955 ptr += len;
4956 program = pos + 2;
4957 }
4958 STRCPY(ptr, program);
4959 }
4960 else
4961 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004962 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004963 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004964 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004965 STRCPY(new_cmdline, program);
4966 STRCAT(new_cmdline, " ");
4967 STRCAT(new_cmdline, p);
4968 }
4969 msg_make(p);
4970
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004971 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004972 vim_free(*cmdlinep);
4973 *cmdlinep = new_cmdline;
4974 p = new_cmdline;
4975 }
4976 return p;
4977}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004978#endif
4979
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980/*
4981 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004982 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 * Return FAIL for failure, OK otherwise.
4984 */
4985 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004986expand_filename(
4987 exarg_T *eap,
4988 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004989 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004991 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 char_u *repl;
4993 int srclen;
4994 char_u *p;
4995 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004996 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997
Bram Moolenaar748bf032005-02-02 23:04:36 +00004998#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004999 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005000 p = skip_grep_pat(eap);
5001#else
5002 p = eap->arg;
5003#endif
5004
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 /*
5006 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5007 * the file name contains a wildcard it should not cause expanding.
5008 * (it will be expanded anyway if there is a wildcard before replacing).
5009 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005010 has_wildcards = mch_has_wildcard(p);
5011 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005013#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005014 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005015 if (p[0] == '`' && p[1] == '=')
5016 {
5017 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005018 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005019 if (*p == '`')
5020 ++p;
5021 continue;
5022 }
5023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 /*
5025 * Quick check if this cannot be the start of a special string.
5026 * Also removes backslash before '%', '#' and '<'.
5027 */
5028 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5029 {
5030 ++p;
5031 continue;
5032 }
5033
5034 /*
5035 * Try to find a match at this position.
5036 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005037 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005038 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005039 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005041 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 {
5043 p += srclen;
5044 continue;
5045 }
5046
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005047 // Wildcards won't be expanded below, the replacement is taken
5048 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005049 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5050 {
5051 char_u *l = repl;
5052
5053 repl = expand_env_save(repl);
5054 vim_free(l);
5055 }
5056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005057 // Need to escape white space et al. with a backslash.
5058 // Don't do this for:
5059 // - replacement that already has been escaped: "##"
5060 // - shell commands (may have to use quotes instead).
5061 // - non-unix systems when there is a single argument (spaces don't
5062 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005064 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 && eap->cmdidx != CMD_grep
5067 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005068 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005069 && eap->cmdidx != CMD_lgrep
5070 && eap->cmdidx != CMD_lgrepadd
5071 && eap->cmdidx != CMD_lmake
5072 && eap->cmdidx != CMD_make
5073 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005075 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076#endif
5077 )
5078 {
5079 char_u *l;
5080#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005081 // Don't escape a backslash here, because rem_backslash() doesn't
5082 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 static char_u *nobslash = (char_u *)" \t\"|";
5084# define ESCAPE_CHARS nobslash
5085#else
5086# define ESCAPE_CHARS escape_chars
5087#endif
5088
5089 for (l = repl; *l; ++l)
5090 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5091 {
5092 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5093 if (l != NULL)
5094 {
5095 vim_free(repl);
5096 repl = l;
5097 }
5098 break;
5099 }
5100 }
5101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005102 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005103 if ((eap->usefilter || eap->cmdidx == CMD_bang
5104 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005105 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
5107 char_u *l;
5108
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005109 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 if (l != NULL)
5111 {
5112 vim_free(repl);
5113 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115 }
5116
5117 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5118 vim_free(repl);
5119 if (p == NULL)
5120 return FAIL;
5121 }
5122
5123 /*
5124 * One file argument: Expand wildcards.
5125 * Don't do this with ":r !command" or ":w !command".
5126 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005127 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 /*
5130 * May do this twice:
5131 * 1. Replace environment variables.
5132 * 2. Replace any other wildcards, remove backslashes.
5133 */
5134 for (n = 1; n <= 2; ++n)
5135 {
5136 if (n == 2)
5137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 /*
5139 * Halve the number of backslashes (this is Vi compatible).
5140 * For Unix and OS/2, when wildcards are expanded, this is
5141 * done by ExpandOne() below.
5142 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005143#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 if (!has_wildcards)
5145#endif
5146 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 }
5148
5149 if (has_wildcards)
5150 {
5151 if (n == 1)
5152 {
5153 /*
5154 * First loop: May expand environment variables. This
5155 * can be done much faster with expand_env() than with
5156 * something else (e.g., calling a shell).
5157 * After expanding environment variables, check again
5158 * if there are still wildcards present.
5159 */
5160 if (vim_strchr(eap->arg, '$') != NULL
5161 || vim_strchr(eap->arg, '~') != NULL)
5162 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005163 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005164 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 has_wildcards = mch_has_wildcard(NameBuff);
5166 p = NameBuff;
5167 }
5168 else
5169 p = NULL;
5170 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005171 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
5173 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005174 int options = WILD_LIST_NOTFOUND
5175 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176
5177 ExpandInit(&xpc);
5178 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005179 if (p_wic)
5180 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005182 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 if (p == NULL)
5184 return FAIL;
5185 }
5186 if (p != NULL)
5187 {
5188 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5189 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005190 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 vim_free(p);
5192 }
5193 }
5194 }
5195 }
5196 return OK;
5197}
5198
5199/*
5200 * Replace part of the command line, keeping eap->cmd, eap->arg and
5201 * eap->nextcmd correct.
5202 * "src" points to the part that is to be replaced, of length "srclen".
5203 * "repl" is the replacement string.
5204 * Returns a pointer to the character after the replaced string.
5205 * Returns NULL for failure.
5206 */
5207 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005208repl_cmdline(
5209 exarg_T *eap,
5210 char_u *src,
5211 int srclen,
5212 char_u *repl,
5213 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214{
5215 int len;
5216 int i;
5217 char_u *new_cmdline;
5218
5219 /*
5220 * The new command line is build in new_cmdline[].
5221 * First allocate it.
5222 * Careful: a "+cmd" argument may have been NUL terminated.
5223 */
5224 len = (int)STRLEN(repl);
5225 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005226 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005227 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005228 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005229 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230
5231 /*
5232 * Copy the stuff before the expanded part.
5233 * Copy the expanded stuff.
5234 * Copy what came after the expanded part.
5235 * Copy the next commands, if there are any.
5236 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005237 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005239
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005241 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005243 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005245 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 {
5247 i = (int)STRLEN(new_cmdline) + 1;
5248 STRCPY(new_cmdline + i, eap->nextcmd);
5249 eap->nextcmd = new_cmdline + i;
5250 }
5251 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5252 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5253 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5254 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5255 vim_free(*cmdlinep);
5256 *cmdlinep = new_cmdline;
5257
5258 return src;
5259}
5260
5261/*
5262 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005263 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 */
5265 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005266separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
5268 char_u *p;
5269
Bram Moolenaar86b68352004-12-27 21:59:20 +00005270#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005271 p = skip_grep_pat(eap);
5272#else
5273 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005274#endif
5275
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005276 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
5278 if (*p == Ctrl_V)
5279 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005280 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005281 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005283 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005284 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005285 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 break;
5287 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005288
5289#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005290 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005291 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005292 {
5293 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005294 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005295 if (*p == NUL) // stop at NUL after CTRL-V
5296 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005297 }
5298#endif
5299
Bram Moolenaareda29c92022-10-02 12:59:00 +01005300 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005301 // :@" and :*" do not start a comment!
5302 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005303 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005304 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005305 && !(eap->argt & EX_NOTRLCOM)
5306 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5307 || p != eap->arg)
5308 && (eap->cmdidx != CMD_redir
5309 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005310 || (*p == '#'
5311 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005312 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005313 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 || *p == '|' || *p == '\n')
5315 {
5316 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005317 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 * AND 'b' is present in 'cpoptions'.
5319 */
5320 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005321 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005323 if (!keep_backslash)
5324 {
5325 STRMOVE(p - 1, p); // remove the '\'
5326 --p;
5327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329 else
5330 {
5331 eap->nextcmd = check_nextcmd(p);
5332 *p = NUL;
5333 break;
5334 }
5335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005337
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005338 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 del_trailing_spaces(eap->arg);
5340}
5341
5342/*
5343 * get + command from ex argument
5344 */
5345 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005346getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347{
5348 char_u *arg = *argp;
5349 char_u *command = NULL;
5350
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005351 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
5353 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005354 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 command = dollar_command;
5356 else
5357 {
5358 command = arg;
5359 arg = skip_cmd_arg(command, TRUE);
5360 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005361 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005364 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 *argp = arg;
5366 }
5367 return command;
5368}
5369
5370/*
5371 * Find end of "+command" argument. Skip over "\ " and "\\".
5372 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005373 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005374skip_cmd_arg(
5375 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005376 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
5378 while (*p && !vim_isspace(*p))
5379 {
5380 if (*p == '\\' && p[1] != NUL)
5381 {
5382 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005383 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 else
5385 ++p;
5386 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005387 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389 return p;
5390}
5391
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005392 int
5393get_bad_opt(char_u *p, exarg_T *eap)
5394{
5395 if (STRICMP(p, "keep") == 0)
5396 eap->bad_char = BAD_KEEP;
5397 else if (STRICMP(p, "drop") == 0)
5398 eap->bad_char = BAD_DROP;
5399 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5400 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005401 else
5402 return FAIL;
5403 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005404}
5405
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406/*
5407 * Get "++opt=arg" argument.
5408 * Return FAIL or OK.
5409 */
5410 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005411getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412{
5413 char_u *arg = eap->arg + 2;
5414 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005415 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005418 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5420 {
5421 if (*arg == 'n')
5422 {
5423 arg += 2;
5424 eap->force_bin = FORCE_NOBIN;
5425 }
5426 else
5427 eap->force_bin = FORCE_BIN;
5428 if (!checkforcmd(&arg, "binary", 3))
5429 return FAIL;
5430 eap->arg = skipwhite(arg);
5431 return OK;
5432 }
5433
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005434 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005435 if (STRNCMP(arg, "edit", 4) == 0)
5436 {
5437 eap->read_edit = TRUE;
5438 eap->arg = skipwhite(arg + 4);
5439 return OK;
5440 }
5441
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 if (STRNCMP(arg, "ff", 2) == 0)
5443 {
5444 arg += 2;
5445 pp = &eap->force_ff;
5446 }
5447 else if (STRNCMP(arg, "fileformat", 10) == 0)
5448 {
5449 arg += 10;
5450 pp = &eap->force_ff;
5451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 else if (STRNCMP(arg, "enc", 3) == 0)
5453 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005454 if (STRNCMP(arg, "encoding", 8) == 0)
5455 arg += 8;
5456 else
5457 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 pp = &eap->force_enc;
5459 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005460 else if (STRNCMP(arg, "bad", 3) == 0)
5461 {
5462 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005463 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
5466 if (pp == NULL || *arg != '=')
5467 return FAIL;
5468
5469 ++arg;
5470 *pp = (int)(arg - eap->cmd);
5471 arg = skip_cmd_arg(arg, FALSE);
5472 eap->arg = skipwhite(arg);
5473 *arg = NUL;
5474
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if (pp == &eap->force_ff)
5476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5478 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005479 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005481 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005483 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5485 *p = TOLOWER_ASC(*p);
5486 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005487 else
5488 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005489 // Check ++bad= argument. Must be a single-byte character, "keep" or
5490 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005491 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005492 return FAIL;
5493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
5495 return OK;
5496}
5497
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005499ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500{
5501 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005502 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 * directory for security reasons.
5504 */
5505 if (secure)
5506 {
5507 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005508 eap->errmsg =
5509 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 }
5511 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005512 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 else
5514 do_augroup(eap->arg, eap->forceit);
5515}
5516
5517/*
5518 * ":doautocmd": Apply the automatic commands to the current buffer.
5519 */
5520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005521ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005523 char_u *arg = eap->arg;
5524 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005525 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005526
Bram Moolenaar1610d052016-06-09 22:53:01 +02005527 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005528 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005529 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005530 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533/*
5534 * :[N]bunload[!] [N] [bufname] unload buffer
5535 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5536 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5537 */
5538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005539ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005541 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005542 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 eap->errmsg = do_bufdel(
5544 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5545 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5546 : DOBUF_UNLOAD, eap->arg,
5547 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5548}
5549
5550/*
5551 * :[N]buffer [N] to buffer N
5552 * :[N]sbuffer [N] to buffer N
5553 */
5554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005555ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005557 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005558 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005560 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 else
5562 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005563 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5565 else
5566 goto_buffer(eap, DOBUF_FIRST, 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/*
5573 * :[N]bmodified [N] to next mod. buffer
5574 * :[N]sbmodified [N] to next mod. buffer
5575 */
5576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005580 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005581 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582}
5583
5584/*
5585 * :[N]bnext [N] to next buffer
5586 * :[N]sbnext [N] split and to next buffer
5587 */
5588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005589ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005591 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005592 return;
5593
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005595 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005596 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597}
5598
5599/*
5600 * :[N]bNext [N] to previous buffer
5601 * :[N]bprevious [N] to previous buffer
5602 * :[N]sbNext [N] split and to previous buffer
5603 * :[N]sbprevious [N] split and to previous buffer
5604 */
5605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005608 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005609 return;
5610
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005612 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005613 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614}
5615
5616/*
5617 * :brewind to first buffer
5618 * :bfirst to first buffer
5619 * :sbrewind split and to first buffer
5620 * :sbfirst split and to first buffer
5621 */
5622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005623ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005625 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005626 return;
5627
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005629 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005630 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631}
5632
5633/*
5634 * :blast to last buffer
5635 * :sblast split and to last buffer
5636 */
5637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005638ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005640 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005641 return;
5642
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005644 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005645 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647
Bram Moolenaar7a092242020-04-16 22:10:49 +02005648/*
5649 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005650 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005653ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005655 int comment_char = '"';
5656
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005657 if (in_vim9script())
5658 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005659 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005660}
5661
5662/*
5663 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5664 * to "cmd_start" or has a white space character before it.
5665 */
5666 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005667ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005668{
5669 int c = *cmd;
5670
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005671 if (c == NUL || c == '|' || c == '\n')
5672 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005673 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005674 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5675 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005676 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005677 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678}
5679
5680#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5681 || defined(PROTO)
5682/*
5683 * Return the next command, after the first '|' or '\n'.
5684 * Return NULL if not found.
5685 */
5686 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005687find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 while (*p != '|' && *p != '\n')
5690 {
5691 if (*p == NUL)
5692 return NULL;
5693 ++p;
5694 }
5695 return (p + 1);
5696}
5697#endif
5698
5699/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005700 * Check if *p is a separator between Ex commands, skipping over white space.
5701 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 */
5703 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005704check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005706 char_u *s = skipwhite(p);
5707
5708 if (*s == '|' || *s == '\n')
5709 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 else
5711 return NULL;
5712}
5713
5714/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005715 * If "eap->nextcmd" is not set, check for a next command at "p".
5716 */
5717 void
5718set_nextcmd(exarg_T *eap, char_u *arg)
5719{
5720 char_u *p = check_nextcmd(arg);
5721
5722 if (eap->nextcmd == NULL)
5723 eap->nextcmd = p;
5724 else if (p != NULL)
5725 // cannot use "| command" inside a {} block
5726 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5727}
5728
5729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 * - if there are more files to edit
5731 * - and this is the last window
5732 * - and forceit not used
5733 * - and not repeated twice on a row
5734 * return FAIL and give error message if 'message' TRUE
5735 * return OK otherwise
5736 */
5737 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005738check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005739 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005740 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5743
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005744 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005745 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 {
5747 if (message)
5748 {
5749#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005750 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5751 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005753 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005755 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5756 NGETTEXT("%d more file to edit. Quit anyway?",
5757 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5759 return OK;
5760 return FAIL;
5761 }
5762#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005763 semsg(NGETTEXT(e_nr_more_file_to_edit,
5764 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005765 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767 return FAIL;
5768 }
5769 return OK;
5770}
5771
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772/*
5773 * Function given to ExpandGeneric() to obtain the list of command names.
5774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005776get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
5778 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005779 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 return cmdnames[idx].cmd_name;
5781}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005784ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
Bram Moolenaare6850792010-05-14 15:28:44 +02005786 if (*eap->arg == NUL)
5787 {
5788#ifdef FEAT_EVAL
5789 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5790 char_u *p = NULL;
5791
5792 if (expr != NULL)
5793 {
5794 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005795 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005796 --emsg_off;
5797 vim_free(expr);
5798 }
5799 if (p != NULL)
5800 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005801 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005802 vim_free(p);
5803 }
5804 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005805 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005806#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005807 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005808#endif
5809 }
5810 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005811 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005812
5813#ifdef FEAT_VTP
5814 else if (has_vtp_working())
5815 {
5816 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005817 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005818 redrawcmd();
5819 }
5820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821}
5822
5823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005824ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005827 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 do_highlight(eap->arg, eap->forceit, FALSE);
5829}
5830
5831
5832/*
5833 * Call this function if we thought we were going to exit, but we won't
5834 * (because of an error). May need to restore the terminal mode.
5835 */
5836 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005837not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 exiting = FALSE;
5840 settmode(TMODE_RAW);
5841}
5842
Bram Moolenaar3552e742021-05-29 12:21:58 +02005843 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005844before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5845{
5846 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5847
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005848 // Bail out when autocommands closed the window.
5849 // Refuse to quit when the buffer in the last window is being closed (can
5850 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005851 if (!win_valid(wp)
5852 || curbuf_locked()
5853 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5854 return TRUE;
5855
5856 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5857 {
5858 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005859 // Refuse to quit when locked or when the window was closed or the
5860 // buffer in the last window is being closed (can only happen in
5861 // autocommands).
5862 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005863 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5864 return TRUE;
5865 }
5866
5867 return FALSE;
5868}
5869
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005871 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005872 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005873 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005875 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005876ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005878 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005879
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 if (cmdwin_type != 0)
5881 {
5882 cmdwin_result = Ctrl_C;
5883 return;
5884 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005885 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005886 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005887 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005888 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005889 return;
5890 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005891 if (eap->addr_count > 0)
5892 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005893 int wnr = eap->line2;
5894
5895 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5896 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005897 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005898 }
5899 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005900 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005901
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005902 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005903 if (curbuf_locked())
5904 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005905
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005906 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005907 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005908 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
5910#ifdef FEAT_NETBEANS_INTG
5911 netbeansForcedQuit = eap->forceit;
5912#endif
5913
5914 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005915 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 */
5917 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5918 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005919 if ((!buf_hide(wp->w_buffer)
5920 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005921 | (eap->forceit ? CCGD_FORCEIT : 0)
5922 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005924 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
5926 not_exiting();
5927 }
5928 else
5929 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005930 // quit last window
5931 // Note: only_one_window() returns true, even so a help window is
5932 // still open. In that case only quit, if no address has been
5933 // specified. Example:
5934 // :h|wincmd w|1q - don't quit
5935 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005936 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005938 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005939#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005941#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005942 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005943 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 }
5945}
5946
5947/*
5948 * ":cquit".
5949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005951ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005953 // this does not always pass on the exit code to the Manx compiler. why?
5954 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955}
5956
5957/*
5958 * ":qall": try to quit all windows
5959 */
5960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005961ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 if (cmdwin_type != 0)
5964 {
5965 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005966 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 else
5968 cmdwin_result = K_XF2;
5969 return;
5970 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005971
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005972 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005973 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005974 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005975 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005976 return;
5977 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005978
5979 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005980 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005981
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005983 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 getout(0);
5985 not_exiting();
5986}
5987
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988/*
5989 * ":close": close current window, unless it is the last one
5990 */
5991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005992ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005994 win_T *win;
5995 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005997 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005999 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006000 {
6001 if (eap->addr_count == 0)
6002 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006003 else
6004 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006005 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006006 {
6007 winnr++;
6008 if (winnr == eap->line2)
6009 break;
6010 }
6011 if (win == NULL)
6012 win = lastwin;
6013 ex_win_close(eap->forceit, win, NULL);
6014 }
6015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016}
6017
Bram Moolenaar4033c552017-09-16 20:54:51 +02006018#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006019/*
6020 * ":pclose": Close any preview window.
6021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006023ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006024{
6025 win_T *win;
6026
Bram Moolenaar79648732019-07-18 21:43:07 +02006027 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006028 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006029 if (win->w_p_pvw)
6030 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006031 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006032 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006033 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006034# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006035 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006036 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006037# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006038}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006039#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006040
Bram Moolenaarf740b292006-02-16 22:11:02 +00006041/*
6042 * Close window "win" and take care of handling closing the last window for a
6043 * modified buffer.
6044 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006045 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006046ex_win_close(
6047 int forceit,
6048 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006049 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
6051 int need_hide;
6052 buf_T *buf = win->w_buffer;
6053
Bram Moolenaarcf844172020-06-26 19:44:06 +02006054 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006055 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006056 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006057 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006058 return;
6059 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006060 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006061 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006064 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006066#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006067 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006069# ifdef FEAT_TERMINAL
6070 if (term_job_running(buf->b_term))
6071 {
6072 if (term_confirm_stop(buf) == FAIL)
6073 return;
6074 // Manually kill the terminal here because this command will
6075 // hide it otherwise.
6076 free_terminal(buf);
6077 need_hide = FALSE;
6078 }
6079 else
6080# endif
6081 {
6082 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006083
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006084 set_bufref(&bufref, buf);
6085 dialog_changed(buf, FALSE);
6086 if (bufref_valid(&bufref) && bufIsChanged(buf))
6087 return;
6088 need_hide = FALSE;
6089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 }
6091 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006094 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 return;
6096 }
6097 }
6098
Bram Moolenaar4033c552017-09-16 20:54:51 +02006099#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006101#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006104 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006105 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006106 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006107 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108}
6109
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006111 * Handle the argument for a tabpage related ex command.
6112 * Returns a tabpage number.
6113 * When an error is encountered then eap->errmsg is set.
6114 */
6115 static int
6116get_tabpage_arg(exarg_T *eap)
6117{
6118 int tab_number;
6119 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6120
6121 if (eap->arg && *eap->arg != NUL)
6122 {
6123 char_u *p = eap->arg;
6124 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006125 int relative = 0; // argument +N/-N means: go to N places to the
6126 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006127
6128 if (*p == '-')
6129 {
6130 relative = -1;
6131 p++;
6132 }
6133 else if (*p == '+')
6134 {
6135 relative = 1;
6136 p++;
6137 }
6138
6139 p_save = p;
6140 tab_number = getdigits(&p);
6141
6142 if (relative == 0)
6143 {
6144 if (STRCMP(p, "$") == 0)
6145 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006146 else if (STRCMP(p, "#") == 0)
6147 if (valid_tabpage(lastused_tabpage))
6148 tab_number = tabpage_index(lastused_tabpage);
6149 else
6150 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006151 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006152 tab_number = 0;
6153 goto theend;
6154 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006155 else if (p == p_save || *p_save == '-' || *p != NUL
6156 || tab_number > LAST_TAB_NR)
6157 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006158 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006159 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006160 goto theend;
6161 }
6162 }
6163 else
6164 {
6165 if (*p_save == NUL)
6166 tab_number = 1;
6167 else if (p == p_save || *p_save == '-' || *p != NUL
6168 || tab_number == 0)
6169 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006170 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006171 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006172 goto theend;
6173 }
6174 tab_number = tab_number * relative + tabpage_index(curtab);
6175 if (!unaccept_arg0 && relative == -1)
6176 --tab_number;
6177 }
6178 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006179 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006180 }
6181 else if (eap->addr_count > 0)
6182 {
6183 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006184 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006185 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006186 tab_number = 0;
6187 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006188 else
6189 {
6190 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006191 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006192 {
6193 --tab_number;
6194 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006195 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006196 }
6197 }
6198 }
6199 else
6200 {
6201 switch (eap->cmdidx)
6202 {
6203 case CMD_tabnext:
6204 tab_number = tabpage_index(curtab) + 1;
6205 if (tab_number > LAST_TAB_NR)
6206 tab_number = 1;
6207 break;
6208 case CMD_tabmove:
6209 tab_number = LAST_TAB_NR;
6210 break;
6211 default:
6212 tab_number = tabpage_index(curtab);
6213 }
6214 }
6215
6216theend:
6217 return tab_number;
6218}
6219
6220/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006221 * ":tabclose": close current tab page, unless it is the last one.
6222 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 */
6224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006225ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006227 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006228 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006229
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006230 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006231 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006232 cmdwin_result = K_IGNORE;
6233 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006234 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006235
6236 if (first_tabpage->tp_next == NULL)
6237 {
6238 emsg(_(e_cannot_close_last_tab_page));
6239 return;
6240 }
6241
6242 if (window_layout_locked(CMD_tabclose))
6243 return;
6244
6245 tab_number = get_tabpage_arg(eap);
6246 if (eap->errmsg != NULL)
6247 return;
6248
6249 tp = find_tabpage(tab_number);
6250 if (tp == NULL)
6251 {
6252 beep_flush();
6253 return;
6254 }
6255 if (tp != curtab)
6256 {
6257 tabpage_close_other(tp, eap->forceit);
6258 return;
6259 }
6260 else if (!text_locked() && !curbuf_locked())
6261 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006262}
6263
6264/*
6265 * ":tabonly": close all tab pages except the current one
6266 */
6267 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006268ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006269{
6270 tabpage_T *tp;
6271 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006272 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006273
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006274 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006275 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006276 cmdwin_result = K_IGNORE;
6277 return;
6278 }
6279
6280 if (first_tabpage->tp_next == NULL)
6281 {
6282 msg(_("Already only one tab page"));
6283 return;
6284 }
6285
6286 if (window_layout_locked(CMD_tabonly))
6287 return;
6288
6289 tab_number = get_tabpage_arg(eap);
6290 if (eap->errmsg != NULL)
6291 return;
6292
6293 goto_tabpage(tab_number);
6294 // Repeat this up to a 1000 times, because autocommands may
6295 // mess up the lists.
6296 for (done = 0; done < 1000; ++done)
6297 {
6298 FOR_ALL_TABPAGES(tp)
6299 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006300 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006301 tabpage_close_other(tp, eap->forceit);
6302 // if we failed to close it quit
6303 if (valid_tabpage(tp))
6304 done = 1000;
6305 // start over, "tp" is now invalid
6306 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006307 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006308 if (first_tabpage->tp_next == NULL)
6309 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006314 * Close the current tab page.
6315 */
6316 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006317tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006318{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006319 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006320 return;
6321
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006322 // First close all the windows but the current one. If that worked then
6323 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006324 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006325 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006326 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006327 ex_win_close(forceit, curwin, NULL);
6328# ifdef FEAT_GUI
6329 need_mouse_correct = TRUE;
6330# endif
6331}
6332
6333/*
6334 * Close tab page "tp", which is not the current tab page.
6335 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006336 * Also takes care of the tab pages line disappearing when closing the
6337 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006338 */
6339 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006340tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006341{
6342 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006343 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006344
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006345 // Limit to 1000 windows, autocommands may add a window while we close
6346 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006347 while (++done < 1000)
6348 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006349 wp = tp->tp_firstwin;
6350 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006351
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006352 // Autocommands may delete the tab page under our fingers and we may
6353 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006354 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006355 break;
6356 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006357
Bram Moolenaar29323592016-07-24 22:04:11 +02006358 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006359}
6360
6361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 * ":only".
6363 */
6364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006365ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006367 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006368 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369# ifdef FEAT_GUI
6370 need_mouse_correct = TRUE;
6371# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006372 if (eap->addr_count > 0)
6373 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006374 win_T *wp;
6375 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006376 for (wp = firstwin; --wnr > 0; )
6377 {
6378 if (wp->w_next == NULL)
6379 break;
6380 else
6381 wp = wp->w_next;
6382 }
6383 win_goto(wp);
6384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 close_others(TRUE, eap->forceit);
6386}
6387
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006389ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006391 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006392 if (eap->skip)
6393 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006394
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006395 if (window_layout_locked(CMD_hide))
6396 return;
6397#ifdef FEAT_GUI
6398 need_mouse_correct = TRUE;
6399#endif
6400 if (eap->addr_count == 0)
6401 win_close(curwin, FALSE); // don't free buffer
6402 else
6403 {
6404 int winnr = 0;
6405 win_T *win;
6406
6407 FOR_ALL_WINDOWS(win)
6408 {
6409 winnr++;
6410 if (winnr == eap->line2)
6411 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006413 if (win == NULL)
6414 win = lastwin;
6415 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
6417}
6418
6419/*
6420 * ":stop" and ":suspend": Suspend Vim.
6421 */
dbivolaruab16ad32021-12-29 19:41:47 +00006422 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006423ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
6425 /*
6426 * Disallow suspending for "rvim".
6427 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006428 if (check_restricted())
6429 return;
6430
6431 if (!eap->forceit)
6432 autowrite_all();
6433 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6434 windgoto((int)Rows - 1, 0);
6435 out_char('\n');
6436 out_flush();
6437 stoptermcap();
6438 out_flush(); // needed for SUN to restore xterm buffer
6439 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6440 ui_suspend(); // call machine specific function
6441 maketitle();
6442 resettitle(); // force updating the title
6443 starttermcap();
6444 scroll_start(); // scroll screen before redrawing
6445 redraw_later_clear();
6446 shell_resized(); // may have resized window
6447 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448}
6449
6450/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006451 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 */
6453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006454ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006456#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006457 if (not_in_vim9(eap) == FAIL)
6458 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 if (cmdwin_type != 0)
6461 {
6462 cmdwin_result = Ctrl_C;
6463 return;
6464 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006465 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006466 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006467 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006468 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006469 return;
6470 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006471
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006473 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 */
6475 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6476 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006477
6478 // Write the buffer for ":wq" or when it was changed.
6479 // Trigger QuitPre and ExitPre.
6480 // Check if we can exit now, after autocommands have changed things.
6481 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6482 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006484 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 {
6486 not_exiting();
6487 }
6488 else
6489 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006490 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006492 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493# ifdef FEAT_GUI
6494 need_mouse_correct = TRUE;
6495# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006496 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006497 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 }
6499}
6500
6501/*
6502 * ":print", ":list", ":number".
6503 */
6504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006505ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006507 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006508 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006509 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006511 for ( ;!got_int; ui_breakcheck())
6512 {
6513 print_line(eap->line1,
6514 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6515 || (eap->flags & EXFLAG_NR)),
6516 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6517 if (++eap->line1 > eap->line2)
6518 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006519 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006520 }
6521 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006522 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006523 curwin->w_cursor.lnum = eap->line2;
6524 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 }
6526
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528}
6529
6530#ifdef FEAT_BYTEOFF
6531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006532ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
6534 goto_byte(eap->line2);
6535}
6536#endif
6537
6538/*
6539 * ":shell".
6540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006542ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543{
6544 do_shell(NULL, 0);
6545}
6546
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006547#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006549static int drop_busy = FALSE;
6550static int drop_filec;
6551static char_u **drop_filev = NULL;
6552static int drop_split;
6553static void (*drop_callback)(void *);
6554static void *drop_cookie;
6555
6556 static void
6557handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558{
6559 exarg_T ea;
6560 int save_msg_scroll = msg_scroll;
6561
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006562 // Setting the argument list may cause screen updates and being called
6563 // recursively. Avoid that by setting drop_busy.
6564 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006566 // Check whether the current buffer is changed. If so, we will need
6567 // to split the current window or data could be lost.
6568 // We don't need to check if the 'hidden' option is set, as in this
6569 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006570 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 {
6572 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006573 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 --emsg_off;
6575 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006576 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 if (win_split(0, 0) == FAIL)
6579 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006580 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006582 // When splitting the window, create a new alist. Otherwise the
6583 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 alist_unlink(curwin->w_alist);
6585 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 }
6587
6588 /*
6589 * Set up the new argument list.
6590 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006591 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
6593 /*
6594 * Move to the first file.
6595 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006596 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006597 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 ea.cmd = (char_u *)"next";
6599 do_argfile(&ea, 0);
6600
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006601 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6602 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 need_start_insertmode = FALSE;
6604
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006605 // Restore msg_scroll, otherwise a following command may cause scrolling
6606 // unexpectedly. The screen will be redrawn by the caller, thus
6607 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006609
6610 if (drop_callback != NULL)
6611 drop_callback(drop_cookie);
6612
6613 drop_filev = NULL;
6614 drop_busy = FALSE;
6615}
6616
6617/*
6618 * Handle a file drop. The code is here because a drop is *nearly* like an
6619 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006620 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006621 * code is very similar to :args and hence needs access to a lot of the static
6622 * functions in this file.
6623 *
6624 * The "filev" list must have been allocated using alloc(), as should each item
6625 * in the list. This function takes over responsibility for freeing the "filev"
6626 * list.
6627 */
6628 void
6629handle_drop(
6630 int filec, // the number of files dropped
6631 char_u **filev, // the list of files dropped
6632 int split, // force splitting the window
6633 void (*callback)(void *), // to be called after setting the argument
6634 // list
6635 void *cookie) // argument for "callback" (allocated)
6636{
6637 // Cannot handle recursive drops, finish the pending one.
6638 if (drop_busy)
6639 {
6640 FreeWild(filec, filev);
6641 vim_free(cookie);
6642 return;
6643 }
6644
6645 // When calling handle_drop() more than once in a row we only use the last
6646 // one.
6647 if (drop_filev != NULL)
6648 {
6649 FreeWild(drop_filec, drop_filev);
6650 vim_free(drop_cookie);
6651 }
6652
6653 drop_filec = filec;
6654 drop_filev = filev;
6655 drop_split = split;
6656 drop_callback = callback;
6657 drop_cookie = cookie;
6658
6659 // Postpone this when:
6660 // - editing the command line
6661 // - not possible to change the current buffer
6662 // - updating the screen
6663 // As it may change buffers and window structures that are in use and cause
6664 // freed memory to be used.
6665 if (text_locked() || curbuf_locked() || updating_screen)
6666 return;
6667
6668 handle_drop_internal();
6669}
6670
6671/*
6672 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6673 * reset: Handle a postponed drop.
6674 */
6675 void
6676handle_any_postponed_drop(void)
6677{
6678 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006679 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006680 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681}
6682#endif
6683
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 * ":preserve".
6686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006688ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006690 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 ml_preserve(curbuf, TRUE);
6692}
6693
6694/*
6695 * ":recover".
6696 */
6697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006698ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006700 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006702 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6703 | CCGD_MULTWIN
6704 | (eap->forceit ? CCGD_FORCEIT : 0)
6705 | CCGD_EXCMD)
6706
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 && (*eap->arg == NUL
6708 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006709 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 recoverymode = FALSE;
6711}
6712
6713/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006714 * Command modifier used in a wrong way. Also for other commands that can't
6715 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 */
6717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006718ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006720 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721}
6722
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723/*
6724 * :sview [+command] file split window with new file, read-only
6725 * :split [[+command] file] split window with current or new file
6726 * :vsplit [[+command] file] split window vertically with current or new file
6727 * :new [[+command] file] split window with no or new file
6728 * :vnew [[+command] file] split vertically window with no or new file
6729 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006730 *
6731 * :tabedit open new Tab page with empty window
6732 * :tabedit [+command] file open new Tab page and edit "file"
6733 * :tabnew [[+command] file] just like :tabedit
6734 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 */
6736 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006737ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006739 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006741#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006742 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006743 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006744#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006745 int use_tab = eap->cmdidx == CMD_tabedit
6746 || eap->cmdidx == CMD_tabfind
6747 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006749 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006750 return;
6751
Bram Moolenaar4033c552017-09-16 20:54:51 +02006752#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
Bram Moolenaar4033c552017-09-16 20:54:51 +02006756#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006757 // A ":split" in the quickfix window works like ":new". Don't want two
6758 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006759 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 {
6761 if (eap->cmdidx == CMD_split)
6762 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 if (eap->cmdidx == CMD_vsplit)
6764 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006768 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {
6770 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6771 FNAME_MESS, TRUE, curbuf->b_ffname);
6772 if (fname == NULL)
6773 goto theend;
6774 eap->arg = fname;
6775 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006776# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006777 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 && eap->cmdidx != CMD_new)
6780 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006781 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006782# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006783 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006784# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006785 au_has_group((char_u *)"FileExplorer"))
6786 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006787 // No browsing supported but we do have the file explorer:
6788 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006789 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006790 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006791 }
6792 else
6793 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006794 fname = do_browse(0, (char_u *)(use_tab
6795 ? _("Edit File in new tab page")
6796 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006798 if (fname == NULL)
6799 goto theend;
6800 eap->arg = fname;
6801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006803 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006806 /*
6807 * Either open new tab page or split the window.
6808 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006809 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006810 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006811 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006812 : eap->addr_count == 0 ? 0
6813 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006814 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006815 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006816
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006817 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006818 if (curwin != old_curwin
6819 && win_valid(old_curwin)
6820 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006821 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006822 old_curwin->w_alt_fnum = curbuf->b_fnum;
6823 }
6824 }
6825 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6827 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006828 // Reset 'scrollbind' when editing another file, but keep it when
6829 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006830 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006831 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 else
6833 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 do_exedit(eap, old_curwin);
6835 }
6836
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006837# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006838 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841theend:
6842 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006844
6845/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006846 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006847 */
6848 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006849tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006850{
6851 exarg_T ea;
6852
Bram Moolenaara80faa82020-04-12 19:37:17 +02006853 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006854 ea.cmdidx = CMD_tabnew;
6855 ea.cmd = (char_u *)"tabn";
6856 ea.arg = (char_u *)"";
6857 ex_splitview(&ea);
6858}
6859
6860/*
6861 * :tabnext command
6862 */
6863 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006864ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006865{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006866 int tab_number;
6867
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006868 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006869 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006870 switch (eap->cmdidx)
6871 {
6872 case CMD_tabfirst:
6873 case CMD_tabrewind:
6874 goto_tabpage(1);
6875 break;
6876 case CMD_tablast:
6877 goto_tabpage(9999);
6878 break;
6879 case CMD_tabprevious:
6880 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006881 if (eap->arg && *eap->arg != NUL)
6882 {
6883 char_u *p = eap->arg;
6884 char_u *p_save = p;
6885
6886 tab_number = getdigits(&p);
6887 if (p == p_save || *p_save == '-' || *p != NUL
6888 || tab_number == 0)
6889 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006890 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006891 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006892 return;
6893 }
6894 }
6895 else
6896 {
6897 if (eap->addr_count == 0)
6898 tab_number = 1;
6899 else
6900 {
6901 tab_number = eap->line2;
6902 if (tab_number < 1)
6903 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006904 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006905 return;
6906 }
6907 }
6908 }
6909 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006910 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006911 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006912 tab_number = get_tabpage_arg(eap);
6913 if (eap->errmsg == NULL)
6914 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006915 break;
6916 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006917}
6918
6919/*
6920 * :tabmove command
6921 */
6922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006923ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006924{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006925 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006926
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006927 tab_number = get_tabpage_arg(eap);
6928 if (eap->errmsg == NULL)
6929 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006930}
6931
6932/*
6933 * :tabs command: List tabs and their contents.
6934 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006936ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006937{
6938 tabpage_T *tp;
6939 win_T *wp;
6940 int tabcount = 1;
6941
6942 msg_start();
6943 msg_scroll = TRUE;
6944 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6945 {
6946 msg_putchar('\n');
6947 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006948 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006949 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006950 ui_breakcheck();
6951
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006952 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006953 wp = firstwin;
6954 else
6955 wp = tp->tp_firstwin;
6956 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6957 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006958 msg_putchar('\n');
6959 msg_putchar(wp == curwin ? '>' : ' ');
6960 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006961 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6962 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006963 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006964 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006965 else
6966 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6967 IObuff, IOSIZE, TRUE);
6968 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006969 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006970 ui_breakcheck();
6971 }
6972 }
6973}
6974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975/*
6976 * ":mode": Set screen mode.
6977 * If no argument given, just get the screen size and redraw.
6978 */
6979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006980ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
6982 if (*eap->arg == NUL)
6983 shell_resized();
6984 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006985 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986}
6987
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988/*
6989 * ":resize".
6990 * set, increment or decrement current window height
6991 */
6992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006993ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994{
6995 int n;
6996 win_T *wp = curwin;
6997
6998 if (eap->addr_count > 0)
6999 {
7000 n = eap->line2;
7001 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7002 ;
7003 }
7004
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007005# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007009 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
7011 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007012 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007013 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007015 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 }
7017 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {
7019 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007020 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007021 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007023 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 }
7025}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026
7027/*
7028 * ":find [+command] <file>" command.
7029 */
7030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007031ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 char_u *fname;
7034 int count;
7035
7036 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7037 TRUE, curbuf->b_ffname);
7038 if (eap->addr_count > 0)
7039 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007040 // Repeat finding the file "count" times. This matters when it
7041 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 count = eap->line2;
7043 while (fname != NULL && --count > 0)
7044 {
7045 vim_free(fname);
7046 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7047 FALSE, curbuf->b_ffname);
7048 }
7049 }
7050
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007051 if (fname == NULL)
7052 return;
7053
7054 eap->arg = fname;
7055 do_exedit(eap, NULL);
7056 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057}
7058
7059/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007060 * ":open" simulation: for now just work like ":visual".
7061 */
7062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007063ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007064{
7065 regmatch_T regmatch;
7066 char_u *p;
7067
Bram Moolenaar65088802021-03-13 21:07:21 +01007068#ifdef FEAT_EVAL
7069 if (not_in_vim9(eap) == FAIL)
7070 return;
7071#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007072 curwin->w_cursor.lnum = eap->line2;
7073 beginline(BL_SOL | BL_FIX);
7074 if (*eap->arg == '/')
7075 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007076 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007077 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007078 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007079 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007080 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007081 if (regmatch.regprog != NULL)
7082 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007083 // make a copy of the line, when searching for a mark it might be
7084 // flushed
7085 char_u *line = vim_strsave(ml_get_curline());
7086
Bram Moolenaardf177f62005-02-22 08:39:57 +00007087 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007088 if (vim_regexec(&regmatch, line, (colnr_T)0))
7089 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007090 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007091 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007092 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007093 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007094 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007095 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007096 eap->arg += STRLEN(eap->arg);
7097 }
7098 check_cursor();
7099
7100 eap->cmdidx = CMD_visual;
7101 do_exedit(eap, NULL);
7102}
7103
7104/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007105 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 */
7107 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007108ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
7110 do_exedit(eap, NULL);
7111}
7112
7113/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007114 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007117do_exedit(
7118 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007119 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120{
7121 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007123 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007125 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7126 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007127 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 /*
7129 * ":vi" command ends Ex mode.
7130 */
7131 if (exmode_active && (eap->cmdidx == CMD_visual
7132 || eap->cmdidx == CMD_view))
7133 {
7134 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007135 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007137 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007138 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007139 if (global_busy)
7140 {
7141 int rd = RedrawingDisabled;
7142 int nwr = no_wait_return;
7143 int ms = msg_scroll;
7144#ifdef FEAT_GUI
7145 int he = hold_gui_events;
7146#endif
7147
7148 if (eap->nextcmd != NULL)
7149 {
7150 stuffReadbuff(eap->nextcmd);
7151 eap->nextcmd = NULL;
7152 }
7153
7154 if (exmode_was != EXMODE_VIM)
7155 settmode(TMODE_RAW);
7156 RedrawingDisabled = 0;
7157 no_wait_return = 0;
7158 need_wait_return = FALSE;
7159 msg_scroll = 0;
7160#ifdef FEAT_GUI
7161 hold_gui_events = 0;
7162#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007163 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007164 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007165
7166 main_loop(FALSE, TRUE);
7167
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007168 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007169 RedrawingDisabled = rd;
7170 no_wait_return = nwr;
7171 msg_scroll = ms;
7172#ifdef FEAT_GUI
7173 hold_gui_events = he;
7174#endif
7175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 }
7179
7180 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007181 || eap->cmdidx == CMD_tabnew
7182 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007183 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007185 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 setpcmark();
7187 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007188 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7189 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007191 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 || *eap->arg != NUL
7193#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007194 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195#endif
7196 )
7197 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007198 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7199 // Only ":edit" or ":script" can bring us here, others are stopped
7200 // earlier.
7201 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007202 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007203
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 n = readonlymode;
7205 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7206 readonlymode = TRUE;
7207 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007208 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7209 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007210 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7211 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7213 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007214 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7216 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7217 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007218 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007220 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007221 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007222 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7223 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007224 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007226 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 if (old_curwin != NULL)
7228 {
7229 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007230 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007232#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007233 cleanup_T cs;
7234
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007235 // Reset the error/interrupt/exception state here so that
7236 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007237 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007238#endif
7239#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007241#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007242 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007243
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007244#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007245 // Restore the error/interrupt/exception state if not
7246 // discarded by a new aborting error, interrupt, or
7247 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007248 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 }
7251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 }
7253 else if (readonlymode && curbuf->b_nwindows == 1)
7254 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007255 // When editing an already visited buffer, 'readonly' won't be set
7256 // but the previous value is kept. With ":view" and ":sview" we
7257 // want the file to be readonly, except when another window is
7258 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 curbuf->b_p_ro = TRUE;
7260 }
7261 readonlymode = n;
7262 }
7263 else
7264 {
7265 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007266 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 if (n != curwin->w_arg_idx_invalid)
7270 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 }
7272
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 /*
7274 * if ":split file" worked, set alternate file name in old window to new
7275 * file
7276 */
7277 if (old_curwin != NULL
7278 && *eap->arg != NUL
7279 && curwin != old_curwin
7280 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007281 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007282 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284
7285 ex_no_reprint = TRUE;
7286}
7287
7288#ifndef FEAT_GUI
7289/*
7290 * ":gui" and ":gvim" when there is no GUI.
7291 */
7292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007293ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007295 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296}
7297#endif
7298
Bram Moolenaar4f974752019-02-17 17:44:42 +01007299#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007301ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303 gui_make_tearoff(eap->arg);
7304}
7305#endif
7306
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007307#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7308 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007310ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007312# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7313 if (gui.in_use)
7314 gui_make_popup(eap->arg, eap->forceit);
7315# ifdef FEAT_TERM_POPUP_MENU
7316 else
7317# endif
7318# endif
7319# ifdef FEAT_TERM_POPUP_MENU
7320 pum_make_popup(eap->arg, eap->forceit);
7321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322}
7323#endif
7324
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007326ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327{
7328 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007329 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007331 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332}
7333
7334/*
7335 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7336 * offset.
7337 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007340ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007343 win_T *save_curwin = curwin;
7344 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 long topline;
7346 long y;
7347 linenr_T old_linenr = curwin->w_cursor.lnum;
7348
7349 setpcmark();
7350
7351 /*
7352 * determine max topline
7353 */
7354 if (curwin->w_p_scb)
7355 {
7356 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007357 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 {
7359 if (wp->w_p_scb && wp->w_buffer)
7360 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007361 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 if (topline > y)
7363 topline = y;
7364 }
7365 }
7366 if (topline < 1)
7367 topline = 1;
7368 }
7369 else
7370 {
7371 topline = 1;
7372 }
7373
7374
7375 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007376 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007378 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 {
7380 if (curwin->w_p_scb)
7381 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007382 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 y = topline - curwin->w_topline;
7384 if (y > 0)
7385 scrollup(y, TRUE);
7386 else
7387 scrolldown(-y, TRUE);
7388 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007389 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 }
7393 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007394 curwin = save_curwin;
7395 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 if (curwin->w_p_scb)
7397 {
7398 did_syncbind = TRUE;
7399 checkpcmark();
7400 if (old_linenr != curwin->w_cursor.lnum)
7401 {
7402 char_u ctrl_o[2];
7403
7404 ctrl_o[0] = Ctrl_O;
7405 ctrl_o[1] = 0;
7406 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7407 }
7408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409}
7410
7411
7412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007413ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007415 int i;
7416 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7417 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007419 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007421 do_bang(1, eap, FALSE, FALSE, TRUE);
7422 return;
7423 }
7424
7425 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7426 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427
7428#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007429 if (cmdmod.cmod_flags & CMOD_BROWSE)
7430 {
7431 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007433 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7434 NULL, NULL, NULL, curbuf);
7435 if (browseFile != NULL)
7436 {
7437 i = readfile(browseFile, NULL,
7438 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7439 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 }
7441 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007442 i = OK;
7443 }
7444 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007446 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007448 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 return;
7450 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007451 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 }
7453 else
7454 {
7455 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7456 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7457 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007458 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459
7460 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007461 if (i != OK)
7462 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007463#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007464 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007466 semsg(_(e_cant_open_file_str), eap->arg);
7467 }
7468 else
7469 {
7470 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007471 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007472 // Delete the empty line that remains. Historically ex does
7473 // this but vi doesn't.
7474 if (eap->line2 == 0)
7475 lnum = curbuf->b_ml.ml_line_count;
7476 else
7477 lnum = 1;
7478 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007479 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007480 ml_delete(lnum);
7481 if (curwin->w_cursor.lnum > 1
7482 && curwin->w_cursor.lnum >= lnum)
7483 --curwin->w_cursor.lnum;
7484 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007485 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007486 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007487 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 }
7489}
7490
Bram Moolenaarea408852005-06-25 22:49:46 +00007491static char_u *prev_dir = NULL;
7492
7493#if defined(EXITFREE) || defined(PROTO)
7494 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007495free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007496{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007497 VIM_CLEAR(prev_dir);
7498 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007499}
7500#endif
7501
Bram Moolenaarf4258302013-06-02 18:20:17 +02007502/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007503 * Get the previous directory for the given chdir scope.
7504 */
7505 static char_u *
7506get_prevdir(cdscope_T scope)
7507{
7508 if (scope == CDSCOPE_WINDOW)
7509 return curwin->w_prevdir;
7510 else if (scope == CDSCOPE_TABPAGE)
7511 return curtab->tp_prevdir;
7512 return prev_dir;
7513}
7514
7515/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007516 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007517 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7518 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007519 */
7520 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007521post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007522{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007523 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007524 // Clear tab local directory for both :cd and :tcd
7525 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007526 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007527 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007528 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007529 char_u *pdir = get_prevdir(scope);
7530
7531 // If still in the global directory, need to remember current
7532 // directory as the global directory.
7533 if (globaldir == NULL && pdir != NULL)
7534 globaldir = vim_strsave(pdir);
7535
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007536 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007537 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007538 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007539 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007540 curtab->tp_localdir = vim_strsave(NameBuff);
7541 else
7542 curwin->w_localdir = vim_strsave(NameBuff);
7543 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007544 }
7545 else
7546 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007547 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007548 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007549 }
7550
zeertzjq64be6aa2021-11-19 11:59:08 +00007551 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007552 shorten_fnames(TRUE);
7553}
7554
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007555/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007556 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7557 */
7558 void
7559trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7560{
7561#ifdef FEAT_EVAL
7562 dict_T *v_event;
7563 save_v_event_T save_v_event;
7564
7565 v_event = get_v_event(&save_v_event);
7566 (void)dict_add_string(v_event, "directory", new_dir);
7567 dict_set_items_ro(v_event);
7568#endif
7569 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7570#ifdef FEAT_EVAL
7571 restore_v_event(v_event, &save_v_event);
7572#endif
7573}
7574
7575/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007576 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007577 * chdir() function.
7578 * scope == CDSCOPE_WINDOW: changes the window-local directory
7579 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7580 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007581 * Returns TRUE if the directory is successfully changed.
7582 */
7583 int
7584changedir_func(
7585 char_u *new_dir,
7586 int forceit,
7587 cdscope_T scope)
7588{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007589 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007590 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007591 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007592 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007593 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007594
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007595 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007596 return FALSE;
7597
7598 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7599 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007600 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007601 return FALSE;
7602 }
7603
7604 // ":cd -": Change to previous directory
7605 if (STRCMP(new_dir, "-") == 0)
7606 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007607 pdir = get_prevdir(scope);
7608 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007609 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007610 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007611 return FALSE;
7612 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007613 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007614 }
7615
7616 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007617 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007618 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007619 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007620 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007621
Bakudankun29f3a452021-12-11 12:28:08 +00007622 // For UNIX ":cd" means: go to home directory.
7623 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007624#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007625 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007626#else
7627 if (*new_dir == NUL && p_cdh)
7628#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007629 {
7630 // use NameBuff for home directory name
7631# ifdef VMS
7632 char_u *p;
7633
7634 p = mch_getenv((char_u *)"SYS$LOGIN");
7635 if (p == NULL || *p == NUL) // empty is the same as not set
7636 NameBuff[0] = NUL;
7637 else
7638 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7639# else
7640 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7641# endif
7642 new_dir = NameBuff;
7643 }
zeertzjqf38aad82021-12-30 13:45:57 +00007644 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007645 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7646 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007647 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007648 if (scope == CDSCOPE_WINDOW)
7649 acmd_fname = (char_u *)"window";
7650 else if (scope == CDSCOPE_TABPAGE)
7651 acmd_fname = (char_u *)"tabpage";
7652 else
7653 acmd_fname = (char_u *)"global";
7654 trigger_DirChangedPre(acmd_fname, new_dir);
7655
7656 if (vim_chdir(new_dir))
7657 {
7658 emsg(_(e_command_failed));
7659 vim_free(pdir);
7660 return FALSE;
7661 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007662 }
zeertzjq73257142022-02-02 13:16:37 +00007663
7664 if (scope == CDSCOPE_WINDOW)
7665 pp = &curwin->w_prevdir;
7666 else if (scope == CDSCOPE_TABPAGE)
7667 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007668 else
zeertzjq73257142022-02-02 13:16:37 +00007669 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007670 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007671 *pp = pdir;
7672
7673 post_chdir(scope);
7674
7675 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007676 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007677 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007678 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007679}
Bram Moolenaarea408852005-06-25 22:49:46 +00007680
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007682 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007684 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007685ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007687 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688
7689 new_dir = eap->arg;
7690#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007691 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7692 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007694 ex_pwd(NULL);
7695 return;
7696 }
7697#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007698
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007699 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007700
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007701 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7702 scope = CDSCOPE_WINDOW;
7703 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7704 scope = CDSCOPE_TABPAGE;
7705
7706 if (changedir_func(new_dir, eap->forceit, scope))
7707 {
7708 // Echo the new current directory if the command was typed.
7709 if (KeyTyped || p_verbose >= 5)
7710 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 }
7712}
7713
7714/*
7715 * ":pwd".
7716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007718ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719{
7720 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7721 {
7722#ifdef BACKSLASH_IN_FILENAME
7723 slash_adjust(NameBuff);
7724#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007725 if (p_verbose > 0)
7726 {
7727 char *context = "global";
7728
Bram Moolenaar05268152021-11-18 18:53:45 +00007729 if (last_chdir_reason != NULL)
7730 context = last_chdir_reason;
7731 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007732 context = "window";
7733 else if (curtab->tp_localdir != NULL)
7734 context = "tabpage";
7735 smsg("[%s] %s", context, (char *)NameBuff);
7736 }
7737 else
7738 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 }
7740 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007741 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742}
7743
7744/*
7745 * ":=".
7746 */
7747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007748ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007750 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007751 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752}
7753
7754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007755ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007757 int n;
7758 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759
7760 if (cursor_valid())
7761 {
7762 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7763 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007764 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007766
7767 len = eap->line2;
7768 switch (*eap->arg)
7769 {
7770 case 'm': break;
7771 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007772 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007773 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007774
7775 // Hide the cursor if invoked with !
7776 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777}
7778
7779/*
7780 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007781 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 */
7783 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007784do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
Bram Moolenaar52953192019-08-16 10:27:13 +02007786 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007787 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007788# ifdef ELAPSED_FUNC
7789 elapsed_T start_tv;
7790
7791 // Remember at what time we started, so that we know how much longer we
7792 // should wait after waiting for a bit.
7793 ELAPSED_INIT(start_tv);
7794# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007796 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007797 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007798 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007799 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007800
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007801 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007802 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007804 wait_now = msec - done > 1000L ? 1000L : msec - done;
7805#ifdef FEAT_TIMERS
7806 {
7807 long due_time = check_due_timer();
7808
7809 if (due_time > 0 && due_time < wait_now)
7810 wait_now = due_time;
7811 }
7812#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007813#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007814 if (has_any_channel() && wait_now > 20L)
7815 wait_now = 20L;
7816#endif
7817#ifdef FEAT_SOUND
7818 if (has_any_sound_callback() && wait_now > 20L)
7819 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007820#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007821 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007822
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007823#ifdef FEAT_JOB_CHANNEL
7824 if (has_any_channel())
7825 ui_breakcheck_force(TRUE);
7826 else
7827#endif
7828 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007829#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007830 // Process the netbeans and clientserver messages that may have been
7831 // received in the call to ui_breakcheck() when the GUI is in use. This
7832 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007833 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007834#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007835
7836# ifdef ELAPSED_FUNC
7837 // actual time passed
7838 done = ELAPSED_FUNC(start_tv);
7839# else
7840 // guestimate time passed (will actually be more)
7841 done += wait_now;
7842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007844
7845 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7846 // input buffer, otherwise a following call to input() fails.
7847 if (got_int)
7848 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007849
7850 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007851 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852}
7853
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854/*
7855 * ":winsize" command (obsolete).
7856 */
7857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007858ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860 int w, h;
7861 char_u *arg = eap->arg;
7862 char_u *p;
7863
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007864 if (!isdigit(*arg))
7865 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007866 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007867 return;
7868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 w = getdigits(&arg);
7870 arg = skipwhite(arg);
7871 p = arg;
7872 h = getdigits(&arg);
7873 if (*p != NUL && *arg == NUL)
7874 set_shellsize(w, h, TRUE);
7875 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007876 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877}
7878
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007880ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
7882 int xchar = NUL;
7883 char_u *p;
7884
7885 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7886 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007887 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 if (eap->arg[1] == NUL)
7889 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007890 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 return;
7892 }
7893 xchar = eap->arg[1];
7894 p = eap->arg + 2;
7895 }
7896 else
7897 p = eap->arg + 1;
7898
Bram Moolenaar63b91732021-08-05 20:40:03 +02007899 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007901 if (*p != NUL && *p != (
7902#ifdef FEAT_EVAL
7903 in_vim9script() ? '#' :
7904#endif
7905 '"')
7906 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007907 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007908 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007910 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007911 postponed_split_flags = cmdmod.cmod_split;
7912 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7914 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007915 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 }
7917}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918
Bram Moolenaar843ee412004-06-30 16:16:41 +00007919#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920/*
7921 * ":winpos".
7922 */
7923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007924ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925{
7926 int x, y;
7927 char_u *arg = eap->arg;
7928 char_u *p;
7929
7930 if (*arg == NUL)
7931 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007932# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007933# ifdef VIMDLL
7934 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7935 mch_get_winpos(&x, &y) != FAIL)
7936# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007938# else
7939 if (mch_get_winpos(&x, &y) != FAIL)
7940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 {
7942 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007943 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 }
7945 else
7946# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007947 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 }
7949 else
7950 {
7951 x = getdigits(&arg);
7952 arg = skipwhite(arg);
7953 p = arg;
7954 y = getdigits(&arg);
7955 if (*p == NUL || *arg != NUL)
7956 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007957 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 return;
7959 }
7960# ifdef FEAT_GUI
7961 if (gui.in_use)
7962 gui_mch_set_winpos(x, y);
7963 else if (gui.starting)
7964 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007965 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 gui_win_x = x;
7967 gui_win_y = y;
7968 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007969# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 else
7971# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007972# endif
7973# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007974 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975# endif
7976# ifdef HAVE_TGETENT
7977 if (*T_CWP)
7978 term_set_winpos(x, y);
7979# endif
7980 }
7981}
7982#endif
7983
7984/*
7985 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7986 */
7987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007988ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
7990 oparg_T oa;
7991
7992 clear_oparg(&oa);
7993 oa.regname = eap->regname;
7994 oa.start.lnum = eap->line1;
7995 oa.end.lnum = eap->line2;
7996 oa.line_count = eap->line2 - eap->line1 + 1;
7997 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007999 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {
8001 setpcmark();
8002 curwin->w_cursor.lnum = eap->line1;
8003 beginline(BL_SOL | BL_FIX);
8004 }
8005
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008006 if (VIsual_active)
8007 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008008
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 switch (eap->cmdidx)
8010 {
8011 case CMD_delete:
8012 oa.op_type = OP_DELETE;
8013 op_delete(&oa);
8014 break;
8015
8016 case CMD_yank:
8017 oa.op_type = OP_YANK;
8018 (void)op_yank(&oa, FALSE, TRUE);
8019 break;
8020
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008021 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008022 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008024 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8025#else
8026 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008028 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 oa.op_type = OP_RSHIFT;
8030 else
8031 oa.op_type = OP_LSHIFT;
8032 op_shift(&oa, FALSE, eap->amount);
8033 break;
8034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008036 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037}
8038
8039/*
8040 * ":put".
8041 */
8042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008043ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008045 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 if (eap->line2 == 0)
8047 {
8048 eap->line2 = 1;
8049 eap->forceit = TRUE;
8050 }
8051 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008052 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008053 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008054 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055}
8056
8057/*
8058 * Handle ":copy" and ":move".
8059 */
8060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008061ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
8063 long n;
8064
Bram Moolenaar491799b2020-08-01 19:23:43 +02008065#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008066 if (not_in_vim9(eap) == FAIL)
8067 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008068#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008069 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008070 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {
8072 eap->nextcmd = NULL;
8073 return;
8074 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008075 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076
8077 /*
8078 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8079 */
8080 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8081 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008082 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 return;
8084 }
8085
8086 if (eap->cmdidx == CMD_move)
8087 {
8088 if (do_move(eap->line1, eap->line2, n) == FAIL)
8089 return;
8090 }
8091 else
8092 ex_copy(eap->line1, eap->line2, n);
8093 u_clearline();
8094 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008095 ex_may_print(eap);
8096}
8097
8098/*
8099 * Print the current line if flags were given to the Ex command.
8100 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008101 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008102ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008103{
8104 if (eap->flags != 0)
8105 {
8106 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8107 (eap->flags & EXFLAG_LIST));
8108 ex_no_reprint = TRUE;
8109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110}
8111
8112/*
8113 * ":smagic" and ":snomagic".
8114 */
8115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008116ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008118 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008120 magic_overruled = eap->cmdidx == CMD_smagic
8121 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008122 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008123 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124}
8125
8126/*
8127 * ":join".
8128 */
8129 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008130ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131{
8132 curwin->w_cursor.lnum = eap->line1;
8133 if (eap->line1 == eap->line2)
8134 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008135 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 return;
8137 if (eap->line2 == curbuf->b_ml.ml_line_count)
8138 {
8139 beep_flush();
8140 return;
8141 }
8142 ++eap->line2;
8143 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008144 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008146 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147}
8148
8149/*
8150 * ":[addr]@r" or ":[addr]*r": execute register
8151 */
8152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008153ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154{
8155 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008156 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157
8158 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008159 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160
8161#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008162 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163#endif
8164
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008165 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 c = *eap->arg;
8167 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8168 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008169 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008170 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8171 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008174 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008177 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008179 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008181 /*
8182 * Execute from the typeahead buffer.
8183 * Continue until the stuff buffer is empty and all added characters
8184 * have been consumed.
8185 */
8186 while (!stuff_empty() || typebuf.tb_len > prev_len)
8187 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8188
8189 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190}
8191
8192/*
8193 * ":!".
8194 */
8195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008196ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
8198 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8199}
8200
8201/*
8202 * ":undo".
8203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008205ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008207 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008208 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008209 else
8210 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211}
8212
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008213#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008215ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008216{
8217 char_u hash[UNDO_HASH_SIZE];
8218
8219 u_compute_hash(hash);
8220 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8221}
8222
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008224ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008225{
8226 char_u hash[UNDO_HASH_SIZE];
8227
8228 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008229 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008230}
8231#endif
8232
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233/*
8234 * ":redo".
8235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008237ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238{
8239 u_redo(1);
8240}
8241
8242/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008243 * ":earlier" and ":later".
8244 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008246ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008247{
8248 long count = 0;
8249 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008250 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008251 char_u *p = eap->arg;
8252
8253 if (*p == NUL)
8254 count = 1;
8255 else if (isdigit(*p))
8256 {
8257 count = getdigits(&p);
8258 switch (*p)
8259 {
8260 case 's': ++p; sec = TRUE; break;
8261 case 'm': ++p; sec = TRUE; count *= 60; break;
8262 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008263 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8264 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008265 }
8266 }
8267
8268 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008269 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008270 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008271 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8272 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008273}
8274
8275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 * ":redir": start/stop redirection.
8277 */
8278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008279ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280{
8281 char *mode;
8282 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008283 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284
Bram Moolenaarba768492016-07-08 15:32:54 +02008285#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008286 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008287 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008288 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008289 return;
8290 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008291#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008292
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 if (STRICMP(eap->arg, "END") == 0)
8294 close_redir();
8295 else
8296 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008297 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008299 ++arg;
8300 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008302 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 mode = "a";
8304 }
8305 else
8306 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008307 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308
8309 close_redir();
8310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008311 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008312 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 if (fname == NULL)
8314 return;
8315#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008316 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {
8318 char_u *browseFile;
8319
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008320 browseFile = do_browse(BROWSE_SAVE,
8321 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008322 fname, NULL, NULL,
8323 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008325 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 vim_free(fname);
8327 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008328 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 }
8330#endif
8331
8332 redir_fd = open_exfile(fname, eap->forceit, mode);
8333 vim_free(fname);
8334 }
8335#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008336 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008338 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008340 ++arg;
8341 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008343 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008344 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008346 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008348 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008349 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008350 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008351 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008353 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008354 if (*arg == '>')
8355 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008356 // Make register empty when not using @A-@Z and the
8357 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008358 if (*arg == NUL && !isupper(redir_reg))
8359 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008361 }
8362 if (*arg != NUL)
8363 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008364 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008365 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008366 }
8367 }
8368 else if (*arg == '=' && arg[1] == '>')
8369 {
8370 int append;
8371
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008372 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008373 close_redir();
8374 arg += 2;
8375
8376 if (*arg == '>')
8377 {
8378 ++arg;
8379 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 }
8381 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008382 append = FALSE;
8383
8384 if (var_redir_start(skipwhite(arg), append) == OK)
8385 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 }
8387#endif
8388
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008389 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008392 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008394
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008395 // Make sure redirection is not off. Can happen for cmdline completion
8396 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008397 if (redir_fd != NULL
8398#ifdef FEAT_EVAL
8399 || redir_reg || redir_vname
8400#endif
8401 )
8402 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403}
8404
8405/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008406 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008408 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008409ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008411 redraw_cmd(eap->forceit);
8412}
8413
8414/*
8415 * ":redraw": force redraw, with clear if "clear" is TRUE.
8416 */
8417 void
8418redraw_cmd(int clear)
8419{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 int r = RedrawingDisabled;
8421 int p = p_lz;
8422
8423 RedrawingDisabled = 0;
8424 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008425 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008427 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 if (need_maketitle)
8429 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008430#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8431# ifdef VIMDLL
8432 if (!gui.in_use)
8433# endif
8434 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 RedrawingDisabled = r;
8437 p_lz = p;
8438
Bram Moolenaar5017c662022-04-07 18:06:08 +01008439 // After drawing the statusline screen_attr may still be set.
8440 screen_stop_highlight();
8441
Bram Moolenaara2a89732022-08-31 14:46:18 +01008442 // Reset msg_didout, so that a message that's there is overwritten.
8443 msg_didout = FALSE;
8444 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008446 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008447 need_wait_return = FALSE;
8448
Bram Moolenaara653e532022-04-19 11:38:24 +01008449 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008450 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008451 redrawcmdline();
8452
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 out_flush();
8454}
8455
8456/*
8457 * ":redrawstatus": force redraw of status line(s)
8458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008460ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 int r = RedrawingDisabled;
8463 int p = p_lz;
8464
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 if (eap->forceit)
8466 status_redraw_all();
8467 else
8468 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008469 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008470 return; // redraw later
8471
8472 RedrawingDisabled = 0;
8473 p_lz = FALSE;
zeertzjq320d9102022-09-20 17:12:13 +01008474 if (State & MODE_CMDLINE)
8475 redraw_statuslines();
8476 else
8477 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 RedrawingDisabled = r;
8479 p_lz = p;
8480 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481}
8482
Bram Moolenaare12bab32019-01-08 22:02:56 +01008483/*
8484 * ":redrawtabline": force redraw of the tabline
8485 */
8486 static void
8487ex_redrawtabline(exarg_T *eap UNUSED)
8488{
8489 int r = RedrawingDisabled;
8490 int p = p_lz;
8491
8492 RedrawingDisabled = 0;
8493 p_lz = FALSE;
8494
8495 draw_tabline();
8496
8497 RedrawingDisabled = r;
8498 p_lz = p;
8499 out_flush();
8500}
8501
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008503close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504{
8505 if (redir_fd != NULL)
8506 {
8507 fclose(redir_fd);
8508 redir_fd = NULL;
8509 }
8510#ifdef FEAT_EVAL
8511 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008512 if (redir_vname)
8513 {
8514 var_redir_stop();
8515 redir_vname = 0;
8516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517#endif
8518}
8519
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008520#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008521 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008522vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008523{
8524 if (vim_mkdir(name, prot) != 0)
8525 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008526 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008527 return FAIL;
8528 }
8529 return OK;
8530}
8531#endif
8532
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533/*
8534 * Open a file for writing for an Ex command, with some checks.
8535 * Return file descriptor, or NULL on failure.
8536 */
8537 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008538open_exfile(
8539 char_u *fname,
8540 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008541 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542{
8543 FILE *fd;
8544
8545#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008546 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 if (mch_isdir(fname))
8548 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008549 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 return NULL;
8551 }
8552#endif
8553 if (!forceit && *mode != 'a' && vim_fexists(fname))
8554 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008555 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 return NULL;
8557 }
8558
8559 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008560 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561
8562 return fd;
8563}
8564
8565/*
8566 * ":mark" and ":k".
8567 */
8568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008569ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570{
8571 pos_T pos;
8572
Bram Moolenaar10b94212021-02-19 21:42:57 +01008573#ifdef FEAT_EVAL
8574 if (not_in_vim9(eap) == FAIL)
8575 return;
8576#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008577 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008579 emsg(_(e_argument_required));
8580 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008582
8583 if (eap->arg[1] != NUL) // more than one character?
8584 {
8585 semsg(_(e_trailing_characters_str), eap->arg);
8586 return;
8587 }
8588
8589 pos = curwin->w_cursor; // save curwin->w_cursor
8590 curwin->w_cursor.lnum = eap->line2;
8591 beginline(BL_WHITE | BL_FIX);
8592 if (setmark(*eap->arg) == FAIL) // set mark
8593 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
8594 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595}
8596
8597/*
8598 * Update w_topline, w_leftcol and the cursor position.
8599 */
8600 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008601update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008603 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 update_topline();
8605 if (!curwin->w_p_wrap)
8606 validate_cursor();
8607 update_curswant();
8608}
8609
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008611 * Save the current State and go to Normal mode.
8612 * Return TRUE if the typeahead could be saved.
8613 */
8614 int
8615save_current_state(save_state_T *sst)
8616{
8617 sst->save_msg_scroll = msg_scroll;
8618 sst->save_restart_edit = restart_edit;
8619 sst->save_msg_didout = msg_didout;
8620 sst->save_State = State;
8621 sst->save_insertmode = p_im;
8622 sst->save_finish_op = finish_op;
8623 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008624 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008625 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008626
Bram Moolenaar4324d872020-12-01 20:12:24 +01008627 msg_scroll = FALSE; // no msg scrolling in Normal mode
8628 restart_edit = 0; // don't go to Insert mode
8629 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008630
Bram Moolenaara452b802020-12-01 21:47:59 +01008631 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008632 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008633
8634 /*
8635 * Save the current typeahead. This is required to allow using ":normal"
8636 * from an event handler and makes sure we don't hang when the argument
8637 * ends with half a command.
8638 */
8639 save_typeahead(&sst->tabuf);
8640 return sst->tabuf.typebuf_valid;
8641}
8642
8643 void
8644restore_current_state(save_state_T *sst)
8645{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008646 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008647 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008648
8649 msg_scroll = sst->save_msg_scroll;
8650 restart_edit = sst->save_restart_edit;
8651 p_im = sst->save_insertmode;
8652 finish_op = sst->save_finish_op;
8653 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008654 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008655 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008656 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008657 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008658
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008659 // Restore the state (needed when called from a function executed for
8660 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008661 State = sst->save_State;
8662#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008663 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008664#endif
8665}
8666
8667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 * ":normal[!] {commands}": Execute normal mode commands.
8669 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008670 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008671ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008673 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 char_u *arg = NULL;
8675 int l;
8676 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008678 if (ex_normal_lock > 0)
8679 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008680 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008681 return;
8682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 if (ex_normal_busy >= p_mmd)
8684 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008685 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 return;
8687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 /*
8690 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8691 * this for the K_SPECIAL leading byte, otherwise special keys will not
8692 * work.
8693 */
8694 if (has_mbyte)
8695 {
8696 int len = 0;
8697
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008698 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 for (p = eap->arg; *p != NUL; ++p)
8700 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008701#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008702 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008704#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008705 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008706 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008707#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 )
8711 len += 2;
8712 }
8713 if (len > 0)
8714 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008715 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 if (arg != NULL)
8717 {
8718 len = 0;
8719 for (p = eap->arg; *p != NUL; ++p)
8720 {
8721 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008722#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 if (*p == CSI)
8724 {
8725 arg[len++] = KS_EXTRA;
8726 arg[len++] = (int)KE_CSI;
8727 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008728#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008729 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 {
8731 arg[len++] = *++p;
8732 if (*p == K_SPECIAL)
8733 {
8734 arg[len++] = KS_SPECIAL;
8735 arg[len++] = KE_FILLER;
8736 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008737#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 else if (*p == CSI)
8739 {
8740 arg[len++] = KS_EXTRA;
8741 arg[len++] = (int)KE_CSI;
8742 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 }
8745 arg[len] = NUL;
8746 }
8747 }
8748 }
8749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008751 ++ex_normal_busy;
8752 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {
8754 /*
8755 * Repeat the :normal command for each line in the range. When no
8756 * range given, execute it just once, without positioning the cursor
8757 * first.
8758 */
8759 do
8760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 if (eap->addr_count != 0)
8762 {
8763 curwin->w_cursor.lnum = eap->line1++;
8764 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008765 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 }
8767
Bram Moolenaar13505972019-01-24 15:04:48 +01008768 exec_normal_cmd(arg != NULL
8769 ? arg
8770 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 }
8772 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8773 }
8774
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008775 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 update_topline_cursor();
8777
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008778 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008780 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008781#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008782 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008783#endif
8784
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786}
8787
8788/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008789 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 */
8791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008792ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008794 if (eap->forceit)
8795 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008796 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008797 if (!curwin->w_cursor.lnum)
8798 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008799 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008800 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008801#ifdef FEAT_TERMINAL
8802 // Ignore this when running in an active terminal.
8803 if (term_job_running(curbuf->b_term))
8804 return;
8805#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008806
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008807 // Ignore the command when already in Insert mode. Inserting an
8808 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01008809 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00008810 return;
8811
Bram Moolenaar64969662005-12-14 21:59:55 +00008812 if (eap->cmdidx == CMD_startinsert)
8813 restart_edit = 'a';
8814 else if (eap->cmdidx == CMD_startreplace)
8815 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008817 restart_edit = 'V';
8818
8819 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008821 if (eap->cmdidx == CMD_startinsert)
8822 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008823 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008825
8826 if (VIsual_active)
8827 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828}
8829
8830/*
8831 * ":stopinsert"
8832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008834ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
8836 restart_edit = 0;
8837 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008838 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008841/*
8842 * Execute normal mode command "cmd".
8843 * "remap" can be REMAP_NONE or REMAP_YES.
8844 */
8845 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008846exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008847{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008848 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008849 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008850 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008851}
Bram Moolenaar25281632016-01-21 23:32:32 +01008852
Bram Moolenaar25281632016-01-21 23:32:32 +01008853/*
8854 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008855 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008856 */
8857 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008858exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008859{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008860 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008861 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008862
Bram Moolenaar905dd902019-04-07 14:21:47 +02008863 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8864 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008865 clear_oparg(&oa);
8866 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008867 while ((!stuff_empty()
8868 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008869 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008870 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008871 {
8872 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008873#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008874 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008875 && oa.op_type == OP_NOP && oa.regname == NUL
8876 && !VIsual_active)
8877 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008878 // If terminal_loop() returns OK we got a key that is handled
8879 // in Normal model. With FAIL we first need to position the
8880 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008881 if (terminal_loop(TRUE) == OK)
8882 normal_cmd(&oa, TRUE);
8883 }
8884 else
8885#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008886 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008887 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008888 }
8889}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008890
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891#ifdef FEAT_FIND_ID
8892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008893ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894{
8895 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8896 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8897 (linenr_T)1, (linenr_T)MAXLNUM);
8898}
8899
Bram Moolenaar4033c552017-09-16 20:54:51 +02008900#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901/*
8902 * ":psearch"
8903 */
8904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008905ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
8907 g_do_tagpreview = p_pvh;
8908 ex_findpat(eap);
8909 g_do_tagpreview = 0;
8910}
8911#endif
8912
8913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008914ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915{
8916 int whole = TRUE;
8917 long n;
8918 char_u *p;
8919 int action;
8920
8921 switch (cmdnames[eap->cmdidx].cmd_name[2])
8922 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008923 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8925 action = ACTION_GOTO;
8926 else
8927 action = ACTION_SHOW;
8928 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008929 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 action = ACTION_SHOW_ALL;
8931 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008932 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 action = ACTION_GOTO;
8934 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008935 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 action = ACTION_SPLIT;
8937 break;
8938 }
8939
8940 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008941 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 {
8943 n = getdigits(&eap->arg);
8944 eap->arg = skipwhite(eap->arg);
8945 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008946 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 {
8948 whole = FALSE;
8949 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008950 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 if (*p)
8952 {
8953 *p++ = NUL;
8954 p = skipwhite(p);
8955
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008956 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008957 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008958 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008960 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 }
8962 }
8963 if (!eap->skip)
8964 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8965 whole, !eap->forceit,
8966 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8967 n, action, eap->line1, eap->line2);
8968}
8969#endif
8970
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971
Bram Moolenaar4033c552017-09-16 20:54:51 +02008972#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973/*
8974 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8975 */
8976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008977ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008979 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8981}
8982
8983/*
8984 * ":pedit"
8985 */
8986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008987ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
8989 win_T *curwin_save = curwin;
8990
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008991 if (ERROR_IF_ANY_POPUP_WINDOW)
8992 return;
8993
Bram Moolenaar026587b2019-08-17 15:08:00 +02008994 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008996 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008997
Bram Moolenaar026587b2019-08-17 15:08:00 +02008998 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009000
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 if (curwin != curwin_save && win_valid(curwin_save))
9002 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009003 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009005 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 win_enter(curwin_save, TRUE);
9007 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009008# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009009 else if (WIN_IS_POPUP(curwin))
9010 {
9011 // can't keep focus in popup window
9012 win_enter(firstwin, TRUE);
9013 }
9014# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 g_do_tagpreview = 0;
9016}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018
9019/*
9020 * ":stag", ":stselect" and ":stjump".
9021 */
9022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009023ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024{
9025 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009026 postponed_split_flags = cmdmod.cmod_split;
9027 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9029 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009030 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032
9033/*
9034 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9035 */
9036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009037ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
9039 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9040}
9041
9042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009043ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044{
9045 int cmd;
9046
9047 switch (name[1])
9048 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009049 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009051 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009053 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009055 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009057 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009059 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009061 case 'f': // ":tfirst"
9062 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009064 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009066 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009068 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009070 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 return;
9072 }
9073#endif
9074 cmd = DT_TAG;
9075 break;
9076 }
9077
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009078 if (name[0] == 'l')
9079 {
9080#ifndef FEAT_QUICKFIX
9081 ex_ni(eap);
9082 return;
9083#else
9084 cmd = DT_LTAG;
9085#endif
9086 }
9087
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9089 eap->forceit, TRUE);
9090}
9091
9092/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009093 * Check "str" for starting with a special cmdline variable.
9094 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9095 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9096 */
9097 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009098find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009099{
9100 int len;
9101 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009102 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009103 "%",
9104#define SPEC_PERC 0
9105 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009106#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009107 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02009108#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009109 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02009110#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009111 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02009112#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009113 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009114#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009115 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009116#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009117 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02009118#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02009119 "<stack>", // call stack
9120#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01009121 "<script>", // script file name
9122#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009123 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01009124#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009125 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009126#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009127 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009128#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009129 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009130#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02009131 "<SID>", // script ID: <SNR>123_
9132#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009133#ifdef FEAT_CLIENTSERVER
9134 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02009135# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009136#endif
9137 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009138
K.Takataeeec2542021-06-02 13:28:16 +02009139 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009140 {
9141 len = (int)STRLEN(spec_str[i]);
9142 if (STRNCMP(src, spec_str[i], len) == 0)
9143 {
9144 *usedlen = len;
9145 return i;
9146 }
9147 }
9148 return -1;
9149}
9150
9151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 * Evaluate cmdline variables.
9153 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009154 * change "%" to curbuf->b_ffname
9155 * "#" to curwin->w_alt_fnum
9156 * "%%" to curwin->w_alt_fnum in Vim9 script
9157 * "<cword>" to word under the cursor
9158 * "<cWORD>" to WORD under the cursor
9159 * "<cexpr>" to C-expression under the cursor
9160 * "<cfile>" to path name under the cursor
9161 * "<sfile>" to sourced file name
9162 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009163 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009164 * "<slnum>" to sourced file line number
9165 * "<afile>" to file name for autocommand
9166 * "<abuf>" to buffer number for autocommand
9167 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 *
9169 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9170 * "" for error without a message) and NULL is returned.
9171 * Returns an allocated string if a valid match was found.
9172 * Returns NULL if no match was found. "usedlen" then still contains the
9173 * number of characters to skip.
9174 */
9175 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009176eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009177 char_u *src, // pointer into commandline
9178 char_u *srcstart, // beginning of valid memory for src
9179 int *usedlen, // characters after src that are used
9180 linenr_T *lnump, // line number for :e command, or NULL
9181 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009182 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009183 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009184 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185{
9186 int i;
9187 char_u *s;
9188 char_u *result;
9189 char_u *resultbuf = NULL;
9190 int resultlen;
9191 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009192 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009194 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197
9198 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009199 if (escaped != NULL)
9200 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
9202 /*
9203 * Check if there is something to do.
9204 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009205 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009206 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 {
9208 *usedlen = 1;
9209 return NULL;
9210 }
9211
9212 /*
9213 * Skip when preceded with a backslash "\%" and "\#".
9214 * Note: In "\\%" the % is also not recognized!
9215 */
9216 if (src > srcstart && src[-1] == '\\')
9217 {
9218 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009219 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 return NULL;
9221 }
9222
9223 /*
9224 * word or WORD under cursor
9225 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009226 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9227 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009229 resultlen = find_ident_under_cursor(&result,
9230 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9231 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9232 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 if (resultlen == 0)
9234 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009235 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 return NULL;
9237 }
9238 }
9239
9240 /*
9241 * '#': Alternate file name
9242 * '%': Current file name
9243 * File name under the cursor
9244 * File name for autocommand
9245 * and following modifiers
9246 */
9247 else
9248 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009249 int off = 0;
9250
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 switch (spec_idx)
9252 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009253 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009254#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009255 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009258 // '%': current file
9259 if (curbuf->b_fname == NULL)
9260 {
9261 result = (char_u *)"";
9262 valid = 0; // Must have ":p:h" to be valid
9263 }
9264 else
9265 {
9266 result = curbuf->b_fname;
9267 tilde_file = STRCMP(result, "~") == 0;
9268 }
9269 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009271#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009272 // "%%" alternate file
9273 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009274#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009275 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009276 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009277 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009279 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 result = arg_all();
9281 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009282 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009283 if (escaped != NULL)
9284 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 break;
9287 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009288 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009289 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009290 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009292 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009293 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009294 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009295 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009297 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009299 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009300 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009301 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009302 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009303 return NULL;
9304 }
9305#ifdef FEAT_EVAL
9306 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9307 (long)i);
9308 if (result == NULL)
9309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009310 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009311 return NULL;
9312 }
9313#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009314 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 }
9318 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009319 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009320 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9321 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009322 buf = buflist_findnr(i);
9323 if (buf == NULL)
9324 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009325 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009326 return NULL;
9327 }
9328 if (lnump != NULL)
9329 *lnump = ECMD_LAST;
9330 if (buf->b_fname == NULL)
9331 {
9332 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009333 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009334 }
9335 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009336 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009337 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009338 tilde_file = STRCMP(result, "~") == 0;
9339 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 break;
9342
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009343 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009344 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 if (result == NULL)
9346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009347 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 return NULL;
9349 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009350 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009353 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009355 if (result != NULL && !autocmd_fname_full)
9356 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009357 // Still need to turn the fname into a full path. It is
9358 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009359 autocmd_fname_full = TRUE;
9360 result = FullName_save(autocmd_fname, FALSE);
9361 vim_free(autocmd_fname);
9362 autocmd_fname = result;
9363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 if (result == NULL)
9365 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009366 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 return NULL;
9368 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009369 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 break;
9371
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009372 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 if (autocmd_bufnr <= 0)
9374 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009375 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 return NULL;
9377 }
9378 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9379 result = strbuf;
9380 break;
9381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009382 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 result = autocmd_match;
9384 if (result == NULL)
9385 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009386 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 return NULL;
9388 }
9389 break;
9390
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009391 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009392 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 if (result == NULL)
9394 {
LemonBoy6013d002022-04-09 21:42:10 +01009395 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9396 return NULL;
9397 }
9398 resultbuf = result; // remember allocated string
9399 break;
9400 case SPEC_STACK: // call stack
9401 result = estack_sfile(ESTACK_STACK);
9402 if (result == NULL)
9403 {
9404 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9405 return NULL;
9406 }
9407 resultbuf = result; // remember allocated string
9408 break;
9409 case SPEC_SCRIPT: // script file name
9410 result = estack_sfile(ESTACK_SCRIPT);
9411 if (result == NULL)
9412 {
9413 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 return NULL;
9415 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009416 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009418
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009419 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009420 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009421 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009422 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009423 return NULL;
9424 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009425 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009426 result = strbuf;
9427 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009428
9429#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009430 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009431 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009432 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009433 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009434 return NULL;
9435 }
9436 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009437 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009438 result = strbuf;
9439 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009440
Bram Moolenaar90944302020-08-01 20:45:11 +02009441 case SPEC_SID:
9442 if (current_sctx.sc_sid <= 0)
9443 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009444 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009445 return NULL;
9446 }
9447 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9448 result = strbuf;
9449 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009450#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009451
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009452#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009453 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009454 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9455 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 result = strbuf;
9457 break;
9458#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009459
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009460 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009461 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009462 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 }
9464
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009465 resultlen = (int)STRLEN(result); // length of new string
9466 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 {
9468 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 resultlen = (int)(s - result);
9471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 else if (!skip_mod)
9473 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009474 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 &resultlen);
9476 if (result == NULL)
9477 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009478 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 return NULL;
9480 }
9481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 }
9483
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009484 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009486 if (empty_is_error)
9487 {
9488 if (valid != VALID_HEAD + VALID_PATH)
9489 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9490 else
9491 *errormsg = _(e_evaluates_to_an_empty_string);
9492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 result = NULL;
9494 }
9495 else
9496 result = vim_strnsave(result, resultlen);
9497 vim_free(resultbuf);
9498 return result;
9499}
9500
9501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 * Expand the <sfile> string in "arg".
9503 *
9504 * Returns an allocated string, or NULL for any error.
9505 */
9506 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009507expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009509 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 int len;
9511 char_u *result;
9512 char_u *newres;
9513 char_u *repl;
9514 int srclen;
9515 char_u *p;
9516
9517 result = vim_strsave(arg);
9518 if (result == NULL)
9519 return NULL;
9520
9521 for (p = result; *p; )
9522 {
9523 if (STRNCMP(p, "<sfile>", 7) != 0)
9524 ++p;
9525 else
9526 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009527 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009528 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 if (errormsg != NULL)
9530 {
9531 if (*errormsg)
9532 emsg(errormsg);
9533 vim_free(result);
9534 return NULL;
9535 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009536 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 {
9538 p += srclen;
9539 continue;
9540 }
9541 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9542 newres = alloc(len);
9543 if (newres == NULL)
9544 {
9545 vim_free(repl);
9546 vim_free(result);
9547 return NULL;
9548 }
9549 mch_memmove(newres, result, (size_t)(p - result));
9550 STRCPY(newres + (p - result), repl);
9551 len = (int)STRLEN(newres);
9552 STRCAT(newres, p + srclen);
9553 vim_free(repl);
9554 vim_free(result);
9555 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009556 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 }
9558 }
9559
9560 return result;
9561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009564/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009565 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009566 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009569dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 if (fname == NULL)
9572 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009573 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574}
9575#endif
9576
9577/*
9578 * ":behave {mswin,xterm}"
9579 */
9580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009581ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582{
9583 if (STRCMP(eap->arg, "mswin") == 0)
9584 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009585 set_option_value_give_err((char_u *)"selection",
9586 0L, (char_u *)"exclusive", 0);
9587 set_option_value_give_err((char_u *)"selectmode",
9588 0L, (char_u *)"mouse,key", 0);
9589 set_option_value_give_err((char_u *)"mousemodel",
9590 0L, (char_u *)"popup", 0);
9591 set_option_value_give_err((char_u *)"keymodel",
9592 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 }
9594 else if (STRCMP(eap->arg, "xterm") == 0)
9595 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009596 set_option_value_give_err((char_u *)"selection",
9597 0L, (char_u *)"inclusive", 0);
9598 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9599 set_option_value_give_err((char_u *)"mousemodel",
9600 0L, (char_u *)"extend", 0);
9601 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 }
9603 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009604 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605}
9606
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607static int filetype_detect = FALSE;
9608static int filetype_plugin = FALSE;
9609static int filetype_indent = FALSE;
9610
9611/*
9612 * ":filetype [plugin] [indent] {on,off,detect}"
9613 * on: Load the filetype.vim file to install autocommands for file types.
9614 * off: Load the ftoff.vim file to remove all autocommands for file types.
9615 * plugin on: load filetype.vim and ftplugin.vim
9616 * plugin off: load ftplugof.vim
9617 * indent on: load filetype.vim and indent.vim
9618 * indent off: load indoff.vim
9619 */
9620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009621ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622{
9623 char_u *arg = eap->arg;
9624 int plugin = FALSE;
9625 int indent = FALSE;
9626
9627 if (*eap->arg == NUL)
9628 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009629 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009630 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 filetype_detect ? "ON" : "OFF",
9632 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9633 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9634 return;
9635 }
9636
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009637 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 for (;;)
9639 {
9640 if (STRNCMP(arg, "plugin", 6) == 0)
9641 {
9642 plugin = TRUE;
9643 arg = skipwhite(arg + 6);
9644 continue;
9645 }
9646 if (STRNCMP(arg, "indent", 6) == 0)
9647 {
9648 indent = TRUE;
9649 arg = skipwhite(arg + 6);
9650 continue;
9651 }
9652 break;
9653 }
9654 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9655 {
9656 if (*arg == 'o' || !filetype_detect)
9657 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009658 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 filetype_detect = TRUE;
9660 if (plugin)
9661 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009662 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 filetype_plugin = TRUE;
9664 }
9665 if (indent)
9666 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009667 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 filetype_indent = TRUE;
9669 }
9670 }
9671 if (*arg == 'd')
9672 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009673 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009674 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 }
9676 }
9677 else if (STRCMP(arg, "off") == 0)
9678 {
9679 if (plugin || indent)
9680 {
9681 if (plugin)
9682 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009683 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 filetype_plugin = FALSE;
9685 }
9686 if (indent)
9687 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009688 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 filetype_indent = FALSE;
9690 }
9691 }
9692 else
9693 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009694 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 filetype_detect = FALSE;
9696 }
9697 }
9698 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009699 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700}
9701
9702/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009703 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 */
9705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009706ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707{
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009708 if (did_filetype)
9709 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009710
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009711 char_u *arg = eap->arg;
9712 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9713 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009714
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009715 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9716 if (arg != eap->arg)
9717 did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009721ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722{
9723#ifdef FEAT_DIGRAPHS
9724 if (*eap->arg != NUL)
9725 putdigraph(eap->arg);
9726 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009727 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728#else
mityu61065042021-07-19 20:07:21 +02009729 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730#endif
9731}
9732
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009733#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9734 void
9735set_no_hlsearch(int flag)
9736{
9737 no_hlsearch = flag;
9738# ifdef FEAT_EVAL
9739 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9740# endif
9741}
9742
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743/*
9744 * ":nohlsearch"
9745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009747ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009749 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009750 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752#endif
9753
9754#ifdef FEAT_CRYPT
9755/*
9756 * ":X": Get crypt key
9757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009759ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009761 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009762 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763}
9764#endif
9765
9766#ifdef FEAT_FOLDING
9767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009768ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769{
9770 if (foldManualAllowed(TRUE))
9771 foldCreate(eap->line1, eap->line2);
9772}
9773
9774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009775ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776{
9777 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9778 eap->forceit, FALSE);
9779}
9780
9781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009782ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
9784 linenr_T lnum;
9785
Bram Moolenaar4facea32019-10-12 20:17:40 +02009786# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009787 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009788# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009789
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009790 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9792 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9793 ml_setmarked(lnum);
9794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009795 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009797 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009798# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009799 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801}
9802#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009804#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009805/*
9806 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9807 * instead of a quickfix command.
9808 */
9809 int
9810is_loclist_cmd(int cmdidx)
9811{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009812 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009813 return FALSE;
9814 return cmdnames[cmdidx].cmd_name[0] == 'l';
9815}
9816#endif
9817
Bram Moolenaar4facea32019-10-12 20:17:40 +02009818#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009819 int
9820get_pressedreturn(void)
9821{
9822 return ex_pressedreturn;
9823}
9824
9825 void
9826set_pressedreturn(int val)
9827{
9828 ex_pressedreturn = val;
9829}
9830#endif