blob: 900a0c1088663ab818d2414902a5790d7da795ad [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
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001389handle_did_throw(void)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001390{
1391 char *p = NULL;
1392 msglist_T *messages = NULL;
ichizok7e5fe382023-04-15 13:17:50 +01001393 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);
ichizok7e5fe382023-04-15 13:17:50 +01001419 ESTACK_CHECK_SETUP;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001420 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);
ichizok7e5fe382023-04-15 13:17:50 +01001449 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001450 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
Dominique Pellee764d1b2023-03-12 21:20:59 +00003572#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003573/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003574 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3575 * call.
3576 */
3577 int
3578number_method(char_u *cmd)
3579{
3580 char_u *p = skipdigits(cmd);
3581
3582 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3583}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003584#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003585
3586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003588 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003589 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003590 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 *
3592 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3593 * assignment without "let". Sets eap->cmdidx to the command while returning
3594 * "eap->cmd".
3595 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 * Returns NULL for an ambiguous user command.
3597 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 char_u *
3599find_ex_command(
3600 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003601 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003602 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003603 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
3605 int len;
3606 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003607 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003608#ifndef FEAT_EVAL
3609 int vim9 = FALSE;
3610#else
3611 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 /*
3614 * Recognize a Vim9 script function/method call and assignment:
3615 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3616 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003617 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003618 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003620 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003622 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003623 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3624 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003626 int oplen;
3627 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003628 char_u *swp;
3629
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003630 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003631 || (eap->cmd[0] == '$'
3632 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003633 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003634 && (valid_yank_reg(eap->cmd[1], FALSE)
3635 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003636 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003637 if (*eap->cmd == '&')
3638 {
3639 p = eap->cmd + 1;
3640 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3641 p += 2;
3642 p = to_name_end(p, FALSE);
3643 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003644 else if (*eap->cmd == '$')
3645 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003646 else
3647 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003648 if (ends_excmd(*skipwhite(p)))
3649 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003650 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3651 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003652 eap->cmdidx = CMD_eval;
3653 return eap->cmd;
3654 }
3655 // "&option" can be followed by "->" or "=", check below
3656 }
3657
3658 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003660 if (
3661 // "(..." is an expression.
3662 // "funcname(" is always a function call.
3663 *p == '('
3664 || (p == eap->cmd
3665 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003666 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003667 *eap->cmd == '{'
3668 // "'string'->func()" is an expression.
3669 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003670 // '"string"->func()' is an expression.
3671 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003672 // '$"string"->func()' is an expression.
3673 // "$'string'->func()" is an expression.
3674 || (eap->cmd[0] == '$'
3675 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3676 // '0z1234->func()' is an expression.
3677 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003678 // "g:varname" is an expression.
3679 || eap->cmd[1] == ':'
3680 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003681 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003682 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003683 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003684 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3685 {
3686 // "{" by itself is the start of a block.
3687 eap->cmdidx = CMD_block;
3688 return eap->cmd + 1;
3689 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003690 eap->cmdidx = CMD_eval;
3691 return eap->cmd;
3692 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003693
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003694 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003695 // "varname[]" is an expression.
3696 *p == '['
3697 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003698 || (*p == '.'
3699 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3700 // g:[key] is an expression
3701 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003702 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003703 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003704
3705 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003706 // Skip over the whole thing, it can be:
3707 // name.member = val
3708 // name[a : b] = val
3709 // name[idx] = val
3710 // name[idx].member = val
3711 // etc.
3712 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003713 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003714 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003715 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003716 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003717 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003718 || (after[0] == '.' && after[1] == '.'
3719 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003720 eap->cmdidx = CMD_var;
3721 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003722 --emsg_silent;
3723 return eap->cmd;
3724 }
3725
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003726 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3727 // an assignment.
3728 // If there is no line break inside the "[...]" then "p" is
3729 // advanced to after the "]" by to_name_const_end(): check if a "="
3730 // follows.
3731 // If "[...]" has a line break "p" still points at the "[" and it
3732 // can't be an assignment.
3733 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003734 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003735 char_u *eq;
3736
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003737 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003738 if (p == eap->cmd && *p == '[')
3739 {
3740 int count = 0;
3741 int semicolon = FALSE;
3742
3743 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3744 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003745 eq = p;
3746 if (eq != NULL)
3747 {
3748 eq = skipwhite(eq);
3749 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3750 ++eq;
3751 }
3752 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003753 {
3754 eap->cmdidx = CMD_eval;
3755 return eap->cmd;
3756 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003757 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003758 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003759 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003760 return eap->cmd;
3761 }
3762 }
3763
3764 // Recognize an assignment if we recognize the variable name:
3765 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003766 // "@r = expr"
3767 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003768 // "var = expr" where "var" is a variable name or we are skipping
3769 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003770 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003771 oplen = assignment_len(skipwhite(p), &heredoc);
3772 if (oplen > 0)
3773 {
3774 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3775 || *eap->cmd == '&'
3776 || *eap->cmd == '$'
3777 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003778 || (eap->skip && IS_WHITE_OR_NUL(
3779 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003780 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003781 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003782 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003783 return eap->cmd;
3784 }
3785 }
3786
Bram Moolenaar9510d222022-09-11 15:14:05 +01003787 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003788 // "w:varname: number = 123".
3789 if (eap->cmd[1] == ':' && *p == ':')
3790 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003791 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003792 return eap->cmd;
3793 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003794 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003795
Bram Moolenaar31d99482022-05-26 22:24:43 +01003796 // 1234->func() is a method call
3797 if (number_method(eap->cmd))
3798 {
3799 eap->cmdidx = CMD_eval;
3800 return eap->cmd;
3801 }
3802
Bram Moolenaar7b829262021-10-13 15:04:34 +01003803 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3804 // an expression. A global/substitute/list command needs to use a
3805 // longer name.
3806 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3807 {
3808 eap->cmdidx = CMD_eval;
3809 return eap->cmd;
3810 }
3811
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003812 // If it is an ID it might be a variable with an operator on the next
3813 // line, if the variable exists it can't be an Ex command.
3814 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003815 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003816 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3817 {
3818 eap->cmdidx = CMD_eval;
3819 return eap->cmd;
3820 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003821
3822 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003823 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3824 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003825 {
3826 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3827 return eap->cmd + 2;
3828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 }
3830#endif
3831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 /*
3833 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 */
3835 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003836 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 ++p;
3839 }
3840 else
3841 {
3842 while (ASCII_ISALPHA(*p))
3843 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003844 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003845 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003847 while (ASCII_ISALNUM(*p))
3848 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 }
3850 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3851 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003852 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 ++p;
3854 while (ASCII_ISALPHA(*p))
3855 ++p;
3856 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003857
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003858 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003859 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 ++p;
3861 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003862 // The "d" command can directly be followed by 'l' or 'p' flag, when
3863 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003864 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003865 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003866 // Check for ":dl", ":dell", etc. to ":deletel": that's
3867 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003868 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003869 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003870 break;
3871 if (i == len - 1)
3872 {
3873 --len;
3874 if (p[-1] == 'l')
3875 eap->flags |= EXFLAG_LIST;
3876 else
3877 eap->flags |= EXFLAG_PRINT;
3878 }
3879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003881 if (ASCII_ISLOWER(eap->cmd[0]))
3882 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003883 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003884 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003885
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003886 if (command_count != (int)CMD_SIZE)
3887 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003888 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003889 getout(1);
3890 }
3891
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003892 // Use a precomputed index for fast look-up in cmdnames[]
3893 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003894 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3895 if (ASCII_ISLOWER(c2))
3896 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3897 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003898 else if (ASCII_ISUPPER(eap->cmd[0]))
3899 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003901 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902
3903 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3904 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3905 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3906 (size_t)len) == 0)
3907 {
3908#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003909 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 *full = TRUE;
3911#endif
3912 break;
3913 }
3914
Bram Moolenaar204852a2022-03-05 12:56:44 +00003915 // :Print and :mode are not supported in Vim9 script.
3916 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003917 if (vim9 && eap->cmdidx != CMD_SIZE)
3918 {
3919 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3920 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003921 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003922 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003923 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003924 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003925 eap->cmdidx = CMD_SIZE;
3926 }
3927 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003928
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003929 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003930 // 'cpoptions'.
3931 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3932 p = eap->cmd;
3933
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003934 // Look for a user defined command as a last resort. Let ":Print" be
3935 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003936 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3937 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003939 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 while (ASCII_ISALNUM(*p))
3941 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003942 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003944 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 eap->cmdidx = CMD_SIZE;
3946 }
3947
Bram Moolenaar204852a2022-03-05 12:56:44 +00003948 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3949 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003950 eap->cmdidx = CMD_finally;
3951
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003952#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003953 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003954 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003955 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003956 && (eap->cmdidx < 0 ||
3957 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003958 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003959 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3960
3961 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003962 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003963 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003964 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003965#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003966
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 return p;
3968}
3969
3970#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003971static struct cmdmod
3972{
3973 char *name;
3974 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003975 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003976} cmdmods[] = {
3977 {"aboveleft", 3, FALSE},
3978 {"belowright", 3, FALSE},
3979 {"botright", 2, FALSE},
3980 {"browse", 3, FALSE},
3981 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003982 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003983 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003984 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003985 {"keepalt", 5, FALSE},
3986 {"keepjumps", 5, FALSE},
3987 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003988 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003989 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003990 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003991 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003992 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003993 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003994 {"rightbelow", 6, FALSE},
3995 {"sandbox", 3, FALSE},
3996 {"silent", 3, FALSE},
3997 {"tab", 3, TRUE},
3998 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003999 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004000 {"verbose", 4, TRUE},
4001 {"vertical", 4, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004002 {"vim9cmd", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004003};
4004
4005/*
4006 * Return length of a command modifier (including optional count).
4007 * Return zero when it's not a modifier.
4008 */
4009 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004010modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004011{
4012 int i, j;
4013 char_u *p = cmd;
4014
4015 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004016 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02004017 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004018 {
4019 for (j = 0; p[j] != NUL; ++j)
4020 if (p[j] != cmdmods[i].name[j])
4021 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02004022 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00004023 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00004024 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004025 }
4026 return 0;
4027}
4028
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029/*
4030 * Return > 0 if an Ex command "name" exists.
4031 * Return 2 if there is an exact match.
4032 * Return 3 if there is an ambiguous match.
4033 */
4034 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004035cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036{
4037 exarg_T ea;
4038 int full = FALSE;
4039 int i;
4040 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004041 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004043 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02004044 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 {
4046 for (j = 0; name[j] != NUL; ++j)
4047 if (name[j] != cmdmods[i].name[j])
4048 break;
4049 if (name[j] == NUL && j >= cmdmods[i].minlen)
4050 return (cmdmods[i].name[j] == NUL ? 2 : 1);
4051 }
4052
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004053 // Check built-in commands and user defined commands.
4054 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004055 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004057 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004059 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004061 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4062 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004063 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004064 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4066}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004067
4068/*
4069 * "fullcommand" function
4070 */
4071 void
4072f_fullcommand(typval_T *argvars, typval_T *rettv)
4073{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004074 exarg_T ea;
4075 char_u *name;
4076 char_u *p;
4077 int vim9script = in_vim9script();
4078 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004079
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004080 rettv->v_type = VAR_STRING;
4081 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004082
Bram Moolenaaraa534142022-09-15 21:46:02 +01004083 if (in_vim9script()
4084 && (check_for_string_arg(argvars, 0) == FAIL
4085 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004086 return;
4087
4088 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004089 if (name == NULL)
4090 return;
4091
Bram Moolenaaraa534142022-09-15 21:46:02 +01004092 if (argvars[1].v_type != VAR_UNKNOWN)
4093 {
4094 vim9script = tv_get_bool(&argvars[1]);
4095 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4096 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4097 }
4098
zeertzjqc024ed92022-01-04 16:22:52 +00004099 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004100 name++;
4101 name = skip_range(name, TRUE, NULL);
4102
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004103 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4104 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004105 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004106 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004107 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004108 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004109 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004110 goto theend;
4111
4112 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004113 {
4114 int res;
4115
4116 ++emsg_silent;
4117 res = not_in_vim9(&ea);
4118 --emsg_silent;
4119
4120 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004121 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004122 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004123
4124 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004125 ? get_user_command_name(ea.useridx, ea.cmdidx)
4126 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004127theend:
4128 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004129}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130#endif
4131
Bram Moolenaard0190392019-08-23 21:17:35 +02004132 cmdidx_T
4133excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
Bram Moolenaard0190392019-08-23 21:17:35 +02004135 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004137 if (!one_letter_cmd(cmd, &idx))
4138 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4139 idx = (cmdidx_T)((int)idx + 1))
4140 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4141 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
Bram Moolenaard0190392019-08-23 21:17:35 +02004143 return idx;
4144}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
Bram Moolenaard0190392019-08-23 21:17:35 +02004146 long
4147excmd_get_argt(cmdidx_T idx)
4148{
4149 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150}
4151
4152/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004153 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 *
4155 * Backslashed delimiters after / or ? will be skipped, and commands will
4156 * not be expanded between /'s and ?'s or after "'".
4157 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004158 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 * Returns the "cmd" pointer advanced to beyond the range.
4160 */
4161 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004162skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004163 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004164 int skip_star, // skip "*" used for Visual range
4165 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004167 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004168 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004170 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004172 if (*cmd == '\\')
4173 {
4174 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4175 ++cmd;
4176 else
4177 break;
4178 }
4179 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004181 char_u *p = cmd;
4182
4183 // a quote is only valid at the start or after a separator
4184 while (p > cmd_start)
4185 {
4186 --p;
4187 if (!VIM_ISWHITE(*p))
4188 break;
4189 }
4190 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4191 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (*++cmd == NUL && ctx != NULL)
4193 *ctx = EXPAND_NOTHING;
4194 }
4195 else if (*cmd == '/' || *cmd == '?')
4196 {
4197 delim = *cmd++;
4198 while (*cmd != NUL && *cmd != delim)
4199 if (*cmd++ == '\\' && *cmd != NUL)
4200 ++cmd;
4201 if (*cmd == NUL && ctx != NULL)
4202 *ctx = EXPAND_NOTHING;
4203 }
4204 if (*cmd != NUL)
4205 ++cmd;
4206 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004207
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004208 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004209 while (*cmd == ':')
4210 cmd = skipwhite(cmd + 1);
4211
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004212 // Skip "*" used for Visual range.
4213 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4214 cmd = skipwhite(cmd + 1);
4215
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return cmd;
4217}
4218
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004219 static void
4220addr_error(cmd_addr_T addr_type)
4221{
4222 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004223 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004224 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004225 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004226}
4227
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004229 * Return the default address for an address type.
4230 */
4231 static linenr_T
4232default_address(exarg_T *eap)
4233{
4234 linenr_T lnum = 0;
4235
4236 switch (eap->addr_type)
4237 {
4238 case ADDR_LINES:
4239 case ADDR_OTHER:
4240 // Default is the cursor line number. Avoid using an invalid
4241 // line number though.
4242 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4243 lnum = curbuf->b_ml.ml_line_count;
4244 else
4245 lnum = curwin->w_cursor.lnum;
4246 break;
4247 case ADDR_WINDOWS:
4248 lnum = CURRENT_WIN_NR;
4249 break;
4250 case ADDR_ARGUMENTS:
4251 lnum = curwin->w_arg_idx + 1;
4252 if (lnum > ARGCOUNT)
4253 lnum = ARGCOUNT;
4254 break;
4255 case ADDR_LOADED_BUFFERS:
4256 case ADDR_BUFFERS:
4257 lnum = curbuf->b_fnum;
4258 break;
4259 case ADDR_TABS:
4260 lnum = CURRENT_TAB_NR;
4261 break;
4262 case ADDR_TABS_RELATIVE:
4263 case ADDR_UNSIGNED:
4264 lnum = 1;
4265 break;
4266 case ADDR_QUICKFIX:
4267#ifdef FEAT_QUICKFIX
4268 lnum = qf_get_cur_idx(eap);
4269#endif
4270 break;
4271 case ADDR_QUICKFIX_VALID:
4272#ifdef FEAT_QUICKFIX
4273 lnum = qf_get_cur_valid_idx(eap);
4274#endif
4275 break;
4276 case ADDR_NONE:
4277 // Will give an error later if a range is found.
4278 break;
4279 }
4280 return lnum;
4281}
4282
4283/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004284 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 *
4286 * Set ptr to the next character after the part that was interpreted.
4287 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004288 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 *
4290 * Return MAXLNUM when no Ex address was found.
4291 */
4292 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004293get_address(
4294 exarg_T *eap UNUSED,
4295 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004296 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004297 int skip, // only skip the address, don't use it
4298 int silent, // no errors or side effects
4299 int to_other_file, // flag: may jump to other file
4300 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301{
4302 int c;
4303 int i;
4304 long n;
4305 char_u *cmd;
4306 pos_T pos;
4307 pos_T *fp;
4308 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004309 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 cmd = skipwhite(*ptr);
4312 lnum = MAXLNUM;
4313 do
4314 {
4315 switch (*cmd)
4316 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004317 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004318 ++cmd;
4319 switch (addr_type)
4320 {
4321 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004322 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 lnum = curwin->w_cursor.lnum;
4324 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004325 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004326 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004327 break;
4328 case ADDR_ARGUMENTS:
4329 lnum = curwin->w_arg_idx + 1;
4330 break;
4331 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004332 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004333 lnum = curbuf->b_fnum;
4334 break;
4335 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004336 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004337 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004338 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004339 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004340 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004341 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004342 cmd = NULL;
4343 goto error;
4344 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004345 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004346#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004347 lnum = qf_get_cur_idx(eap);
4348#endif
4349 break;
4350 case ADDR_QUICKFIX_VALID:
4351#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004352 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004353#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004354 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004355 }
4356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004358 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004359 ++cmd;
4360 switch (addr_type)
4361 {
4362 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004363 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 lnum = curbuf->b_ml.ml_line_count;
4365 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004366 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004367 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004368 break;
4369 case ADDR_ARGUMENTS:
4370 lnum = ARGCOUNT;
4371 break;
4372 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004373 buf = lastbuf;
4374 while (buf->b_ml.ml_mfp == NULL)
4375 {
4376 if (buf->b_prev == NULL)
4377 break;
4378 buf = buf->b_prev;
4379 }
4380 lnum = buf->b_fnum;
4381 break;
4382 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004383 lnum = lastbuf->b_fnum;
4384 break;
4385 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004386 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004387 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004388 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004389 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004390 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004391 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004392 cmd = NULL;
4393 goto error;
4394 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004395 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004396#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004397 lnum = qf_get_size(eap);
4398 if (lnum == 0)
4399 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004400#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004401 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004402 case ADDR_QUICKFIX_VALID:
4403#ifdef FEAT_QUICKFIX
4404 lnum = qf_get_valid_size(eap);
4405 if (lnum == 0)
4406 lnum = 1;
4407#endif
4408 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004409 }
4410 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004412 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004413 if (*++cmd == NUL)
4414 {
4415 cmd = NULL;
4416 goto error;
4417 }
4418 if (addr_type != ADDR_LINES)
4419 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004420 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004421 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004422 goto error;
4423 }
4424 if (skip)
4425 ++cmd;
4426 else
4427 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004428 // Only accept a mark in another file when it is
4429 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004430 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4431 ++cmd;
4432 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004433 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004434 lnum = curwin->w_cursor.lnum;
4435 else
4436 {
4437 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
4439 cmd = NULL;
4440 goto error;
4441 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 lnum = fp->lnum;
4443 }
4444 }
4445 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004448 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 c = *cmd++;
4450 if (addr_type != ADDR_LINES)
4451 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004452 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004453 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004454 goto error;
4455 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004456 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004458 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 if (*cmd == c)
4460 ++cmd;
4461 }
4462 else
4463 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004464 int flags;
4465
4466 pos = curwin->w_cursor; // save curwin->w_cursor
4467
4468 // When '/' or '?' follows another address, start from
4469 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004470 if (lnum > 0 && lnum != MAXLNUM)
4471 curwin->w_cursor.lnum =
4472 lnum > curbuf->b_ml.ml_line_count
4473 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004474
4475 // Start a forward search at the end of the line (unless
4476 // before the first line).
4477 // Start a backward search at the start of the line.
4478 // This makes sure we never match in the current
4479 // line, and can match anywhere in the
4480 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004481 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 curwin->w_cursor.col = MAXCOL;
4483 else
4484 curwin->w_cursor.col = 0;
4485 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004486 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004487 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004488 {
4489 curwin->w_cursor = pos;
4490 cmd = NULL;
4491 goto error;
4492 }
4493 lnum = curwin->w_cursor.lnum;
4494 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004495 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004496 cmd += searchcmdlen;
4497 }
4498 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004500 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004501 ++cmd;
4502 if (addr_type != ADDR_LINES)
4503 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004504 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004505 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004506 goto error;
4507 }
4508 if (*cmd == '&')
4509 i = RE_SUBST;
4510 else if (*cmd == '?' || *cmd == '/')
4511 i = RE_SEARCH;
4512 else
4513 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004514 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004515 cmd = NULL;
4516 goto error;
4517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004519 if (!skip)
4520 {
4521 /*
4522 * When search follows another address, start from
4523 * there.
4524 */
4525 if (lnum != MAXLNUM)
4526 pos.lnum = lnum;
4527 else
4528 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004530 /*
4531 * Start the search just like for the above
4532 * do_search().
4533 */
4534 if (*cmd != '?')
4535 pos.col = MAXCOL;
4536 else
4537 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004538 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004539 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004540 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004541 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004542 lnum = pos.lnum;
4543 else
4544 {
4545 cmd = NULL;
4546 goto error;
4547 }
4548 }
4549 ++cmd;
4550 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004553 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004554 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 }
4556
4557 for (;;)
4558 {
4559 cmd = skipwhite(cmd);
4560 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4561 break;
4562
4563 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004564 {
4565 switch (addr_type)
4566 {
4567 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004568 case ADDR_OTHER:
4569 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004570 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004571 break;
4572 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004573 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004574 break;
4575 case ADDR_ARGUMENTS:
4576 lnum = curwin->w_arg_idx + 1;
4577 break;
4578 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004579 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004580 lnum = curbuf->b_fnum;
4581 break;
4582 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004583 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004584 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004585 case ADDR_TABS_RELATIVE:
4586 lnum = 1;
4587 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004588 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004589#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004590 lnum = qf_get_cur_idx(eap);
4591#endif
4592 break;
4593 case ADDR_QUICKFIX_VALID:
4594#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004595 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004596#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004597 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004598 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004599 case ADDR_UNSIGNED:
4600 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004601 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004602 }
4603 }
4604
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004606 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 else
4608 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004609 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 n = 1;
4611 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004612 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004613 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004615 if (n == MAXLNUM)
4616 {
4617 emsg(_(e_line_number_out_of_range));
4618 goto error;
4619 }
4620 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004621
4622 if (addr_type == ADDR_TABS_RELATIVE)
4623 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004624 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004625 cmd = NULL;
4626 goto error;
4627 }
4628 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004629 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004630 lnum = compute_buffer_local_count(
4631 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004633 {
4634#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004635 // Relative line addressing: need to adjust for lines in a
4636 // closed fold after the first address.
4637 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4638 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004639 (void)hasFolding(lnum, NULL, &lnum);
4640#endif
4641 if (i == '-')
4642 lnum -= n;
4643 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004644 {
4645 if (n >= LONG_MAX - lnum)
4646 {
4647 emsg(_(e_line_number_out_of_range));
4648 goto error;
4649 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004650 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004651 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
4654 } while (*cmd == '/' || *cmd == '?');
4655
4656error:
4657 *ptr = cmd;
4658 return lnum;
4659}
4660
4661/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004662 * Set eap->line1 and eap->line2 to the whole range.
4663 * Used for commands with the EX_DFLALL flag and no range given.
4664 */
4665 static void
4666address_default_all(exarg_T *eap)
4667{
4668 eap->line1 = 1;
4669 switch (eap->addr_type)
4670 {
4671 case ADDR_LINES:
4672 case ADDR_OTHER:
4673 eap->line2 = curbuf->b_ml.ml_line_count;
4674 break;
4675 case ADDR_LOADED_BUFFERS:
4676 {
4677 buf_T *buf = firstbuf;
4678
4679 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4680 buf = buf->b_next;
4681 eap->line1 = buf->b_fnum;
4682 buf = lastbuf;
4683 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4684 buf = buf->b_prev;
4685 eap->line2 = buf->b_fnum;
4686 }
4687 break;
4688 case ADDR_BUFFERS:
4689 eap->line1 = firstbuf->b_fnum;
4690 eap->line2 = lastbuf->b_fnum;
4691 break;
4692 case ADDR_WINDOWS:
4693 eap->line2 = LAST_WIN_NR;
4694 break;
4695 case ADDR_TABS:
4696 eap->line2 = LAST_TAB_NR;
4697 break;
4698 case ADDR_TABS_RELATIVE:
4699 eap->line2 = 1;
4700 break;
4701 case ADDR_ARGUMENTS:
4702 if (ARGCOUNT == 0)
4703 eap->line1 = eap->line2 = 0;
4704 else
4705 eap->line2 = ARGCOUNT;
4706 break;
4707 case ADDR_QUICKFIX_VALID:
4708#ifdef FEAT_QUICKFIX
4709 eap->line2 = qf_get_valid_size(eap);
4710 if (eap->line2 == 0)
4711 eap->line2 = 1;
4712#endif
4713 break;
4714 case ADDR_NONE:
4715 case ADDR_UNSIGNED:
4716 case ADDR_QUICKFIX:
4717 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4718 break;
4719 }
4720}
4721
4722
4723/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004724 * Get flags from an Ex command argument.
4725 */
4726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004727get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004728{
4729 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4730 {
4731 if (*eap->arg == 'l')
4732 eap->flags |= EXFLAG_LIST;
4733 else if (*eap->arg == 'p')
4734 eap->flags |= EXFLAG_PRINT;
4735 else
4736 eap->flags |= EXFLAG_NR;
4737 eap->arg = skipwhite(eap->arg + 1);
4738 }
4739}
4740
4741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 * Function called for command which is Not Implemented. NI!
4743 */
4744 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004745ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746{
4747 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004748 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004749 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750}
4751
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004752#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753/*
4754 * Function called for script command which is Not Implemented. NI!
4755 * Skips over ":perl <<EOF" constructs.
4756 */
4757 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004758ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
4760 if (!eap->skip)
4761 ex_ni(eap);
4762 else
4763 vim_free(script_get(eap, eap->arg));
4764}
4765#endif
4766
4767/*
4768 * Check range in Ex command for validity.
4769 * Return NULL when valid, error message when invalid.
4770 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004771 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004772invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004774 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004775
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 if ( eap->line1 < 0
4777 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004778 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004779 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004780
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004781 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004782 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004783 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004784 {
4785 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004786 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004787#ifdef FEAT_DIFF
4788 + (eap->cmdidx == CMD_diffget)
4789#endif
4790 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004791 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004792 break;
4793 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004794 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004795 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004796 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004797 break;
4798 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004799 // Only a boundary check, not whether the buffers actually
4800 // exist.
4801 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004802 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004803 break;
4804 case ADDR_LOADED_BUFFERS:
4805 buf = firstbuf;
4806 while (buf->b_ml.ml_mfp == NULL)
4807 {
4808 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004809 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004810 buf = buf->b_next;
4811 }
4812 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004813 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004814 buf = lastbuf;
4815 while (buf->b_ml.ml_mfp == NULL)
4816 {
4817 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004818 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004819 buf = buf->b_prev;
4820 }
4821 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004822 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004823 break;
4824 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004825 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004826 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004827 break;
4828 case ADDR_TABS:
4829 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004830 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004831 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004832 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004833 case ADDR_OTHER:
4834 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004835 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004836 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004837#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004838 // No error for value that is too big, will use the last entry.
4839 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004840 {
4841 if (eap->addr_count == 0)
4842 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004843 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004844 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004845#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004846 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004847 case ADDR_QUICKFIX_VALID:
4848#ifdef FEAT_QUICKFIX
4849 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4850 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004851 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004852#endif
4853 break;
4854 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004855 case ADDR_NONE:
4856 // Will give an error elsewhere.
4857 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004858 }
4859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 return NULL;
4861}
4862
4863/*
4864 * Correct the range for zero line number, if required.
4865 */
4866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004867correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004869 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
4871 if (eap->line1 == 0)
4872 eap->line1 = 1;
4873 if (eap->line2 == 0)
4874 eap->line2 = 1;
4875 }
4876}
4877
Bram Moolenaar748bf032005-02-02 23:04:36 +00004878#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004879/*
4880 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4881 * pattern. Otherwise return eap->arg.
4882 */
4883 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004884skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004885{
4886 char_u *p = eap->arg;
4887
Bram Moolenaara37420f2006-02-04 22:37:47 +00004888 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4889 || eap->cmdidx == CMD_vimgrepadd
4890 || eap->cmdidx == CMD_lvimgrepadd
4891 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004892 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004893 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004894 if (p == NULL)
4895 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004896 }
4897 return p;
4898}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004899
4900/*
4901 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4902 * in the command line, so that things like % get expanded.
4903 */
4904 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004905replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004906{
4907 char_u *new_cmdline;
4908 char_u *program;
4909 char_u *pos;
4910 char_u *ptr;
4911 int len;
4912 int i;
4913
4914 /*
4915 * Don't do it when ":vimgrep" is used for ":grep".
4916 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004917 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4918 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4919 || eap->cmdidx == CMD_grepadd
4920 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004921 && !grep_internal(eap->cmdidx))
4922 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004923 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4924 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004925 {
4926 if (*curbuf->b_p_gp == NUL)
4927 program = p_gp;
4928 else
4929 program = curbuf->b_p_gp;
4930 }
4931 else
4932 {
4933 if (*curbuf->b_p_mp == NUL)
4934 program = p_mp;
4935 else
4936 program = curbuf->b_p_mp;
4937 }
4938
4939 p = skipwhite(p);
4940
4941 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4942 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004943 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004944 i = 1;
4945 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4946 ++i;
4947 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004948 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004949 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004950 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004951 ptr = new_cmdline;
4952 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4953 {
4954 i = (int)(pos - program);
4955 STRNCPY(ptr, program, i);
4956 STRCPY(ptr += i, p);
4957 ptr += len;
4958 program = pos + 2;
4959 }
4960 STRCPY(ptr, program);
4961 }
4962 else
4963 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004964 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004965 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004966 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004967 STRCPY(new_cmdline, program);
4968 STRCAT(new_cmdline, " ");
4969 STRCAT(new_cmdline, p);
4970 }
4971 msg_make(p);
4972
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004973 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004974 vim_free(*cmdlinep);
4975 *cmdlinep = new_cmdline;
4976 p = new_cmdline;
4977 }
4978 return p;
4979}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004980#endif
4981
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982/*
4983 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004984 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 * Return FAIL for failure, OK otherwise.
4986 */
4987 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004988expand_filename(
4989 exarg_T *eap,
4990 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004991 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004993 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 char_u *repl;
4995 int srclen;
4996 char_u *p;
4997 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004998 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
Bram Moolenaar748bf032005-02-02 23:04:36 +00005000#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005001 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005002 p = skip_grep_pat(eap);
5003#else
5004 p = eap->arg;
5005#endif
5006
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 /*
5008 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5009 * the file name contains a wildcard it should not cause expanding.
5010 * (it will be expanded anyway if there is a wildcard before replacing).
5011 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005012 has_wildcards = mch_has_wildcard(p);
5013 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005015#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005016 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005017 if (p[0] == '`' && p[1] == '=')
5018 {
5019 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005020 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005021 if (*p == '`')
5022 ++p;
5023 continue;
5024 }
5025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 /*
5027 * Quick check if this cannot be the start of a special string.
5028 * Also removes backslash before '%', '#' and '<'.
5029 */
5030 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5031 {
5032 ++p;
5033 continue;
5034 }
5035
5036 /*
5037 * Try to find a match at this position.
5038 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005039 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005040 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005041 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005043 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
5045 p += srclen;
5046 continue;
5047 }
5048
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005049 // Wildcards won't be expanded below, the replacement is taken
5050 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005051 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5052 {
5053 char_u *l = repl;
5054
5055 repl = expand_env_save(repl);
5056 vim_free(l);
5057 }
5058
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005059 // Need to escape white space et al. with a backslash.
5060 // Don't do this for:
5061 // - replacement that already has been escaped: "##"
5062 // - shell commands (may have to use quotes instead).
5063 // - non-unix systems when there is a single argument (spaces don't
5064 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005066 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 && eap->cmdidx != CMD_grep
5069 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005070 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005071 && eap->cmdidx != CMD_lgrep
5072 && eap->cmdidx != CMD_lgrepadd
5073 && eap->cmdidx != CMD_lmake
5074 && eap->cmdidx != CMD_make
5075 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005077 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078#endif
5079 )
5080 {
5081 char_u *l;
5082#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005083 // Don't escape a backslash here, because rem_backslash() doesn't
5084 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 static char_u *nobslash = (char_u *)" \t\"|";
5086# define ESCAPE_CHARS nobslash
5087#else
5088# define ESCAPE_CHARS escape_chars
5089#endif
5090
5091 for (l = repl; *l; ++l)
5092 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5093 {
5094 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5095 if (l != NULL)
5096 {
5097 vim_free(repl);
5098 repl = l;
5099 }
5100 break;
5101 }
5102 }
5103
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005104 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005105 if ((eap->usefilter || eap->cmdidx == CMD_bang
5106 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005107 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
5109 char_u *l;
5110
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005111 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (l != NULL)
5113 {
5114 vim_free(repl);
5115 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
5117 }
5118
5119 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5120 vim_free(repl);
5121 if (p == NULL)
5122 return FAIL;
5123 }
5124
5125 /*
5126 * One file argument: Expand wildcards.
5127 * Don't do this with ":r !command" or ":w !command".
5128 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005129 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
5131 /*
5132 * May do this twice:
5133 * 1. Replace environment variables.
5134 * 2. Replace any other wildcards, remove backslashes.
5135 */
5136 for (n = 1; n <= 2; ++n)
5137 {
5138 if (n == 2)
5139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 /*
5141 * Halve the number of backslashes (this is Vi compatible).
5142 * For Unix and OS/2, when wildcards are expanded, this is
5143 * done by ExpandOne() below.
5144 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005145#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (!has_wildcards)
5147#endif
5148 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150
5151 if (has_wildcards)
5152 {
5153 if (n == 1)
5154 {
5155 /*
5156 * First loop: May expand environment variables. This
5157 * can be done much faster with expand_env() than with
5158 * something else (e.g., calling a shell).
5159 * After expanding environment variables, check again
5160 * if there are still wildcards present.
5161 */
5162 if (vim_strchr(eap->arg, '$') != NULL
5163 || vim_strchr(eap->arg, '~') != NULL)
5164 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005165 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005166 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 has_wildcards = mch_has_wildcard(NameBuff);
5168 p = NameBuff;
5169 }
5170 else
5171 p = NULL;
5172 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005173 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
5175 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005176 int options = WILD_LIST_NOTFOUND
5177 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178
5179 ExpandInit(&xpc);
5180 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005181 if (p_wic)
5182 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005184 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (p == NULL)
5186 return FAIL;
5187 }
5188 if (p != NULL)
5189 {
5190 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5191 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005192 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 vim_free(p);
5194 }
5195 }
5196 }
5197 }
5198 return OK;
5199}
5200
5201/*
5202 * Replace part of the command line, keeping eap->cmd, eap->arg and
5203 * eap->nextcmd correct.
5204 * "src" points to the part that is to be replaced, of length "srclen".
5205 * "repl" is the replacement string.
5206 * Returns a pointer to the character after the replaced string.
5207 * Returns NULL for failure.
5208 */
5209 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005210repl_cmdline(
5211 exarg_T *eap,
5212 char_u *src,
5213 int srclen,
5214 char_u *repl,
5215 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
5217 int len;
5218 int i;
5219 char_u *new_cmdline;
5220
5221 /*
5222 * The new command line is build in new_cmdline[].
5223 * First allocate it.
5224 * Careful: a "+cmd" argument may have been NUL terminated.
5225 */
5226 len = (int)STRLEN(repl);
5227 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005228 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005229 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005230 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005231 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232
5233 /*
5234 * Copy the stuff before the expanded part.
5235 * Copy the expanded stuff.
5236 * Copy what came after the expanded part.
5237 * Copy the next commands, if there are any.
5238 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005239 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005241
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005243 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005245 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005247 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 {
5249 i = (int)STRLEN(new_cmdline) + 1;
5250 STRCPY(new_cmdline + i, eap->nextcmd);
5251 eap->nextcmd = new_cmdline + i;
5252 }
5253 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5254 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5255 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5256 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5257 vim_free(*cmdlinep);
5258 *cmdlinep = new_cmdline;
5259
5260 return src;
5261}
5262
5263/*
5264 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005265 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 */
5267 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005268separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269{
5270 char_u *p;
5271
Bram Moolenaar86b68352004-12-27 21:59:20 +00005272#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005273 p = skip_grep_pat(eap);
5274#else
5275 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005276#endif
5277
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005278 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280 if (*p == Ctrl_V)
5281 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005282 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005283 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005285 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005286 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005287 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 break;
5289 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005290
5291#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005292 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005293 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005294 {
5295 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005296 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005297 if (*p == NUL) // stop at NUL after CTRL-V
5298 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005299 }
5300#endif
5301
Bram Moolenaareda29c92022-10-02 12:59:00 +01005302 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005303 // :@" and :*" do not start a comment!
5304 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005305 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005306 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005307 && !(eap->argt & EX_NOTRLCOM)
5308 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5309 || p != eap->arg)
5310 && (eap->cmdidx != CMD_redir
5311 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005312 || (*p == '#'
5313 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005314 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005315 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 || *p == '|' || *p == '\n')
5317 {
5318 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005319 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 * AND 'b' is present in 'cpoptions'.
5321 */
5322 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005323 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005325 if (!keep_backslash)
5326 {
5327 STRMOVE(p - 1, p); // remove the '\'
5328 --p;
5329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331 else
5332 {
5333 eap->nextcmd = check_nextcmd(p);
5334 *p = NUL;
5335 break;
5336 }
5337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005339
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005340 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 del_trailing_spaces(eap->arg);
5342}
5343
5344/*
5345 * get + command from ex argument
5346 */
5347 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005348getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
5350 char_u *arg = *argp;
5351 char_u *command = NULL;
5352
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005353 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
5355 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005356 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 command = dollar_command;
5358 else
5359 {
5360 command = arg;
5361 arg = skip_cmd_arg(command, TRUE);
5362 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005363 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 }
5365
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005366 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 *argp = arg;
5368 }
5369 return command;
5370}
5371
5372/*
5373 * Find end of "+command" argument. Skip over "\ " and "\\".
5374 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005375 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005376skip_cmd_arg(
5377 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005378 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380 while (*p && !vim_isspace(*p))
5381 {
5382 if (*p == '\\' && p[1] != NUL)
5383 {
5384 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005385 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 else
5387 ++p;
5388 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005389 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391 return p;
5392}
5393
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005394 int
5395get_bad_opt(char_u *p, exarg_T *eap)
5396{
5397 if (STRICMP(p, "keep") == 0)
5398 eap->bad_char = BAD_KEEP;
5399 else if (STRICMP(p, "drop") == 0)
5400 eap->bad_char = BAD_DROP;
5401 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5402 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005403 else
5404 return FAIL;
5405 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005406}
5407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408/*
5409 * Get "++opt=arg" argument.
5410 * Return FAIL or OK.
5411 */
5412 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005413getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 char_u *arg = eap->arg + 2;
5416 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005417 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005420 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5422 {
5423 if (*arg == 'n')
5424 {
5425 arg += 2;
5426 eap->force_bin = FORCE_NOBIN;
5427 }
5428 else
5429 eap->force_bin = FORCE_BIN;
5430 if (!checkforcmd(&arg, "binary", 3))
5431 return FAIL;
5432 eap->arg = skipwhite(arg);
5433 return OK;
5434 }
5435
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005436 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005437 if (STRNCMP(arg, "edit", 4) == 0)
5438 {
5439 eap->read_edit = TRUE;
5440 eap->arg = skipwhite(arg + 4);
5441 return OK;
5442 }
5443
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 if (STRNCMP(arg, "ff", 2) == 0)
5445 {
5446 arg += 2;
5447 pp = &eap->force_ff;
5448 }
5449 else if (STRNCMP(arg, "fileformat", 10) == 0)
5450 {
5451 arg += 10;
5452 pp = &eap->force_ff;
5453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 else if (STRNCMP(arg, "enc", 3) == 0)
5455 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005456 if (STRNCMP(arg, "encoding", 8) == 0)
5457 arg += 8;
5458 else
5459 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 pp = &eap->force_enc;
5461 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005462 else if (STRNCMP(arg, "bad", 3) == 0)
5463 {
5464 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005465 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467
5468 if (pp == NULL || *arg != '=')
5469 return FAIL;
5470
5471 ++arg;
5472 *pp = (int)(arg - eap->cmd);
5473 arg = skip_cmd_arg(arg, FALSE);
5474 eap->arg = skipwhite(arg);
5475 *arg = NUL;
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 if (pp == &eap->force_ff)
5478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5480 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005481 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005483 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005485 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5487 *p = TOLOWER_ASC(*p);
5488 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005489 else
5490 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005491 // Check ++bad= argument. Must be a single-byte character, "keep" or
5492 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005493 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005494 return FAIL;
5495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
5497 return OK;
5498}
5499
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005501ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502{
5503 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005504 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 * directory for security reasons.
5506 */
5507 if (secure)
5508 {
5509 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005510 eap->errmsg =
5511 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 }
5513 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005514 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 else
5516 do_augroup(eap->arg, eap->forceit);
5517}
5518
5519/*
5520 * ":doautocmd": Apply the automatic commands to the current buffer.
5521 */
5522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005523ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005525 char_u *arg = eap->arg;
5526 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005527 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005528
Bram Moolenaar1610d052016-06-09 22:53:01 +02005529 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005530 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005531 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005532 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535/*
5536 * :[N]bunload[!] [N] [bufname] unload buffer
5537 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5538 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5539 */
5540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005541ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005543 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005544 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 eap->errmsg = do_bufdel(
5546 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5547 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5548 : DOBUF_UNLOAD, eap->arg,
5549 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5550}
5551
5552/*
5553 * :[N]buffer [N] to buffer N
5554 * :[N]sbuffer [N] to buffer N
5555 */
5556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005557ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005559 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005560 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005562 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 else
5564 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005565 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5567 else
5568 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005569 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005570 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
5572}
5573
5574/*
5575 * :[N]bmodified [N] to next mod. buffer
5576 * :[N]sbmodified [N] to next mod. buffer
5577 */
5578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005579ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005582 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005583 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584}
5585
5586/*
5587 * :[N]bnext [N] to next buffer
5588 * :[N]sbnext [N] split and to next buffer
5589 */
5590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005591ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005593 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005594 return;
5595
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005597 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005598 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599}
5600
5601/*
5602 * :[N]bNext [N] to previous buffer
5603 * :[N]bprevious [N] to previous buffer
5604 * :[N]sbNext [N] split and to previous buffer
5605 * :[N]sbprevious [N] split and to previous buffer
5606 */
5607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005608ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005610 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005611 return;
5612
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005614 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005615 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616}
5617
5618/*
5619 * :brewind to first buffer
5620 * :bfirst to first buffer
5621 * :sbrewind split and to first buffer
5622 * :sbfirst split and to first buffer
5623 */
5624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005627 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005628 return;
5629
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005631 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005632 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633}
5634
5635/*
5636 * :blast to last buffer
5637 * :sblast split and to last buffer
5638 */
5639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005640ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005642 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005643 return;
5644
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005646 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005647 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
Bram Moolenaar7a092242020-04-16 22:10:49 +02005650/*
5651 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005652 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005657 int comment_char = '"';
5658
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005659 if (in_vim9script())
5660 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005661 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005662}
5663
5664/*
5665 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5666 * to "cmd_start" or has a white space character before it.
5667 */
5668 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005669ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005670{
5671 int c = *cmd;
5672
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005673 if (c == NUL || c == '|' || c == '\n')
5674 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005675 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005676 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5677 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005678 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005679 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680}
5681
5682#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5683 || defined(PROTO)
5684/*
5685 * Return the next command, after the first '|' or '\n'.
5686 * Return NULL if not found.
5687 */
5688 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005689find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690{
5691 while (*p != '|' && *p != '\n')
5692 {
5693 if (*p == NUL)
5694 return NULL;
5695 ++p;
5696 }
5697 return (p + 1);
5698}
5699#endif
5700
5701/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005702 * Check if *p is a separator between Ex commands, skipping over white space.
5703 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 */
5705 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005706check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005708 char_u *s = skipwhite(p);
5709
5710 if (*s == '|' || *s == '\n')
5711 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 else
5713 return NULL;
5714}
5715
5716/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005717 * If "eap->nextcmd" is not set, check for a next command at "p".
5718 */
5719 void
5720set_nextcmd(exarg_T *eap, char_u *arg)
5721{
5722 char_u *p = check_nextcmd(arg);
5723
5724 if (eap->nextcmd == NULL)
5725 eap->nextcmd = p;
5726 else if (p != NULL)
5727 // cannot use "| command" inside a {} block
5728 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5729}
5730
5731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 * - if there are more files to edit
5733 * - and this is the last window
5734 * - and forceit not used
5735 * - and not repeated twice on a row
5736 * return FAIL and give error message if 'message' TRUE
5737 * return OK otherwise
5738 */
5739 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005740check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005741 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005742 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743{
5744 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5745
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005746 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005747 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
5749 if (message)
5750 {
5751#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005752 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5753 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005755 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005757 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5758 NGETTEXT("%d more file to edit. Quit anyway?",
5759 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5761 return OK;
5762 return FAIL;
5763 }
5764#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005765 semsg(NGETTEXT(e_nr_more_file_to_edit,
5766 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005767 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
5769 return FAIL;
5770 }
5771 return OK;
5772}
5773
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774/*
5775 * Function given to ExpandGeneric() to obtain the list of command names.
5776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005778get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
5780 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005781 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 return cmdnames[idx].cmd_name;
5783}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005786ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
Bram Moolenaare6850792010-05-14 15:28:44 +02005788 if (*eap->arg == NUL)
5789 {
5790#ifdef FEAT_EVAL
5791 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5792 char_u *p = NULL;
5793
5794 if (expr != NULL)
5795 {
5796 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005797 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005798 --emsg_off;
5799 vim_free(expr);
5800 }
5801 if (p != NULL)
5802 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005803 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005804 vim_free(p);
5805 }
5806 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005807 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005808#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005809 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005810#endif
5811 }
5812 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005813 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005814
5815#ifdef FEAT_VTP
5816 else if (has_vtp_working())
5817 {
5818 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005819 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005820 redrawcmd();
5821 }
5822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823}
5824
5825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005826ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
5828 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005829 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 do_highlight(eap->arg, eap->forceit, FALSE);
5831}
5832
5833
5834/*
5835 * Call this function if we thought we were going to exit, but we won't
5836 * (because of an error). May need to restore the terminal mode.
5837 */
5838 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005839not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840{
5841 exiting = FALSE;
5842 settmode(TMODE_RAW);
5843}
5844
Bram Moolenaar3552e742021-05-29 12:21:58 +02005845 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005846before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5847{
5848 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5849
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005850 // Bail out when autocommands closed the window.
5851 // Refuse to quit when the buffer in the last window is being closed (can
5852 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005853 if (!win_valid(wp)
5854 || curbuf_locked()
5855 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5856 return TRUE;
5857
5858 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5859 {
5860 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005861 // Refuse to quit when locked or when the window was closed or the
5862 // buffer in the last window is being closed (can only happen in
5863 // autocommands).
5864 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005865 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5866 return TRUE;
5867 }
5868
5869 return FALSE;
5870}
5871
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005873 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005874 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005875 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005877 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005878ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005880 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005881
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 if (cmdwin_type != 0)
5883 {
5884 cmdwin_result = Ctrl_C;
5885 return;
5886 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005887 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005888 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005889 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005890 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005891 return;
5892 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005893 if (eap->addr_count > 0)
5894 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005895 int wnr = eap->line2;
5896
5897 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5898 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005899 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005900 }
5901 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005902 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005903
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005904 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005905 if (curbuf_locked())
5906 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005907
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005908 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005909 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005910 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
5912#ifdef FEAT_NETBEANS_INTG
5913 netbeansForcedQuit = eap->forceit;
5914#endif
5915
5916 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005917 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 */
5919 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5920 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005921 if ((!buf_hide(wp->w_buffer)
5922 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005923 | (eap->forceit ? CCGD_FORCEIT : 0)
5924 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005926 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 {
5928 not_exiting();
5929 }
5930 else
5931 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005932 // quit last window
5933 // Note: only_one_window() returns true, even so a help window is
5934 // still open. In that case only quit, if no address has been
5935 // specified. Example:
5936 // :h|wincmd w|1q - don't quit
5937 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005938 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005940 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005941#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005943#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005944 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005945 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 }
5947}
5948
5949/*
5950 * ":cquit".
5951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005953ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005955 // this does not always pass on the exit code to the Manx compiler. why?
5956 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957}
5958
5959/*
5960 * ":qall": try to quit all windows
5961 */
5962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005963ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 if (cmdwin_type != 0)
5966 {
5967 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005968 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 else
5970 cmdwin_result = K_XF2;
5971 return;
5972 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005973
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005974 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005975 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005976 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005977 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005978 return;
5979 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005980
5981 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005982 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005983
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005985 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 getout(0);
5987 not_exiting();
5988}
5989
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990/*
5991 * ":close": close current window, unless it is the last one
5992 */
5993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005994ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005996 win_T *win;
5997 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005999 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006001 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006002 {
6003 if (eap->addr_count == 0)
6004 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006005 else
6006 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006007 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006008 {
6009 winnr++;
6010 if (winnr == eap->line2)
6011 break;
6012 }
6013 if (win == NULL)
6014 win = lastwin;
6015 ex_win_close(eap->forceit, win, NULL);
6016 }
6017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018}
6019
Bram Moolenaar4033c552017-09-16 20:54:51 +02006020#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006021/*
6022 * ":pclose": Close any preview window.
6023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006025ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006026{
6027 win_T *win;
6028
Bram Moolenaar79648732019-07-18 21:43:07 +02006029 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006030 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006031 if (win->w_p_pvw)
6032 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006033 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006034 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006035 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006036# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006037 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006038 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006039# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006040}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006041#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006042
Bram Moolenaarf740b292006-02-16 22:11:02 +00006043/*
6044 * Close window "win" and take care of handling closing the last window for a
6045 * modified buffer.
6046 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006047 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006048ex_win_close(
6049 int forceit,
6050 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006051 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052{
6053 int need_hide;
6054 buf_T *buf = win->w_buffer;
6055
Bram Moolenaarcf844172020-06-26 19:44:06 +02006056 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006057 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006058 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006059 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006060 return;
6061 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006062 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006063 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006064
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006066 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006068#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006069 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006071# ifdef FEAT_TERMINAL
6072 if (term_job_running(buf->b_term))
6073 {
6074 if (term_confirm_stop(buf) == FAIL)
6075 return;
6076 // Manually kill the terminal here because this command will
6077 // hide it otherwise.
6078 free_terminal(buf);
6079 need_hide = FALSE;
6080 }
6081 else
6082# endif
6083 {
6084 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006085
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006086 set_bufref(&bufref, buf);
6087 dialog_changed(buf, FALSE);
6088 if (bufref_valid(&bufref) && bufIsChanged(buf))
6089 return;
6090 need_hide = FALSE;
6091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 }
6093 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006096 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 return;
6098 }
6099 }
6100
Bram Moolenaar4033c552017-09-16 20:54:51 +02006101#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006103#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006104
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006105 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006106 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006107 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006108 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006109 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110}
6111
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006113 * Handle the argument for a tabpage related ex command.
6114 * Returns a tabpage number.
6115 * When an error is encountered then eap->errmsg is set.
6116 */
6117 static int
6118get_tabpage_arg(exarg_T *eap)
6119{
6120 int tab_number;
6121 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6122
6123 if (eap->arg && *eap->arg != NUL)
6124 {
6125 char_u *p = eap->arg;
6126 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006127 int relative = 0; // argument +N/-N means: go to N places to the
6128 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006129
6130 if (*p == '-')
6131 {
6132 relative = -1;
6133 p++;
6134 }
6135 else if (*p == '+')
6136 {
6137 relative = 1;
6138 p++;
6139 }
6140
6141 p_save = p;
6142 tab_number = getdigits(&p);
6143
6144 if (relative == 0)
6145 {
6146 if (STRCMP(p, "$") == 0)
6147 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006148 else if (STRCMP(p, "#") == 0)
6149 if (valid_tabpage(lastused_tabpage))
6150 tab_number = tabpage_index(lastused_tabpage);
6151 else
6152 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006153 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006154 tab_number = 0;
6155 goto theend;
6156 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006157 else if (p == p_save || *p_save == '-' || *p != NUL
6158 || tab_number > LAST_TAB_NR)
6159 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006160 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006161 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006162 goto theend;
6163 }
6164 }
6165 else
6166 {
6167 if (*p_save == NUL)
6168 tab_number = 1;
6169 else if (p == p_save || *p_save == '-' || *p != NUL
6170 || tab_number == 0)
6171 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006172 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006173 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006174 goto theend;
6175 }
6176 tab_number = tab_number * relative + tabpage_index(curtab);
6177 if (!unaccept_arg0 && relative == -1)
6178 --tab_number;
6179 }
6180 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006181 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006182 }
6183 else if (eap->addr_count > 0)
6184 {
6185 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006186 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006187 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006188 tab_number = 0;
6189 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006190 else
6191 {
6192 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006193 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006194 {
6195 --tab_number;
6196 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006197 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006198 }
6199 }
6200 }
6201 else
6202 {
6203 switch (eap->cmdidx)
6204 {
6205 case CMD_tabnext:
6206 tab_number = tabpage_index(curtab) + 1;
6207 if (tab_number > LAST_TAB_NR)
6208 tab_number = 1;
6209 break;
6210 case CMD_tabmove:
6211 tab_number = LAST_TAB_NR;
6212 break;
6213 default:
6214 tab_number = tabpage_index(curtab);
6215 }
6216 }
6217
6218theend:
6219 return tab_number;
6220}
6221
6222/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006223 * ":tabclose": close current tab page, unless it is the last one.
6224 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 */
6226 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006227ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006229 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006230 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006231
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006233 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006234 cmdwin_result = K_IGNORE;
6235 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006236 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006237
6238 if (first_tabpage->tp_next == NULL)
6239 {
6240 emsg(_(e_cannot_close_last_tab_page));
6241 return;
6242 }
6243
6244 if (window_layout_locked(CMD_tabclose))
6245 return;
6246
6247 tab_number = get_tabpage_arg(eap);
6248 if (eap->errmsg != NULL)
6249 return;
6250
6251 tp = find_tabpage(tab_number);
6252 if (tp == NULL)
6253 {
6254 beep_flush();
6255 return;
6256 }
6257 if (tp != curtab)
6258 {
6259 tabpage_close_other(tp, eap->forceit);
6260 return;
6261 }
6262 else if (!text_locked() && !curbuf_locked())
6263 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006264}
6265
6266/*
6267 * ":tabonly": close all tab pages except the current one
6268 */
6269 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006270ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006271{
6272 tabpage_T *tp;
6273 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006274 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006275
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006276 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006277 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006278 cmdwin_result = K_IGNORE;
6279 return;
6280 }
6281
6282 if (first_tabpage->tp_next == NULL)
6283 {
6284 msg(_("Already only one tab page"));
6285 return;
6286 }
6287
6288 if (window_layout_locked(CMD_tabonly))
6289 return;
6290
6291 tab_number = get_tabpage_arg(eap);
6292 if (eap->errmsg != NULL)
6293 return;
6294
6295 goto_tabpage(tab_number);
6296 // Repeat this up to a 1000 times, because autocommands may
6297 // mess up the lists.
6298 for (done = 0; done < 1000; ++done)
6299 {
6300 FOR_ALL_TABPAGES(tp)
6301 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006302 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006303 tabpage_close_other(tp, eap->forceit);
6304 // if we failed to close it quit
6305 if (valid_tabpage(tp))
6306 done = 1000;
6307 // start over, "tp" is now invalid
6308 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006309 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006310 if (first_tabpage->tp_next == NULL)
6311 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006316 * Close the current tab page.
6317 */
6318 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006319tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006320{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006321 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006322 return;
6323
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006324 // First close all the windows but the current one. If that worked then
6325 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006326 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006327 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006328 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006329 ex_win_close(forceit, curwin, NULL);
6330# ifdef FEAT_GUI
6331 need_mouse_correct = TRUE;
6332# endif
6333}
6334
6335/*
6336 * Close tab page "tp", which is not the current tab page.
6337 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006338 * Also takes care of the tab pages line disappearing when closing the
6339 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006340 */
6341 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006342tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006343{
6344 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006345 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006346
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006347 // Limit to 1000 windows, autocommands may add a window while we close
6348 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006349 while (++done < 1000)
6350 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006351 wp = tp->tp_firstwin;
6352 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006353
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006354 // Autocommands may delete the tab page under our fingers and we may
6355 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006356 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006357 break;
6358 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006359
Bram Moolenaar29323592016-07-24 22:04:11 +02006360 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006361}
6362
6363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 * ":only".
6365 */
6366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006367ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006369 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006370 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371# ifdef FEAT_GUI
6372 need_mouse_correct = TRUE;
6373# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006374 if (eap->addr_count > 0)
6375 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006376 win_T *wp;
6377 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006378 for (wp = firstwin; --wnr > 0; )
6379 {
6380 if (wp->w_next == NULL)
6381 break;
6382 else
6383 wp = wp->w_next;
6384 }
6385 win_goto(wp);
6386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 close_others(TRUE, eap->forceit);
6388}
6389
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006391ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006393 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006394 if (eap->skip)
6395 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006396
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006397 if (window_layout_locked(CMD_hide))
6398 return;
6399#ifdef FEAT_GUI
6400 need_mouse_correct = TRUE;
6401#endif
6402 if (eap->addr_count == 0)
6403 win_close(curwin, FALSE); // don't free buffer
6404 else
6405 {
6406 int winnr = 0;
6407 win_T *win;
6408
6409 FOR_ALL_WINDOWS(win)
6410 {
6411 winnr++;
6412 if (winnr == eap->line2)
6413 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006415 if (win == NULL)
6416 win = lastwin;
6417 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419}
6420
6421/*
6422 * ":stop" and ":suspend": Suspend Vim.
6423 */
dbivolaruab16ad32021-12-29 19:41:47 +00006424 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006425ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426{
6427 /*
6428 * Disallow suspending for "rvim".
6429 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006430 if (check_restricted())
6431 return;
6432
6433 if (!eap->forceit)
6434 autowrite_all();
6435 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6436 windgoto((int)Rows - 1, 0);
6437 out_char('\n');
6438 out_flush();
6439 stoptermcap();
6440 out_flush(); // needed for SUN to restore xterm buffer
6441 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6442 ui_suspend(); // call machine specific function
6443 maketitle();
6444 resettitle(); // force updating the title
6445 starttermcap();
6446 scroll_start(); // scroll screen before redrawing
6447 redraw_later_clear();
6448 shell_resized(); // may have resized window
6449 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450}
6451
6452/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006453 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 */
6455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006456ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006458#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006459 if (not_in_vim9(eap) == FAIL)
6460 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 if (cmdwin_type != 0)
6463 {
6464 cmdwin_result = Ctrl_C;
6465 return;
6466 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006467 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006468 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006469 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006470 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006471 return;
6472 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006473
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006475 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 */
6477 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6478 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006479
6480 // Write the buffer for ":wq" or when it was changed.
6481 // Trigger QuitPre and ExitPre.
6482 // Check if we can exit now, after autocommands have changed things.
6483 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6484 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006486 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 {
6488 not_exiting();
6489 }
6490 else
6491 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006492 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006494 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495# ifdef FEAT_GUI
6496 need_mouse_correct = TRUE;
6497# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006498 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006499 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 }
6501}
6502
6503/*
6504 * ":print", ":list", ":number".
6505 */
6506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006507ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006509 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006510 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006511 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006513 for ( ;!got_int; ui_breakcheck())
6514 {
6515 print_line(eap->line1,
6516 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6517 || (eap->flags & EXFLAG_NR)),
6518 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6519 if (++eap->line1 > eap->line2)
6520 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006521 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006522 }
6523 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006524 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006525 curwin->w_cursor.lnum = eap->line2;
6526 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 }
6528
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530}
6531
6532#ifdef FEAT_BYTEOFF
6533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006534ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535{
6536 goto_byte(eap->line2);
6537}
6538#endif
6539
6540/*
6541 * ":shell".
6542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006544ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
6546 do_shell(NULL, 0);
6547}
6548
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006549#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006551static int drop_busy = FALSE;
6552static int drop_filec;
6553static char_u **drop_filev = NULL;
6554static int drop_split;
6555static void (*drop_callback)(void *);
6556static void *drop_cookie;
6557
6558 static void
6559handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
6561 exarg_T ea;
6562 int save_msg_scroll = msg_scroll;
6563
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006564 // Setting the argument list may cause screen updates and being called
6565 // recursively. Avoid that by setting drop_busy.
6566 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006568 // Check whether the current buffer is changed. If so, we will need
6569 // to split the current window or data could be lost.
6570 // We don't need to check if the 'hidden' option is set, as in this
6571 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006572 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 {
6574 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006575 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 --emsg_off;
6577 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006578 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 if (win_split(0, 0) == FAIL)
6581 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006582 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006584 // When splitting the window, create a new alist. Otherwise the
6585 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 alist_unlink(curwin->w_alist);
6587 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 }
6589
6590 /*
6591 * Set up the new argument list.
6592 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006593 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594
6595 /*
6596 * Move to the first file.
6597 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006598 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006599 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 ea.cmd = (char_u *)"next";
6601 do_argfile(&ea, 0);
6602
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006603 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6604 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 need_start_insertmode = FALSE;
6606
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006607 // Restore msg_scroll, otherwise a following command may cause scrolling
6608 // unexpectedly. The screen will be redrawn by the caller, thus
6609 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006611
6612 if (drop_callback != NULL)
6613 drop_callback(drop_cookie);
6614
6615 drop_filev = NULL;
6616 drop_busy = FALSE;
6617}
6618
6619/*
6620 * Handle a file drop. The code is here because a drop is *nearly* like an
6621 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006622 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006623 * code is very similar to :args and hence needs access to a lot of the static
6624 * functions in this file.
6625 *
6626 * The "filev" list must have been allocated using alloc(), as should each item
6627 * in the list. This function takes over responsibility for freeing the "filev"
6628 * list.
6629 */
6630 void
6631handle_drop(
6632 int filec, // the number of files dropped
6633 char_u **filev, // the list of files dropped
6634 int split, // force splitting the window
6635 void (*callback)(void *), // to be called after setting the argument
6636 // list
6637 void *cookie) // argument for "callback" (allocated)
6638{
6639 // Cannot handle recursive drops, finish the pending one.
6640 if (drop_busy)
6641 {
6642 FreeWild(filec, filev);
6643 vim_free(cookie);
6644 return;
6645 }
6646
6647 // When calling handle_drop() more than once in a row we only use the last
6648 // one.
6649 if (drop_filev != NULL)
6650 {
6651 FreeWild(drop_filec, drop_filev);
6652 vim_free(drop_cookie);
6653 }
6654
6655 drop_filec = filec;
6656 drop_filev = filev;
6657 drop_split = split;
6658 drop_callback = callback;
6659 drop_cookie = cookie;
6660
6661 // Postpone this when:
6662 // - editing the command line
6663 // - not possible to change the current buffer
6664 // - updating the screen
6665 // As it may change buffers and window structures that are in use and cause
6666 // freed memory to be used.
6667 if (text_locked() || curbuf_locked() || updating_screen)
6668 return;
6669
6670 handle_drop_internal();
6671}
6672
6673/*
6674 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6675 * reset: Handle a postponed drop.
6676 */
6677 void
6678handle_any_postponed_drop(void)
6679{
6680 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006681 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006682 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683}
6684#endif
6685
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 * ":preserve".
6688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006690ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006692 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 ml_preserve(curbuf, TRUE);
6694}
6695
6696/*
6697 * ":recover".
6698 */
6699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006700ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006702 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006704 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6705 | CCGD_MULTWIN
6706 | (eap->forceit ? CCGD_FORCEIT : 0)
6707 | CCGD_EXCMD)
6708
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 && (*eap->arg == NUL
6710 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006711 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 recoverymode = FALSE;
6713}
6714
6715/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006716 * Command modifier used in a wrong way. Also for other commands that can't
6717 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 */
6719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006720ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006722 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723}
6724
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725/*
6726 * :sview [+command] file split window with new file, read-only
6727 * :split [[+command] file] split window with current or new file
6728 * :vsplit [[+command] file] split window vertically with current or new file
6729 * :new [[+command] file] split window with no or new file
6730 * :vnew [[+command] file] split vertically window with no or new file
6731 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006732 *
6733 * :tabedit open new Tab page with empty window
6734 * :tabedit [+command] file open new Tab page and edit "file"
6735 * :tabnew [[+command] file] just like :tabedit
6736 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 */
6738 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006739ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006741 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006743#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006744 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006745 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006746#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006747 int use_tab = eap->cmdidx == CMD_tabedit
6748 || eap->cmdidx == CMD_tabfind
6749 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006751 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006752 return;
6753
Bram Moolenaar4033c552017-09-16 20:54:51 +02006754#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
Bram Moolenaar4033c552017-09-16 20:54:51 +02006758#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006759 // A ":split" in the quickfix window works like ":new". Don't want two
6760 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006761 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
6763 if (eap->cmdidx == CMD_split)
6764 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 if (eap->cmdidx == CMD_vsplit)
6766 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006770 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006772 char_u *file_to_find = NULL;
6773 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006775 FNAME_MESS, TRUE, curbuf->b_ffname,
6776 &file_to_find, &search_ctx);
6777 vim_free(file_to_find);
6778 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if (fname == NULL)
6780 goto theend;
6781 eap->arg = fname;
6782 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006783# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006784 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 && eap->cmdidx != CMD_new)
6787 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006788 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006789# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006790 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006791# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006792 au_has_group((char_u *)"FileExplorer"))
6793 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006794 // No browsing supported but we do have the file explorer:
6795 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006796 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006797 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006798 }
6799 else
6800 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006801 fname = do_browse(0, (char_u *)(use_tab
6802 ? _("Edit File in new tab page")
6803 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006805 if (fname == NULL)
6806 goto theend;
6807 eap->arg = fname;
6808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006810 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006813 /*
6814 * Either open new tab page or split the window.
6815 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006816 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006817 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006818 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006819 : eap->addr_count == 0 ? 0
6820 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006821 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006822 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006823
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006824 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006825 if (curwin != old_curwin
6826 && win_valid(old_curwin)
6827 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006828 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006829 old_curwin->w_alt_fnum = curbuf->b_fnum;
6830 }
6831 }
6832 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6834 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006835 // Reset 'scrollbind' when editing another file, but keep it when
6836 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006837 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006838 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 else
6840 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 do_exedit(eap, old_curwin);
6842 }
6843
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006844# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006845 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848theend:
6849 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006851
6852/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006853 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006854 */
6855 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006856tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006857{
6858 exarg_T ea;
6859
Bram Moolenaara80faa82020-04-12 19:37:17 +02006860 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006861 ea.cmdidx = CMD_tabnew;
6862 ea.cmd = (char_u *)"tabn";
6863 ea.arg = (char_u *)"";
6864 ex_splitview(&ea);
6865}
6866
6867/*
6868 * :tabnext command
6869 */
6870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006871ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006872{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006873 int tab_number;
6874
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006875 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006876 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006877 switch (eap->cmdidx)
6878 {
6879 case CMD_tabfirst:
6880 case CMD_tabrewind:
6881 goto_tabpage(1);
6882 break;
6883 case CMD_tablast:
6884 goto_tabpage(9999);
6885 break;
6886 case CMD_tabprevious:
6887 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006888 if (eap->arg && *eap->arg != NUL)
6889 {
6890 char_u *p = eap->arg;
6891 char_u *p_save = p;
6892
6893 tab_number = getdigits(&p);
6894 if (p == p_save || *p_save == '-' || *p != NUL
6895 || tab_number == 0)
6896 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006897 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006898 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006899 return;
6900 }
6901 }
6902 else
6903 {
6904 if (eap->addr_count == 0)
6905 tab_number = 1;
6906 else
6907 {
6908 tab_number = eap->line2;
6909 if (tab_number < 1)
6910 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006911 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006912 return;
6913 }
6914 }
6915 }
6916 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006917 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006918 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006919 tab_number = get_tabpage_arg(eap);
6920 if (eap->errmsg == NULL)
6921 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006922 break;
6923 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006924}
6925
6926/*
6927 * :tabmove command
6928 */
6929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006930ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006931{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006932 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006933
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006934 tab_number = get_tabpage_arg(eap);
6935 if (eap->errmsg == NULL)
6936 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006937}
6938
6939/*
6940 * :tabs command: List tabs and their contents.
6941 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006943ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006944{
6945 tabpage_T *tp;
6946 win_T *wp;
6947 int tabcount = 1;
6948
6949 msg_start();
6950 msg_scroll = TRUE;
6951 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6952 {
6953 msg_putchar('\n');
6954 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006955 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006956 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006957 ui_breakcheck();
6958
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006959 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006960 wp = firstwin;
6961 else
6962 wp = tp->tp_firstwin;
6963 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6964 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006965 msg_putchar('\n');
6966 msg_putchar(wp == curwin ? '>' : ' ');
6967 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006968 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6969 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006970 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006971 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006972 else
6973 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6974 IObuff, IOSIZE, TRUE);
6975 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006976 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006977 ui_breakcheck();
6978 }
6979 }
6980}
6981
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982/*
6983 * ":mode": Set screen mode.
6984 * If no argument given, just get the screen size and redraw.
6985 */
6986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006987ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989 if (*eap->arg == NUL)
6990 shell_resized();
6991 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006992 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993}
6994
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995/*
6996 * ":resize".
6997 * set, increment or decrement current window height
6998 */
6999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007000ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001{
7002 int n;
7003 win_T *wp = curwin;
7004
7005 if (eap->addr_count > 0)
7006 {
7007 n = eap->line2;
7008 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7009 ;
7010 }
7011
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007012# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007014# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007016 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 {
7018 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007019 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007020 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007022 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 }
7024 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {
7026 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007027 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007028 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007030 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 }
7032}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
7034/*
7035 * ":find [+command] <file>" command.
7036 */
7037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007038ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 char_u *fname;
7041 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007042 char_u *file_to_find = NULL;
7043 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
7045 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007046 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 if (eap->addr_count > 0)
7048 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007049 // Repeat finding the file "count" times. This matters when it appears
7050 // several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 count = eap->line2;
7052 while (fname != NULL && --count > 0)
7053 {
7054 vim_free(fname);
7055 fname = find_file_in_path(NULL, 0, FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007056 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 }
7058 }
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007059 VIM_CLEAR(file_to_find);
7060 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007062 if (fname == NULL)
7063 return;
7064
7065 eap->arg = fname;
7066 do_exedit(eap, NULL);
7067 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068}
7069
7070/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007071 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007072 */
7073 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007074ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007075{
7076 regmatch_T regmatch;
7077 char_u *p;
7078
Bram Moolenaar65088802021-03-13 21:07:21 +01007079#ifdef FEAT_EVAL
7080 if (not_in_vim9(eap) == FAIL)
7081 return;
7082#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007083 curwin->w_cursor.lnum = eap->line2;
7084 beginline(BL_SOL | BL_FIX);
7085 if (*eap->arg == '/')
7086 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007087 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007088 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007089 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007090 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007091 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007092 if (regmatch.regprog != NULL)
7093 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007094 // make a copy of the line, when searching for a mark it might be
7095 // flushed
7096 char_u *line = vim_strsave(ml_get_curline());
7097
Bram Moolenaardf177f62005-02-22 08:39:57 +00007098 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007099 if (vim_regexec(&regmatch, line, (colnr_T)0))
7100 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007101 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007102 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007103 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007104 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007105 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007106 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007107 eap->arg += STRLEN(eap->arg);
7108 }
7109 check_cursor();
7110
7111 eap->cmdidx = CMD_visual;
7112 do_exedit(eap, NULL);
7113}
7114
7115/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007116 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 */
7118 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007119ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120{
7121 do_exedit(eap, NULL);
7122}
7123
7124/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007125 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007128do_exedit(
7129 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007130 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
7132 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007134 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007136 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7137 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007138 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 /*
7140 * ":vi" command ends Ex mode.
7141 */
7142 if (exmode_active && (eap->cmdidx == CMD_visual
7143 || eap->cmdidx == CMD_view))
7144 {
7145 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007146 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007148 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007149 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007150 if (global_busy)
7151 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007152 if (eap->nextcmd != NULL)
7153 {
7154 stuffReadbuff(eap->nextcmd);
7155 eap->nextcmd = NULL;
7156 }
7157
7158 if (exmode_was != EXMODE_VIM)
7159 settmode(TMODE_RAW);
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007160 int save_rd = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007161 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007162 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007163 no_wait_return = 0;
7164 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007165 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007166 msg_scroll = 0;
7167#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007168 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007169 hold_gui_events = 0;
7170#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007171 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007172 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007173
7174 main_loop(FALSE, TRUE);
7175
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007176 pending_exmode_active = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007177 RedrawingDisabled = save_rd;
7178 no_wait_return = save_nwr;
7179 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007180#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007181 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007182#endif
7183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 }
7187
7188 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007189 || eap->cmdidx == CMD_tabnew
7190 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007191 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007193 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 setpcmark();
7195 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007196 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7197 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007199 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 || *eap->arg != NUL
7201#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007202 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203#endif
7204 )
7205 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007206 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7207 // Only ":edit" or ":script" can bring us here, others are stopped
7208 // earlier.
7209 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007210 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007211
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 n = readonlymode;
7213 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7214 readonlymode = TRUE;
7215 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007216 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7217 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007218 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7219 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7221 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007222 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7224 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7225 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007226 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007228 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007229 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007230 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7231 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007232 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007234 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 if (old_curwin != NULL)
7236 {
7237 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007238 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007240#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007241 cleanup_T cs;
7242
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007243 // Reset the error/interrupt/exception state here so that
7244 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007245 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007246#endif
7247#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007249#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007250 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007251
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007252#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007253 // Restore the error/interrupt/exception state if not
7254 // discarded by a new aborting error, interrupt, or
7255 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007256 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
7259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261 else if (readonlymode && curbuf->b_nwindows == 1)
7262 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007263 // When editing an already visited buffer, 'readonly' won't be set
7264 // but the previous value is kept. With ":view" and ":sview" we
7265 // want the file to be readonly, except when another window is
7266 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 curbuf->b_p_ro = TRUE;
7268 }
7269 readonlymode = n;
7270 }
7271 else
7272 {
7273 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007274 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 if (n != curwin->w_arg_idx_invalid)
7278 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 }
7280
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 /*
7282 * if ":split file" worked, set alternate file name in old window to new
7283 * file
7284 */
7285 if (old_curwin != NULL
7286 && *eap->arg != NUL
7287 && curwin != old_curwin
7288 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007289 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007290 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292
7293 ex_no_reprint = TRUE;
7294}
7295
7296#ifndef FEAT_GUI
7297/*
7298 * ":gui" and ":gvim" when there is no GUI.
7299 */
7300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007301ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007303 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304}
7305#endif
7306
Bram Moolenaar4f974752019-02-17 17:44:42 +01007307#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007309ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
7311 gui_make_tearoff(eap->arg);
7312}
7313#endif
7314
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007315#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7316 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007318ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007320# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7321 if (gui.in_use)
7322 gui_make_popup(eap->arg, eap->forceit);
7323# ifdef FEAT_TERM_POPUP_MENU
7324 else
7325# endif
7326# endif
7327# ifdef FEAT_TERM_POPUP_MENU
7328 pum_make_popup(eap->arg, eap->forceit);
7329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330}
7331#endif
7332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007334ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335{
7336 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007337 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007339 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340}
7341
7342/*
7343 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7344 * offset.
7345 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007348ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007351 win_T *save_curwin = curwin;
7352 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 long topline;
7354 long y;
7355 linenr_T old_linenr = curwin->w_cursor.lnum;
7356
7357 setpcmark();
7358
7359 /*
7360 * determine max topline
7361 */
7362 if (curwin->w_p_scb)
7363 {
7364 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007365 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 {
7367 if (wp->w_p_scb && wp->w_buffer)
7368 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007369 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 if (topline > y)
7371 topline = y;
7372 }
7373 }
7374 if (topline < 1)
7375 topline = 1;
7376 }
7377 else
7378 {
7379 topline = 1;
7380 }
7381
7382
7383 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007384 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007386 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 {
7388 if (curwin->w_p_scb)
7389 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007390 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 y = topline - curwin->w_topline;
7392 if (y > 0)
7393 scrollup(y, TRUE);
7394 else
7395 scrolldown(-y, TRUE);
7396 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007397 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 }
7401 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007402 curwin = save_curwin;
7403 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 if (curwin->w_p_scb)
7405 {
7406 did_syncbind = TRUE;
7407 checkpcmark();
7408 if (old_linenr != curwin->w_cursor.lnum)
7409 {
7410 char_u ctrl_o[2];
7411
7412 ctrl_o[0] = Ctrl_O;
7413 ctrl_o[1] = 0;
7414 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7415 }
7416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417}
7418
7419
7420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007421ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007423 int i;
7424 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7425 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007427 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007429 do_bang(1, eap, FALSE, FALSE, TRUE);
7430 return;
7431 }
7432
7433 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7434 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435
7436#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007437 if (cmdmod.cmod_flags & CMOD_BROWSE)
7438 {
7439 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007441 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7442 NULL, NULL, NULL, curbuf);
7443 if (browseFile != NULL)
7444 {
7445 i = readfile(browseFile, NULL,
7446 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7447 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 }
7449 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007450 i = OK;
7451 }
7452 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007454 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007456 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 return;
7458 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007459 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 }
7461 else
7462 {
7463 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7464 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7465 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007466 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
7468 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007469 if (i != OK)
7470 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007471#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007472 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007474 semsg(_(e_cant_open_file_str), eap->arg);
7475 }
7476 else
7477 {
7478 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007479 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007480 // Delete the empty line that remains. Historically ex does
7481 // this but vi doesn't.
7482 if (eap->line2 == 0)
7483 lnum = curbuf->b_ml.ml_line_count;
7484 else
7485 lnum = 1;
7486 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007487 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007488 ml_delete(lnum);
7489 if (curwin->w_cursor.lnum > 1
7490 && curwin->w_cursor.lnum >= lnum)
7491 --curwin->w_cursor.lnum;
7492 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007493 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007494 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007495 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 }
7497}
7498
Bram Moolenaarea408852005-06-25 22:49:46 +00007499static char_u *prev_dir = NULL;
7500
7501#if defined(EXITFREE) || defined(PROTO)
7502 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007503free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007504{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007505 VIM_CLEAR(prev_dir);
7506 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007507}
7508#endif
7509
Bram Moolenaarf4258302013-06-02 18:20:17 +02007510/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007511 * Get the previous directory for the given chdir scope.
7512 */
7513 static char_u *
7514get_prevdir(cdscope_T scope)
7515{
7516 if (scope == CDSCOPE_WINDOW)
7517 return curwin->w_prevdir;
7518 else if (scope == CDSCOPE_TABPAGE)
7519 return curtab->tp_prevdir;
7520 return prev_dir;
7521}
7522
7523/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007524 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007525 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7526 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007527 */
7528 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007529post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007530{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007531 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007532 // Clear tab local directory for both :cd and :tcd
7533 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007534 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007535 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007536 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007537 char_u *pdir = get_prevdir(scope);
7538
7539 // If still in the global directory, need to remember current
7540 // directory as the global directory.
7541 if (globaldir == NULL && pdir != NULL)
7542 globaldir = vim_strsave(pdir);
7543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007544 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007545 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007546 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007547 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007548 curtab->tp_localdir = vim_strsave(NameBuff);
7549 else
7550 curwin->w_localdir = vim_strsave(NameBuff);
7551 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007552 }
7553 else
7554 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007555 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007556 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007557 }
7558
zeertzjq64be6aa2021-11-19 11:59:08 +00007559 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007560 shorten_fnames(TRUE);
7561}
7562
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007563/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007564 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7565 */
7566 void
7567trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7568{
7569#ifdef FEAT_EVAL
7570 dict_T *v_event;
7571 save_v_event_T save_v_event;
7572
7573 v_event = get_v_event(&save_v_event);
7574 (void)dict_add_string(v_event, "directory", new_dir);
7575 dict_set_items_ro(v_event);
7576#endif
7577 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7578#ifdef FEAT_EVAL
7579 restore_v_event(v_event, &save_v_event);
7580#endif
7581}
7582
7583/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007584 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007585 * chdir() function.
7586 * scope == CDSCOPE_WINDOW: changes the window-local directory
7587 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7588 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007589 * Returns TRUE if the directory is successfully changed.
7590 */
7591 int
7592changedir_func(
7593 char_u *new_dir,
7594 int forceit,
7595 cdscope_T scope)
7596{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007597 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007598 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007599 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007600 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007601 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007602
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007603 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007604 return FALSE;
7605
7606 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7607 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007608 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007609 return FALSE;
7610 }
7611
7612 // ":cd -": Change to previous directory
7613 if (STRCMP(new_dir, "-") == 0)
7614 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007615 pdir = get_prevdir(scope);
7616 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007617 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007618 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007619 return FALSE;
7620 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007621 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007622 }
7623
7624 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007625 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007626 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007627 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007628 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007629
Bakudankun29f3a452021-12-11 12:28:08 +00007630 // For UNIX ":cd" means: go to home directory.
7631 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007632#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007633 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007634#else
7635 if (*new_dir == NUL && p_cdh)
7636#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007637 {
7638 // use NameBuff for home directory name
7639# ifdef VMS
7640 char_u *p;
7641
7642 p = mch_getenv((char_u *)"SYS$LOGIN");
7643 if (p == NULL || *p == NUL) // empty is the same as not set
7644 NameBuff[0] = NUL;
7645 else
7646 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7647# else
7648 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7649# endif
7650 new_dir = NameBuff;
7651 }
zeertzjqf38aad82021-12-30 13:45:57 +00007652 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007653 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7654 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007655 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007656 if (scope == CDSCOPE_WINDOW)
7657 acmd_fname = (char_u *)"window";
7658 else if (scope == CDSCOPE_TABPAGE)
7659 acmd_fname = (char_u *)"tabpage";
7660 else
7661 acmd_fname = (char_u *)"global";
7662 trigger_DirChangedPre(acmd_fname, new_dir);
7663
7664 if (vim_chdir(new_dir))
7665 {
7666 emsg(_(e_command_failed));
7667 vim_free(pdir);
7668 return FALSE;
7669 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007670 }
zeertzjq73257142022-02-02 13:16:37 +00007671
7672 if (scope == CDSCOPE_WINDOW)
7673 pp = &curwin->w_prevdir;
7674 else if (scope == CDSCOPE_TABPAGE)
7675 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007676 else
zeertzjq73257142022-02-02 13:16:37 +00007677 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007678 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007679 *pp = pdir;
7680
7681 post_chdir(scope);
7682
7683 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007684 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007685 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007686 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007687}
Bram Moolenaarea408852005-06-25 22:49:46 +00007688
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007690 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007692 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007693ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007695 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696
7697 new_dir = eap->arg;
7698#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007699 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7700 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007702 ex_pwd(NULL);
7703 return;
7704 }
7705#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007706
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007707 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007708
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007709 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7710 scope = CDSCOPE_WINDOW;
7711 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7712 scope = CDSCOPE_TABPAGE;
7713
7714 if (changedir_func(new_dir, eap->forceit, scope))
7715 {
7716 // Echo the new current directory if the command was typed.
7717 if (KeyTyped || p_verbose >= 5)
7718 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 }
7720}
7721
7722/*
7723 * ":pwd".
7724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007726ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7729 {
7730#ifdef BACKSLASH_IN_FILENAME
7731 slash_adjust(NameBuff);
7732#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007733 if (p_verbose > 0)
7734 {
7735 char *context = "global";
7736
Bram Moolenaar05268152021-11-18 18:53:45 +00007737 if (last_chdir_reason != NULL)
7738 context = last_chdir_reason;
7739 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007740 context = "window";
7741 else if (curtab->tp_localdir != NULL)
7742 context = "tabpage";
7743 smsg("[%s] %s", context, (char *)NameBuff);
7744 }
7745 else
7746 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 }
7748 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007749 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750}
7751
7752/*
7753 * ":=".
7754 */
7755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007756ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007758 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007759 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760}
7761
7762 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007763ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007765 int n;
7766 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767
7768 if (cursor_valid())
7769 {
7770 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7771 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007772 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007774
7775 len = eap->line2;
7776 switch (*eap->arg)
7777 {
7778 case 'm': break;
7779 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007780 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007781 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007782
7783 // Hide the cursor if invoked with !
7784 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785}
7786
7787/*
7788 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007789 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 */
7791 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007792do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
Bram Moolenaar52953192019-08-16 10:27:13 +02007794 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007795 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007796# ifdef ELAPSED_FUNC
7797 elapsed_T start_tv;
7798
7799 // Remember at what time we started, so that we know how much longer we
7800 // should wait after waiting for a bit.
7801 ELAPSED_INIT(start_tv);
7802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007804 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007805 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007806 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007807 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007808
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007809 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007810 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007812 wait_now = msec - done > 1000L ? 1000L : msec - done;
7813#ifdef FEAT_TIMERS
7814 {
7815 long due_time = check_due_timer();
7816
7817 if (due_time > 0 && due_time < wait_now)
7818 wait_now = due_time;
7819 }
7820#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007821#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007822 if (has_any_channel() && wait_now > 20L)
7823 wait_now = 20L;
7824#endif
7825#ifdef FEAT_SOUND
7826 if (has_any_sound_callback() && wait_now > 20L)
7827 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007828#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007829 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007830
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007831#ifdef FEAT_JOB_CHANNEL
7832 if (has_any_channel())
7833 ui_breakcheck_force(TRUE);
7834 else
7835#endif
7836 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007837#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007838 // Process the netbeans and clientserver messages that may have been
7839 // received in the call to ui_breakcheck() when the GUI is in use. This
7840 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007841 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007842#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007843
7844# ifdef ELAPSED_FUNC
7845 // actual time passed
7846 done = ELAPSED_FUNC(start_tv);
7847# else
7848 // guestimate time passed (will actually be more)
7849 done += wait_now;
7850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007852
7853 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7854 // input buffer, otherwise a following call to input() fails.
7855 if (got_int)
7856 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007857
7858 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007859 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860}
7861
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862/*
7863 * ":winsize" command (obsolete).
7864 */
7865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007866ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867{
7868 int w, h;
7869 char_u *arg = eap->arg;
7870 char_u *p;
7871
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007872 if (!isdigit(*arg))
7873 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007874 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007875 return;
7876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 w = getdigits(&arg);
7878 arg = skipwhite(arg);
7879 p = arg;
7880 h = getdigits(&arg);
7881 if (*p != NUL && *arg == NUL)
7882 set_shellsize(w, h, TRUE);
7883 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007884 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885}
7886
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007888ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
7890 int xchar = NUL;
7891 char_u *p;
7892
7893 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7894 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007895 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 if (eap->arg[1] == NUL)
7897 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007898 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 return;
7900 }
7901 xchar = eap->arg[1];
7902 p = eap->arg + 2;
7903 }
7904 else
7905 p = eap->arg + 1;
7906
Bram Moolenaar63b91732021-08-05 20:40:03 +02007907 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007909 if (*p != NUL && *p != (
7910#ifdef FEAT_EVAL
7911 in_vim9script() ? '#' :
7912#endif
7913 '"')
7914 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007915 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007916 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007918 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007919 postponed_split_flags = cmdmod.cmod_split;
7920 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7922 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007923 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 }
7925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926
Bram Moolenaar843ee412004-06-30 16:16:41 +00007927#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928/*
7929 * ":winpos".
7930 */
7931 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007932ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934 int x, y;
7935 char_u *arg = eap->arg;
7936 char_u *p;
7937
7938 if (*arg == NUL)
7939 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007940# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007941# ifdef VIMDLL
7942 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7943 mch_get_winpos(&x, &y) != FAIL)
7944# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007946# else
7947 if (mch_get_winpos(&x, &y) != FAIL)
7948# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {
7950 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007951 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 }
7953 else
7954# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007955 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 }
7957 else
7958 {
7959 x = getdigits(&arg);
7960 arg = skipwhite(arg);
7961 p = arg;
7962 y = getdigits(&arg);
7963 if (*p == NUL || *arg != NUL)
7964 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007965 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 return;
7967 }
7968# ifdef FEAT_GUI
7969 if (gui.in_use)
7970 gui_mch_set_winpos(x, y);
7971 else if (gui.starting)
7972 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007973 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 gui_win_x = x;
7975 gui_win_y = y;
7976 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007977# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 else
7979# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007980# endif
7981# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007982 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983# endif
7984# ifdef HAVE_TGETENT
7985 if (*T_CWP)
7986 term_set_winpos(x, y);
7987# endif
7988 }
7989}
7990#endif
7991
7992/*
7993 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7994 */
7995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007996ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997{
7998 oparg_T oa;
7999
8000 clear_oparg(&oa);
8001 oa.regname = eap->regname;
8002 oa.start.lnum = eap->line1;
8003 oa.end.lnum = eap->line2;
8004 oa.line_count = eap->line2 - eap->line1 + 1;
8005 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008007 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {
8009 setpcmark();
8010 curwin->w_cursor.lnum = eap->line1;
8011 beginline(BL_SOL | BL_FIX);
8012 }
8013
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008014 if (VIsual_active)
8015 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008016
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 switch (eap->cmdidx)
8018 {
8019 case CMD_delete:
8020 oa.op_type = OP_DELETE;
8021 op_delete(&oa);
8022 break;
8023
8024 case CMD_yank:
8025 oa.op_type = OP_YANK;
8026 (void)op_yank(&oa, FALSE, TRUE);
8027 break;
8028
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008029 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008030 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008032 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8033#else
8034 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008036 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 oa.op_type = OP_RSHIFT;
8038 else
8039 oa.op_type = OP_LSHIFT;
8040 op_shift(&oa, FALSE, eap->amount);
8041 break;
8042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008044 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045}
8046
8047/*
8048 * ":put".
8049 */
8050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008051ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008053 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 if (eap->line2 == 0)
8055 {
8056 eap->line2 = 1;
8057 eap->forceit = TRUE;
8058 }
8059 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008060 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008061 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008062 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063}
8064
8065/*
8066 * Handle ":copy" and ":move".
8067 */
8068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008069ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070{
8071 long n;
8072
Bram Moolenaar491799b2020-08-01 19:23:43 +02008073#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008074 if (not_in_vim9(eap) == FAIL)
8075 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008076#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008077 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008078 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
8080 eap->nextcmd = NULL;
8081 return;
8082 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008083 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084
8085 /*
8086 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8087 */
8088 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8089 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008090 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 return;
8092 }
8093
8094 if (eap->cmdidx == CMD_move)
8095 {
8096 if (do_move(eap->line1, eap->line2, n) == FAIL)
8097 return;
8098 }
8099 else
8100 ex_copy(eap->line1, eap->line2, n);
8101 u_clearline();
8102 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008103 ex_may_print(eap);
8104}
8105
8106/*
8107 * Print the current line if flags were given to the Ex command.
8108 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008109 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008110ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008111{
8112 if (eap->flags != 0)
8113 {
8114 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8115 (eap->flags & EXFLAG_LIST));
8116 ex_no_reprint = TRUE;
8117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118}
8119
8120/*
8121 * ":smagic" and ":snomagic".
8122 */
8123 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008124ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008126 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008128 magic_overruled = eap->cmdidx == CMD_smagic
8129 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008130 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008131 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132}
8133
8134/*
8135 * ":join".
8136 */
8137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008138ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139{
8140 curwin->w_cursor.lnum = eap->line1;
8141 if (eap->line1 == eap->line2)
8142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008143 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 return;
8145 if (eap->line2 == curbuf->b_ml.ml_line_count)
8146 {
8147 beep_flush();
8148 return;
8149 }
8150 ++eap->line2;
8151 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008152 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008154 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155}
8156
8157/*
8158 * ":[addr]@r" or ":[addr]*r": execute register
8159 */
8160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008161ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162{
8163 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008164 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165
8166 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008167 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168
8169#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008170 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171#endif
8172
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008173 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 c = *eap->arg;
8175 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8176 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008177 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008178 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8179 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008182 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008185 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008187 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008189 /*
8190 * Execute from the typeahead buffer.
8191 * Continue until the stuff buffer is empty and all added characters
8192 * have been consumed.
8193 */
8194 while (!stuff_empty() || typebuf.tb_len > prev_len)
8195 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8196
8197 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198}
8199
8200/*
8201 * ":!".
8202 */
8203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008204ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205{
8206 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8207}
8208
8209/*
8210 * ":undo".
8211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008213ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008215 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008216 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008217 else
8218 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219}
8220
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008221#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008222 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008223ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008224{
8225 char_u hash[UNDO_HASH_SIZE];
8226
8227 u_compute_hash(hash);
8228 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8229}
8230
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008232ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008233{
8234 char_u hash[UNDO_HASH_SIZE];
8235
8236 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008237 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008238}
8239#endif
8240
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241/*
8242 * ":redo".
8243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008245ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246{
8247 u_redo(1);
8248}
8249
8250/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008251 * ":earlier" and ":later".
8252 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008254ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008255{
8256 long count = 0;
8257 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008258 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008259 char_u *p = eap->arg;
8260
8261 if (*p == NUL)
8262 count = 1;
8263 else if (isdigit(*p))
8264 {
8265 count = getdigits(&p);
8266 switch (*p)
8267 {
8268 case 's': ++p; sec = TRUE; break;
8269 case 'm': ++p; sec = TRUE; count *= 60; break;
8270 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008271 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8272 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008273 }
8274 }
8275
8276 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008277 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008278 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008279 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8280 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008281}
8282
8283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 * ":redir": start/stop redirection.
8285 */
8286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008287ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
8289 char *mode;
8290 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008291 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292
Bram Moolenaarba768492016-07-08 15:32:54 +02008293#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008294 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008295 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008296 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008297 return;
8298 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008299#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008300
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 if (STRICMP(eap->arg, "END") == 0)
8302 close_redir();
8303 else
8304 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008305 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008307 ++arg;
8308 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008310 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 mode = "a";
8312 }
8313 else
8314 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008315 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316
8317 close_redir();
8318
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008319 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008320 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 if (fname == NULL)
8322 return;
8323#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008324 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {
8326 char_u *browseFile;
8327
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008328 browseFile = do_browse(BROWSE_SAVE,
8329 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008330 fname, NULL, NULL,
8331 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008333 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 vim_free(fname);
8335 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008336 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 }
8338#endif
8339
8340 redir_fd = open_exfile(fname, eap->forceit, mode);
8341 vim_free(fname);
8342 }
8343#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008344 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008346 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008348 ++arg;
8349 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008351 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008352 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008354 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008356 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008357 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008358 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008359 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008361 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008362 if (*arg == '>')
8363 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008364 // Make register empty when not using @A-@Z and the
8365 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008366 if (*arg == NUL && !isupper(redir_reg))
8367 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008369 }
8370 if (*arg != NUL)
8371 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008372 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008373 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008374 }
8375 }
8376 else if (*arg == '=' && arg[1] == '>')
8377 {
8378 int append;
8379
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008380 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008381 close_redir();
8382 arg += 2;
8383
8384 if (*arg == '>')
8385 {
8386 ++arg;
8387 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 }
8389 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008390 append = FALSE;
8391
8392 if (var_redir_start(skipwhite(arg), append) == OK)
8393 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 }
8395#endif
8396
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008397 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008400 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008402
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008403 // Make sure redirection is not off. Can happen for cmdline completion
8404 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008405 if (redir_fd != NULL
8406#ifdef FEAT_EVAL
8407 || redir_reg || redir_vname
8408#endif
8409 )
8410 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411}
8412
8413/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008414 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008416 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008417ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008419 redraw_cmd(eap->forceit);
8420}
8421
8422/*
8423 * ":redraw": force redraw, with clear if "clear" is TRUE.
8424 */
8425 void
8426redraw_cmd(int clear)
8427{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 int r = RedrawingDisabled;
8429 int p = p_lz;
8430
8431 RedrawingDisabled = 0;
8432 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008433 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008435 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 if (need_maketitle)
8437 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008438#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8439# ifdef VIMDLL
8440 if (!gui.in_use)
8441# endif
8442 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 RedrawingDisabled = r;
8445 p_lz = p;
8446
Bram Moolenaar5017c662022-04-07 18:06:08 +01008447 // After drawing the statusline screen_attr may still be set.
8448 screen_stop_highlight();
8449
Bram Moolenaara2a89732022-08-31 14:46:18 +01008450 // Reset msg_didout, so that a message that's there is overwritten.
8451 msg_didout = FALSE;
8452 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008454 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008455 need_wait_return = FALSE;
8456
Bram Moolenaara653e532022-04-19 11:38:24 +01008457 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008458 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008459 redrawcmdline();
8460
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 out_flush();
8462}
8463
8464/*
8465 * ":redrawstatus": force redraw of status line(s)
8466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008468ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 int r = RedrawingDisabled;
8471 int p = p_lz;
8472
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 if (eap->forceit)
8474 status_redraw_all();
8475 else
8476 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008477 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008478 return; // redraw later
8479
8480 RedrawingDisabled = 0;
8481 p_lz = FALSE;
zeertzjq320d9102022-09-20 17:12:13 +01008482 if (State & MODE_CMDLINE)
8483 redraw_statuslines();
8484 else
8485 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 RedrawingDisabled = r;
8487 p_lz = p;
8488 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489}
8490
Bram Moolenaare12bab32019-01-08 22:02:56 +01008491/*
8492 * ":redrawtabline": force redraw of the tabline
8493 */
8494 static void
8495ex_redrawtabline(exarg_T *eap UNUSED)
8496{
8497 int r = RedrawingDisabled;
8498 int p = p_lz;
8499
8500 RedrawingDisabled = 0;
8501 p_lz = FALSE;
8502
8503 draw_tabline();
8504
8505 RedrawingDisabled = r;
8506 p_lz = p;
8507 out_flush();
8508}
8509
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008511close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512{
8513 if (redir_fd != NULL)
8514 {
8515 fclose(redir_fd);
8516 redir_fd = NULL;
8517 }
8518#ifdef FEAT_EVAL
8519 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008520 if (redir_vname)
8521 {
8522 var_redir_stop();
8523 redir_vname = 0;
8524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525#endif
8526}
8527
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008528#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008529 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008530vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008531{
8532 if (vim_mkdir(name, prot) != 0)
8533 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008534 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008535 return FAIL;
8536 }
8537 return OK;
8538}
8539#endif
8540
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541/*
8542 * Open a file for writing for an Ex command, with some checks.
8543 * Return file descriptor, or NULL on failure.
8544 */
8545 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008546open_exfile(
8547 char_u *fname,
8548 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008549 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 FILE *fd;
8552
8553#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008554 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 if (mch_isdir(fname))
8556 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008557 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 return NULL;
8559 }
8560#endif
8561 if (!forceit && *mode != 'a' && vim_fexists(fname))
8562 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008563 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 return NULL;
8565 }
8566
8567 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008568 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
8570 return fd;
8571}
8572
8573/*
8574 * ":mark" and ":k".
8575 */
8576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008577ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 pos_T pos;
8580
Bram Moolenaar10b94212021-02-19 21:42:57 +01008581#ifdef FEAT_EVAL
8582 if (not_in_vim9(eap) == FAIL)
8583 return;
8584#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008585 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008587 emsg(_(e_argument_required));
8588 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008590
8591 if (eap->arg[1] != NUL) // more than one character?
8592 {
8593 semsg(_(e_trailing_characters_str), eap->arg);
8594 return;
8595 }
8596
8597 pos = curwin->w_cursor; // save curwin->w_cursor
8598 curwin->w_cursor.lnum = eap->line2;
8599 beginline(BL_WHITE | BL_FIX);
8600 if (setmark(*eap->arg) == FAIL) // set mark
8601 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
8602 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603}
8604
8605/*
8606 * Update w_topline, w_leftcol and the cursor position.
8607 */
8608 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008609update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008611 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 update_topline();
8613 if (!curwin->w_p_wrap)
8614 validate_cursor();
8615 update_curswant();
8616}
8617
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008619 * Save the current State and go to Normal mode.
8620 * Return TRUE if the typeahead could be saved.
8621 */
8622 int
8623save_current_state(save_state_T *sst)
8624{
8625 sst->save_msg_scroll = msg_scroll;
8626 sst->save_restart_edit = restart_edit;
8627 sst->save_msg_didout = msg_didout;
8628 sst->save_State = State;
8629 sst->save_insertmode = p_im;
8630 sst->save_finish_op = finish_op;
8631 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008632 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008633 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008634
Bram Moolenaar4324d872020-12-01 20:12:24 +01008635 msg_scroll = FALSE; // no msg scrolling in Normal mode
8636 restart_edit = 0; // don't go to Insert mode
8637 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008638
Bram Moolenaara452b802020-12-01 21:47:59 +01008639 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008640 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008641
8642 /*
8643 * Save the current typeahead. This is required to allow using ":normal"
8644 * from an event handler and makes sure we don't hang when the argument
8645 * ends with half a command.
8646 */
8647 save_typeahead(&sst->tabuf);
8648 return sst->tabuf.typebuf_valid;
8649}
8650
8651 void
8652restore_current_state(save_state_T *sst)
8653{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008654 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008655 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008656
8657 msg_scroll = sst->save_msg_scroll;
8658 restart_edit = sst->save_restart_edit;
8659 p_im = sst->save_insertmode;
8660 finish_op = sst->save_finish_op;
8661 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008662 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008663 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008664 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008665 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008666
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008667 // Restore the state (needed when called from a function executed for
8668 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008669 State = sst->save_State;
8670#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008671 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008672#endif
8673}
8674
8675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 * ":normal[!] {commands}": Execute normal mode commands.
8677 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008678 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008679ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008681 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 char_u *arg = NULL;
8683 int l;
8684 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008686 if (ex_normal_lock > 0)
8687 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008688 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008689 return;
8690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 if (ex_normal_busy >= p_mmd)
8692 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008693 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 return;
8695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 /*
8698 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8699 * this for the K_SPECIAL leading byte, otherwise special keys will not
8700 * work.
8701 */
8702 if (has_mbyte)
8703 {
8704 int len = 0;
8705
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008706 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 for (p = eap->arg; *p != NUL; ++p)
8708 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008709#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008710 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008712#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008713 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008714 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008715#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 )
8719 len += 2;
8720 }
8721 if (len > 0)
8722 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008723 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 if (arg != NULL)
8725 {
8726 len = 0;
8727 for (p = eap->arg; *p != NUL; ++p)
8728 {
8729 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008730#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 if (*p == CSI)
8732 {
8733 arg[len++] = KS_EXTRA;
8734 arg[len++] = (int)KE_CSI;
8735 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008736#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008737 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {
8739 arg[len++] = *++p;
8740 if (*p == K_SPECIAL)
8741 {
8742 arg[len++] = KS_SPECIAL;
8743 arg[len++] = KE_FILLER;
8744 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008745#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 else if (*p == CSI)
8747 {
8748 arg[len++] = KS_EXTRA;
8749 arg[len++] = (int)KE_CSI;
8750 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 }
8753 arg[len] = NUL;
8754 }
8755 }
8756 }
8757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008759 ++ex_normal_busy;
8760 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 {
8762 /*
8763 * Repeat the :normal command for each line in the range. When no
8764 * range given, execute it just once, without positioning the cursor
8765 * first.
8766 */
8767 do
8768 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 if (eap->addr_count != 0)
8770 {
8771 curwin->w_cursor.lnum = eap->line1++;
8772 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008773 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 }
8775
Bram Moolenaar13505972019-01-24 15:04:48 +01008776 exec_normal_cmd(arg != NULL
8777 ? arg
8778 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 }
8780 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8781 }
8782
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008783 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 update_topline_cursor();
8785
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008786 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008788 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008789#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008790 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008791#endif
8792
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794}
8795
8796/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008797 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 */
8799 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008800ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008802 if (eap->forceit)
8803 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008804 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008805 if (!curwin->w_cursor.lnum)
8806 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008807 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008808 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008809#ifdef FEAT_TERMINAL
8810 // Ignore this when running in an active terminal.
8811 if (term_job_running(curbuf->b_term))
8812 return;
8813#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008814
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008815 // Ignore the command when already in Insert mode. Inserting an
8816 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01008817 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00008818 return;
8819
Bram Moolenaar64969662005-12-14 21:59:55 +00008820 if (eap->cmdidx == CMD_startinsert)
8821 restart_edit = 'a';
8822 else if (eap->cmdidx == CMD_startreplace)
8823 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008825 restart_edit = 'V';
8826
8827 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008829 if (eap->cmdidx == CMD_startinsert)
8830 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008831 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008833
8834 if (VIsual_active)
8835 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836}
8837
8838/*
8839 * ":stopinsert"
8840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008842ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843{
8844 restart_edit = 0;
8845 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008846 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008849/*
8850 * Execute normal mode command "cmd".
8851 * "remap" can be REMAP_NONE or REMAP_YES.
8852 */
8853 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008854exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008855{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008856 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008857 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008858 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008859}
Bram Moolenaar25281632016-01-21 23:32:32 +01008860
Bram Moolenaar25281632016-01-21 23:32:32 +01008861/*
8862 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008863 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008864 */
8865 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008866exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008867{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008868 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008869 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008870
Bram Moolenaar905dd902019-04-07 14:21:47 +02008871 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8872 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008873 clear_oparg(&oa);
8874 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008875 while ((!stuff_empty()
8876 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008877 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008878 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008879 {
8880 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008881#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008882 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008883 && oa.op_type == OP_NOP && oa.regname == NUL
8884 && !VIsual_active)
8885 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008886 // If terminal_loop() returns OK we got a key that is handled
8887 // in Normal model. With FAIL we first need to position the
8888 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008889 if (terminal_loop(TRUE) == OK)
8890 normal_cmd(&oa, TRUE);
8891 }
8892 else
8893#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008894 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008895 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008896 }
8897}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008898
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899#ifdef FEAT_FIND_ID
8900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008901ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
8903 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8904 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8905 (linenr_T)1, (linenr_T)MAXLNUM);
8906}
8907
Bram Moolenaar4033c552017-09-16 20:54:51 +02008908#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909/*
8910 * ":psearch"
8911 */
8912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008913ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914{
8915 g_do_tagpreview = p_pvh;
8916 ex_findpat(eap);
8917 g_do_tagpreview = 0;
8918}
8919#endif
8920
8921 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008922ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923{
8924 int whole = TRUE;
8925 long n;
8926 char_u *p;
8927 int action;
8928
8929 switch (cmdnames[eap->cmdidx].cmd_name[2])
8930 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008931 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8933 action = ACTION_GOTO;
8934 else
8935 action = ACTION_SHOW;
8936 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008937 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 action = ACTION_SHOW_ALL;
8939 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008940 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 action = ACTION_GOTO;
8942 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008943 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 action = ACTION_SPLIT;
8945 break;
8946 }
8947
8948 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008949 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 {
8951 n = getdigits(&eap->arg);
8952 eap->arg = skipwhite(eap->arg);
8953 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008954 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 {
8956 whole = FALSE;
8957 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008958 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 if (*p)
8960 {
8961 *p++ = NUL;
8962 p = skipwhite(p);
8963
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008964 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008965 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008966 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008968 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 }
8970 }
8971 if (!eap->skip)
8972 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8973 whole, !eap->forceit,
8974 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8975 n, action, eap->line1, eap->line2);
8976}
8977#endif
8978
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
Bram Moolenaar4033c552017-09-16 20:54:51 +02008980#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981/*
8982 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8983 */
8984 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008985ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008987 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8989}
8990
8991/*
8992 * ":pedit"
8993 */
8994 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008995ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996{
8997 win_T *curwin_save = curwin;
8998
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008999 if (ERROR_IF_ANY_POPUP_WINDOW)
9000 return;
9001
Bram Moolenaar026587b2019-08-17 15:08:00 +02009002 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009004 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009005
Bram Moolenaar026587b2019-08-17 15:08:00 +02009006 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009008
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 if (curwin != curwin_save && win_valid(curwin_save))
9010 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009011 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009013 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 win_enter(curwin_save, TRUE);
9015 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009016# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009017 else if (WIN_IS_POPUP(curwin))
9018 {
9019 // can't keep focus in popup window
9020 win_enter(firstwin, TRUE);
9021 }
9022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 g_do_tagpreview = 0;
9024}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026
9027/*
9028 * ":stag", ":stselect" and ":stjump".
9029 */
9030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009031ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032{
9033 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009034 postponed_split_flags = cmdmod.cmod_split;
9035 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9037 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009038 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040
9041/*
9042 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9043 */
9044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009045ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046{
9047 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9048}
9049
9050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009051ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052{
9053 int cmd;
9054
9055 switch (name[1])
9056 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009057 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009059 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009061 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009063 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009065 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009067 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009069 case 'f': // ":tfirst"
9070 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009072 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009074 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009076 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009078 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 return;
9080 }
9081#endif
9082 cmd = DT_TAG;
9083 break;
9084 }
9085
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009086 if (name[0] == 'l')
9087 {
9088#ifndef FEAT_QUICKFIX
9089 ex_ni(eap);
9090 return;
9091#else
9092 cmd = DT_LTAG;
9093#endif
9094 }
9095
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9097 eap->forceit, TRUE);
9098}
9099
9100/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009101 * Check "str" for starting with a special cmdline variable.
9102 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9103 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9104 */
9105 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009106find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009107{
9108 int len;
9109 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009110 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009111 "%",
9112#define SPEC_PERC 0
9113 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009114#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009115 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02009116#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009117 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02009118#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009119 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02009120#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009121 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009122#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009123 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009124#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009125 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02009126#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02009127 "<stack>", // call stack
9128#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01009129 "<script>", // script file name
9130#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009131 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01009132#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009133 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009134#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009135 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009136#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009137 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009138#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02009139 "<SID>", // script ID: <SNR>123_
9140#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009141#ifdef FEAT_CLIENTSERVER
9142 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02009143# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009144#endif
9145 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009146
K.Takataeeec2542021-06-02 13:28:16 +02009147 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009148 {
9149 len = (int)STRLEN(spec_str[i]);
9150 if (STRNCMP(src, spec_str[i], len) == 0)
9151 {
9152 *usedlen = len;
9153 return i;
9154 }
9155 }
9156 return -1;
9157}
9158
9159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 * Evaluate cmdline variables.
9161 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009162 * change "%" to curbuf->b_ffname
9163 * "#" to curwin->w_alt_fnum
9164 * "%%" to curwin->w_alt_fnum in Vim9 script
9165 * "<cword>" to word under the cursor
9166 * "<cWORD>" to WORD under the cursor
9167 * "<cexpr>" to C-expression under the cursor
9168 * "<cfile>" to path name under the cursor
9169 * "<sfile>" to sourced file name
9170 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009171 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009172 * "<slnum>" to sourced file line number
9173 * "<afile>" to file name for autocommand
9174 * "<abuf>" to buffer number for autocommand
9175 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 *
9177 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9178 * "" for error without a message) and NULL is returned.
9179 * Returns an allocated string if a valid match was found.
9180 * Returns NULL if no match was found. "usedlen" then still contains the
9181 * number of characters to skip.
9182 */
9183 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009184eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009185 char_u *src, // pointer into commandline
9186 char_u *srcstart, // beginning of valid memory for src
9187 int *usedlen, // characters after src that are used
9188 linenr_T *lnump, // line number for :e command, or NULL
9189 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009190 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009191 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009192 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193{
9194 int i;
9195 char_u *s;
9196 char_u *result;
9197 char_u *resultbuf = NULL;
9198 int resultlen;
9199 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009200 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009202 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205
9206 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009207 if (escaped != NULL)
9208 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209
9210 /*
9211 * Check if there is something to do.
9212 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009213 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009214 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 {
9216 *usedlen = 1;
9217 return NULL;
9218 }
9219
9220 /*
9221 * Skip when preceded with a backslash "\%" and "\#".
9222 * Note: In "\\%" the % is also not recognized!
9223 */
9224 if (src > srcstart && src[-1] == '\\')
9225 {
9226 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009227 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 return NULL;
9229 }
9230
9231 /*
9232 * word or WORD under cursor
9233 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009234 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9235 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009237 resultlen = find_ident_under_cursor(&result,
9238 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9239 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9240 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 if (resultlen == 0)
9242 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009243 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 return NULL;
9245 }
9246 }
9247
9248 /*
9249 * '#': Alternate file name
9250 * '%': Current file name
9251 * File name under the cursor
9252 * File name for autocommand
9253 * and following modifiers
9254 */
9255 else
9256 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009257 int off = 0;
9258
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 switch (spec_idx)
9260 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009261 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009262#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009263 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009266 // '%': current file
9267 if (curbuf->b_fname == NULL)
9268 {
9269 result = (char_u *)"";
9270 valid = 0; // Must have ":p:h" to be valid
9271 }
9272 else
9273 {
9274 result = curbuf->b_fname;
9275 tilde_file = STRCMP(result, "~") == 0;
9276 }
9277 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009279#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009280 // "%%" alternate file
9281 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009282#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009283 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009284 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009285 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009287 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 result = arg_all();
9289 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009290 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009291 if (escaped != NULL)
9292 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 break;
9295 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009296 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009297 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009298 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009300 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009301 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009302 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009303 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009305 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009307 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009308 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009309 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009310 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009311 return NULL;
9312 }
9313#ifdef FEAT_EVAL
9314 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9315 (long)i);
9316 if (result == NULL)
9317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009318 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009319 return NULL;
9320 }
9321#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009322 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 }
9326 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009327 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009328 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9329 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009330 buf = buflist_findnr(i);
9331 if (buf == NULL)
9332 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009333 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009334 return NULL;
9335 }
9336 if (lnump != NULL)
9337 *lnump = ECMD_LAST;
9338 if (buf->b_fname == NULL)
9339 {
9340 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009341 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009342 }
9343 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009344 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009345 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009346 tilde_file = STRCMP(result, "~") == 0;
9347 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 break;
9350
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009351 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009352 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 if (result == NULL)
9354 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009355 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 return NULL;
9357 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009358 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009361 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009363 if (result != NULL && !autocmd_fname_full)
9364 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009365 // Still need to turn the fname into a full path. It is
9366 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009367 autocmd_fname_full = TRUE;
9368 result = FullName_save(autocmd_fname, FALSE);
9369 vim_free(autocmd_fname);
9370 autocmd_fname = result;
9371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 if (result == NULL)
9373 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009374 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 return NULL;
9376 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009377 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 break;
9379
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009380 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 if (autocmd_bufnr <= 0)
9382 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009383 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 return NULL;
9385 }
9386 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9387 result = strbuf;
9388 break;
9389
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009390 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 result = autocmd_match;
9392 if (result == NULL)
9393 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009394 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 return NULL;
9396 }
9397 break;
9398
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009399 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009400 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 if (result == NULL)
9402 {
LemonBoy6013d002022-04-09 21:42:10 +01009403 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9404 return NULL;
9405 }
9406 resultbuf = result; // remember allocated string
9407 break;
9408 case SPEC_STACK: // call stack
9409 result = estack_sfile(ESTACK_STACK);
9410 if (result == NULL)
9411 {
9412 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9413 return NULL;
9414 }
9415 resultbuf = result; // remember allocated string
9416 break;
9417 case SPEC_SCRIPT: // script file name
9418 result = estack_sfile(ESTACK_SCRIPT);
9419 if (result == NULL)
9420 {
9421 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 return NULL;
9423 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009424 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009426
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009427 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009428 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009429 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009430 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009431 return NULL;
9432 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009433 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009434 result = strbuf;
9435 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009436
9437#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009438 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009439 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009440 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009441 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009442 return NULL;
9443 }
9444 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009445 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009446 result = strbuf;
9447 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009448
Bram Moolenaar90944302020-08-01 20:45:11 +02009449 case SPEC_SID:
9450 if (current_sctx.sc_sid <= 0)
9451 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009452 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009453 return NULL;
9454 }
9455 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9456 result = strbuf;
9457 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009458#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009459
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009460#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009461 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009462 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9463 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 result = strbuf;
9465 break;
9466#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009467
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009468 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009469 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009470 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 }
9472
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009473 resultlen = (int)STRLEN(result); // length of new string
9474 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 {
9476 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 resultlen = (int)(s - result);
9479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 else if (!skip_mod)
9481 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009482 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 &resultlen);
9484 if (result == NULL)
9485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009486 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 return NULL;
9488 }
9489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 }
9491
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009492 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009494 if (empty_is_error)
9495 {
9496 if (valid != VALID_HEAD + VALID_PATH)
9497 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9498 else
9499 *errormsg = _(e_evaluates_to_an_empty_string);
9500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 result = NULL;
9502 }
9503 else
9504 result = vim_strnsave(result, resultlen);
9505 vim_free(resultbuf);
9506 return result;
9507}
9508
9509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 * Expand the <sfile> string in "arg".
9511 *
9512 * Returns an allocated string, or NULL for any error.
9513 */
9514 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009515expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009517 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 int len;
9519 char_u *result;
9520 char_u *newres;
9521 char_u *repl;
9522 int srclen;
9523 char_u *p;
9524
9525 result = vim_strsave(arg);
9526 if (result == NULL)
9527 return NULL;
9528
9529 for (p = result; *p; )
9530 {
9531 if (STRNCMP(p, "<sfile>", 7) != 0)
9532 ++p;
9533 else
9534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009535 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009536 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 if (errormsg != NULL)
9538 {
9539 if (*errormsg)
9540 emsg(errormsg);
9541 vim_free(result);
9542 return NULL;
9543 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009544 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 {
9546 p += srclen;
9547 continue;
9548 }
9549 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9550 newres = alloc(len);
9551 if (newres == NULL)
9552 {
9553 vim_free(repl);
9554 vim_free(result);
9555 return NULL;
9556 }
9557 mch_memmove(newres, result, (size_t)(p - result));
9558 STRCPY(newres + (p - result), repl);
9559 len = (int)STRLEN(newres);
9560 STRCAT(newres, p + srclen);
9561 vim_free(repl);
9562 vim_free(result);
9563 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009564 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 }
9566 }
9567
9568 return result;
9569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009572/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009573 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009574 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009577dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 if (fname == NULL)
9580 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009581 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582}
9583#endif
9584
9585/*
9586 * ":behave {mswin,xterm}"
9587 */
9588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009589ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590{
9591 if (STRCMP(eap->arg, "mswin") == 0)
9592 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009593 set_option_value_give_err((char_u *)"selection",
9594 0L, (char_u *)"exclusive", 0);
9595 set_option_value_give_err((char_u *)"selectmode",
9596 0L, (char_u *)"mouse,key", 0);
9597 set_option_value_give_err((char_u *)"mousemodel",
9598 0L, (char_u *)"popup", 0);
9599 set_option_value_give_err((char_u *)"keymodel",
9600 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 }
9602 else if (STRCMP(eap->arg, "xterm") == 0)
9603 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009604 set_option_value_give_err((char_u *)"selection",
9605 0L, (char_u *)"inclusive", 0);
9606 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9607 set_option_value_give_err((char_u *)"mousemodel",
9608 0L, (char_u *)"extend", 0);
9609 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 }
9611 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009612 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613}
9614
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615static int filetype_detect = FALSE;
9616static int filetype_plugin = FALSE;
9617static int filetype_indent = FALSE;
9618
9619/*
9620 * ":filetype [plugin] [indent] {on,off,detect}"
9621 * on: Load the filetype.vim file to install autocommands for file types.
9622 * off: Load the ftoff.vim file to remove all autocommands for file types.
9623 * plugin on: load filetype.vim and ftplugin.vim
9624 * plugin off: load ftplugof.vim
9625 * indent on: load filetype.vim and indent.vim
9626 * indent off: load indoff.vim
9627 */
9628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009629ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630{
9631 char_u *arg = eap->arg;
9632 int plugin = FALSE;
9633 int indent = FALSE;
9634
9635 if (*eap->arg == NUL)
9636 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009637 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009638 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 filetype_detect ? "ON" : "OFF",
9640 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9641 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9642 return;
9643 }
9644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009645 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 for (;;)
9647 {
9648 if (STRNCMP(arg, "plugin", 6) == 0)
9649 {
9650 plugin = TRUE;
9651 arg = skipwhite(arg + 6);
9652 continue;
9653 }
9654 if (STRNCMP(arg, "indent", 6) == 0)
9655 {
9656 indent = TRUE;
9657 arg = skipwhite(arg + 6);
9658 continue;
9659 }
9660 break;
9661 }
9662 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9663 {
9664 if (*arg == 'o' || !filetype_detect)
9665 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009666 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 filetype_detect = TRUE;
9668 if (plugin)
9669 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009670 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 filetype_plugin = TRUE;
9672 }
9673 if (indent)
9674 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009675 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 filetype_indent = TRUE;
9677 }
9678 }
9679 if (*arg == 'd')
9680 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009681 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009682 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 }
9684 }
9685 else if (STRCMP(arg, "off") == 0)
9686 {
9687 if (plugin || indent)
9688 {
9689 if (plugin)
9690 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009691 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 filetype_plugin = FALSE;
9693 }
9694 if (indent)
9695 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009696 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 filetype_indent = FALSE;
9698 }
9699 }
9700 else
9701 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009702 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 filetype_detect = FALSE;
9704 }
9705 }
9706 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009707 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708}
9709
9710/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009711 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 */
9713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009714ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715{
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009716 if (did_filetype)
9717 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009718
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009719 char_u *arg = eap->arg;
9720 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9721 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009722
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009723 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9724 if (arg != eap->arg)
9725 did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009729ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731#ifdef FEAT_DIGRAPHS
9732 if (*eap->arg != NUL)
9733 putdigraph(eap->arg);
9734 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009735 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736#else
mityu61065042021-07-19 20:07:21 +02009737 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738#endif
9739}
9740
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009741#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9742 void
9743set_no_hlsearch(int flag)
9744{
9745 no_hlsearch = flag;
9746# ifdef FEAT_EVAL
9747 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9748# endif
9749}
9750
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751/*
9752 * ":nohlsearch"
9753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009755ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009757 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009758 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760#endif
9761
9762#ifdef FEAT_CRYPT
9763/*
9764 * ":X": Get crypt key
9765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009767ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009769 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009770 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771}
9772#endif
9773
9774#ifdef FEAT_FOLDING
9775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009776ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
9778 if (foldManualAllowed(TRUE))
9779 foldCreate(eap->line1, eap->line2);
9780}
9781
9782 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009783ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
9785 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9786 eap->forceit, FALSE);
9787}
9788
9789 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009790ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791{
9792 linenr_T lnum;
9793
Bram Moolenaar4facea32019-10-12 20:17:40 +02009794# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009795 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009796# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009797
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009798 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9800 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9801 ml_setmarked(lnum);
9802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009803 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009805 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009806# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009807 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809}
9810#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009811
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009812#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009813/*
9814 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9815 * instead of a quickfix command.
9816 */
9817 int
9818is_loclist_cmd(int cmdidx)
9819{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009820 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009821 return FALSE;
9822 return cmdnames[cmdidx].cmd_name[0] == 'l';
9823}
9824#endif
9825
Bram Moolenaar4facea32019-10-12 20:17:40 +02009826#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009827 int
9828get_pressedreturn(void)
9829{
9830 return ex_pressedreturn;
9831}
9832
9833 void
9834set_pressedreturn(int val)
9835{
9836 ex_pressedreturn = val;
9837}
9838#endif