blob: 80ec8a1a6fd7a1cf18361aa5d4cb83aa1cb69802 [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);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +010039static void do_exbuffer(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010040static void ex_bmodified(exarg_T *eap);
41static void ex_bnext(exarg_T *eap);
42static void ex_bprevious(exarg_T *eap);
43static void ex_brewind(exarg_T *eap);
44static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifndef FEAT_QUICKFIX
48# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000049# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# define ex_cc ex_ni
51# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020052# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define ex_cfile ex_ni
54# define qf_list ex_ni
55# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020056# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000058# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000059# define ex_cclose ex_ni
60# define ex_copen ex_ni
61# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020062# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000064#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
65# define ex_cexpr ex_ni
66#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020068static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020069static 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 +020070static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020072#if !defined(FEAT_PERL) \
73 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
74 || !defined(FEAT_TCL) \
75 || !defined(FEAT_RUBY) \
76 || !defined(FEAT_LUA) \
77 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000078# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010079static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010081static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010082static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000083#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000085#endif
John Marriotted908f72024-04-18 22:46:56 +020086static char_u *repl_cmdline(exarg_T *eap, char_u *src, size_t srclen, char_u *repl, char_u **cmdlinep);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static void ex_highlight(exarg_T *eap);
88static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010089static void ex_cquit(exarg_T *eap);
90static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010091static void ex_close(exarg_T *eap);
92static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
93static void ex_only(exarg_T *eap);
94static void ex_resize(exarg_T *eap);
95static void ex_stag(exarg_T *eap);
96static void ex_tabclose(exarg_T *eap);
97static void ex_tabonly(exarg_T *eap);
98static void ex_tabnext(exarg_T *eap);
99static void ex_tabmove(exarg_T *eap);
100static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200101#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100102static void ex_pclose(exarg_T *eap);
103static void ex_ptag(exarg_T *eap);
104static void ex_pedit(exarg_T *eap);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +0100105static void ex_pbuffer(exarg_T *eap);
106static void prepare_preview_window(void);
107static void back_to_current_window(win_T *curwin_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#else
109# define ex_pclose ex_ni
110# define ex_ptag ex_ni
111# define ex_pedit ex_ni
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +0100112# define ex_pbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static void ex_hide(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100115static void ex_exit(exarg_T *eap);
116static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#else
120# define ex_goto ex_ni
121#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_shell(exarg_T *eap);
123static void ex_preserve(exarg_T *eap);
124static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100125static void ex_mode(exarg_T *eap);
126static void ex_wrongmodifier(exarg_T *eap);
127static void ex_find(exarg_T *eap);
128static void ex_open(exarg_T *eap);
129static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#ifndef FEAT_GUI
131# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100134#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100135static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136#else
137# define ex_tearoff ex_ni
138#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100139#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
140 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#else
143# define ex_popup ex_ni
144#endif
145#ifndef FEAT_GUI_MSWIN
146# define ex_simalt ex_ni
147#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000148#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# define gui_mch_find_dialog ex_ni
150# define gui_mch_replace_dialog ex_ni
151#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000152#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153# define ex_helpfind ex_ni
154#endif
155#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100156# define ex_cscope ex_ni
157# define ex_scscope ex_ni
158# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
160#ifndef FEAT_SYN_HL
161# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200162# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000163#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200164#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200165# define ex_syntime ex_ni
166#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000167#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000168# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000170# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000171# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000172# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000173#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200174#ifndef FEAT_PERSISTENT_UNDO
175# define ex_rundo ex_ni
176# define ex_wundo ex_ni
177#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200178#ifndef FEAT_LUA
179# define ex_lua ex_script_ni
180# define ex_luado ex_ni
181# define ex_luafile ex_ni
182#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000183#ifndef FEAT_MZSCHEME
184# define ex_mzscheme ex_script_ni
185# define ex_mzfile ex_ni
186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#ifndef FEAT_PERL
188# define ex_perl ex_script_ni
189# define ex_perldo ex_ni
190#endif
191#ifndef FEAT_PYTHON
192# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200193# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194# define ex_pyfile ex_ni
195#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200197# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200198# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200199# define ex_py3file ex_ni
200#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100201#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
202# define ex_pyx ex_script_ni
203# define ex_pyxdo ex_ni
204# define ex_pyxfile ex_ni
205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#ifndef FEAT_TCL
207# define ex_tcl ex_script_ni
208# define ex_tcldo ex_ni
209# define ex_tclfile ex_ni
210#endif
211#ifndef FEAT_RUBY
212# define ex_ruby ex_script_ni
213# define ex_rubydo ex_ni
214# define ex_rubyfile ex_ni
215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#ifndef FEAT_KEYMAP
217# define ex_loadkeymap ex_ni
218#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void ex_swapname(exarg_T *eap);
220static void ex_syncbind(exarg_T *eap);
221static void ex_read(exarg_T *eap);
222static void ex_pwd(exarg_T *eap);
223static void ex_equal(exarg_T *eap);
224static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000227#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#else
230# define ex_winpos ex_ni
231#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ex_operators(exarg_T *eap);
233static void ex_put(exarg_T *eap);
234static void ex_copymove(exarg_T *eap);
235static void ex_submagic(exarg_T *eap);
236static void ex_join(exarg_T *eap);
237static void ex_at(exarg_T *eap);
238static void ex_bang(exarg_T *eap);
239static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200240#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ex_wundo(exarg_T *eap);
242static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200243#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void ex_redo(exarg_T *eap);
245static void ex_later(exarg_T *eap);
246static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100248static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_startinsert(exarg_T *eap);
252static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100254static void ex_checkpath(exarg_T *eap);
255static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#else
257# define ex_findpat ex_ni
258# define ex_checkpath ex_ni
259#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200260#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#else
263# define ex_psearch ex_ni
264#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static void ex_tag(exarg_T *eap);
266static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200268# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200269# define ex_break ex_ni
270# define ex_breakadd ex_ni
271# define ex_breakdel ex_ni
272# define ex_breaklist ex_ni
273# define ex_call ex_ni
274# define ex_catch ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000275# define ex_class ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200276# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200277# define ex_continue ex_ni
278# define ex_debug ex_ni
279# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200280# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200281# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283# define ex_echo ex_ni
284# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200286# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_endfunction ex_ni
288# define ex_endif ex_ni
289# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290# define ex_endwhile ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000291# define ex_enum ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200292# define ex_eval ex_ni
293# define ex_execute ex_ni
294# define ex_finally ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000295# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200296# define ex_finish ex_ni
297# define ex_function ex_ni
298# define ex_if ex_ni
299# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200300# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200301# define ex_lockvar ex_ni
302# define ex_oldfiles ex_ni
303# define ex_options ex_ni
304# define ex_packadd ex_ni
305# define ex_packloadall ex_ni
306# define ex_return ex_ni
307# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308# define ex_throw ex_ni
309# define ex_try ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000310# define ex_type ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200312# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313# define ex_import ex_ni
314# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200316#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317# define ex_loadview ex_ni
318#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200319#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# define ex_viminfo ex_ni
321#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100322static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100323static void ex_filetype(exarg_T *eap);
324static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000326# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327# define ex_diffpatch ex_ni
328# define ex_diffgetput ex_ni
329# define ex_diffsplit ex_ni
330# define ex_diffthis ex_ni
331# define ex_diffupdate ex_ni
332#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#else
337# define ex_nohlsearch ex_ni
338# define ex_match ex_ni
339#endif
340#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#else
343# define ex_X ex_ni
344#endif
345#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100346static void ex_fold(exarg_T *eap);
347static void ex_foldopen(exarg_T *eap);
348static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#else
350# define ex_fold ex_ni
351# define ex_foldopen ex_ni
352# define ex_folddo ex_ni
353#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100354#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355# define ex_language ex_ni
356#endif
357#ifndef FEAT_SIGNS
358# define ex_sign ex_ni
359#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000360#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200361# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000362# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200363# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365
Bram Moolenaar05159a02005-02-26 23:04:13 +0000366#ifndef FEAT_PROFILE
367# define ex_profile ex_ni
368#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200369#ifndef FEAT_TERMINAL
370# define ex_terminal ex_ni
371#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200372#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
373# define ex_xrestore ex_ni
374#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100375#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200376# define ex_popupclear ex_ni
377#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379/*
380 * Declare cmdnames[].
381 */
382#define DO_DECLARE_EXCMD
383#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200384#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static char_u dollar_command[2] = {'$', 0};
387
388
389#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100390// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391typedef struct
392{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100393 char_u *line; // command line
394 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395} wcmd_T;
396
397/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000402struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100404 garray_T *lines_gap; // growarray with line info
405 int current_line; // last read line from growarray
406 int repeating; // TRUE when looping a second time
407 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100408 char_u *(*lc_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 void *cookie;
410};
411
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200412static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100413static int store_loop_line(garray_T *gap, char_u *line);
414static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000415
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100416// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000417struct dbg_stuff
418{
419 int trylevel;
420 int force_abort;
421 except_T *caught_stack;
422 char_u *vv_exception;
423 char_u *vv_throwpoint;
424 int did_emsg;
425 int got_int;
426 int did_throw;
427 int need_rethrow;
428 int check_cstack;
429 except_T *current_exception;
430};
431
Bram Moolenaared203462004-06-16 11:19:22 +0000432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100433save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000434{
435 dsp->trylevel = trylevel; trylevel = 0;
436 dsp->force_abort = force_abort; force_abort = FALSE;
437 dsp->caught_stack = caught_stack; caught_stack = NULL;
438 dsp->vv_exception = v_exception(NULL);
439 dsp->vv_throwpoint = v_throwpoint(NULL);
440
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100441 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000442 dsp->did_emsg = did_emsg; did_emsg = FALSE;
443 dsp->got_int = got_int; got_int = FALSE;
444 dsp->did_throw = did_throw; did_throw = FALSE;
445 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
446 dsp->check_cstack = check_cstack; check_cstack = FALSE;
447 dsp->current_exception = current_exception; current_exception = NULL;
448}
449
450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100451restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000452{
453 suppress_errthrow = FALSE;
454 trylevel = dsp->trylevel;
455 force_abort = dsp->force_abort;
456 caught_stack = dsp->caught_stack;
457 (void)v_exception(dsp->vv_exception);
458 (void)v_throwpoint(dsp->vv_throwpoint);
459 did_emsg = dsp->did_emsg;
460 got_int = dsp->got_int;
461 did_throw = dsp->did_throw;
462 need_rethrow = dsp->need_rethrow;
463 check_cstack = dsp->check_cstack;
464 current_exception = dsp->current_exception;
465}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468/*
Colin Kennedy65e580b2024-03-26 18:29:30 +0100469 * Check if ffname differs from fnum.
470 * fnum is a buffer number. 0 == current buffer, 1-or-more must be a valid buffer ID.
471 * ffname is a full path to where a buffer lives on-disk or would live on-disk.
472 *
473 */
474 static int
475is_other_file(int fnum, char_u *ffname)
476{
477 if (fnum != 0)
478 {
479 if (fnum == curbuf->b_fnum)
480 return FALSE;
481
482 return TRUE;
483 }
484
485 if (ffname == NULL)
486 return TRUE;
487
488 if (*ffname == NUL)
489 return FALSE;
490
491 // TODO: Need a reliable way to know whether a buffer is meant to live on-disk
492 // !curbuf->b_dev_valid is not always available (example: missing on Windows)
493 if (curbuf->b_sfname != NULL
494 && *curbuf->b_sfname != NUL)
495 // This occurs with unsaved buffers. In which case `ffname`
496 // actually corresponds to curbuf->b_sfname
497 return fnamecmp(ffname, curbuf->b_sfname) != 0;
498
499 return otherfile(ffname);
500}
501
502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
504 * command is given.
505 */
506 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100507do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100508 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509{
510 int save_msg_scroll;
511 int prev_msg_row;
512 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100513 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000514
515 if (improved)
516 exmode_active = EXMODE_VIM;
517 else
518 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100519 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100520 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000521
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100522 // When using ":global /pat/ visual" and then "Q" we return to continue
523 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000524 if (global_busy)
525 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526
527 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100528 ++RedrawingDisabled; // don't redisplay the window
529 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100531 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 ++hold_gui_events;
533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
Bram Moolenaar32526b32019-01-19 17:43:09 +0100535 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 while (exmode_active)
537 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100538 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000539 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
540 {
541 exmode_active = FALSE;
542 break;
543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 msg_scroll = TRUE;
545 need_wait_return = FALSE;
546 ex_pressedreturn = FALSE;
547 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100548 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 prev_msg_row = msg_row;
550 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 if (improved)
552 {
553 cmdline_row = msg_row;
554 do_cmdline(NULL, getexline, NULL, 0);
555 }
556 else
557 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
558 lines_left = Rows - 1;
559
Bram Moolenaardf177f62005-02-22 08:39:57 +0000560 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100561 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000563 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000564 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000565 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000567 if (ex_pressedreturn)
568 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100569 // go up one line, to overwrite the ":<CR>" line, so the
570 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000571 msg_row = prev_msg_row;
572 if (prev_msg_row == Rows - 1)
573 msg_row--;
574 }
575 msg_col = 0;
576 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
577 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100580 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000581 {
582 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000583 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000584 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000585 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
588
589#ifdef FEAT_GUI
590 --hold_gui_events;
591#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100592 if (RedrawingDisabled > 0)
593 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100595 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 need_wait_return = FALSE;
597 msg_scroll = save_msg_scroll;
598}
599
600/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200601 * Print the executed command for when 'verbose' is set.
602 * When "lnum" is 0 only print the command.
603 */
604 static void
605msg_verbose_cmd(linenr_T lnum, char_u *cmd)
606{
607 ++no_wait_return;
608 verbose_enter_scroll();
609
610 if (lnum == 0)
611 smsg(_("Executing: %s"), cmd);
612 else
613 smsg(_("line %ld: %s"), (long)lnum, cmd);
614 if (msg_silent == 0)
615 msg_puts("\n"); // don't overwrite this
616
617 verbose_leave_scroll();
618 --no_wait_return;
619}
620
621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 * Execute a simple command line. Used for translated commands like "*".
623 */
624 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100625do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
627 return do_cmdline(cmd, NULL, NULL,
628 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
629}
630
631/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100632 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
633 * This allows for using a range without ":" in Vim9 script.
634 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200635 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100636do_cmd_argument(char_u *cmd)
637{
638 return do_cmdline(cmd, NULL, NULL,
639 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
640}
641
642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 * do_cmdline(): execute one Ex command line
644 *
645 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100646 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 * 2. Split up in parts separated with '|'.
648 *
649 * This function can be called recursively!
650 *
651 * flags:
652 * DOCMD_VERBOSE - The command will be included in the error message.
653 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100654 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 * DOCMD_KEYTYPED - Don't reset KeyTyped.
656 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
657 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
658 *
659 * return FAIL if cmdline could not be executed, OK otherwise
660 */
661 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100662do_cmdline(
663 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200664 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100666 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 char_u *next_cmdline; // next cmd to execute
669 char_u *cmdline_copy = NULL; // copy of cmd line
670 int used_getline = FALSE; // used "fgetline" to obtain command
671 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100673 int count = 0; // line number count
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100674 int did_inc_RedrawingDisabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int retval = OK;
676#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100677 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100678 garray_T lines_ga; // keep lines for ":while"/":for"
679 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200680 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100681 char_u *fname = NULL; // function or script name
682 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
683 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
684 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200686 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200687 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100689 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200690 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000692 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000694 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697# define cmd_cookie cookie
698#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100699 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200700#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100701 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
702 // location for storing error messages to be converted to an exception.
703 // This ensures that the do_errthrow() call in do_one_cmd() does not
704 // combine the messages stored by an earlier invocation of do_one_cmd()
705 // with the command name of the later one. This would happen when
706 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 saved_msg_list = msg_list;
708 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#endif
710
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100711 // It's possible to create an endless loop with ":execute", catch that
712 // here. The value of 200 allows nested function calls, ":source", etc.
713 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100714 if (call_depth >= 200
715#ifdef FEAT_EVAL
716 && call_depth >= p_mfd
717#endif
718 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000720 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100722 // When converting to an exception, we do not include the command name
723 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100724 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 msg_list = saved_msg_list;
726#endif
727 return FAIL;
728 }
729 ++call_depth;
730
731#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100732 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000734 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100738 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100739 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000740 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 ++ex_nesting_level;
742
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100743 // Get the function or script name and the address where the next breakpoint
744 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000745 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
747 fname = func_name(real_cookie);
748 breakpoint = func_breakpoint(real_cookie);
749 dbg_tick = func_dbg_tick(real_cookie);
750 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100753 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 breakpoint = source_breakpoint(real_cookie);
755 dbg_tick = source_dbg_tick(real_cookie);
756 }
757
758 /*
759 * Initialize "force_abort" and "suppress_errthrow" at the top level.
760 */
761 if (!recursive)
762 {
763 force_abort = FALSE;
764 suppress_errthrow = FALSE;
765 }
766
767 /*
768 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000769 * exception handling (used when debugging). Otherwise clear it to avoid
770 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000772 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000773 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000774 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200775 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
777 initial_trylevel = trylevel;
778
779 /*
780 * "did_throw" will be set to TRUE when an exception is being thrown.
781 */
782 did_throw = FALSE;
783#endif
784 /*
785 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000786 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 * If force_abort is set, we cancel everything.
788 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100789#ifdef FEAT_EVAL
790 did_emsg_cumul += did_emsg;
791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 did_emsg = FALSE;
793
794 /*
795 * KeyTyped is only set when calling vgetc(). Reset it here when not
796 * calling vgetc() (sourced command lines).
797 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100798 if (!(flags & DOCMD_KEYTYPED)
799 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 KeyTyped = FALSE;
801
802 /*
803 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000804 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 * - for multiple commands on one line, separated with '|'
806 * - when repeating until there are no more lines (for ":source")
807 */
808 next_cmdline = cmdline;
809 do
810 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100812 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813#endif
814
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100815 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (next_cmdline == NULL
817#ifdef FEAT_EVAL
818 && !force_abort
819 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#endif
822 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100823 {
824#ifdef FEAT_EVAL
825 did_emsg_cumul += did_emsg;
826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
830 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000831 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * 3. If a line is given: Make a copy, so we can mess with it.
834 */
835
836#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100837 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000838 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100840 // Each '|' separated command is stored separately in lines_ga, to
841 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100842 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100844 // Check if a function has returned or, unless it has an unclosed
845 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000849 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850 func_line_end(real_cookie);
851# endif
852 if (func_has_ended(real_cookie))
853 {
854 retval = FAIL;
855 break;
856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000858#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000859 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100860 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000861 script_line_end();
862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100864 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 {
867 retval = FAIL;
868 break;
869 }
870
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100871 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 if (breakpoint != NULL && dbg_tick != NULL
873 && *dbg_tick != debug_tick)
874 {
875 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100876 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100877 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 *dbg_tick = debug_tick;
879 }
880
881 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100882 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100884 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100886 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100888 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100889 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100891 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100892 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 *dbg_tick = debug_tick;
894 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000895# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000896 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897 {
898 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100899 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100900 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000901 script_line_start();
902 }
903# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905#endif
906
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100907 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (next_cmdline == NULL)
909 {
910 /*
911 * Need to set msg_didout for the first line after an ":if",
912 * otherwise the ":if" will be overwritten.
913 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100914 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100916 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917#ifdef FEAT_EVAL
918 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
919#else
920 0
921#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100922 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
923 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100925 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100926 // returned for the end of a sourced file or executed function
927 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (KeyTyped && !(flags & DOCMD_REPEAT))
929 need_wait_return = FALSE;
930 retval = FAIL;
931 break;
932 }
933 used_getline = TRUE;
934
935 /*
936 * Keep the first typed line. Clear it when more lines are typed.
937 */
938 if (flags & DOCMD_KEEPLINE)
939 {
940 vim_free(repeat_cmdline);
941 if (count == 0)
942 repeat_cmdline = vim_strsave(next_cmdline);
943 else
944 repeat_cmdline = NULL;
945 }
946 }
947
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100948 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 else if (cmdline_copy == NULL)
950 {
951 next_cmdline = vim_strsave(next_cmdline);
952 if (next_cmdline == NULL)
953 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200954 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 retval = FAIL;
956 break;
957 }
958 }
959 cmdline_copy = next_cmdline;
960
961#ifdef FEAT_EVAL
962 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200963 * Inside a while/for loop, and when the command looks like a ":while"
964 * or ":for", the line is stored, because we may need it later when
965 * looping.
966 *
967 * When there is a '|' and another command, it is stored separately,
968 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000969 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200970 *
971 * Pass a different "fgetline" function to do_one_cmd() below,
972 * that it stores lines in or reads them from "lines_ga". Makes it
973 * possible to define a function inside a while/for loop and handles
974 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200976 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200978 cmd_getline = get_loop_line;
979 cmd_cookie = (void *)&cmd_loop_cookie;
980 cmd_loop_cookie.lines_gap = &lines_ga;
981 cmd_loop_cookie.current_line = current_line;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100982 cmd_loop_cookie.lc_getline = fgetline;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200983 cmd_loop_cookie.cookie = cookie;
984 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
985
986 // Save the current line when encountering it the first time.
987 if (current_line == lines_ga.ga_len
988 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
990 retval = FAIL;
991 break;
992 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200993 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200995 else
996 {
997 cmd_getline = fgetline;
998 cmd_cookie = cookie;
999 }
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 did_endif = FALSE;
1002#endif
1003
1004 if (count++ == 0)
1005 {
1006 /*
1007 * All output from the commands is put below each other, without
1008 * waiting for a return. Don't do this when executing commands
1009 * from a script or when being called recursive (e.g. for ":e
1010 * +command file").
1011 */
1012 if (!(flags & DOCMD_NOWAIT) && !recursive)
1013 {
1014 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001015 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001017 msg_scroll = TRUE; // put messages below each other
1018 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001020 did_inc_RedrawingDisabled = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 }
1022 }
1023
Bram Moolenaar823654b2020-05-29 23:03:09 +02001024 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001025 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 /*
1028 * 2. Execute one '|' separated command.
1029 * do_one_cmd() will return NULL if there is no trailing '|'.
1030 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1031 */
1032 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001033 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034#ifdef FEAT_EVAL
1035 &cstack,
1036#endif
1037 cmd_getline, cmd_cookie);
1038 --recursive;
1039
1040#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001042 // Use "current_line" from "cmd_loop_cookie", it may have been
1043 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045#endif
1046
1047 if (next_cmdline == NULL)
1048 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001049 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001050
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 /*
1052 * If the command was typed, remember it for the ':' register.
1053 * Do this AFTER executing the command to make :@: work.
1054 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001055 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 && new_last_cmdline != NULL)
1057 {
1058 vim_free(last_cmdline);
1059 last_cmdline = new_last_cmdline;
1060 new_last_cmdline = NULL;
1061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
1063 else
1064 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001065 // need to copy the command after the '|' to cmdline_copy, for the
1066 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001067 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 next_cmdline = cmdline_copy;
1069 }
1070
1071
1072#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001073 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001075 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001077 {
1078 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
1084 ++current_line;
1085
1086 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001087 * An ":endwhile", ":endfor" and ":continue" is handled here.
1088 * If we were executing commands, jump back to the ":while" or
1089 * ":for".
1090 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001096 // Jump back to the matching ":while" or ":for". Be careful
1097 // not to use a cs_line[] from an entry that isn't a ":while"
1098 // or ":for": It would make "current_line" invalid and can
1099 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (!did_emsg && !got_int && !did_throw
1101 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 && (cstack.cs_flags[cstack.cs_idx]
1103 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 && cstack.cs_line[cstack.cs_idx] >= 0
1105 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1106 {
1107 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001108 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001110 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001112 // Check for the next breakpoint at or after the ":while"
1113 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001114 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
1116 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001117 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 fname,
1119 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1120 *dbg_tick = debug_tick;
1121 }
1122 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001125 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001127 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1128 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130 }
1131
1132 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001138 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140 }
1141
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001142 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001143 if (breakpoint != NULL && has_watchexpr())
1144 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001145 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001146 *dbg_tick = debug_tick;
1147 }
1148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 /*
1150 * When not inside any ":while" loop, clear remembered lines.
1151 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001152 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
1154 if (lines_ga.ga_len > 0)
1155 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001156 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1158 free_cmdlines(&lines_ga);
1159 }
1160 current_line = 0;
1161 }
1162
1163 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001164 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1165 * being restored at the ":endtry". Reset them here and set the
1166 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1167 * This includes the case where a missing ":endif", ":endwhile" or
1168 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 cstack.cs_lflags &= ~CSL_HAD_FINA;
1173 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1174 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 did_throw ? (void *)current_exception : NULL);
1176 did_emsg = got_int = did_throw = FALSE;
1177 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1178 }
1179
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001180 // Update global "trylevel" for recursive calls to do_cmdline() from
1181 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 trylevel = initial_trylevel + cstack.cs_trylevel;
1183
1184 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001185 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 * files) is aborted because of an error, an interrupt, or an uncaught
1187 * exception, cancel everything. If it is left normally, reset
1188 * force_abort to get the non-EH compatible abortion behavior for
1189 * the rest of the script.
1190 */
1191 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1192 force_abort = FALSE;
1193
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001194 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001196#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
1198 }
1199 /*
1200 * Continue executing command lines when:
1201 * - no CTRL-C typed, no aborting error, no exception thrown or try
1202 * conditionals need to be checked for executing finally clauses or
1203 * catching an interrupt exception
1204 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001205 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 * looping for ":source" command or function call.
1207 */
1208 while (!((got_int
1209#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001210 || (did_emsg && (force_abort || in_vim9script()))
1211 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212#endif
1213 )
1214#ifdef FEAT_EVAL
1215 && cstack.cs_trylevel == 0
1216#endif
1217 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001218 && !(did_emsg
1219#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001220 // Keep going when inside try/catch, so that the error can be
1221 // deal with, except when it is a syntax error, it may cause
1222 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001223 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1224#endif
1225 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001226 && (getline_equal(fgetline, cookie, getexmodeline)
1227 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 && (next_cmdline != NULL
1229#ifdef FEAT_EVAL
1230 || cstack.cs_idx >= 0
1231#endif
1232 || (flags & DOCMD_REPEAT)));
1233
1234 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001235 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236#ifdef FEAT_EVAL
1237 free_cmdlines(&lines_ga);
1238 ga_clear(&lines_ga);
1239
1240 if (cstack.cs_idx >= 0)
1241 {
1242 /*
1243 * If a sourced file or executed function ran to its end, report the
1244 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001245 * In Vim9 script do not give a second error, executing aborts after
1246 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001248 if (!got_int && !did_throw && !aborting()
1249 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001250 && ((getline_equal(fgetline, cookie, getsourceline)
1251 && !source_finished(fgetline, cookie))
1252 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 && !func_has_ended(real_cookie))))
1254 {
1255 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001256 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001258 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001259 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001260 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001262 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264
1265 /*
1266 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1267 * ":endtry" in a sourced file or executed function. If the try
1268 * conditional is in its finally clause, ignore anything pending.
1269 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001270 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 */
1272 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001273 {
1274 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1275
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001276 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001277 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001278 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1279 &cstack.cs_looplevel);
1280 }
1281 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 trylevel = initial_trylevel;
1283 }
1284
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001285 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1286 // lack was reported above and the error message is to be converted to an
1287 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001288 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 ? (char_u *)"endfunction" : (char_u *)NULL);
1290
1291 if (trylevel == 0)
1292 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001293 // Just in case did_throw got set but current_exception wasn't.
1294 if (current_exception == NULL)
1295 did_throw = FALSE;
1296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 /*
1298 * When an exception is being thrown out of the outermost try
1299 * conditional, discard the uncaught exception, disable the conversion
1300 * of interrupts or errors to exceptions, and ensure that no more
1301 * commands are executed.
1302 */
1303 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001304 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
1306 /*
1307 * On an interrupt or an aborting error not converted to an exception,
1308 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001309 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 * when force_abort is set and did_emsg unset in case of an interrupt
1311 * from a finally clause after an error.
1312 */
1313 else if (got_int || (did_emsg && force_abort))
1314 suppress_errthrow = TRUE;
1315 }
1316
1317 /*
1318 * The current cstack will be freed when do_cmdline() returns. An uncaught
1319 * exception will have to be rethrown in the previous cstack. If a function
1320 * has just returned or a script file was just finished and the previous
1321 * cstack belongs to the same function or, respectively, script file, it
1322 * will have to be checked for finally clauses to be executed due to the
1323 * ":return" or ":finish". This is done in do_one_cmd().
1324 */
1325 if (did_throw)
1326 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001327 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && ex_nesting_level > func_level(real_cookie) + 1))
1331 {
1332 if (!did_throw)
1333 check_cstack = TRUE;
1334 }
1335 else
1336 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001337 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001338 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 --ex_nesting_level;
1340 /*
1341 * Go to debug mode when returning from a function in which we are
1342 * single-stepping.
1343 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001344 if ((getline_equal(fgetline, cookie, getsourceline)
1345 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001347 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 ? (char_u *)_("End of sourced file")
1349 : (char_u *)_("End of function"));
1350 }
1351
1352 /*
1353 * Restore the exception environment (done after returning from the
1354 * debugger).
1355 */
1356 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001357 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
1359 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001360
1361 // Cleanup if "cs_emsg_silent_list" remains.
1362 if (cstack.cs_emsg_silent_list != NULL)
1363 {
1364 eslist_T *elem, *temp;
1365
1366 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1367 {
1368 temp = elem->next;
1369 vim_free(elem);
1370 }
1371 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001372#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373
1374 /*
1375 * If there was too much output to fit on the command line, ask the user to
1376 * hit return before redrawing the screen. With the ":global" command we do
1377 * this only once after the command is finished.
1378 */
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001379 if (did_inc_RedrawingDisabled)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001381 if (RedrawingDisabled > 0)
1382 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 --no_wait_return;
1384 msg_scroll = FALSE;
1385
1386 /*
1387 * When just finished an ":if"-":else" which was typed, no need to
1388 * wait for hit-return. Also for an error situation.
1389 */
1390 if (retval == FAIL
1391#ifdef FEAT_EVAL
1392 || (did_endif && KeyTyped && !did_emsg)
1393#endif
1394 )
1395 {
1396 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001397 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
1399 else if (need_wait_return)
1400 {
1401 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001402 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 * here should not overwrite the command that may be shown before
1404 * doing that.
1405 */
1406 msg_didout |= msg_didout_before_start;
1407 wait_return(FALSE);
1408 }
1409 }
1410
Bram Moolenaar2a942252012-11-28 23:03:07 +01001411#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001412 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001413#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 /*
1415 * Reset if_level, in case a sourced script file contains more ":if" than
1416 * ":endif" (could be ":if x | foo | endif").
1417 */
1418 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001419#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 --call_depth;
1422 return retval;
1423}
1424
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001425#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001426/*
1427 * Handle when "did_throw" is set after executing commands.
1428 */
1429 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001430handle_did_throw(void)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001431{
1432 char *p = NULL;
1433 msglist_T *messages = NULL;
ichizok7e5fe382023-04-15 13:17:50 +01001434 ESTACK_CHECK_DECLARATION;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001435
1436 /*
1437 * If the uncaught exception is a user exception, report it as an
1438 * error. If it is an error exception, display the saved error
1439 * message now. For an interrupt exception, do nothing; the
1440 * interrupt message is given elsewhere.
1441 */
1442 switch (current_exception->type)
1443 {
1444 case ET_USER:
1445 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001446 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001447 current_exception->value);
1448 p = (char *)vim_strsave(IObuff);
1449 break;
1450 case ET_ERROR:
1451 messages = current_exception->messages;
1452 current_exception->messages = NULL;
1453 break;
1454 case ET_INTERRUPT:
1455 break;
1456 }
1457
1458 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1459 current_exception->throw_lnum);
ichizok7e5fe382023-04-15 13:17:50 +01001460 ESTACK_CHECK_SETUP;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001461 current_exception->throw_name = NULL;
1462
1463 discard_current_exception(); // uses IObuff if 'verbose'
LemonBoy749ba0f2024-07-04 19:23:16 +02001464
1465 // If "silent!" is active the uncaught exception is not fatal.
1466 if (emsg_silent == 0)
1467 {
1468 suppress_errthrow = TRUE;
1469 force_abort = TRUE;
1470 }
Bram Moolenaar620c9592021-07-31 21:32:31 +02001471
1472 if (messages != NULL)
1473 {
1474 do
1475 {
1476 msglist_T *next = messages->next;
1477 int save_compiling = estack_compiling;
1478
1479 estack_compiling = messages->msg_compiling;
1480 emsg(messages->msg);
1481 vim_free(messages->msg);
1482 vim_free(messages->sfile);
1483 vim_free(messages);
1484 messages = next;
1485 estack_compiling = save_compiling;
1486 }
1487 while (messages != NULL);
1488 }
1489 else if (p != NULL)
1490 {
1491 emsg(p);
1492 vim_free(p);
1493 }
1494 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01001495 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001496 estack_pop();
1497}
1498
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001500 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
1502 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001503get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001505 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 wcmd_T *wp;
1507 char_u *line;
1508
1509 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1510 {
1511 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001512 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001514 // First time inside the ":while"/":for": get line normally.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001515 if (cp->lc_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001516 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001518 line = cp->lc_getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001519 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 ++cp->current_line;
1521
1522 return line;
1523 }
1524
1525 KeyTyped = FALSE;
1526 ++cp->current_line;
1527 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001528 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 return vim_strsave(wp->line);
1530}
1531
1532/*
1533 * Store a line in "gap" so that a ":while" loop can execute it again.
1534 */
1535 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001536store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 if (ga_grow(gap, 1) == FAIL)
1539 return FAIL;
1540 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001541 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 return OK;
1544}
1545
1546/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 */
1549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001550free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 while (gap->ga_len > 0)
1553 {
1554 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1555 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 }
1557}
1558#endif
1559
1560/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001561 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1562 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001565getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001566 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001567 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001568 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
1570#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001571 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001572 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001574 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1575 // function that's originally used to obtain the lines. This may be
1576 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001577 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001578 cp = (struct loop_cookie *)cookie;
1579 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001581 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 cp = cp->cookie;
1583 }
1584 return gp == func;
1585#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001586 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587#endif
1588}
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001591 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 * getline function. Otherwise return "cookie".
1593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001595getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001596 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001597 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
Bram Moolenaar13505972019-01-24 15:04:48 +01001599#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001600 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001601 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001603 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1604 // cookie that's originally used to obtain the lines. This may be nested
1605 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001606 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001607 cp = (struct loop_cookie *)cookie;
1608 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001610 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 cp = cp->cookie;
1612 }
1613 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001614#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001617}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001619#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001620/*
1621 * Get the next line source line without advancing.
1622 */
1623 char_u *
1624getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001625 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001626 void *cookie) // argument for fgetline()
1627{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001628 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001629 struct loop_cookie *cp;
1630 wcmd_T *wp;
1631
1632 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1633 // cookie that's originally used to obtain the lines. This may be nested
1634 // several levels.
1635 gp = fgetline;
1636 cp = (struct loop_cookie *)cookie;
1637 while (gp == get_loop_line)
1638 {
1639 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1640 {
1641 // executing lines a second time, use the stored copy
1642 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1643 return wp->line;
1644 }
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001645 gp = cp->lc_getline;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001646 cp = cp->cookie;
1647 }
1648 if (gp == getsourceline)
1649 return source_nextline(cp);
1650 return NULL;
1651}
1652#endif
1653
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001654
1655/*
1656 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1657 * ":bwipeout", etc.
1658 * Returns the buffer number.
1659 */
1660 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001661compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001662{
1663 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001664 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665 int count = offset;
1666
1667 buf = firstbuf;
1668 while (buf->b_next != NULL && buf->b_fnum < lnum)
1669 buf = buf->b_next;
1670 while (count != 0)
1671 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001672 count += (offset < 0) ? 1 : -1;
1673 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1674 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001676 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001678 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001679 while (buf->b_ml.ml_mfp == NULL)
1680 {
1681 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1682 if (nextbuf == NULL)
1683 break;
1684 buf = nextbuf;
1685 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001686 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001687 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001688 if (addr_type == ADDR_LOADED_BUFFERS)
1689 while (buf->b_ml.ml_mfp == NULL)
1690 {
1691 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1692 if (nextbuf == NULL)
1693 break;
1694 buf = nextbuf;
1695 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001696 return buf->b_fnum;
1697}
1698
Bram Moolenaarb7316892019-05-01 18:08:42 +02001699/*
1700 * Return the window number of "win".
1701 * When "win" is NULL return the number of windows.
1702 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001703 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001704current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001705{
1706 win_T *wp;
1707 int nr = 0;
1708
Bram Moolenaar29323592016-07-24 22:04:11 +02001709 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001710 {
1711 ++nr;
1712 if (wp == win)
1713 break;
1714 }
1715 return nr;
1716}
1717
1718 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001719current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001720{
1721 tabpage_T *tp;
1722 int nr = 0;
1723
Bram Moolenaar29323592016-07-24 22:04:11 +02001724 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001725 {
1726 ++nr;
1727 if (tp == tab)
1728 break;
1729 }
1730 return nr;
1731}
1732
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001733 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001734comment_start(char_u *p, int starts_with_colon UNUSED)
1735{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001736 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001737 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001738 return *p == '"';
1739}
1740
Bram Moolenaarf240e182014-11-27 18:33:02 +01001741# define CURRENT_WIN_NR current_win_nr(curwin)
1742# define LAST_WIN_NR current_win_nr(NULL)
1743# define CURRENT_TAB_NR current_tab_nr(curtab)
1744# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746/*
1747 * Execute one Ex command.
1748 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001749 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1750 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 *
1752 * 1. skip comment lines and leading space
1753 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001754 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001755 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001756 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001757 * 6. parse arguments
1758 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001760 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 *
1762 * This function may be called recursively!
1763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001765do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001766 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001767 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001769 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001771 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001772 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001774 char_u *p;
1775 linenr_T lnum;
1776 long n;
1777 char *errormsg = NULL; // error message
1778 char_u *after_modifier = NULL;
1779 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001780 cmdmod_T save_cmdmod;
1781 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001782 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001783 int ni; // set when Not Implemented
1784 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001785 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001786 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001787#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001788 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001789#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001790 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001791 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
Bram Moolenaara80faa82020-04-12 19:37:17 +02001793 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 ea.line1 = 1;
1795 ea.line2 = 1;
1796#ifdef FEAT_EVAL
1797 ++ex_nesting_level;
1798#endif
1799
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001800 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (quitmore
1802#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001803 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001804 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001805#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001806 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001807 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 --quitmore;
1809
1810 /*
1811 * Reset browse, confirm, etc.. They are restored when returning, for
1812 * recursive calls.
1813 */
1814 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001816 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001817 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1818 goto doend;
1819
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001820/*
1821 * 1. Skip comment lines and leading white space and colons.
1822 * 2. Handle command modifiers.
1823 */
1824 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001826 ea.cmdlinep = cmdlinep;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001827 ea.ea_getline = fgetline;
Bram Moolenaareffed932018-08-14 13:38:17 +02001828 ea.cookie = cookie;
1829#ifdef FEAT_EVAL
1830 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001831 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001833 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001834 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001835 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001836 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
1838#ifdef FEAT_EVAL
1839 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1840 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1841#else
1842 ea.skip = (if_level > 0);
1843#endif
1844
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001846 * 3. Skip over the range to find the command. Let "p" point to after it.
1847 *
1848 * We need the command to know what kind of range it uses.
1849 */
1850 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001851
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001852 // In Vim9 script a colon is required before the range. This may also be
1853 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001854 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001855 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001856 {
1857 may_have_range = FALSE;
1858 for (p = ea.cmd; p >= *cmdlinep; --p)
1859 {
1860 if (*p == ':')
1861 may_have_range = TRUE;
1862 if (p < ea.cmd && !VIM_ISWHITE(*p))
1863 break;
1864 }
1865 }
1866 else
1867 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001868 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001869 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001871#ifdef FEAT_EVAL
1872 // Handle ":export" - it functions almost like a command modifier.
1873 // ":export var Name: type"
1874 // ":export def Name(..."
1875 // etc.
1876 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1877 is_export = TRUE;
1878#endif
1879
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001880 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001881 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001882 if (ea.cmd == cmd + 1 && *cmd == '$')
1883 // should be "$VAR = val"
1884 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001885#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001886 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001887#else
1888 p = find_ex_command(&ea, NULL, NULL, NULL);
1889#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001890 if (ea.cmdidx == CMD_SIZE)
1891 {
1892 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1893
1894 // If a ':' before the range is missing, give a clearer error
1895 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001896 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001897 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001898 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001899 goto doend;
1900 }
1901 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001902 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001905
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001906#ifdef FEAT_EVAL
1907# ifdef FEAT_PROFILE
1908 // Count this line for profiling if skip is TRUE.
1909 if (do_profiling == PROF_YES
1910 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1911 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1912 {
1913 int skip = did_emsg || got_int || did_throw;
1914
1915 if (ea.cmdidx == CMD_catch)
1916 skip = !skip && !(cstack->cs_idx >= 0
1917 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1918 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1919 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1920 skip = skip || !(cstack->cs_idx >= 0
1921 && !(cstack->cs_flags[cstack->cs_idx]
1922 & (CSF_ACTIVE | CSF_TRUE)));
1923 else if (ea.cmdidx == CMD_finally)
1924 skip = FALSE;
1925 else if (ea.cmdidx != CMD_endif
1926 && ea.cmdidx != CMD_endfor
1927 && ea.cmdidx != CMD_endtry
1928 && ea.cmdidx != CMD_endwhile)
1929 skip = ea.skip;
1930
1931 if (!skip)
1932 {
1933 if (getline_equal(fgetline, cookie, get_func_line))
1934 func_line_exec(getline_cookie(fgetline, cookie));
1935 else if (getline_equal(fgetline, cookie, getsourceline))
1936 script_line_exec();
1937 }
1938 }
1939# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001940#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001941
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001942 ea.cmd = cmd;
1943
1944#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001945 // May go to debug mode. If this happens and the ">quit" debug command is
1946 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001947 dbg_check_breakpoint(&ea);
1948 if (!ea.skip && got_int)
1949 {
1950 ea.skip = TRUE;
1951 (void)do_intthrow(cstack);
1952 }
1953#endif
1954
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001955/*
1956 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 *
1958 * where 'addr' is:
1959 *
1960 * % (entire file)
1961 * $ [+-NUM]
1962 * 'x [+-NUM] (where x denotes a currently defined mark)
1963 * . [+-NUM]
1964 * [+-NUM]..
1965 * NUM
1966 *
1967 * The ea.cmd pointer is updated to point to the first character following the
1968 * range spec. If an initial address is found, but no second, the upper bound
1969 * is equal to the lower.
1970 */
1971
Bram Moolenaar25190db2019-05-04 15:05:28 +02001972 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001973 if (!IS_USER_CMDIDX(ea.cmdidx))
1974 {
1975 if (ea.cmdidx != CMD_SIZE)
1976 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1977 else
1978 ea.addr_type = ADDR_LINES;
1979
Bram Moolenaar25190db2019-05-04 15:05:28 +02001980 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001981 if (ea.cmdidx == CMD_wincmd && p != NULL)
1982 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001983#ifdef FEAT_QUICKFIX
1984 // :.cc in quickfix window uses line number
1985 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1986 ea.addr_type = ADDR_OTHER;
1987#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001988 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001989
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001990 if (!may_have_range)
1991 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001992 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1993 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001996 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 */
1998
1999 /*
2000 * Skip ':' and any white space
2001 */
2002 ea.cmd = skipwhite(ea.cmd);
2003 while (*ea.cmd == ':')
2004 ea.cmd = skipwhite(ea.cmd + 1);
2005
2006 /*
2007 * If we got a line, but no command, then go to the line.
2008 * If we find a '|' or '\n' we set ea.nextcmd.
2009 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002010 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
2011 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {
2013 /*
2014 * strange vi behaviour:
2015 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01002016 * ":3|..." prints line 3 (not in Vim9 script)
2017 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002019 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002021 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 goto doend;
2023 }
2024
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002025 // If this looks like an undefined user command and there are CmdUndefined
2026 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002027 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2028 && ASCII_ISUPPER(*ea.cmd)
2029 && has_cmdundefined())
2030 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002031 int ret;
2032
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002033 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002034 while (ASCII_ISALNUM(*p))
2035 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002036 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002037 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2038 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002039 // If the autocommands did something and didn't cause an error, try
2040 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002041 p = (ret
2042#ifdef FEAT_EVAL
2043 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002044#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002046 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (p == NULL)
2049 {
2050 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002051 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 goto doend;
2053 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002054 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002055 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2056 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
2058 errormsg = uc_fun_cmd();
2059 goto doend;
2060 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002061
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (ea.cmdidx == CMD_SIZE)
2063 {
2064 if (!ea.skip)
2065 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002066 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002068 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002069 // If the modifier was parsed OK the error must be in the
2070 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002071 if (after_modifier != NULL)
2072 append_command(after_modifier);
2073 else
2074 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002075 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002076 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002077 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002078 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080 goto doend;
2081 }
2082
Bram Moolenaar958636c2014-10-21 20:01:58 +02002083 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2084 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002085#ifdef HAVE_EX_SCRIPT_NI
2086 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2087#endif
2088 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090#ifndef FEAT_EVAL
2091 /*
2092 * When the expression evaluation is disabled, recognize the ":if" and
2093 * ":endif" commands and ignore everything in between it.
2094 */
2095 if (ea.cmdidx == CMD_if)
2096 ++if_level;
2097 if (if_level)
2098 {
2099 if (ea.cmdidx == CMD_endif)
2100 --if_level;
2101 goto doend;
2102 }
2103
2104#endif
2105
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002106 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002107 if (*p == '!' && ea.cmdidx != CMD_substitute
2108 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
2110 ++p;
2111 ea.forceit = TRUE;
2112 }
2113 else
2114 ea.forceit = FALSE;
2115
2116/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002117 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002119 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002120 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
2122 if (!ea.skip)
2123 {
2124#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002125 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002127 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002128 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 goto doend;
2130 }
2131#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002132 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002133 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002134 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002135 goto doend;
2136 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002137 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002139 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002140 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 goto doend;
2142 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002143
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002144 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002146 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2147 {
2148 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002149 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002150 goto doend;
2151 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002152 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2153 {
2154 // Command not allowed when text is locked
2155 errormsg = _(get_text_locked_msg());
2156 goto doend;
2157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002159
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002160 // Disallow editing another buffer when "curbuf_lock" is set.
2161 // Do allow ":checktime" (it is postponed).
2162 // Do allow ":edit" (check for an argument later).
2163 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002164 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002165 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002166 && ea.cmdidx != CMD_edit
2167 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002168 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002169 && curbuf_locked())
2170 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002172 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002174 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 goto doend;
2176 }
2177 }
2178
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002179 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002181 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 goto doend;
2183 }
2184
2185 /*
2186 * Don't complain about the range if it is not used
2187 * (could happen if line_count is accidentally set to 0).
2188 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002189 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 {
2191 /*
2192 * If the range is backwards, ask for confirmation and, if given, swap
2193 * ea.line1 & ea.line2 so it's forwards again.
2194 * When global command is busy, don't ask, will fail below.
2195 */
2196 if (!global_busy && ea.line1 > ea.line2)
2197 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002198 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002200 if (sourcing || exmode_active)
2201 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002202 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002203 goto doend;
2204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (ask_yesno((char_u *)
2206 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002207 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209 lnum = ea.line1;
2210 ea.line1 = ea.line2;
2211 ea.line2 = lnum;
2212 }
2213 if ((errormsg = invalid_range(&ea)) != NULL)
2214 goto doend;
2215 }
2216
Bram Moolenaarb7316892019-05-01 18:08:42 +02002217 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2218 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 ea.line2 = 1;
2220
2221 correct_range(&ea);
2222
2223#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002224 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002225 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002227 // Put the first line at the start of a closed fold, put the last line
2228 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 (void)hasFolding(ea.line1, &ea.line1, NULL);
2230 (void)hasFolding(ea.line2, NULL, &ea.line2);
2231 }
2232#endif
2233
2234#ifdef FEAT_QUICKFIX
2235 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002236 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 * option here, so things like % get expanded.
2238 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002239 p = replace_makeprg(&ea, p, cmdlinep);
2240 if (p == NULL)
2241 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242#endif
2243
2244 /*
2245 * Skip to start of argument.
2246 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2247 */
2248 if (ea.cmdidx == CMD_bang)
2249 ea.arg = p;
2250 else
2251 ea.arg = skipwhite(p);
2252
Bram Moolenaar379fb762018-08-30 15:58:28 +02002253 // ":file" cannot be run with an argument when "curbuf_lock" is set
2254 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2255 goto doend;
2256
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 /*
2258 * Check for "++opt=val" argument.
2259 * Must be first, allow ":w ++enc=utf8 !cmd"
2260 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002261 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2263 if (getargopt(&ea) == FAIL && !ni)
2264 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002265 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 goto doend;
2267 }
2268
2269 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2270 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002273 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002275 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 goto doend;
2277 }
2278 ea.arg = skipwhite(ea.arg + 1);
2279 ea.append = TRUE;
2280 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002281 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
2283 ++ea.arg;
2284 ea.usefilter = TRUE;
2285 }
2286 }
2287
2288 if (ea.cmdidx == CMD_read)
2289 {
2290 if (ea.forceit)
2291 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 ea.forceit = FALSE;
2294 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002295 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 ++ea.arg;
2298 ea.usefilter = TRUE;
2299 }
2300 }
2301
2302 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2303 {
2304 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002305 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
2307 ++ea.arg;
2308 ++ea.amount;
2309 }
2310 ea.arg = skipwhite(ea.arg);
2311 }
2312
2313 /*
2314 * Check for "+command" argument, before checking for next command.
2315 * Don't do this for ":read !cmd" and ":write !cmd".
2316 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002317 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2319
2320 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002321 * For commands that do not use '|' inside their argument: Check for '|' to
2322 * separate commands and '"' or '#' to start comments.
2323 *
2324 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002325 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002326 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002327 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002329 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2330 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002331 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002332 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002333 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002334 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002335 || ea.cmdidx == CMD_global
2336 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002337 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002338#ifdef FEAT_EVAL
2339 || inside_block(&ea)
2340#endif
2341 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
2343 for (p = ea.arg; *p; ++p)
2344 {
Mohamed Akramf3daa452024-07-06 17:12:09 +02002345 // Remove one backslash before a newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002347 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002348 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
2350 ea.nextcmd = p + 1;
2351 *p = NUL;
2352 break;
2353 }
2354 }
2355 }
2356
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002357 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002358 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002360 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002361 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002363 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002364 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002365 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002367#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002368 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002369 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002371 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002372 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374#endif
Christian Brabandt762a79e2025-03-16 21:39:58 +01002375 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
Bram Moolenaar958636c2014-10-21 20:01:58 +02002376 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002377 {
2378 ea.regname = *ea.arg++;
2379#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002380 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002381 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2382 {
kuuote08d7b1c2021-10-04 22:17:36 +01002383 if (!ea.skip)
2384 {
2385 set_expr_line(vim_strsave(ea.arg), &ea);
2386 did_set_expr_line = TRUE;
2387 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002388 ea.arg += STRLEN(ea.arg);
2389 }
2390#endif
2391 ea.arg = skipwhite(ea.arg);
2392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
2394
2395 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002396 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 * count, it's a buffer name.
2398 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002399 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002400 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002401 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002403 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002405 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002407 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 goto doend;
2409 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002410 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 {
2412 ea.line2 = n;
2413 if (ea.addr_count == 0)
2414 ea.addr_count = 1;
2415 }
2416 else
2417 {
2418 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002419 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002420 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002421 else
2422 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 ++ea.addr_count;
2424 /*
2425 * Be vi compatible: no error message for out of range.
2426 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002427 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 ea.line2 = curbuf->b_ml.ml_line_count;
2429 }
2430 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002431
2432 /*
2433 * Check for flags: 'l', 'p' and '#'.
2434 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002435 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002436 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002437 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2438 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002440 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002441 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 goto doend;
2443 }
2444
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002445 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002447 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 goto doend;
2449 }
2450
2451#ifdef FEAT_EVAL
2452 /*
2453 * Skip the command when it's not going to be executed.
2454 * The commands like :if, :endif, etc. always need to be executed.
2455 * Also make an exception for commands that handle a trailing command
2456 * themselves.
2457 */
2458 if (ea.skip)
2459 {
2460 switch (ea.cmdidx)
2461 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002462 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 case CMD_while:
2464 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002465 case CMD_for:
2466 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 case CMD_if:
2468 case CMD_elseif:
2469 case CMD_else:
2470 case CMD_endif:
2471 case CMD_try:
2472 case CMD_catch:
2473 case CMD_finally:
2474 case CMD_endtry:
2475 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002476 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 break;
2478
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002479 // Commands that handle '|' themselves. Check: A command should
2480 // either have the EX_TRLBAR flag, appear in this list or appear in
2481 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 case CMD_aboveleft:
2483 case CMD_and:
2484 case CMD_belowright:
2485 case CMD_botright:
2486 case CMD_browse:
2487 case CMD_call:
2488 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002489 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 case CMD_delfunction:
2491 case CMD_djump:
2492 case CMD_dlist:
2493 case CMD_dsearch:
2494 case CMD_dsplit:
2495 case CMD_echo:
2496 case CMD_echoerr:
2497 case CMD_echomsg:
2498 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002499 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002501 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002502 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 case CMD_help:
2504 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002505 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 case CMD_ijump:
2507 case CMD_ilist:
2508 case CMD_isearch:
2509 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002510 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 case CMD_keepjumps:
2512 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002513 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 case CMD_leftabove:
2515 case CMD_let:
2516 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002517 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002518 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002520 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002521 case CMD_noautocmd:
2522 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 case CMD_perl:
2524 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002525 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002526 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002527 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 case CMD_return:
2529 case CMD_rightbelow:
2530 case CMD_ruby:
2531 case CMD_silent:
2532 case CMD_smagic:
2533 case CMD_snomagic:
2534 case CMD_substitute:
2535 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002536 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 case CMD_tcl:
2538 case CMD_throw:
2539 case CMD_tilde:
2540 case CMD_topleft:
2541 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002542 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002543 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 case CMD_verbose:
2545 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002546 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 break;
2548
2549 default: goto doend;
2550 }
2551 }
2552#endif
2553
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002554 if ((ea.argt & EX_XFILE)
2555 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2556 goto doend;
2557
2558#ifdef FEAT_EVAL
2559 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002561 emsg(_(e_invalid_command_after_export));
2562 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 /*
2567 * Accept buffer name. Cannot be used at the same time with a buffer
2568 * number. Don't do this for a user command.
2569 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002570 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002571 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
2573 /*
2574 * :bdelete, :bwipeout and :bunload take several arguments, separated
2575 * by spaces: find next space (skipping over escaped characters).
2576 * The others take one argument: ignore trailing spaces.
2577 */
2578 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2579 || ea.cmdidx == CMD_bunload)
2580 p = skiptowhite_esc(ea.arg);
2581 else
2582 {
2583 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002584 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 --p;
2586 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002587 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002588 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002589 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 goto doend;
2591 ea.addr_count = 1;
2592 ea.arg = skipwhite(p);
2593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002595 // The :try command saves the emsg_silent flag, reset it here when
2596 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002597 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002598 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002599 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002600 if (emsg_silent < 0)
2601 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002602 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002603 }
2604
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002606 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
Bram Moolenaar958636c2014-10-21 20:01:58 +02002609 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 {
2611 /*
2612 * Execute a user-defined command.
2613 */
2614 do_ucmd(&ea);
2615 }
2616 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
2618 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002619 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2622 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002623 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625
2626#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002627 // A command will reset "is_export" when exporting an item. If it is still
LemonBoy90c27b22023-08-27 19:28:15 +02002628 // set something went wrong or the command was never executed.
2629 if (!ea.skip && is_export)
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002630 {
2631 if (errormsg == NULL)
2632 errormsg = _(e_export_with_invalid_argument);
2633 is_export = FALSE;
2634 }
2635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002637 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002638 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002639 && current_sctx.sc_sid > 0
2640 && ea.cmdidx != CMD_endif
2641 && (cstack->cs_idx < 0
2642 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002643 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 /*
2646 * If the command just executed called do_cmdline(), any throw or ":return"
2647 * or ":finish" encountered there must also check the cstack of the still
2648 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2649 * exception, or reanimate a returned function or finished script file and
2650 * return or finish it again.
2651 */
2652 if (need_rethrow)
2653 do_throw(cstack);
2654 else if (check_cstack)
2655 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002656 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002658 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 && current_func_returned())
2660 do_return(&ea, TRUE, FALSE, NULL);
2661 }
2662 need_rethrow = check_cstack = FALSE;
2663#endif
2664
2665doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002666 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002667 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002669 curwin->w_cursor.col = 0;
2670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2673 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002674 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002676 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 {
2678 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002679 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002681 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 }
2683 emsg(errormsg);
2684 }
2685#ifdef FEAT_EVAL
2686 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002687 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2688 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002689
2690 if (did_set_expr_line)
2691 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002692 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693#endif
2694
Bram Moolenaare1004402020-10-24 20:49:43 +02002695 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002697 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002698 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002700 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 ea.nextcmd = NULL;
2702
2703#ifdef FEAT_EVAL
2704 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002705 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706#endif
2707
2708 return ea.nextcmd;
2709}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002711static char ex_error_buf[MSG_BUF_LEN];
2712
2713/*
2714 * Return an error message with argument included.
2715 * Uses a static buffer, only the last error will be kept.
2716 * "msg" will be translated, caller should use N_().
2717 */
2718 char *
2719ex_errmsg(char *msg, char_u *arg)
2720{
2721 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2722 return ex_error_buf;
2723}
2724
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725/*
zeertzjq7d664bf2024-07-14 10:22:54 +02002726 * The "+" string used in place of an empty command in Ex mode.
2727 * This string is used in pointer comparison.
2728 */
2729static char exmode_plus[] = "+";
2730
2731/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002732 * Handle a range without a command.
2733 * Returns an error message on failure.
2734 */
2735 char *
2736ex_range_without_command(exarg_T *eap)
2737{
2738 char *errormsg = NULL;
2739
zeertzjq7d664bf2024-07-14 10:22:54 +02002740 if ((*eap->cmd == '|' ||
2741 (exmode_active && eap->cmd != (char_u *)exmode_plus + 1))
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002742#ifdef FEAT_EVAL
2743 && !in_vim9script()
2744#endif
2745 )
2746 {
2747 eap->cmdidx = CMD_print;
2748 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2749 if ((errormsg = invalid_range(eap)) == NULL)
2750 {
2751 correct_range(eap);
2752 ex_print(eap);
2753 }
2754 }
2755 else if (eap->addr_count != 0)
2756 {
2757 if (eap->line2 > curbuf->b_ml.ml_line_count)
2758 {
2759 // With '-' in 'cpoptions' a line number past the file is an
2760 // error, otherwise put it at the end of the file.
2761 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2762 eap->line2 = -1;
2763 else
2764 eap->line2 = curbuf->b_ml.ml_line_count;
2765 }
2766
2767 if (eap->line2 < 0)
2768 errormsg = _(e_invalid_range);
2769 else
2770 {
2771 if (eap->line2 == 0)
2772 curwin->w_cursor.lnum = 1;
2773 else
2774 curwin->w_cursor.lnum = eap->line2;
2775 beginline(BL_SOL | BL_FIX);
2776 }
2777 }
2778 return errormsg;
2779}
2780
2781/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002782 * Check for an Ex command with optional tail.
2783 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002784 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002785 */
2786 static int
2787checkforcmd_opt(
2788 char_u **pp, // start of command
2789 char *cmd, // name of command
2790 int len, // required length
2791 int noparen)
2792{
2793 int i;
2794
2795 for (i = 0; cmd[i] != NUL; ++i)
2796 if (((char_u *)cmd)[i] != (*pp)[i])
2797 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002798 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002799 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002800 {
2801 *pp = skipwhite(*pp + i);
2802 return TRUE;
2803 }
2804 return FALSE;
2805}
2806
2807/*
2808 * Check for an Ex command with optional tail.
2809 * If there is a match advance "pp" to the argument and return TRUE.
2810 */
2811 int
2812checkforcmd(
2813 char_u **pp, // start of command
2814 char *cmd, // name of command
2815 int len) // required length
2816{
2817 return checkforcmd_opt(pp, cmd, len, FALSE);
2818}
2819
2820/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002821 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002822 * If there is a match advance "pp" to the argument and return TRUE.
2823 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002824 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002825checkforcmd_noparen(
2826 char_u **pp, // start of command
2827 char *cmd, // name of command
2828 int len) // required length
2829{
2830 return checkforcmd_opt(pp, cmd, len, TRUE);
2831}
2832
2833/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002834 * Parse and skip over command modifiers:
2835 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002836 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002838 * When "skip_only" is TRUE the global variables are not changed, except for
2839 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002840 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2841 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002842 * Call apply_cmdmod() to get the side effects of the modifiers:
2843 * - Increment "sandbox" for ":sandbox"
2844 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002845 * - set msg_silent for ":silent"
2846 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 * Return FAIL when the command is not to be executed.
2848 * May set "errormsg" to an error message.
2849 */
2850 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002851parse_command_modifiers(
2852 exarg_T *eap,
2853 char **errormsg,
2854 cmdmod_T *cmod,
2855 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002856{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002857 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002858 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002859 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002860 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002861 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002862 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002863
Bram Moolenaare1004402020-10-24 20:49:43 +02002864 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002865 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002866
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002867 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2868 {
2869 // The automatically inserted Visual area range is skipped, so that
2870 // typing ":cmdmod cmd" in Visual mode works without having to move the
zeertzjqd9be94c2024-07-14 10:20:20 +02002871 // range to after the modifiers. The command will be "'<,'>cmdmod cmd",
2872 // parse "cmdmod cmd" and then put back "'<,'>" before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002873 eap->cmd += 5;
2874 cmd_start = eap->cmd;
2875 has_visual_range = TRUE;
2876 }
2877
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002878 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002879 for (;;)
2880 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002881 char_u *p;
2882
Bram Moolenaareffed932018-08-14 13:38:17 +02002883 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002884 {
2885 if (*eap->cmd == ':')
2886 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002887 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002888 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002889
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002890 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002891 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002892 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2893 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2895 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002896 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002897 if (!skip_only)
2898 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002899 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002900 }
2901
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002902 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002903 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002904 {
2905 // a comment ends at a NL
zeertzjqf7b86092024-09-05 17:26:30 +02002906 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2907 if (eap->nextcmd != NULL)
2908 ++eap->nextcmd;
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002909 if (vim9script)
2910 {
2911 if (has_cmdmod(cmod, FALSE))
2912 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002913#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002914 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2915 && eap->cmd[2] != '{')
2916 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002917#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002918 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002919 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002920 }
zeertzjqf7b86092024-09-05 17:26:30 +02002921 if (*eap->cmd == '\n')
zeertzjq2432b4a2024-09-03 22:58:30 +02002922 {
2923 eap->nextcmd = eap->cmd + 1;
2924 return FAIL;
2925 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002926 if (*eap->cmd == NUL)
2927 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002928 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002929 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002930 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002931 if (vim9script && has_cmdmod(cmod, FALSE))
2932 *errormsg = _(e_command_modifier_without_command);
2933 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002934 return FAIL;
2935 }
2936
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002937 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002938
2939 // In Vim9 script a variable can shadow a command modifier:
2940 // verbose = 123
2941 // verbose += 123
2942 // silent! verbose = func()
2943 // verbose.member = 2
2944 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002945 // But not:
2946 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002947 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002948 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002949 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002950
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002951 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002952 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002953 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002954 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2955 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002956 break;
2957 }
2958
Bram Moolenaareffed932018-08-14 13:38:17 +02002959 switch (*p)
2960 {
John Marriotted908f72024-04-18 22:46:56 +02002961 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002962 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002964 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002965 continue;
2966
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002967 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002968 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002969 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002970 continue;
2971 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002972 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002973 {
2974#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002975 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002976#endif
2977 continue;
2978 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002979 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002980 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002981 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002982 continue;
2983
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002984 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002985 break;
2986#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002987 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002988#endif
2989 continue;
2990
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002991 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002992 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002993 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 continue;
2995 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002996 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002998 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 continue;
3000 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003001 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003003 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 continue;
3005 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003006 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003008 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 continue;
3010
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003011 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003012 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003013 char_u *reg_pat;
3014 char_u *nulp = NULL;
3015 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003016
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003017 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003018 || *p == NUL
3019 || (ends_excmd(*p)
3020#ifdef FEAT_EVAL
3021 // in ":filter #pat# cmd" # does not
3022 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003023 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003024#endif
3025 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003026 break;
3027 if (*p == '!')
3028 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003029 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003030 p = skipwhite(p + 1);
3031 if (*p == NUL || ends_excmd(*p))
3032 break;
3033 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003034#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003035 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003036 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003037 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003038#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003039 if (skip_only)
3040 p = skip_vimgrep_pat(p, NULL, NULL);
3041 else
3042 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003043 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3044 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003045 if (p == NULL || *p == NUL)
3046 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003047 if (!skip_only)
3048 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003049 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003050 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003051 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003052 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003053 // restore the character overwritten by NUL
3054 if (nulp != NULL)
3055 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003056 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003057 eap->cmd = p;
3058 continue;
3059 }
3060
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003061 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3062 {
3063 cmod->cmod_split |= WSP_HOR;
3064 continue;
3065 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003066 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003067 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003068 || *p == NUL || ends_excmd(*p))
3069 break;
3070 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003071 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003072 continue;
3073
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003074 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003075 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003076 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003077 continue;
3078 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003079 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3080 {
3081 if (ends_excmd2(p, eap->cmd))
3082 {
3083 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003084 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003085 return FAIL;
3086 }
3087 cmod->cmod_flags |= CMOD_LEGACY;
3088 continue;
3089 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003090
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003091 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003092 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003093 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003094 continue;
3095
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003096 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003097 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003098 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003099 continue;
3100 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003101 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003102 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003103 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003104 continue;
3105
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003106 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003107 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003108 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003109 continue;
3110
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003111 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003112 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003113 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003114 continue;
3115 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003116 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003117 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003118 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003119 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3120 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003121 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003122 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003123 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003124 }
3125 continue;
3126
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003127 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003128 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003129 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003130 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003131 long tabnr = get_address(eap, &eap->cmd,
3132 ADDR_TABS, eap->skip,
3133 skip_only, FALSE, 1);
3134 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003135 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003136 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003137 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003138 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3139 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003140 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003141 return FAIL;
3142 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003143 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003144 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003145 }
3146 eap->cmd = p;
3147 continue;
3148 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003149 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003150 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003151 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003152 continue;
3153
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003154 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003155 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003156 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003157 continue;
3158
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003159 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003160 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003161 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003162 continue;
3163 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003164 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003165 {
3166 if (ends_excmd2(p, eap->cmd))
3167 {
3168 *errormsg =
3169 _(e_vim9cmd_must_be_followed_by_command);
3170 return FAIL;
3171 }
3172 cmod->cmod_flags |= CMOD_VIM9CMD;
3173 continue;
3174 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003175 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003176 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003177 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003178 {
zeertzjqcd749632022-06-14 13:30:35 +01003179 // zero means not set, one is verbose == 0, etc.
3180 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003181 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003182 else
zeertzjqcd749632022-06-14 13:30:35 +01003183 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003184 eap->cmd = p;
3185 continue;
3186 }
3187 break;
3188 }
3189
Bram Moolenaar1501b632022-03-27 16:56:21 +01003190 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003191 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003192 if (eap->cmd > cmd_start)
3193 {
3194 // Move the '<,'> range to after the modifiers and insert a colon.
3195 // Since the modifiers have been parsed put the colon on top of the
3196 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3197 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003198 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003199 {
3200 size_t len = STRLEN(cmd_start);
3201
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003202 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003203 // "'<,'>mods" -> "mods *+
3204 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003205 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003206 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003207 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003208 }
3209 else
3210 {
3211 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3212 eap->cmd -= 5;
3213 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3214 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003215 }
3216 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003217 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003218 // Special case: change empty command to "+".
3219 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003220 eap->cmd = (char_u *)"'<,'>+";
3221 else
3222 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003223 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003224 else if (use_plus_cmd)
zeertzjq7d664bf2024-07-14 10:22:54 +02003225 eap->cmd = (char_u *)exmode_plus;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003226
Bram Moolenaareffed932018-08-14 13:38:17 +02003227 return OK;
3228}
3229
3230/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003231 * Return TRUE if "cmod" has anything set.
3232 */
3233 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003234has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003235{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003236 return (cmod->cmod_flags != 0 && (!ignore_silent
3237 || (cmod->cmod_flags
3238 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003239 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003240 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003241 || cmod->cmod_tab != 0
3242 || cmod->cmod_filter_regmatch.regprog != NULL;
3243}
3244
Bram Moolenaar8991be22022-02-14 21:51:46 +00003245#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003246/*
3247 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3248 */
3249 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003250cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003251{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003252 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003253 {
3254 emsg(_(e_misplaced_command_modifier));
3255 return TRUE;
3256 }
3257 return FALSE;
3258}
Dominique Pelle748b3082022-01-08 12:41:16 +00003259#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003260
3261/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003262 * Apply the command modifiers. Saves current state in "cmdmod", call
3263 * undo_cmdmod() later.
3264 */
3265 void
3266apply_cmdmod(cmdmod_T *cmod)
3267{
3268#ifdef HAVE_SANDBOX
3269 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3270 {
3271 ++sandbox;
3272 cmod->cmod_did_sandbox = TRUE;
3273 }
3274#endif
zeertzjqcd749632022-06-14 13:30:35 +01003275 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003276 {
3277 if (cmod->cmod_verbose_save == 0)
3278 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003279 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003280 }
3281
3282 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3283 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003284 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003285 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003286 cmod->cmod_save_msg_scroll = msg_scroll;
3287 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003288 if (cmod->cmod_flags & CMOD_SILENT)
3289 ++msg_silent;
3290 if (cmod->cmod_flags & CMOD_UNSILENT)
3291 msg_silent = 0;
3292
3293 if (cmod->cmod_flags & CMOD_ERRSILENT)
3294 {
3295 ++emsg_silent;
3296 ++cmod->cmod_did_esilent;
3297 }
3298
Bram Moolenaare1004402020-10-24 20:49:43 +02003299 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003300 {
3301 // Set 'eventignore' to "all".
3302 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003303 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003304 set_string_option_direct((char_u *)"ei", -1,
3305 (char_u *)"all", OPT_FREE, SID_NONE);
3306 }
3307}
3308
3309/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003310 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003311 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003312 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003313undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003314{
Bram Moolenaare1004402020-10-24 20:49:43 +02003315 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003316 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003317 p_verbose = cmod->cmod_verbose_save - 1;
3318 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003319 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003320
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003321#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003322 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003323 {
3324 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003325 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003326 }
3327#endif
3328
Bram Moolenaare1004402020-10-24 20:49:43 +02003329 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003330 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003331 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003332 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3333 OPT_FREE, SID_NONE);
3334 free_string_option(cmod->cmod_save_ei);
3335 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003336 }
3337
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003338 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003339
Bram Moolenaare1004402020-10-24 20:49:43 +02003340 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003341 {
3342 // messages could be enabled for a serious error, need to check if the
3343 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003344 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3345 msg_silent = cmod->cmod_save_msg_silent - 1;
3346 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003347 if (emsg_silent < 0)
3348 emsg_silent = 0;
3349 // Restore msg_scroll, it's set by file I/O commands, even when no
3350 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003351 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003352
3353 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3354 // somewhere in the line. Put it back in the first column.
3355 if (redirecting())
3356 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003357
Bram Moolenaare1004402020-10-24 20:49:43 +02003358 cmod->cmod_save_msg_silent = 0;
3359 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003360 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003361}
3362
3363/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003364 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003365 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003366 * Return FAIL and set "errormsg" or return OK.
3367 */
3368 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003369parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003370{
3371 int address_count = 1;
3372 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003373 int need_check_cursor = FALSE;
3374 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003375
3376 // Repeat for all ',' or ';' separated addresses.
3377 for (;;)
3378 {
3379 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003380 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003381 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003382 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003383 eap->addr_count == 0, address_count++);
3384 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003385 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003386 if (lnum == MAXLNUM)
3387 {
3388 if (*eap->cmd == '%') // '%' - all lines
3389 {
3390 ++eap->cmd;
3391 switch (eap->addr_type)
3392 {
3393 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003394 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003395 eap->line1 = 1;
3396 eap->line2 = curbuf->b_ml.ml_line_count;
3397 break;
3398 case ADDR_LOADED_BUFFERS:
3399 {
3400 buf_T *buf = firstbuf;
3401
3402 while (buf->b_next != NULL
3403 && buf->b_ml.ml_mfp == NULL)
3404 buf = buf->b_next;
3405 eap->line1 = buf->b_fnum;
3406 buf = lastbuf;
3407 while (buf->b_prev != NULL
3408 && buf->b_ml.ml_mfp == NULL)
3409 buf = buf->b_prev;
3410 eap->line2 = buf->b_fnum;
3411 break;
3412 }
3413 case ADDR_BUFFERS:
3414 eap->line1 = firstbuf->b_fnum;
3415 eap->line2 = lastbuf->b_fnum;
3416 break;
3417 case ADDR_WINDOWS:
3418 case ADDR_TABS:
3419 if (IS_USER_CMDIDX(eap->cmdidx))
3420 {
3421 eap->line1 = 1;
3422 eap->line2 = eap->addr_type == ADDR_WINDOWS
3423 ? LAST_WIN_NR : LAST_TAB_NR;
3424 }
3425 else
3426 {
3427 // there is no Vim command which uses '%' and
3428 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003429 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003430 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003431 }
3432 break;
3433 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003434 case ADDR_UNSIGNED:
3435 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003436 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003437 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003438 case ADDR_ARGUMENTS:
3439 if (ARGCOUNT == 0)
3440 eap->line1 = eap->line2 = 0;
3441 else
3442 {
3443 eap->line1 = 1;
3444 eap->line2 = ARGCOUNT;
3445 }
3446 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003447 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003448#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003449 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003450 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003451 if (eap->line2 == 0)
3452 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003453#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003454 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003455 case ADDR_NONE:
3456 // Will give an error later if a range is found.
3457 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003458 }
3459 ++eap->addr_count;
3460 }
3461 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3462 {
3463 pos_T *fp;
3464
3465 // '*' - visual area
3466 if (eap->addr_type != ADDR_LINES)
3467 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003468 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003469 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003470 }
3471
3472 ++eap->cmd;
3473 if (!eap->skip)
3474 {
3475 fp = getmark('<', FALSE);
3476 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003477 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003478 eap->line1 = fp->lnum;
3479 fp = getmark('>', FALSE);
3480 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003481 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003482 eap->line2 = fp->lnum;
3483 ++eap->addr_count;
3484 }
3485 }
3486 }
3487 else
3488 eap->line2 = lnum;
3489 eap->addr_count++;
3490
3491 if (*eap->cmd == ';')
3492 {
3493 if (!eap->skip)
3494 {
3495 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003496
Bram Moolenaar0e717042020-04-27 19:29:01 +02003497 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003498 // accept zero as address, so 0;/PATTERN/ works correctly
3499 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003500 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003501 if (eap->line2 > 0)
3502 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003503 else
3504 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003505 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003506 }
3507 }
3508 else if (*eap->cmd != ',')
3509 break;
3510 ++eap->cmd;
3511 }
3512
3513 // One address given: set start and end lines.
3514 if (eap->addr_count == 1)
3515 {
3516 eap->line1 = eap->line2;
3517 // ... but only implicit: really no address given
3518 if (lnum == MAXLNUM)
3519 eap->addr_count = 0;
3520 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003521 ret = OK;
3522
3523theend:
3524 if (need_check_cursor)
3525 check_cursor();
3526 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003527}
3528
3529/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003530 * Append "cmd" to the error message in IObuff.
3531 * Takes care of limiting the length and handling 0xa0, which would be
3532 * invisible otherwise.
3533 */
3534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003535append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003536{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003537 size_t len = STRLEN(IObuff);
3538 char_u *s = cmd;
3539 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003540
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003541 if (len > IOSIZE - 100)
3542 {
3543 // Not enough space, truncate and put in "...".
3544 d = IObuff + IOSIZE - 100;
3545 d -= mb_head_off(IObuff, d);
3546 STRCPY(d, "...");
3547 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003548 STRCAT(IObuff, ": ");
3549 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003550 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003551 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003552 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003553 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003554 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003555 STRCPY(d, "<a0>");
3556 d += 4;
3557 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003558 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3559 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003560 else
3561 MB_COPY_CHAR(s, d);
3562 }
3563 *d = NUL;
3564}
3565
Dominique Pelle748b3082022-01-08 12:41:16 +00003566#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003567/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003568 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3569 * the name. Otherwise just return "start".
3570 */
3571 char_u *
3572skip_option_env_lead(char_u *start)
3573{
3574 char_u *name = start;
3575
3576 if (*start == '&')
3577 {
3578 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3579 name += 3;
3580 else
3581 name += 1;
3582 }
3583 else if (*start == '$')
3584 name += 1;
3585 return name;
3586}
Dominique Pelle748b3082022-01-08 12:41:16 +00003587#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003588
3589/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003590 * Return TRUE and set "*idx" if "p" points to a one letter command.
3591 * If not in Vim9 script:
Doug Kearnsea842022024-09-29 17:17:41 +02003592 * - The 'k' command can directly be followed by any character
3593 * but :keepa[lt] is another command, as are :keepj[umps],
3594 * :kee[pmarks] and :keepp[atterns].
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003595 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3596 * but :sre[wind] is another command, as are :scr[iptnames],
3597 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3598 */
3599 static int
3600one_letter_cmd(char_u *p, cmdidx_T *idx)
3601{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003602 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003603 return FALSE;
Doug Kearnsea842022024-09-29 17:17:41 +02003604 if (p[0] == 'k'
3605 && (p[1] != 'e' || (p[1] == 'e' && p[2] != 'e')))
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003606 {
3607 *idx = CMD_k;
3608 return TRUE;
3609 }
3610 if (p[0] == 's'
3611 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3612 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3613 || p[1] == 'g'
3614 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3615 || p[1] == 'I'
3616 || (p[1] == 'r' && p[2] != 'e')))
3617 {
3618 *idx = CMD_substitute;
3619 return TRUE;
3620 }
3621 return FALSE;
3622}
3623
Dominique Pellee764d1b2023-03-12 21:20:59 +00003624#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003625/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003626 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3627 * call.
3628 */
3629 int
3630number_method(char_u *cmd)
3631{
3632 char_u *p = skipdigits(cmd);
3633
3634 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3635}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003636#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003637
3638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003640 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003641 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003642 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 *
3644 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3645 * assignment without "let". Sets eap->cmdidx to the command while returning
3646 * "eap->cmd".
3647 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 * Returns NULL for an ambiguous user command.
3649 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 char_u *
3651find_ex_command(
3652 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003653 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003654 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003655 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656{
3657 int len;
3658 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003659 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003660#ifndef FEAT_EVAL
3661 int vim9 = FALSE;
3662#else
3663 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 /*
3666 * Recognize a Vim9 script function/method call and assignment:
3667 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3668 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003669 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003670 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003672 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003674 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003675 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3676 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003678 int oplen;
3679 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003680 char_u *swp;
3681
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003682 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003683 || (eap->cmd[0] == '$'
3684 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003685 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003686 && (valid_yank_reg(eap->cmd[1], FALSE)
3687 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003688 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003689 if (*eap->cmd == '&')
3690 {
3691 p = eap->cmd + 1;
3692 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3693 p += 2;
3694 p = to_name_end(p, FALSE);
3695 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003696 else if (*eap->cmd == '$')
3697 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003698 else
3699 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003700 if (ends_excmd(*skipwhite(p)))
3701 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003702 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3703 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003704 eap->cmdidx = CMD_eval;
3705 return eap->cmd;
3706 }
3707 // "&option" can be followed by "->" or "=", check below
3708 }
3709
3710 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003712 if (
3713 // "(..." is an expression.
3714 // "funcname(" is always a function call.
3715 *p == '('
3716 || (p == eap->cmd
3717 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003718 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003719 *eap->cmd == '{'
3720 // "'string'->func()" is an expression.
3721 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003722 // '"string"->func()' is an expression.
3723 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003724 // '$"string"->func()' is an expression.
3725 // "$'string'->func()" is an expression.
3726 || (eap->cmd[0] == '$'
3727 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3728 // '0z1234->func()' is an expression.
3729 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003730 // "g:varname" is an expression.
3731 || eap->cmd[1] == ':'
3732 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003733 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003734 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003735 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003736 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3737 {
3738 // "{" by itself is the start of a block.
3739 eap->cmdidx = CMD_block;
3740 return eap->cmd + 1;
3741 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003742 eap->cmdidx = CMD_eval;
3743 return eap->cmd;
3744 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003745
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003746 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003747 // "varname[]" is an expression.
3748 *p == '['
3749 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003750 || (*p == '.'
3751 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3752 // g:[key] is an expression
3753 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003754 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003755 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003756
3757 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003758 // Skip over the whole thing, it can be:
3759 // name.member = val
3760 // name[a : b] = val
3761 // name[idx] = val
3762 // name[idx].member = val
3763 // etc.
3764 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003765 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003766 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003767 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003768 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003769 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003770 || (after[0] == '.' && after[1] == '.'
3771 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003772 eap->cmdidx = CMD_var;
3773 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003774 --emsg_silent;
3775 return eap->cmd;
3776 }
3777
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003778 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3779 // an assignment.
3780 // If there is no line break inside the "[...]" then "p" is
3781 // advanced to after the "]" by to_name_const_end(): check if a "="
3782 // follows.
3783 // If "[...]" has a line break "p" still points at the "[" and it
3784 // can't be an assignment.
3785 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003786 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003787 char_u *eq;
3788
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003789 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003790 if (p == eap->cmd && *p == '[')
3791 {
3792 int count = 0;
3793 int semicolon = FALSE;
3794
3795 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3796 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003797 eq = p;
3798 if (eq != NULL)
3799 {
3800 eq = skipwhite(eq);
3801 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3802 ++eq;
3803 }
3804 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003805 {
3806 eap->cmdidx = CMD_eval;
3807 return eap->cmd;
3808 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003809 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003810 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003811 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003812 return eap->cmd;
3813 }
3814 }
3815
3816 // Recognize an assignment if we recognize the variable name:
3817 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003818 // "@r = expr"
3819 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003820 // "var = expr" where "var" is a variable name or we are skipping
3821 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003822 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003823 oplen = assignment_len(skipwhite(p), &heredoc);
3824 if (oplen > 0)
3825 {
3826 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3827 || *eap->cmd == '&'
3828 || *eap->cmd == '$'
3829 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003830 || (eap->skip && IS_WHITE_OR_NUL(
3831 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003832 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003833 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003834 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003835 return eap->cmd;
3836 }
3837 }
3838
Bram Moolenaar9510d222022-09-11 15:14:05 +01003839 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003840 // "w:varname: number = 123".
3841 if (eap->cmd[1] == ':' && *p == ':')
3842 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003843 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003844 return eap->cmd;
3845 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003846 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003847
Bram Moolenaar31d99482022-05-26 22:24:43 +01003848 // 1234->func() is a method call
3849 if (number_method(eap->cmd))
3850 {
3851 eap->cmdidx = CMD_eval;
3852 return eap->cmd;
3853 }
3854
Bram Moolenaar7b829262021-10-13 15:04:34 +01003855 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3856 // an expression. A global/substitute/list command needs to use a
3857 // longer name.
3858 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3859 {
3860 eap->cmdidx = CMD_eval;
3861 return eap->cmd;
3862 }
3863
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003864 // If it is an ID it might be a variable with an operator on the next
3865 // line, if the variable exists it can't be an Ex command.
3866 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003867 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003868 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3869 {
3870 eap->cmdidx = CMD_eval;
3871 return eap->cmd;
3872 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003873
3874 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003875 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3876 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003877 {
3878 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3879 return eap->cmd + 2;
3880 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 }
3882#endif
3883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 /*
3885 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 */
3887 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003888 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 ++p;
Doug Kearnsea842022024-09-29 17:17:41 +02003891 if (full != NULL)
3892 *full = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
3894 else
3895 {
3896 while (ASCII_ISALPHA(*p))
3897 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003898 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003899 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003901 while (ASCII_ISALNUM(*p))
3902 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 }
3904 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3905 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003906 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 ++p;
3908 while (ASCII_ISALPHA(*p))
3909 ++p;
3910 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003912 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003913 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 ++p;
3915 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003916 // The "d" command can directly be followed by 'l' or 'p' flag, when
3917 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003918 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003919 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003920 // Check for ":dl", ":dell", etc. to ":deletel": that's
3921 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003922 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003923 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003924 break;
3925 if (i == len - 1)
3926 {
3927 --len;
3928 if (p[-1] == 'l')
3929 eap->flags |= EXFLAG_LIST;
3930 else
3931 eap->flags |= EXFLAG_PRINT;
3932 }
3933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003935 if (ASCII_ISLOWER(eap->cmd[0]))
3936 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003937 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003938 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003939
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003940 if (command_count != (int)CMD_SIZE)
3941 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003942 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003943 getout(1);
3944 }
3945
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003946 // Use a precomputed index for fast look-up in cmdnames[]
3947 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003948 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3949 if (ASCII_ISLOWER(c2))
3950 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3951 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003952 else if (ASCII_ISUPPER(eap->cmd[0]))
3953 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003955 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
3957 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3958 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3959 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3960 (size_t)len) == 0)
3961 {
3962#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003963 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 *full = TRUE;
3965#endif
3966 break;
3967 }
3968
Bram Moolenaar204852a2022-03-05 12:56:44 +00003969 // :Print and :mode are not supported in Vim9 script.
3970 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003971 if (vim9 && eap->cmdidx != CMD_SIZE)
3972 {
3973 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3974 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003975 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003976 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003977 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003978 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003979 eap->cmdidx = CMD_SIZE;
3980 }
3981 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003982
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003983 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003984 // 'cpoptions'.
3985 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3986 p = eap->cmd;
3987
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003988 // Look for a user defined command as a last resort. Let ":Print" be
3989 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003990 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3991 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003993 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 while (ASCII_ISALNUM(*p))
3995 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003996 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003998 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 eap->cmdidx = CMD_SIZE;
4000 }
4001
Bram Moolenaar204852a2022-03-05 12:56:44 +00004002 // ":fina" means ":finally" in legacy script, for backwards compatibility.
4003 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02004004 eap->cmdidx = CMD_finally;
4005
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004006#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004007 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004008 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02004009 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004010 && (eap->cmdidx < 0 ||
4011 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004012 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004013 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4014
4015 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004016 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004017 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004018 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004019#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004020
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 return p;
4022}
4023
4024#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004025typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004026{
4027 char *name;
4028 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004029 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004030} cmdmod_info_T;
4031
4032static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004033 {"aboveleft", 3, FALSE},
4034 {"belowright", 3, FALSE},
4035 {"botright", 2, FALSE},
4036 {"browse", 3, FALSE},
4037 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004038 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004039 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004040 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004041 {"keepalt", 5, FALSE},
4042 {"keepjumps", 5, FALSE},
4043 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004044 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004045 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004046 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004047 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004048 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004049 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004050 {"rightbelow", 6, FALSE},
4051 {"sandbox", 3, FALSE},
4052 {"silent", 3, FALSE},
4053 {"tab", 3, TRUE},
4054 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004055 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004056 {"verbose", 4, TRUE},
4057 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004058 {"vim9cmd", 4, FALSE}
4059}; // cmdmod_info_tab
4060
Bram Moolenaared53fb92007-11-24 20:50:24 +00004061/*
4062 * Return length of a command modifier (including optional count).
4063 * Return zero when it's not a modifier.
4064 */
4065 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004066modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004067{
Christian Brabandt70a11a62024-07-26 19:13:55 +02004068 int i, j;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004069 char_u *p = cmd;
4070
4071 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004072 p = skipwhite(skipdigits(cmd + 1));
Christian Brabandt70a11a62024-07-26 19:13:55 +02004073 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004074 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004075 for (j = 0; p[j] != NUL; ++j)
4076 if (p[j] != cmdmod_info_tab[i].name[j])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004077 break;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004078 if (!ASCII_ISALPHA(p[j]) && j >= cmdmod_info_tab[i].minlen
4079 && (p == cmd || cmdmod_info_tab[i].has_count))
4080 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004081 }
4082 return 0;
4083}
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085/*
4086 * Return > 0 if an Ex command "name" exists.
4087 * Return 2 if there is an exact match.
4088 * Return 3 if there is an ambiguous match.
4089 */
4090 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004091cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092{
4093 exarg_T ea;
4094 int full = FALSE;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004095 int i;
4096 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004097 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Christian Brabandt70a11a62024-07-26 19:13:55 +02004099 // Check command modifiers.
4100 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004102 for (j = 0; name[j] != NUL; ++j)
4103 if (name[j] != cmdmod_info_tab[i].name[j])
4104 break;
4105 if (name[j] == NUL && j >= cmdmod_info_tab[i].minlen)
4106 return (cmdmod_info_tab[i].name[j] == NUL ? 2 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004109 // Check built-in commands and user defined commands.
4110 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004111 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004113 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004115 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004117 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4118 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004119 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004120 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4122}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004123
4124/*
4125 * "fullcommand" function
4126 */
4127 void
4128f_fullcommand(typval_T *argvars, typval_T *rettv)
4129{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004130 exarg_T ea;
4131 char_u *name;
4132 char_u *p;
4133 int vim9script = in_vim9script();
4134 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004135
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004136 rettv->v_type = VAR_STRING;
4137 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004138
Bram Moolenaaraa534142022-09-15 21:46:02 +01004139 if (in_vim9script()
4140 && (check_for_string_arg(argvars, 0) == FAIL
4141 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004142 return;
4143
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004144 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004145 if (name == NULL)
4146 return;
4147
Bram Moolenaaraa534142022-09-15 21:46:02 +01004148 if (argvars[1].v_type != VAR_UNKNOWN)
4149 {
4150 vim9script = tv_get_bool(&argvars[1]);
4151 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4152 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4153 }
4154
zeertzjqc024ed92022-01-04 16:22:52 +00004155 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004156 name++;
4157 name = skip_range(name, TRUE, NULL);
4158
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004159 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4160 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004161 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004162 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004163 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004164 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004165 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004166 goto theend;
4167
4168 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004169 {
4170 int res;
4171
4172 ++emsg_silent;
4173 res = not_in_vim9(&ea);
4174 --emsg_silent;
4175
4176 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004177 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004178 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004179
4180 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004181 ? get_user_command_name(ea.useridx, ea.cmdidx)
4182 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004183theend:
4184 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004185}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186#endif
4187
Bram Moolenaard0190392019-08-23 21:17:35 +02004188 cmdidx_T
4189excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190{
Bram Moolenaard0190392019-08-23 21:17:35 +02004191 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004193 if (!one_letter_cmd(cmd, &idx))
4194 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4195 idx = (cmdidx_T)((int)idx + 1))
4196 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4197 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
Bram Moolenaard0190392019-08-23 21:17:35 +02004199 return idx;
4200}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
Bram Moolenaard0190392019-08-23 21:17:35 +02004202 long
4203excmd_get_argt(cmdidx_T idx)
4204{
4205 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206}
4207
4208/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004209 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 *
4211 * Backslashed delimiters after / or ? will be skipped, and commands will
4212 * not be expanded between /'s and ?'s or after "'".
4213 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004214 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 * Returns the "cmd" pointer advanced to beyond the range.
4216 */
4217 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004218skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004219 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004220 int skip_star, // skip "*" used for Visual range
4221 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004223 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004224 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004226 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004228 if (*cmd == '\\')
4229 {
4230 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4231 ++cmd;
4232 else
4233 break;
4234 }
4235 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004237 char_u *p = cmd;
4238
4239 // a quote is only valid at the start or after a separator
4240 while (p > cmd_start)
4241 {
4242 --p;
4243 if (!VIM_ISWHITE(*p))
4244 break;
4245 }
4246 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4247 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (*++cmd == NUL && ctx != NULL)
4249 *ctx = EXPAND_NOTHING;
4250 }
4251 else if (*cmd == '/' || *cmd == '?')
4252 {
4253 delim = *cmd++;
4254 while (*cmd != NUL && *cmd != delim)
4255 if (*cmd++ == '\\' && *cmd != NUL)
4256 ++cmd;
4257 if (*cmd == NUL && ctx != NULL)
4258 *ctx = EXPAND_NOTHING;
4259 }
4260 if (*cmd != NUL)
4261 ++cmd;
4262 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004263
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004264 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004265 while (*cmd == ':')
4266 cmd = skipwhite(cmd + 1);
4267
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004268 // Skip "*" used for Visual range.
4269 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4270 cmd = skipwhite(cmd + 1);
4271
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 return cmd;
4273}
4274
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004275 static void
4276addr_error(cmd_addr_T addr_type)
4277{
4278 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004279 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004280 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004281 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004282}
4283
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004285 * Return the default address for an address type.
4286 */
4287 static linenr_T
4288default_address(exarg_T *eap)
4289{
4290 linenr_T lnum = 0;
4291
4292 switch (eap->addr_type)
4293 {
4294 case ADDR_LINES:
4295 case ADDR_OTHER:
4296 // Default is the cursor line number. Avoid using an invalid
4297 // line number though.
4298 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4299 lnum = curbuf->b_ml.ml_line_count;
4300 else
4301 lnum = curwin->w_cursor.lnum;
4302 break;
4303 case ADDR_WINDOWS:
4304 lnum = CURRENT_WIN_NR;
4305 break;
4306 case ADDR_ARGUMENTS:
4307 lnum = curwin->w_arg_idx + 1;
4308 if (lnum > ARGCOUNT)
4309 lnum = ARGCOUNT;
4310 break;
4311 case ADDR_LOADED_BUFFERS:
4312 case ADDR_BUFFERS:
4313 lnum = curbuf->b_fnum;
4314 break;
4315 case ADDR_TABS:
4316 lnum = CURRENT_TAB_NR;
4317 break;
4318 case ADDR_TABS_RELATIVE:
4319 case ADDR_UNSIGNED:
4320 lnum = 1;
4321 break;
4322 case ADDR_QUICKFIX:
4323#ifdef FEAT_QUICKFIX
4324 lnum = qf_get_cur_idx(eap);
4325#endif
4326 break;
4327 case ADDR_QUICKFIX_VALID:
4328#ifdef FEAT_QUICKFIX
4329 lnum = qf_get_cur_valid_idx(eap);
4330#endif
4331 break;
4332 case ADDR_NONE:
4333 // Will give an error later if a range is found.
4334 break;
4335 }
4336 return lnum;
4337}
4338
4339/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004340 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 *
4342 * Set ptr to the next character after the part that was interpreted.
4343 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004344 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 *
4346 * Return MAXLNUM when no Ex address was found.
4347 */
4348 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004349get_address(
4350 exarg_T *eap UNUSED,
4351 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004352 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004353 int skip, // only skip the address, don't use it
4354 int silent, // no errors or side effects
4355 int to_other_file, // flag: may jump to other file
4356 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
4358 int c;
4359 int i;
4360 long n;
4361 char_u *cmd;
4362 pos_T pos;
4363 pos_T *fp;
4364 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004365 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366
4367 cmd = skipwhite(*ptr);
4368 lnum = MAXLNUM;
4369 do
4370 {
4371 switch (*cmd)
4372 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004373 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004374 ++cmd;
4375 switch (addr_type)
4376 {
4377 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004378 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 lnum = curwin->w_cursor.lnum;
4380 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004381 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004382 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004383 break;
4384 case ADDR_ARGUMENTS:
4385 lnum = curwin->w_arg_idx + 1;
4386 break;
4387 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004388 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004389 lnum = curbuf->b_fnum;
4390 break;
4391 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004392 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004393 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004394 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004395 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004396 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004397 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004398 cmd = NULL;
4399 goto error;
4400 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004401 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004402#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004403 lnum = qf_get_cur_idx(eap);
4404#endif
4405 break;
4406 case ADDR_QUICKFIX_VALID:
4407#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004408 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004409#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004410 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004411 }
4412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004414 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 ++cmd;
4416 switch (addr_type)
4417 {
4418 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004419 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 lnum = curbuf->b_ml.ml_line_count;
4421 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004422 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004423 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004424 break;
4425 case ADDR_ARGUMENTS:
4426 lnum = ARGCOUNT;
4427 break;
4428 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004429 buf = lastbuf;
4430 while (buf->b_ml.ml_mfp == NULL)
4431 {
4432 if (buf->b_prev == NULL)
4433 break;
4434 buf = buf->b_prev;
4435 }
4436 lnum = buf->b_fnum;
4437 break;
4438 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004439 lnum = lastbuf->b_fnum;
4440 break;
4441 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004442 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004443 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004444 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004445 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004446 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004447 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004448 cmd = NULL;
4449 goto error;
4450 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004451 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004452#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004453 lnum = qf_get_size(eap);
4454 if (lnum == 0)
4455 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004456#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004457 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004458 case ADDR_QUICKFIX_VALID:
4459#ifdef FEAT_QUICKFIX
4460 lnum = qf_get_valid_size(eap);
4461 if (lnum == 0)
4462 lnum = 1;
4463#endif
4464 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004465 }
4466 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004468 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004469 if (*++cmd == NUL)
4470 {
4471 cmd = NULL;
4472 goto error;
4473 }
4474 if (addr_type != ADDR_LINES)
4475 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004476 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004477 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004478 goto error;
4479 }
4480 if (skip)
4481 ++cmd;
4482 else
4483 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004484 // Only accept a mark in another file when it is
4485 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4487 ++cmd;
4488 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004489 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 lnum = curwin->w_cursor.lnum;
4491 else
4492 {
4493 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 {
4495 cmd = NULL;
4496 goto error;
4497 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004498 lnum = fp->lnum;
4499 }
4500 }
4501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004504 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 c = *cmd++;
4506 if (addr_type != ADDR_LINES)
4507 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004508 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004509 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 goto error;
4511 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004512 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004513 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004514 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004515 if (*cmd == c)
4516 ++cmd;
4517 }
4518 else
4519 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004520 int flags;
4521
4522 pos = curwin->w_cursor; // save curwin->w_cursor
4523
4524 // When '/' or '?' follows another address, start from
4525 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004526 if (lnum > 0 && lnum != MAXLNUM)
4527 curwin->w_cursor.lnum =
4528 lnum > curbuf->b_ml.ml_line_count
4529 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004530
4531 // Start a forward search at the end of the line (unless
4532 // before the first line).
4533 // Start a backward search at the start of the line.
4534 // This makes sure we never match in the current
4535 // line, and can match anywhere in the
4536 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004537 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004538 curwin->w_cursor.col = MAXCOL;
4539 else
4540 curwin->w_cursor.col = 0;
4541 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004542 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004543 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004544 {
4545 curwin->w_cursor = pos;
4546 cmd = NULL;
4547 goto error;
4548 }
4549 lnum = curwin->w_cursor.lnum;
4550 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004551 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004552 cmd += searchcmdlen;
4553 }
4554 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004556 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004557 ++cmd;
4558 if (addr_type != ADDR_LINES)
4559 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004560 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004561 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004562 goto error;
4563 }
4564 if (*cmd == '&')
4565 i = RE_SUBST;
4566 else if (*cmd == '?' || *cmd == '/')
4567 i = RE_SEARCH;
4568 else
4569 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004570 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004571 cmd = NULL;
4572 goto error;
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004575 if (!skip)
4576 {
4577 /*
4578 * When search follows another address, start from
4579 * there.
4580 */
4581 if (lnum != MAXLNUM)
4582 pos.lnum = lnum;
4583 else
4584 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004586 /*
4587 * Start the search just like for the above
4588 * do_search().
4589 */
4590 if (*cmd != '?')
4591 pos.col = MAXCOL;
4592 else
4593 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004594 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004595 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004596 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004597 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004598 lnum = pos.lnum;
4599 else
4600 {
4601 cmd = NULL;
4602 goto error;
4603 }
4604 }
4605 ++cmd;
4606 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607
4608 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004609 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004610 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
4612
4613 for (;;)
4614 {
4615 cmd = skipwhite(cmd);
4616 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4617 break;
4618
4619 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004620 {
4621 switch (addr_type)
4622 {
4623 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004624 case ADDR_OTHER:
4625 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004626 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004627 break;
4628 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004629 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004630 break;
4631 case ADDR_ARGUMENTS:
4632 lnum = curwin->w_arg_idx + 1;
4633 break;
4634 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004635 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004636 lnum = curbuf->b_fnum;
4637 break;
4638 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004639 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004640 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004641 case ADDR_TABS_RELATIVE:
4642 lnum = 1;
4643 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004644 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004645#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004646 lnum = qf_get_cur_idx(eap);
4647#endif
4648 break;
4649 case ADDR_QUICKFIX_VALID:
4650#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004651 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004652#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004653 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004654 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004655 case ADDR_UNSIGNED:
4656 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004657 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004658 }
4659 }
4660
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004662 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 else
4664 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004665 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 n = 1;
4667 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004668 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004669 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004671 if (n == MAXLNUM)
4672 {
4673 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004674 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004675 goto error;
4676 }
4677 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004678
4679 if (addr_type == ADDR_TABS_RELATIVE)
4680 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004681 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004682 cmd = NULL;
4683 goto error;
4684 }
4685 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004686 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004687 lnum = compute_buffer_local_count(
4688 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004690 {
4691#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004692 // Relative line addressing: need to adjust for lines in a
4693 // closed fold after the first address.
4694 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4695 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004696 (void)hasFolding(lnum, NULL, &lnum);
4697#endif
4698 if (i == '-')
4699 lnum -= n;
4700 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004701 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004702 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004703 {
4704 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004705 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004706 goto error;
4707 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004708 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004709 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
4712 } while (*cmd == '/' || *cmd == '?');
4713
4714error:
4715 *ptr = cmd;
4716 return lnum;
4717}
4718
4719/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004720 * Set eap->line1 and eap->line2 to the whole range.
4721 * Used for commands with the EX_DFLALL flag and no range given.
4722 */
4723 static void
4724address_default_all(exarg_T *eap)
4725{
4726 eap->line1 = 1;
4727 switch (eap->addr_type)
4728 {
4729 case ADDR_LINES:
4730 case ADDR_OTHER:
4731 eap->line2 = curbuf->b_ml.ml_line_count;
4732 break;
4733 case ADDR_LOADED_BUFFERS:
4734 {
4735 buf_T *buf = firstbuf;
4736
4737 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4738 buf = buf->b_next;
4739 eap->line1 = buf->b_fnum;
4740 buf = lastbuf;
4741 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4742 buf = buf->b_prev;
4743 eap->line2 = buf->b_fnum;
4744 }
4745 break;
4746 case ADDR_BUFFERS:
4747 eap->line1 = firstbuf->b_fnum;
4748 eap->line2 = lastbuf->b_fnum;
4749 break;
4750 case ADDR_WINDOWS:
4751 eap->line2 = LAST_WIN_NR;
4752 break;
4753 case ADDR_TABS:
4754 eap->line2 = LAST_TAB_NR;
4755 break;
4756 case ADDR_TABS_RELATIVE:
4757 eap->line2 = 1;
4758 break;
4759 case ADDR_ARGUMENTS:
4760 if (ARGCOUNT == 0)
4761 eap->line1 = eap->line2 = 0;
4762 else
4763 eap->line2 = ARGCOUNT;
4764 break;
4765 case ADDR_QUICKFIX_VALID:
4766#ifdef FEAT_QUICKFIX
4767 eap->line2 = qf_get_valid_size(eap);
4768 if (eap->line2 == 0)
4769 eap->line2 = 1;
4770#endif
4771 break;
4772 case ADDR_NONE:
4773 case ADDR_UNSIGNED:
4774 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004775 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004776 break;
4777 }
4778}
4779
4780
4781/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004782 * Get flags from an Ex command argument.
4783 */
4784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004785get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004786{
4787 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4788 {
4789 if (*eap->arg == 'l')
4790 eap->flags |= EXFLAG_LIST;
4791 else if (*eap->arg == 'p')
4792 eap->flags |= EXFLAG_PRINT;
4793 else
4794 eap->flags |= EXFLAG_NR;
4795 eap->arg = skipwhite(eap->arg + 1);
4796 }
4797}
4798
4799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 * Function called for command which is Not Implemented. NI!
4801 */
4802 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004803ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
4805 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004806 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004807 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808}
4809
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004810#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811/*
4812 * Function called for script command which is Not Implemented. NI!
4813 * Skips over ":perl <<EOF" constructs.
4814 */
4815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004816ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817{
4818 if (!eap->skip)
4819 ex_ni(eap);
4820 else
4821 vim_free(script_get(eap, eap->arg));
4822}
4823#endif
4824
4825/*
4826 * Check range in Ex command for validity.
4827 * Return NULL when valid, error message when invalid.
4828 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004829 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004830invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004832 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004833
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 if ( eap->line1 < 0
4835 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004836 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004837 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004838
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004839 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004840 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004841 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004842 {
4843 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004844 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004845#ifdef FEAT_DIFF
4846 + (eap->cmdidx == CMD_diffget)
4847#endif
4848 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004849 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004850 break;
4851 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004852 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004853 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004854 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004855 break;
4856 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004857 // Only a boundary check, not whether the buffers actually
4858 // exist.
4859 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004860 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004861 break;
4862 case ADDR_LOADED_BUFFERS:
4863 buf = firstbuf;
4864 while (buf->b_ml.ml_mfp == NULL)
4865 {
4866 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004867 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004868 buf = buf->b_next;
4869 }
4870 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004871 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004872 buf = lastbuf;
4873 while (buf->b_ml.ml_mfp == NULL)
4874 {
4875 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004876 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004877 buf = buf->b_prev;
4878 }
4879 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004880 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004881 break;
4882 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004883 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004884 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004885 break;
4886 case ADDR_TABS:
4887 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004888 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004889 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004890 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004891 case ADDR_OTHER:
4892 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004893 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004894 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004895#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004896 // No error for value that is too big, will use the last entry.
4897 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004898 {
4899 if (eap->addr_count == 0)
4900 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004901 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004902 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004903#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004904 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004905 case ADDR_QUICKFIX_VALID:
4906#ifdef FEAT_QUICKFIX
4907 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4908 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004909 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004910#endif
4911 break;
4912 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004913 case ADDR_NONE:
4914 // Will give an error elsewhere.
4915 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004916 }
4917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 return NULL;
4919}
4920
4921/*
4922 * Correct the range for zero line number, if required.
4923 */
4924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004925correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004927 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
4929 if (eap->line1 == 0)
4930 eap->line1 = 1;
4931 if (eap->line2 == 0)
4932 eap->line2 = 1;
4933 }
4934}
4935
Bram Moolenaar748bf032005-02-02 23:04:36 +00004936#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004937/*
4938 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4939 * pattern. Otherwise return eap->arg.
4940 */
4941 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004942skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004943{
4944 char_u *p = eap->arg;
4945
Bram Moolenaara37420f2006-02-04 22:37:47 +00004946 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4947 || eap->cmdidx == CMD_vimgrepadd
4948 || eap->cmdidx == CMD_lvimgrepadd
4949 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004950 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004951 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004952 if (p == NULL)
4953 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004954 }
4955 return p;
4956}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004957
4958/*
4959 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4960 * in the command line, so that things like % get expanded.
4961 */
4962 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004963replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004964{
4965 char_u *new_cmdline;
4966 char_u *program;
4967 char_u *pos;
4968 char_u *ptr;
4969 int len;
4970 int i;
4971
4972 /*
4973 * Don't do it when ":vimgrep" is used for ":grep".
4974 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004975 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4976 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4977 || eap->cmdidx == CMD_grepadd
4978 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004979 && !grep_internal(eap->cmdidx))
4980 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004981 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4982 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004983 {
4984 if (*curbuf->b_p_gp == NUL)
4985 program = p_gp;
4986 else
4987 program = curbuf->b_p_gp;
4988 }
4989 else
4990 {
4991 if (*curbuf->b_p_mp == NUL)
4992 program = p_mp;
4993 else
4994 program = curbuf->b_p_mp;
4995 }
4996
4997 p = skipwhite(p);
4998
4999 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5000 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005001 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005002 i = 1;
5003 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
5004 ++i;
5005 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005006 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005007 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005008 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005009 ptr = new_cmdline;
5010 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5011 {
5012 i = (int)(pos - program);
5013 STRNCPY(ptr, program, i);
5014 STRCPY(ptr += i, p);
5015 ptr += len;
5016 program = pos + 2;
5017 }
5018 STRCPY(ptr, program);
5019 }
5020 else
5021 {
John Marriotted908f72024-04-18 22:46:56 +02005022 size_t program_len = STRLEN(program);
5023
5024 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005025 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005026 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005027 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005028 STRCPY(new_cmdline + program_len, " ");
5029 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005030 }
5031 msg_make(p);
5032
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005033 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005034 vim_free(*cmdlinep);
5035 *cmdlinep = new_cmdline;
5036 p = new_cmdline;
5037 }
5038 return p;
5039}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005040#endif
5041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042/*
5043 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005044 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 * Return FAIL for failure, OK otherwise.
5046 */
5047 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005048expand_filename(
5049 exarg_T *eap,
5050 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005051 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005053 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005055 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 char_u *p;
5057 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005058 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
Bram Moolenaar748bf032005-02-02 23:04:36 +00005060#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005061 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005062 p = skip_grep_pat(eap);
5063#else
5064 p = eap->arg;
5065#endif
5066
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 /*
5068 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5069 * the file name contains a wildcard it should not cause expanding.
5070 * (it will be expanded anyway if there is a wildcard before replacing).
5071 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005072 has_wildcards = mch_has_wildcard(p);
5073 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005075#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005076 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005077 if (p[0] == '`' && p[1] == '=')
5078 {
5079 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005080 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005081 if (*p == '`')
5082 ++p;
5083 continue;
5084 }
5085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 /*
5087 * Quick check if this cannot be the start of a special string.
5088 * Also removes backslash before '%', '#' and '<'.
5089 */
5090 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5091 {
5092 ++p;
5093 continue;
5094 }
5095
5096 /*
5097 * Try to find a match at this position.
5098 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005099 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005100 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005101 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005103 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 p += srclen;
5106 continue;
5107 }
5108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005109 // Wildcards won't be expanded below, the replacement is taken
5110 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005111 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5112 {
5113 char_u *l = repl;
5114
5115 repl = expand_env_save(repl);
5116 vim_free(l);
5117 }
5118
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005119 // Need to escape white space et al. with a backslash.
5120 // Don't do this for:
5121 // - replacement that already has been escaped: "##"
5122 // - shell commands (may have to use quotes instead).
5123 // - non-unix systems when there is a single argument (spaces don't
5124 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005126 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 && eap->cmdidx != CMD_grep
5129 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005130 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005131 && eap->cmdidx != CMD_lgrep
5132 && eap->cmdidx != CMD_lgrepadd
5133 && eap->cmdidx != CMD_lmake
5134 && eap->cmdidx != CMD_make
5135 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005137 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138#endif
5139 )
5140 {
5141 char_u *l;
5142#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005143 // Don't escape a backslash here, because rem_backslash() doesn't
5144 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 static char_u *nobslash = (char_u *)" \t\"|";
5146# define ESCAPE_CHARS nobslash
5147#else
5148# define ESCAPE_CHARS escape_chars
5149#endif
5150
5151 for (l = repl; *l; ++l)
5152 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5153 {
5154 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5155 if (l != NULL)
5156 {
5157 vim_free(repl);
5158 repl = l;
5159 }
5160 break;
5161 }
5162 }
5163
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005164 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005165 if ((eap->usefilter || eap->cmdidx == CMD_bang
5166 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005167 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
5169 char_u *l;
5170
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005171 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (l != NULL)
5173 {
5174 vim_free(repl);
5175 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177 }
5178
Mike Williams51024bb2024-05-30 07:46:30 +02005179 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 vim_free(repl);
5181 if (p == NULL)
5182 return FAIL;
5183 }
5184
5185 /*
5186 * One file argument: Expand wildcards.
5187 * Don't do this with ":r !command" or ":w !command".
5188 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005189 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
5191 /*
5192 * May do this twice:
5193 * 1. Replace environment variables.
5194 * 2. Replace any other wildcards, remove backslashes.
5195 */
5196 for (n = 1; n <= 2; ++n)
5197 {
5198 if (n == 2)
5199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 /*
5201 * Halve the number of backslashes (this is Vi compatible).
5202 * For Unix and OS/2, when wildcards are expanded, this is
5203 * done by ExpandOne() below.
5204 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005205#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 if (!has_wildcards)
5207#endif
5208 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210
5211 if (has_wildcards)
5212 {
5213 if (n == 1)
5214 {
5215 /*
5216 * First loop: May expand environment variables. This
5217 * can be done much faster with expand_env() than with
5218 * something else (e.g., calling a shell).
5219 * After expanding environment variables, check again
5220 * if there are still wildcards present.
5221 */
5222 if (vim_strchr(eap->arg, '$') != NULL
5223 || vim_strchr(eap->arg, '~') != NULL)
5224 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005225 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005226 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 has_wildcards = mch_has_wildcard(NameBuff);
5228 p = NameBuff;
5229 }
5230 else
5231 p = NULL;
5232 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005233 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 {
5235 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005236 int options = WILD_LIST_NOTFOUND
5237 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
5239 ExpandInit(&xpc);
5240 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005241 if (p_wic)
5242 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005244 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 if (p == NULL)
5246 return FAIL;
5247 }
5248 if (p != NULL)
5249 {
John Marriotted908f72024-04-18 22:46:56 +02005250 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005252 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 vim_free(p);
5254 }
5255 }
5256 }
5257 }
5258 return OK;
5259}
5260
5261/*
5262 * Replace part of the command line, keeping eap->cmd, eap->arg and
5263 * eap->nextcmd correct.
5264 * "src" points to the part that is to be replaced, of length "srclen".
5265 * "repl" is the replacement string.
5266 * Returns a pointer to the character after the replaced string.
5267 * Returns NULL for failure.
5268 */
5269 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005270repl_cmdline(
5271 exarg_T *eap,
5272 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005273 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005274 char_u *repl,
5275 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276{
John Marriotted908f72024-04-18 22:46:56 +02005277 size_t repllen;
5278 size_t taillen;
5279 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005281 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283 /*
5284 * The new command line is build in new_cmdline[].
5285 * First allocate it.
5286 * Careful: a "+cmd" argument may have been NUL terminated.
5287 */
John Marriotted908f72024-04-18 22:46:56 +02005288 repllen = STRLEN(repl);
5289 taillen = STRLEN(src + srclen);
5290 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005291 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005292 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005293 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005294 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
5296 /*
5297 * Copy the stuff before the expanded part.
5298 * Copy the expanded stuff.
5299 * Copy what came after the expanded part.
5300 * Copy the next commands, if there are any.
5301 */
John Marriotted908f72024-04-18 22:46:56 +02005302 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5303 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005304
John Marriotted908f72024-04-18 22:46:56 +02005305 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5306 new_cmdlinelen += repllen; // remember the end of the string
5307 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5308 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005310 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 {
John Marriotted908f72024-04-18 22:46:56 +02005312 new_cmdlinelen += taillen + 1;
5313 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5314 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 }
5316 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5317 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5318 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5319 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5320 vim_free(*cmdlinep);
5321 *cmdlinep = new_cmdline;
5322
5323 return src;
5324}
5325
5326/*
5327 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005328 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 */
5330 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005331separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332{
5333 char_u *p;
5334
Bram Moolenaar86b68352004-12-27 21:59:20 +00005335#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005336 p = skip_grep_pat(eap);
5337#else
5338 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005339#endif
5340
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005341 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
5343 if (*p == Ctrl_V)
5344 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005345 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005346 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005348 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005349 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005350 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 break;
5352 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005353
5354#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005355 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005356 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005357 {
5358 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005359 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005360 if (*p == NUL) // stop at NUL after CTRL-V
5361 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005362 }
5363#endif
5364
Bram Moolenaareda29c92022-10-02 12:59:00 +01005365 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005366 // :@" and :*" do not start a comment!
5367 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005368 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005369 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005370 && !(eap->argt & EX_NOTRLCOM)
5371 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5372 || p != eap->arg)
5373 && (eap->cmdidx != CMD_redir
5374 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005375 || (*p == '#'
5376 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005377 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005378 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005379 || (*p == '|'
5380 && eap->cmdidx != CMD_append
5381 && eap->cmdidx != CMD_change
5382 && eap->cmdidx != CMD_insert)
5383 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
5385 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005386 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 * AND 'b' is present in 'cpoptions'.
5388 */
5389 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005390 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005392 if (!keep_backslash)
5393 {
5394 STRMOVE(p - 1, p); // remove the '\'
5395 --p;
5396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
5398 else
5399 {
5400 eap->nextcmd = check_nextcmd(p);
5401 *p = NUL;
5402 break;
5403 }
5404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005406
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005407 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 del_trailing_spaces(eap->arg);
5409}
5410
5411/*
5412 * get + command from ex argument
5413 */
5414 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005415getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
5417 char_u *arg = *argp;
5418 char_u *command = NULL;
5419
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005420 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 {
5422 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005423 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 command = dollar_command;
5425 else
5426 {
5427 command = arg;
5428 arg = skip_cmd_arg(command, TRUE);
5429 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005430 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
5432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005433 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 *argp = arg;
5435 }
5436 return command;
5437}
5438
5439/*
5440 * Find end of "+command" argument. Skip over "\ " and "\\".
5441 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005442 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005443skip_cmd_arg(
5444 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005445 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446{
5447 while (*p && !vim_isspace(*p))
5448 {
5449 if (*p == '\\' && p[1] != NUL)
5450 {
5451 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005452 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 else
5454 ++p;
5455 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005456 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
5458 return p;
5459}
5460
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005461 int
5462get_bad_opt(char_u *p, exarg_T *eap)
5463{
5464 if (STRICMP(p, "keep") == 0)
5465 eap->bad_char = BAD_KEEP;
5466 else if (STRICMP(p, "drop") == 0)
5467 eap->bad_char = BAD_DROP;
5468 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5469 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005470 else
5471 return FAIL;
5472 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005473}
5474
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005476 * Function given to ExpandGeneric() to obtain the list of bad= names.
5477 */
5478 static char_u *
5479get_bad_name(expand_T *xp UNUSED, int idx)
5480{
5481 // Note: Keep this in sync with getargopt.
5482 static char *(p_bad_values[]) =
5483 {
5484 "?",
5485 "keep",
5486 "drop",
5487 };
5488
John Marriotted908f72024-04-18 22:46:56 +02005489 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5490 return NULL;
5491
5492 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005493}
5494
5495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 * Get "++opt=arg" argument.
5497 * Return FAIL or OK.
5498 */
5499 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005500getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501{
5502 char_u *arg = eap->arg + 2;
5503 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005504 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005507 // Note: Keep this in sync with get_argopt_name.
5508
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005509 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5511 {
5512 if (*arg == 'n')
5513 {
5514 arg += 2;
5515 eap->force_bin = FORCE_NOBIN;
5516 }
5517 else
5518 eap->force_bin = FORCE_BIN;
5519 if (!checkforcmd(&arg, "binary", 3))
5520 return FAIL;
5521 eap->arg = skipwhite(arg);
5522 return OK;
5523 }
5524
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005525 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005526 if (STRNCMP(arg, "edit", 4) == 0)
5527 {
5528 eap->read_edit = TRUE;
5529 eap->arg = skipwhite(arg + 4);
5530 return OK;
5531 }
5532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 if (STRNCMP(arg, "ff", 2) == 0)
5534 {
5535 arg += 2;
5536 pp = &eap->force_ff;
5537 }
5538 else if (STRNCMP(arg, "fileformat", 10) == 0)
5539 {
5540 arg += 10;
5541 pp = &eap->force_ff;
5542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 else if (STRNCMP(arg, "enc", 3) == 0)
5544 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005545 if (STRNCMP(arg, "encoding", 8) == 0)
5546 arg += 8;
5547 else
5548 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 pp = &eap->force_enc;
5550 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005551 else if (STRNCMP(arg, "bad", 3) == 0)
5552 {
5553 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005554 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
5557 if (pp == NULL || *arg != '=')
5558 return FAIL;
5559
5560 ++arg;
5561 *pp = (int)(arg - eap->cmd);
5562 arg = skip_cmd_arg(arg, FALSE);
5563 eap->arg = skipwhite(arg);
5564 *arg = NUL;
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 if (pp == &eap->force_ff)
5567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5569 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005570 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005572 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005574 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5576 *p = TOLOWER_ASC(*p);
5577 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005578 else
5579 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005580 // Check ++bad= argument. Must be a single-byte character, "keep" or
5581 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005582 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005583 return FAIL;
5584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
5586 return OK;
5587}
5588
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005589/*
5590 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5591 */
5592 static char_u *
5593get_argopt_name(expand_T *xp UNUSED, int idx)
5594{
5595 // Note: Keep this in sync with getargopt.
5596 static char *(p_opt_values[]) =
5597 {
5598 "fileformat=",
5599 "encoding=",
5600 "binary",
5601 "nobinary",
5602 "bad=",
5603 "edit",
5604 };
5605
John Marriotted908f72024-04-18 22:46:56 +02005606 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5607 return NULL;
5608
5609 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005610}
5611
5612/*
5613 * Command-line expansion for ++opt=name.
5614 */
5615 int
5616expand_argopt(
5617 char_u *pat,
5618 expand_T *xp,
5619 regmatch_T *rmp,
5620 char_u ***matches,
5621 int *numMatches)
5622{
5623 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5624 {
5625 char_u *(*cb)(expand_T *, int) = NULL;
5626
5627 char_u *name_end = xp->xp_pattern - 1;
5628 if (name_end - xp->xp_line >= 2
5629 && STRNCMP(name_end - 2, "ff", 2) == 0)
5630 cb = get_fileformat_name;
5631 else if (name_end - xp->xp_line >= 10
5632 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5633 cb = get_fileformat_name;
5634 else if (name_end - xp->xp_line >= 3
5635 && STRNCMP(name_end - 3, "enc", 3) == 0)
5636 cb = get_encoding_name;
5637 else if (name_end - xp->xp_line >= 8
5638 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5639 cb = get_encoding_name;
5640 else if (name_end - xp->xp_line >= 3
5641 && STRNCMP(name_end - 3, "bad", 3) == 0)
5642 cb = get_bad_name;
5643
5644 if (cb != NULL)
5645 {
5646 return ExpandGeneric(
5647 pat,
5648 xp,
5649 rmp,
5650 matches,
5651 numMatches,
5652 cb,
5653 FALSE);
5654 }
5655 return FAIL;
5656 }
5657
5658 // Special handling of "ff" which acts as a short form of
5659 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005660 if (xp->xp_pattern_len == 2
5661 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005662 {
5663 *matches = ALLOC_MULT(char_u *, 1);
5664 if (*matches == NULL)
5665 return FAIL;
5666 *numMatches = 1;
5667 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5668 return OK;
5669 }
5670
5671 return ExpandGeneric(
5672 pat,
5673 xp,
5674 rmp,
5675 matches,
5676 numMatches,
5677 get_argopt_name,
5678 FALSE);
5679}
5680
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005682ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
5684 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005685 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 * directory for security reasons.
5687 */
5688 if (secure)
5689 {
5690 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005691 eap->errmsg =
5692 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
5694 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005695 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 else
5697 do_augroup(eap->arg, eap->forceit);
5698}
5699
5700/*
5701 * ":doautocmd": Apply the automatic commands to the current buffer.
5702 */
5703 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005704ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005706 char_u *arg = eap->arg;
5707 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005708 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005709
Bram Moolenaar1610d052016-06-09 22:53:01 +02005710 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005711 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005712 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005713 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716/*
5717 * :[N]bunload[!] [N] [bufname] unload buffer
5718 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5719 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5720 */
5721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005722ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005724 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005725 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 eap->errmsg = do_bufdel(
5727 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5728 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5729 : DOBUF_UNLOAD, eap->arg,
5730 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5731}
5732
5733/*
5734 * :[N]buffer [N] to buffer N
5735 * :[N]sbuffer [N] to buffer N
5736 */
5737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005738ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005740 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005741 return;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01005742 do_exbuffer(eap);
5743}
5744
5745/*
5746 * ":buffer" command and alike.
5747 */
5748 static void
5749do_exbuffer(exarg_T *eap)
5750{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005752 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 else
5754 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005755 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5757 else
5758 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005759 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005760 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
5762}
5763
5764/*
5765 * :[N]bmodified [N] to next mod. buffer
5766 * :[N]sbmodified [N] to next mod. buffer
5767 */
5768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005769ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
5771 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005772 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005773 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774}
5775
5776/*
5777 * :[N]bnext [N] to next buffer
5778 * :[N]sbnext [N] split and to next buffer
5779 */
5780 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005781ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005783 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005784 return;
5785
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005787 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005788 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789}
5790
5791/*
5792 * :[N]bNext [N] to previous buffer
5793 * :[N]bprevious [N] to previous buffer
5794 * :[N]sbNext [N] split and to previous buffer
5795 * :[N]sbprevious [N] split and to previous buffer
5796 */
5797 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005798ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005800 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005801 return;
5802
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005804 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005805 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806}
5807
5808/*
5809 * :brewind to first buffer
5810 * :bfirst to first buffer
5811 * :sbrewind split and to first buffer
5812 * :sbfirst split and to first buffer
5813 */
5814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005815ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005817 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005818 return;
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005821 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005822 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823}
5824
5825/*
5826 * :blast to last buffer
5827 * :sblast split and to last buffer
5828 */
5829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005830ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005832 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005833 return;
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005836 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005837 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
Bram Moolenaar7a092242020-04-16 22:10:49 +02005840/*
5841 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005842 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005845ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005847 int comment_char = '"';
5848
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005849 if (in_vim9script())
5850 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005851 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005852}
5853
5854/*
5855 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5856 * to "cmd_start" or has a white space character before it.
5857 */
5858 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005859ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005860{
5861 int c = *cmd;
5862
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005863 if (c == NUL || c == '|' || c == '\n')
5864 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005865 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005866 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5867 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005868 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005869 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870}
5871
5872#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5873 || defined(PROTO)
5874/*
5875 * Return the next command, after the first '|' or '\n'.
5876 * Return NULL if not found.
5877 */
5878 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005879find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
5881 while (*p != '|' && *p != '\n')
5882 {
5883 if (*p == NUL)
5884 return NULL;
5885 ++p;
5886 }
5887 return (p + 1);
5888}
5889#endif
5890
5891/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005892 * Check if *p is a separator between Ex commands, skipping over white space.
5893 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 */
5895 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005896check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005898 char_u *s = skipwhite(p);
5899
5900 if (*s == '|' || *s == '\n')
5901 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 else
5903 return NULL;
5904}
5905
5906/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005907 * If "eap->nextcmd" is not set, check for a next command at "p".
5908 */
5909 void
5910set_nextcmd(exarg_T *eap, char_u *arg)
5911{
5912 char_u *p = check_nextcmd(arg);
5913
5914 if (eap->nextcmd == NULL)
5915 eap->nextcmd = p;
5916 else if (p != NULL)
5917 // cannot use "| command" inside a {} block
5918 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5919}
5920
5921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 * - if there are more files to edit
5923 * - and this is the last window
5924 * - and forceit not used
5925 * - and not repeated twice on a row
5926 * return FAIL and give error message if 'message' TRUE
5927 * return OK otherwise
5928 */
5929 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005930check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005931 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005932 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933{
5934 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5935
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005936 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005937 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 {
5939 if (message)
5940 {
5941#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005942 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5943 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005945 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005947 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5948 NGETTEXT("%d more file to edit. Quit anyway?",
5949 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5951 return OK;
5952 return FAIL;
5953 }
5954#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005955 semsg(NGETTEXT(e_nr_more_file_to_edit,
5956 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005957 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 }
5959 return FAIL;
5960 }
5961 return OK;
5962}
5963
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964/*
5965 * Function given to ExpandGeneric() to obtain the list of command names.
5966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005968get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
5970 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005971 return expand_user_command_name(idx);
Christian Brabandt70a11a62024-07-26 19:13:55 +02005972 // the following are no real commands
5973 if (STRNCMP(cmdnames[idx].cmd_name, "{", 1) == 0 ||
5974 STRNCMP(cmdnames[idx].cmd_name, "}", 1) == 0)
5975 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 return cmdnames[idx].cmd_name;
5977}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005980ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981{
Bram Moolenaare6850792010-05-14 15:28:44 +02005982 if (*eap->arg == NUL)
5983 {
5984#ifdef FEAT_EVAL
5985 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5986 char_u *p = NULL;
5987
5988 if (expr != NULL)
5989 {
5990 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005991 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005992 --emsg_off;
5993 vim_free(expr);
5994 }
5995 if (p != NULL)
5996 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005997 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005998 vim_free(p);
5999 }
6000 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006001 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02006002#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006003 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02006004#endif
6005 }
6006 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00006007 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006008
6009#ifdef FEAT_VTP
6010 else if (has_vtp_working())
6011 {
6012 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006013 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006014 redrawcmd();
6015 }
6016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017}
6018
6019 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006020ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
6022 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006023 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 do_highlight(eap->arg, eap->forceit, FALSE);
6025}
6026
6027
6028/*
6029 * Call this function if we thought we were going to exit, but we won't
6030 * (because of an error). May need to restore the terminal mode.
6031 */
6032 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006033not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034{
6035 exiting = FALSE;
6036 settmode(TMODE_RAW);
6037}
6038
Bram Moolenaar3552e742021-05-29 12:21:58 +02006039 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006040before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6041{
6042 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6043
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006044 // Bail out when autocommands closed the window.
6045 // Refuse to quit when the buffer in the last window is being closed (can
6046 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006047 if (!win_valid(wp)
6048 || curbuf_locked()
6049 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6050 return TRUE;
6051
6052 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6053 {
6054 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006055 // Refuse to quit when locked or when the window was closed or the
6056 // buffer in the last window is being closed (can only happen in
6057 // autocommands).
6058 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006059 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6060 return TRUE;
6061 }
6062
6063 return FALSE;
6064}
6065
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006067 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006068 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006069 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006071 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006072ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006074 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006075
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 if (cmdwin_type != 0)
6077 {
6078 cmdwin_result = Ctrl_C;
6079 return;
6080 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006081 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006082 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006083 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006084 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006085 return;
6086 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006087 if (eap->addr_count > 0)
6088 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006089 int wnr = eap->line2;
6090
6091 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6092 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006093 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006094 }
6095 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006096 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006097
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006098 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006099 if (curbuf_locked())
6100 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006102 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006103 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006104 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106#ifdef FEAT_NETBEANS_INTG
6107 netbeansForcedQuit = eap->forceit;
6108#endif
6109
6110 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006111 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 */
6113 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6114 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006115 if ((!buf_hide(wp->w_buffer)
6116 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006117 | (eap->forceit ? CCGD_FORCEIT : 0)
6118 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006120 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 {
6122 not_exiting();
6123 }
6124 else
6125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006126 // quit last window
6127 // Note: only_one_window() returns true, even so a help window is
6128 // still open. In that case only quit, if no address has been
6129 // specified. Example:
6130 // :h|wincmd w|1q - don't quit
6131 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006132 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006134 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006135#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006137#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006138 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006139 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 }
6141}
6142
6143/*
6144 * ":cquit".
6145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006147ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006149 // this does not always pass on the exit code to the Manx compiler. why?
6150 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151}
6152
6153/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006154 * Do preparations for "qall" and "wqall".
6155 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006157 int
6158before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 if (cmdwin_type != 0)
6161 {
6162 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006163 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 else
6165 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006166 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006169 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006170 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006171 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006172 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006173 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006174 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006175
6176 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006177 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006178
Bram Moolenaar411da642023-05-10 16:53:27 +01006179 return OK;
6180}
6181
6182/*
6183 * ":qall": try to quit all windows
6184 */
6185 static void
6186ex_quit_all(exarg_T *eap)
6187{
6188 if (before_quit_all(eap) == FAIL)
6189 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006191 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 getout(0);
6193 not_exiting();
6194}
6195
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196/*
6197 * ":close": close current window, unless it is the last one
6198 */
6199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006200ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006202 win_T *win;
6203 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006205 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006207 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006208 {
6209 if (eap->addr_count == 0)
6210 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006211 else
6212 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006213 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006214 {
6215 winnr++;
6216 if (winnr == eap->line2)
6217 break;
6218 }
6219 if (win == NULL)
6220 win = lastwin;
6221 ex_win_close(eap->forceit, win, NULL);
6222 }
6223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224}
6225
Bram Moolenaar4033c552017-09-16 20:54:51 +02006226#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006227/*
6228 * ":pclose": Close any preview window.
6229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006231ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232{
6233 win_T *win;
6234
Bram Moolenaar79648732019-07-18 21:43:07 +02006235 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006236 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006237 if (win->w_p_pvw)
6238 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006239 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006240 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006241 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006242# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006243 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006244 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006245# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006246}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006247#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006248
Bram Moolenaarf740b292006-02-16 22:11:02 +00006249/*
6250 * Close window "win" and take care of handling closing the last window for a
6251 * modified buffer.
6252 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006254ex_win_close(
6255 int forceit,
6256 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006257 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258{
6259 int need_hide;
6260 buf_T *buf = win->w_buffer;
6261
Bram Moolenaarcf844172020-06-26 19:44:06 +02006262 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006263 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006264 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006265 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006266 return;
6267 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006268 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006269 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006270
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006272 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006274#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006275 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006277# ifdef FEAT_TERMINAL
6278 if (term_job_running(buf->b_term))
6279 {
6280 if (term_confirm_stop(buf) == FAIL)
6281 return;
6282 // Manually kill the terminal here because this command will
6283 // hide it otherwise.
6284 free_terminal(buf);
6285 need_hide = FALSE;
6286 }
6287 else
6288# endif
6289 {
6290 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006291
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006292 set_bufref(&bufref, buf);
6293 dialog_changed(buf, FALSE);
6294 if (bufref_valid(&bufref) && bufIsChanged(buf))
6295 return;
6296 need_hide = FALSE;
6297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
6299 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006302 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 return;
6304 }
6305 }
6306
Bram Moolenaar4033c552017-09-16 20:54:51 +02006307#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006309#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006311 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006312 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006313 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006314 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006315 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316}
6317
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006319 * Handle the argument for a tabpage related ex command.
6320 * Returns a tabpage number.
6321 * When an error is encountered then eap->errmsg is set.
6322 */
6323 static int
6324get_tabpage_arg(exarg_T *eap)
6325{
6326 int tab_number;
6327 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6328
6329 if (eap->arg && *eap->arg != NUL)
6330 {
6331 char_u *p = eap->arg;
6332 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006333 int relative = 0; // argument +N/-N means: go to N places to the
6334 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006335
6336 if (*p == '-')
6337 {
6338 relative = -1;
6339 p++;
6340 }
6341 else if (*p == '+')
6342 {
6343 relative = 1;
6344 p++;
6345 }
6346
6347 p_save = p;
6348 tab_number = getdigits(&p);
6349
6350 if (relative == 0)
6351 {
6352 if (STRCMP(p, "$") == 0)
6353 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006354 else if (STRCMP(p, "#") == 0)
6355 if (valid_tabpage(lastused_tabpage))
6356 tab_number = tabpage_index(lastused_tabpage);
6357 else
6358 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006359 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6360 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006361 tab_number = 0;
6362 goto theend;
6363 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006364 else if (p == p_save || *p_save == '-' || *p != NUL
6365 || tab_number > LAST_TAB_NR)
6366 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006367 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006368 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006369 goto theend;
6370 }
6371 }
6372 else
6373 {
6374 if (*p_save == NUL)
6375 tab_number = 1;
6376 else if (p == p_save || *p_save == '-' || *p != NUL
6377 || tab_number == 0)
6378 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006379 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006380 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006381 goto theend;
6382 }
6383 tab_number = tab_number * relative + tabpage_index(curtab);
6384 if (!unaccept_arg0 && relative == -1)
6385 --tab_number;
6386 }
6387 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006388 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006389 }
6390 else if (eap->addr_count > 0)
6391 {
6392 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006393 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006394 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006395 tab_number = 0;
6396 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006397 else
6398 {
6399 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006400 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006401 {
zeertzjq076faac2024-03-25 16:41:06 +01006402 char_u *cmdp = eap->cmd;
6403
6404 while (--cmdp > *eap->cmdlinep
6405 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6406 ;
6407 if (*cmdp == '-')
6408 {
6409 --tab_number;
6410 if (tab_number < unaccept_arg0)
6411 eap->errmsg = _(e_invalid_range);
6412 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006413 }
6414 }
6415 }
6416 else
6417 {
6418 switch (eap->cmdidx)
6419 {
6420 case CMD_tabnext:
6421 tab_number = tabpage_index(curtab) + 1;
6422 if (tab_number > LAST_TAB_NR)
6423 tab_number = 1;
6424 break;
6425 case CMD_tabmove:
6426 tab_number = LAST_TAB_NR;
6427 break;
6428 default:
6429 tab_number = tabpage_index(curtab);
6430 }
6431 }
6432
6433theend:
6434 return tab_number;
6435}
6436
6437/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006438 * ":tabclose": close current tab page, unless it is the last one.
6439 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 */
6441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006442ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006444 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006445 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006446
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006447 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006448 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006449 cmdwin_result = K_IGNORE;
6450 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006451 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006452
6453 if (first_tabpage->tp_next == NULL)
6454 {
6455 emsg(_(e_cannot_close_last_tab_page));
6456 return;
6457 }
6458
6459 if (window_layout_locked(CMD_tabclose))
6460 return;
6461
6462 tab_number = get_tabpage_arg(eap);
6463 if (eap->errmsg != NULL)
6464 return;
6465
6466 tp = find_tabpage(tab_number);
6467 if (tp == NULL)
6468 {
6469 beep_flush();
6470 return;
6471 }
6472 if (tp != curtab)
6473 {
6474 tabpage_close_other(tp, eap->forceit);
6475 return;
6476 }
6477 else if (!text_locked() && !curbuf_locked())
6478 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006479}
6480
6481/*
6482 * ":tabonly": close all tab pages except the current one
6483 */
6484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006485ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006486{
6487 tabpage_T *tp;
6488 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006489 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006490
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006491 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006492 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006493 cmdwin_result = K_IGNORE;
6494 return;
6495 }
6496
6497 if (first_tabpage->tp_next == NULL)
6498 {
6499 msg(_("Already only one tab page"));
6500 return;
6501 }
6502
6503 if (window_layout_locked(CMD_tabonly))
6504 return;
6505
6506 tab_number = get_tabpage_arg(eap);
6507 if (eap->errmsg != NULL)
6508 return;
6509
6510 goto_tabpage(tab_number);
6511 // Repeat this up to a 1000 times, because autocommands may
6512 // mess up the lists.
6513 for (done = 0; done < 1000; ++done)
6514 {
6515 FOR_ALL_TABPAGES(tp)
6516 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006517 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006518 tabpage_close_other(tp, eap->forceit);
6519 // if we failed to close it quit
6520 if (valid_tabpage(tp))
6521 done = 1000;
6522 // start over, "tp" is now invalid
6523 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006524 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006525 if (first_tabpage->tp_next == NULL)
6526 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529
6530/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006531 * Close the current tab page.
6532 */
6533 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006534tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006535{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006536 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006537 return;
6538
Jim Zhoubcf66e02025-03-16 20:24:57 +01006539 trigger_tabclosedpre(curtab, TRUE);
6540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006541 // First close all the windows but the current one. If that worked then
6542 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006543 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006544 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006545 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006546 ex_win_close(forceit, curwin, NULL);
6547# ifdef FEAT_GUI
6548 need_mouse_correct = TRUE;
6549# endif
6550}
6551
6552/*
6553 * Close tab page "tp", which is not the current tab page.
6554 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006555 * Also takes care of the tab pages line disappearing when closing the
6556 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006557 */
6558 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006559tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006560{
6561 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006562 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006563
Jim Zhoubcf66e02025-03-16 20:24:57 +01006564 trigger_tabclosedpre(tp, TRUE);
6565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006566 // Limit to 1000 windows, autocommands may add a window while we close
6567 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006568 while (++done < 1000)
6569 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006570 wp = tp->tp_firstwin;
6571 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006572
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006573 // Autocommands may delete the tab page under our fingers and we may
6574 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006575 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006576 break;
6577 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006578
Bram Moolenaar29323592016-07-24 22:04:11 +02006579 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006580}
6581
6582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 * ":only".
6584 */
6585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006586ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006588 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006589 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590# ifdef FEAT_GUI
6591 need_mouse_correct = TRUE;
6592# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006593 if (eap->addr_count > 0)
6594 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006595 win_T *wp;
6596 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006597 for (wp = firstwin; --wnr > 0; )
6598 {
6599 if (wp->w_next == NULL)
6600 break;
6601 else
6602 wp = wp->w_next;
6603 }
6604 win_goto(wp);
6605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 close_others(TRUE, eap->forceit);
6607}
6608
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006610ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006612 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006613 if (eap->skip)
6614 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006615
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006616 if (window_layout_locked(CMD_hide))
6617 return;
6618#ifdef FEAT_GUI
6619 need_mouse_correct = TRUE;
6620#endif
6621 if (eap->addr_count == 0)
6622 win_close(curwin, FALSE); // don't free buffer
6623 else
6624 {
6625 int winnr = 0;
6626 win_T *win;
6627
6628 FOR_ALL_WINDOWS(win)
6629 {
6630 winnr++;
6631 if (winnr == eap->line2)
6632 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006634 if (win == NULL)
6635 win = lastwin;
6636 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 }
6638}
6639
6640/*
6641 * ":stop" and ":suspend": Suspend Vim.
6642 */
dbivolaruab16ad32021-12-29 19:41:47 +00006643 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006644ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
6646 /*
6647 * Disallow suspending for "rvim".
6648 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006649 if (check_restricted())
6650 return;
6651
6652 if (!eap->forceit)
6653 autowrite_all();
6654 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6655 windgoto((int)Rows - 1, 0);
6656 out_char('\n');
6657 out_flush();
6658 stoptermcap();
6659 out_flush(); // needed for SUN to restore xterm buffer
6660 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6661 ui_suspend(); // call machine specific function
6662 maketitle();
6663 resettitle(); // force updating the title
6664 starttermcap();
6665 scroll_start(); // scroll screen before redrawing
6666 redraw_later_clear();
6667 shell_resized(); // may have resized window
6668 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669}
6670
6671/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006672 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 */
6674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006675ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006677#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006678 if (not_in_vim9(eap) == FAIL)
6679 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 if (cmdwin_type != 0)
6682 {
6683 cmdwin_result = Ctrl_C;
6684 return;
6685 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006686 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006687 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006688 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006689 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006690 return;
6691 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006692
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006694 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 */
6696 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6697 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006698
6699 // Write the buffer for ":wq" or when it was changed.
6700 // Trigger QuitPre and ExitPre.
6701 // Check if we can exit now, after autocommands have changed things.
6702 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6703 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006705 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 {
6707 not_exiting();
6708 }
6709 else
6710 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006711 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006713 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714# ifdef FEAT_GUI
6715 need_mouse_correct = TRUE;
6716# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006717 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006718 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 }
6720}
6721
6722/*
6723 * ":print", ":list", ":number".
6724 */
6725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006726ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006728 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006729 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006730 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006732 for ( ;!got_int; ui_breakcheck())
6733 {
6734 print_line(eap->line1,
6735 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6736 || (eap->flags & EXFLAG_NR)),
6737 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6738 if (++eap->line1 > eap->line2)
6739 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006740 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006741 }
6742 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006743 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006744 curwin->w_cursor.lnum = eap->line2;
6745 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 }
6747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749}
6750
6751#ifdef FEAT_BYTEOFF
6752 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006753ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 goto_byte(eap->line2);
6756}
6757#endif
6758
6759/*
6760 * ":shell".
6761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006763ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764{
6765 do_shell(NULL, 0);
6766}
6767
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006768#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006770static int drop_busy = FALSE;
6771static int drop_filec;
6772static char_u **drop_filev = NULL;
6773static int drop_split;
6774static void (*drop_callback)(void *);
6775static void *drop_cookie;
6776
6777 static void
6778handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779{
6780 exarg_T ea;
6781 int save_msg_scroll = msg_scroll;
6782
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006783 // Setting the argument list may cause screen updates and being called
6784 // recursively. Avoid that by setting drop_busy.
6785 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006787 // Check whether the current buffer is changed. If so, we will need
6788 // to split the current window or data could be lost.
6789 // We don't need to check if the 'hidden' option is set, as in this
6790 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006791 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 {
6793 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006794 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 --emsg_off;
6796 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006797 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 if (win_split(0, 0) == FAIL)
6800 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006801 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006803 // When splitting the window, create a new alist. Otherwise the
6804 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 alist_unlink(curwin->w_alist);
6806 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808
6809 /*
6810 * Set up the new argument list.
6811 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006812 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813
6814 /*
6815 * Move to the first file.
6816 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006817 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006818 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 ea.cmd = (char_u *)"next";
6820 do_argfile(&ea, 0);
6821
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006822 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6823 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 need_start_insertmode = FALSE;
6825
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006826 // Restore msg_scroll, otherwise a following command may cause scrolling
6827 // unexpectedly. The screen will be redrawn by the caller, thus
6828 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006830
6831 if (drop_callback != NULL)
6832 drop_callback(drop_cookie);
6833
6834 drop_filev = NULL;
6835 drop_busy = FALSE;
6836}
6837
6838/*
6839 * Handle a file drop. The code is here because a drop is *nearly* like an
6840 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006841 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006842 * code is very similar to :args and hence needs access to a lot of the static
6843 * functions in this file.
6844 *
6845 * The "filev" list must have been allocated using alloc(), as should each item
6846 * in the list. This function takes over responsibility for freeing the "filev"
6847 * list.
6848 */
6849 void
6850handle_drop(
6851 int filec, // the number of files dropped
6852 char_u **filev, // the list of files dropped
6853 int split, // force splitting the window
6854 void (*callback)(void *), // to be called after setting the argument
6855 // list
6856 void *cookie) // argument for "callback" (allocated)
6857{
6858 // Cannot handle recursive drops, finish the pending one.
6859 if (drop_busy)
6860 {
6861 FreeWild(filec, filev);
6862 vim_free(cookie);
6863 return;
6864 }
6865
6866 // When calling handle_drop() more than once in a row we only use the last
6867 // one.
6868 if (drop_filev != NULL)
6869 {
6870 FreeWild(drop_filec, drop_filev);
6871 vim_free(drop_cookie);
6872 }
6873
6874 drop_filec = filec;
6875 drop_filev = filev;
6876 drop_split = split;
6877 drop_callback = callback;
6878 drop_cookie = cookie;
6879
6880 // Postpone this when:
6881 // - editing the command line
6882 // - not possible to change the current buffer
6883 // - updating the screen
6884 // As it may change buffers and window structures that are in use and cause
6885 // freed memory to be used.
6886 if (text_locked() || curbuf_locked() || updating_screen)
6887 return;
6888
6889 handle_drop_internal();
6890}
6891
6892/*
6893 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6894 * reset: Handle a postponed drop.
6895 */
6896 void
6897handle_any_postponed_drop(void)
6898{
6899 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006900 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006901 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902}
6903#endif
6904
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 * ":preserve".
6907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006909ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006911 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 ml_preserve(curbuf, TRUE);
6913}
6914
6915/*
6916 * ":recover".
6917 */
6918 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006919ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006921 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006923 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6924 | CCGD_MULTWIN
6925 | (eap->forceit ? CCGD_FORCEIT : 0)
6926 | CCGD_EXCMD)
6927
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 && (*eap->arg == NUL
6929 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006930 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 recoverymode = FALSE;
6932}
6933
6934/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006935 * Command modifier used in a wrong way. Also for other commands that can't
6936 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 */
6938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006939ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006941 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942}
6943
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006944#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006945
6946// callback function for 'findfunc'
6947static callback_T ffu_cb;
6948
6949 static callback_T *
6950get_findfunc_callback(void)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006951{
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006952 return *curbuf->b_p_ffu != NUL ? &curbuf->b_ffu_cb : &ffu_cb;
6953}
6954
zeertzjq6eda2692024-11-03 09:23:33 +01006955/*
6956 * Call 'findfunc' to obtain a list of file names.
6957 */
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006958 static list_T *
6959call_findfunc(char_u *pat, int cmdcomplete)
6960{
6961 typval_T args[3];
6962 callback_T *cb;
6963 typval_T rettv;
6964 int retval;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006965 sctx_T saved_sctx = current_sctx;
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006966 sctx_T *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006967
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006968 args[0].v_type = VAR_STRING;
6969 args[0].vval.v_string = pat;
6970 args[1].v_type = VAR_BOOL;
6971 args[1].vval.v_number = cmdcomplete;
6972 args[2].v_type = VAR_UNKNOWN;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006973
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006974 // Lock the text to prevent weird things from happening. Also disallow
6975 // switching to another window, it should not be needed and may end up in
6976 // Insert mode in another buffer.
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006977 ++textlock;
6978
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006979 ctx = get_option_sctx("findfunc");
6980 if (ctx != NULL)
6981 current_sctx = *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006982
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006983 cb = get_findfunc_callback();
6984 retval = call_callback(cb, -1, &rettv, 2, args);
6985
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006986 current_sctx = saved_sctx;
6987
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006988 --textlock;
6989
6990 list_T *retlist = NULL;
6991
6992 if (retval == OK)
6993 {
6994 if (rettv.v_type == VAR_LIST)
6995 retlist = list_copy(rettv.vval.v_list, FALSE, FALSE, get_copyID());
6996 else
6997 emsg(_(e_invalid_return_type_from_findfunc));
6998
6999 clear_tv(&rettv);
7000 }
7001
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007002 return retlist;
7003}
7004
7005/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007006 * Find file names matching "pat" using 'findfunc' and return it in "files".
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007007 * Used for expanding the :find, :sfind and :tabfind command argument.
7008 * Returns OK on success and FAIL otherwise.
7009 */
7010 int
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007011expand_findfunc(char_u *pat, char_u ***files, int *numMatches)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007012{
7013 list_T *l;
7014 int len;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007015
7016 *numMatches = 0;
7017 *files = NULL;
7018
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007019 l = call_findfunc(pat, VVAL_TRUE);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007020
7021 if (l == NULL)
7022 return FAIL;
7023
7024 len = list_len(l);
7025 if (len == 0) // empty List
7026 return FAIL;
7027
7028 *files = ALLOC_MULT(char_u *, len);
7029 if (*files == NULL)
7030 return FAIL;
7031
7032 // Copy all the List items
7033 listitem_T *li;
7034 int idx = 0;
7035 FOR_ALL_LIST_ITEMS(l, li)
7036 {
7037 if (li->li_tv.v_type == VAR_STRING)
7038 {
7039 (*files)[idx] = vim_strsave(li->li_tv.vval.v_string);
7040 idx++;
7041 }
7042 }
7043
7044 *numMatches = idx;
7045 list_free(l);
7046
7047 return OK;
7048}
7049
7050/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007051 * Use 'findfunc' to find file 'findarg'. The 'count' argument is used to find
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007052 * the n'th matching file.
7053 */
7054 static char_u *
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007055findfunc_find_file(char_u *findarg, int findarg_len, int count)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007056{
7057 list_T *fname_list;
7058 char_u *ret_fname = NULL;
7059 char_u cc;
7060 int fname_count;
7061
7062 cc = findarg[findarg_len];
7063 findarg[findarg_len] = NUL;
7064
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007065 fname_list = call_findfunc(findarg, VVAL_FALSE);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007066 fname_count = list_len(fname_list);
7067
7068 if (fname_count == 0)
7069 semsg(_(e_cant_find_file_str_in_path), findarg);
7070 else
7071 {
7072 if (count > fname_count)
7073 semsg(_(e_no_more_file_str_found_in_path), findarg);
7074 else
7075 {
7076 listitem_T *li = list_find(fname_list, count - 1);
7077 if (li != NULL && li->li_tv.v_type == VAR_STRING)
7078 ret_fname = vim_strsave(li->li_tv.vval.v_string);
7079 }
7080 }
7081
7082 if (fname_list != NULL)
7083 list_free(fname_list);
7084
7085 findarg[findarg_len] = cc;
7086
7087 return ret_fname;
7088}
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007089
7090/*
7091 * Process the 'findfunc' option value.
7092 * Returns NULL on success and an error message on failure.
7093 */
7094 char *
7095did_set_findfunc(optset_T *args UNUSED)
7096{
7097 int retval;
7098
zeertzjq6eda2692024-11-03 09:23:33 +01007099 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007100 // buffer-local option set
7101 retval = option_set_callback_func(curbuf->b_p_ffu, &curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007102 else
7103 {
7104 // global option set
7105 retval = option_set_callback_func(p_ffu, &ffu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01007106 // when using :set, free the local callback
7107 if (!(args->os_flags & OPT_GLOBAL))
7108 free_callback(&curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007109 }
7110
7111 if (retval == FAIL)
7112 return e_invalid_argument;
7113
7114 // If the option value starts with <SID> or s:, then replace that with
7115 // the script identifier.
7116 char_u **varp = (char_u **)args->os_varp;
7117 char_u *name = get_scriptlocal_funcname(*varp);
7118 if (name != NULL)
7119 {
7120 free_string_option(*varp);
7121 *varp = name;
7122 }
7123
7124 return NULL;
7125}
7126
7127# if defined(EXITFREE) || defined(PROTO)
7128 void
7129free_findfunc_option(void)
7130{
7131 free_callback(&ffu_cb);
7132}
7133# endif
7134
7135/*
7136 * Mark the global 'findfunc' callback with "copyID" so that it is not
7137 * garbage collected.
7138 */
7139 int
7140set_ref_in_findfunc(int copyID UNUSED)
7141{
7142 int abort = FALSE;
7143 abort = set_ref_in_callback(&ffu_cb, copyID);
7144 return abort;
7145}
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007146#endif
7147
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148/*
7149 * :sview [+command] file split window with new file, read-only
7150 * :split [[+command] file] split window with current or new file
7151 * :vsplit [[+command] file] split window vertically with current or new file
7152 * :new [[+command] file] split window with no or new file
7153 * :vnew [[+command] file] split vertically window with no or new file
7154 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007155 *
7156 * :tabedit open new Tab page with empty window
7157 * :tabedit [+command] file open new Tab page and edit "file"
7158 * :tabnew [[+command] file] just like :tabedit
7159 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 */
7161 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007162ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007164 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007166#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007167 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02007168 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007169#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02007170 int use_tab = eap->cmdidx == CMD_tabedit
7171 || eap->cmdidx == CMD_tabfind
7172 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007174 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007175 return;
7176
Bram Moolenaar4033c552017-09-16 20:54:51 +02007177#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
Bram Moolenaar4033c552017-09-16 20:54:51 +02007181#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007182 // A ":split" in the quickfix window works like ":new". Don't want two
7183 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02007184 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 {
7186 if (eap->cmdidx == CMD_split)
7187 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 if (eap->cmdidx == CMD_vsplit)
7189 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007193 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007195 char_u *file_to_find = NULL;
7196 char *search_ctx = NULL;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007197
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007198 if (*get_findfunc() != NUL)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007199 {
7200#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007201 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007202 eap->addr_count > 0 ? eap->line2 : 1);
7203#endif
7204 }
7205 else
7206 {
7207 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7208 FNAME_MESS, TRUE, curbuf->b_ffname,
7209 &file_to_find, &search_ctx);
7210 vim_free(file_to_find);
7211 vim_findfile_cleanup(search_ctx);
7212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 if (fname == NULL)
7214 goto theend;
7215 eap->arg = fname;
7216 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007217# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007218 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 && eap->cmdidx != CMD_new)
7221 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007222 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007223# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007224 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007225# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007226 au_has_group((char_u *)"FileExplorer"))
7227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007228 // No browsing supported but we do have the file explorer:
7229 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007230 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007231 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007232 }
7233 else
7234 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02007235 fname = do_browse(0, (char_u *)(use_tab
7236 ? _("Edit File in new tab page")
7237 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007239 if (fname == NULL)
7240 goto theend;
7241 eap->arg = fname;
7242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007244 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007247 /*
7248 * Either open new tab page or split the window.
7249 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007250 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007251 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007252 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007253 : eap->addr_count == 0 ? 0
7254 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007255 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007256 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007257
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007258 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007259 if (curwin != old_curwin
7260 && win_valid(old_curwin)
7261 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007262 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007263 old_curwin->w_alt_fnum = curbuf->b_fnum;
7264 }
7265 }
7266 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007269 // Reset 'scrollbind' when editing another file, but keep it when
7270 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007271 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007272 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 else
7274 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 do_exedit(eap, old_curwin);
7276 }
7277
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007278# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007279 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282theend:
7283 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007285
7286/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007287 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007288 */
7289 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007290tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007291{
7292 exarg_T ea;
7293
Bram Moolenaara80faa82020-04-12 19:37:17 +02007294 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007295 ea.cmdidx = CMD_tabnew;
7296 ea.cmd = (char_u *)"tabn";
7297 ea.arg = (char_u *)"";
7298 ex_splitview(&ea);
7299}
7300
7301/*
7302 * :tabnext command
7303 */
7304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007305ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007306{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007307 int tab_number;
7308
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007309 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007310 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007311 switch (eap->cmdidx)
7312 {
7313 case CMD_tabfirst:
7314 case CMD_tabrewind:
7315 goto_tabpage(1);
7316 break;
7317 case CMD_tablast:
7318 goto_tabpage(9999);
7319 break;
7320 case CMD_tabprevious:
7321 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007322 if (eap->arg && *eap->arg != NUL)
7323 {
7324 char_u *p = eap->arg;
7325 char_u *p_save = p;
7326
7327 tab_number = getdigits(&p);
7328 if (p == p_save || *p_save == '-' || *p != NUL
7329 || tab_number == 0)
7330 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007331 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007332 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007333 return;
7334 }
7335 }
7336 else
7337 {
7338 if (eap->addr_count == 0)
7339 tab_number = 1;
7340 else
7341 {
7342 tab_number = eap->line2;
7343 if (tab_number < 1)
7344 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007345 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007346 return;
7347 }
7348 }
7349 }
7350 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007351 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007352 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007353 tab_number = get_tabpage_arg(eap);
7354 if (eap->errmsg == NULL)
7355 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007356 break;
7357 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007358}
7359
7360/*
7361 * :tabmove command
7362 */
7363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007364ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007365{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007366 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007367
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007368 tab_number = get_tabpage_arg(eap);
7369 if (eap->errmsg == NULL)
7370 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007371}
7372
7373/*
7374 * :tabs command: List tabs and their contents.
7375 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007377ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007378{
7379 tabpage_T *tp;
7380 win_T *wp;
7381 int tabcount = 1;
7382
7383 msg_start();
7384 msg_scroll = TRUE;
7385 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7386 {
7387 msg_putchar('\n');
7388 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007389 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007390 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007391 ui_breakcheck();
7392
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007393 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007394 wp = firstwin;
7395 else
7396 wp = tp->tp_firstwin;
7397 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7398 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007399 msg_putchar('\n');
7400 msg_putchar(wp == curwin ? '>' : ' ');
7401 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007402 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7403 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007404 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007405 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007406 else
7407 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7408 IObuff, IOSIZE, TRUE);
7409 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007410 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007411 ui_breakcheck();
7412 }
7413 }
7414}
7415
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416/*
7417 * ":mode": Set screen mode.
7418 * If no argument given, just get the screen size and redraw.
7419 */
7420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007421ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
7423 if (*eap->arg == NUL)
7424 shell_resized();
7425 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007426 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427}
7428
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429/*
7430 * ":resize".
7431 * set, increment or decrement current window height
7432 */
7433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007434ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435{
7436 int n;
7437 win_T *wp = curwin;
7438
7439 if (eap->addr_count > 0)
7440 {
7441 n = eap->line2;
7442 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7443 ;
7444 }
7445
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007446# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007450 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {
7452 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007453 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007454 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007456 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 }
7458 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 {
7460 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007461 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007462 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007464 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 }
7466}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
7468/*
7469 * ":find [+command] <file>" command.
7470 */
7471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007472ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473{
Colin Kennedy21570352024-03-03 16:16:47 +01007474 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007475 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007476
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007477 char_u *fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007479 char_u *file_to_find = NULL;
7480 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007482 if (*get_findfunc() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 {
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007484#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007485 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007486 eap->addr_count > 0 ? eap->line2 : 1);
7487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 }
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007489 else
7490 {
7491 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7492 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
7493 if (eap->addr_count > 0)
7494 {
7495 // Repeat finding the file "count" times. This matters when it appears
7496 // several times in the path.
7497 count = eap->line2;
7498 while (fname != NULL && --count > 0)
7499 {
7500 vim_free(fname);
7501 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7502 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
7503 }
7504 }
7505 VIM_CLEAR(file_to_find);
7506 vim_findfile_cleanup(search_ctx);
7507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007509 if (fname == NULL)
7510 return;
7511
7512 eap->arg = fname;
7513 do_exedit(eap, NULL);
7514 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515}
7516
7517/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007518 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007519 */
7520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007521ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007522{
7523 regmatch_T regmatch;
7524 char_u *p;
7525
Bram Moolenaar65088802021-03-13 21:07:21 +01007526#ifdef FEAT_EVAL
7527 if (not_in_vim9(eap) == FAIL)
7528 return;
7529#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007530 curwin->w_cursor.lnum = eap->line2;
7531 beginline(BL_SOL | BL_FIX);
7532 if (*eap->arg == '/')
7533 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007534 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007535 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007536 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007537 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007538 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007539 if (regmatch.regprog != NULL)
7540 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007541 // make a copy of the line, when searching for a mark it might be
7542 // flushed
7543 char_u *line = vim_strsave(ml_get_curline());
7544
Bram Moolenaardf177f62005-02-22 08:39:57 +00007545 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007546 if (vim_regexec(&regmatch, line, (colnr_T)0))
7547 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007548 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007549 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007550 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007551 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007552 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007553 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007554 eap->arg += STRLEN(eap->arg);
7555 }
7556 check_cursor();
7557
7558 eap->cmdidx = CMD_visual;
7559 do_exedit(eap, NULL);
7560}
7561
7562/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007563 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 */
7565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007566ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007568 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7569
Colin Kennedy21570352024-03-03 16:16:47 +01007570 // Exclude commands which keep the window's current buffer
7571 if (
7572 eap->cmdidx != CMD_badd
7573 && eap->cmdidx != CMD_balt
7574 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007575 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7576 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007577 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007578
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 do_exedit(eap, NULL);
7580}
7581
7582/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007583 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007586do_exedit(
7587 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007588 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589{
7590 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007592 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007594 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7595 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007596 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 /*
7598 * ":vi" command ends Ex mode.
7599 */
7600 if (exmode_active && (eap->cmdidx == CMD_visual
7601 || eap->cmdidx == CMD_view))
7602 {
7603 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007604 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007606 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007607 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007608 if (global_busy)
7609 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007610 if (eap->nextcmd != NULL)
7611 {
7612 stuffReadbuff(eap->nextcmd);
7613 eap->nextcmd = NULL;
7614 }
7615
7616 if (exmode_was != EXMODE_VIM)
7617 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007618 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007619 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007620 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007621 no_wait_return = 0;
7622 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007623 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007624 msg_scroll = 0;
7625#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007626 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007627 hold_gui_events = 0;
7628#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007629 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007630 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007631
7632 main_loop(FALSE, TRUE);
7633
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007634 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007635 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007636 no_wait_return = save_nwr;
7637 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007638#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007639 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007640#endif
7641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 }
7645
7646 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007647 || eap->cmdidx == CMD_tabnew
7648 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007649 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007651 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 setpcmark();
7653 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007654 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7655 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007657 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 || *eap->arg != NUL
7659#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007660 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661#endif
7662 )
7663 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007664 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7665 // Only ":edit" or ":script" can bring us here, others are stopped
7666 // earlier.
7667 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007668 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007669
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 n = readonlymode;
7671 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7672 readonlymode = TRUE;
7673 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007674 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7675 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007676 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7677 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7679 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007680 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7682 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7683 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007684 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007686 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007687 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007688 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7689 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007690 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007692 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 if (old_curwin != NULL)
7694 {
7695 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007696 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007698#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007699 cleanup_T cs;
7700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007701 // Reset the error/interrupt/exception state here so that
7702 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007703 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007704#endif
7705#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007707#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007708 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007709
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007710#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007711 // Restore the error/interrupt/exception state if not
7712 // discarded by a new aborting error, interrupt, or
7713 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007714 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 }
7717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 }
7719 else if (readonlymode && curbuf->b_nwindows == 1)
7720 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007721 // When editing an already visited buffer, 'readonly' won't be set
7722 // but the previous value is kept. With ":view" and ":sview" we
7723 // want the file to be readonly, except when another window is
7724 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 curbuf->b_p_ro = TRUE;
7726 }
7727 readonlymode = n;
7728 }
7729 else
7730 {
7731 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007732 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 if (n != curwin->w_arg_idx_invalid)
7736 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 }
7738
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 /*
7740 * if ":split file" worked, set alternate file name in old window to new
7741 * file
7742 */
7743 if (old_curwin != NULL
7744 && *eap->arg != NUL
7745 && curwin != old_curwin
7746 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007747 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007748 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750
7751 ex_no_reprint = TRUE;
7752}
7753
7754#ifndef FEAT_GUI
7755/*
7756 * ":gui" and ":gvim" when there is no GUI.
7757 */
7758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007759ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007761 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762}
7763#endif
7764
Bram Moolenaar4f974752019-02-17 17:44:42 +01007765#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007767ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769 gui_make_tearoff(eap->arg);
7770}
7771#endif
7772
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007773#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7774 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007776ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007778# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7779 if (gui.in_use)
7780 gui_make_popup(eap->arg, eap->forceit);
7781# ifdef FEAT_TERM_POPUP_MENU
7782 else
7783# endif
7784# endif
7785# ifdef FEAT_TERM_POPUP_MENU
7786 pum_make_popup(eap->arg, eap->forceit);
7787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788}
7789#endif
7790
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007792ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007795 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007797 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798}
7799
7800/*
7801 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7802 * offset.
7803 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007806ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007809 win_T *save_curwin = curwin;
7810 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 long topline;
7812 long y;
7813 linenr_T old_linenr = curwin->w_cursor.lnum;
7814
7815 setpcmark();
7816
7817 /*
7818 * determine max topline
7819 */
7820 if (curwin->w_p_scb)
7821 {
7822 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007823 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {
7825 if (wp->w_p_scb && wp->w_buffer)
7826 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007827 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 if (topline > y)
7829 topline = y;
7830 }
7831 }
7832 if (topline < 1)
7833 topline = 1;
7834 }
7835 else
7836 {
7837 topline = 1;
7838 }
7839
7840
7841 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007842 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007844 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {
7846 if (curwin->w_p_scb)
7847 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007848 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 y = topline - curwin->w_topline;
7850 if (y > 0)
7851 scrollup(y, TRUE);
7852 else
7853 scrolldown(-y, TRUE);
7854 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007855 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 }
7859 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007860 curwin = save_curwin;
7861 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 if (curwin->w_p_scb)
7863 {
7864 did_syncbind = TRUE;
7865 checkpcmark();
7866 if (old_linenr != curwin->w_cursor.lnum)
7867 {
7868 char_u ctrl_o[2];
7869
7870 ctrl_o[0] = Ctrl_O;
7871 ctrl_o[1] = 0;
7872 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7873 }
7874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875}
7876
7877
7878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007879ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007881 int i;
7882 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7883 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007885 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007887 do_bang(1, eap, FALSE, FALSE, TRUE);
7888 return;
7889 }
7890
7891 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7892 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893
7894#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007895 if (cmdmod.cmod_flags & CMOD_BROWSE)
7896 {
7897 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007899 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7900 NULL, NULL, NULL, curbuf);
7901 if (browseFile != NULL)
7902 {
7903 i = readfile(browseFile, NULL,
7904 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7905 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 }
7907 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007908 i = OK;
7909 }
7910 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007912 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007914 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 return;
7916 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007917 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 }
7919 else
7920 {
7921 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7922 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7923 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007924 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925
7926 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007927 if (i != OK)
7928 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007929#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007930 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007932 semsg(_(e_cant_open_file_str), eap->arg);
7933 }
7934 else
7935 {
7936 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007937 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007938 // Delete the empty line that remains. Historically ex does
7939 // this but vi doesn't.
7940 if (eap->line2 == 0)
7941 lnum = curbuf->b_ml.ml_line_count;
7942 else
7943 lnum = 1;
7944 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007945 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007946 ml_delete(lnum);
7947 if (curwin->w_cursor.lnum > 1
7948 && curwin->w_cursor.lnum >= lnum)
7949 --curwin->w_cursor.lnum;
7950 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007951 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007952 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007953 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 }
7955}
7956
Bram Moolenaarea408852005-06-25 22:49:46 +00007957static char_u *prev_dir = NULL;
7958
7959#if defined(EXITFREE) || defined(PROTO)
7960 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007961free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007962{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007963 VIM_CLEAR(prev_dir);
7964 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007965}
7966#endif
7967
Bram Moolenaarf4258302013-06-02 18:20:17 +02007968/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007969 * Get the previous directory for the given chdir scope.
7970 */
7971 static char_u *
7972get_prevdir(cdscope_T scope)
7973{
7974 if (scope == CDSCOPE_WINDOW)
7975 return curwin->w_prevdir;
7976 else if (scope == CDSCOPE_TABPAGE)
7977 return curtab->tp_prevdir;
7978 return prev_dir;
7979}
7980
7981/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007982 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007983 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7984 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007985 */
7986 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007987post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007988{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007989 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007990 // Clear tab local directory for both :cd and :tcd
7991 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007992 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007993 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007994 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007995 char_u *pdir = get_prevdir(scope);
7996
7997 // If still in the global directory, need to remember current
7998 // directory as the global directory.
7999 if (globaldir == NULL && pdir != NULL)
8000 globaldir = vim_strsave(pdir);
8001
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008002 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02008003 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008004 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008005 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008006 curtab->tp_localdir = vim_strsave(NameBuff);
8007 else
8008 curwin->w_localdir = vim_strsave(NameBuff);
8009 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02008010 }
8011 else
8012 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008013 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01008014 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008015 }
8016
zeertzjq64be6aa2021-11-19 11:59:08 +00008017 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008018 shorten_fnames(TRUE);
8019}
8020
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008021/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008022 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
8023 */
8024 void
8025trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
8026{
8027#ifdef FEAT_EVAL
8028 dict_T *v_event;
8029 save_v_event_T save_v_event;
8030
8031 v_event = get_v_event(&save_v_event);
8032 (void)dict_add_string(v_event, "directory", new_dir);
8033 dict_set_items_ro(v_event);
8034#endif
8035 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
8036#ifdef FEAT_EVAL
8037 restore_v_event(v_event, &save_v_event);
8038#endif
8039}
8040
8041/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008042 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02008043 * chdir() function.
8044 * scope == CDSCOPE_WINDOW: changes the window-local directory
8045 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
8046 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008047 * Returns TRUE if the directory is successfully changed.
8048 */
8049 int
8050changedir_func(
8051 char_u *new_dir,
8052 int forceit,
8053 cdscope_T scope)
8054{
Bram Moolenaar002bc792020-06-05 22:33:42 +02008055 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008056 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008057 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00008058 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008059 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008060
Bram Moolenaar7cc96922020-01-31 22:41:38 +01008061 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008062 return FALSE;
8063
8064 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
8065 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00008066 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008067 return FALSE;
8068 }
8069
8070 // ":cd -": Change to previous directory
8071 if (STRCMP(new_dir, "-") == 0)
8072 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008073 pdir = get_prevdir(scope);
8074 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008075 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008076 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008077 return FALSE;
8078 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02008079 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008080 }
8081
8082 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008083 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02008084 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008085 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02008086 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008087
Bakudankun29f3a452021-12-11 12:28:08 +00008088 // For UNIX ":cd" means: go to home directory.
8089 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008090#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008091 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00008092#else
8093 if (*new_dir == NUL && p_cdh)
8094#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008095 {
8096 // use NameBuff for home directory name
8097# ifdef VMS
8098 char_u *p;
8099
8100 p = mch_getenv((char_u *)"SYS$LOGIN");
8101 if (p == NULL || *p == NUL) // empty is the same as not set
8102 NameBuff[0] = NUL;
8103 else
8104 vim_strncpy(NameBuff, p, MAXPATHL - 1);
8105# else
8106 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8107# endif
8108 new_dir = NameBuff;
8109 }
zeertzjqf38aad82021-12-30 13:45:57 +00008110 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008111 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
8112 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00008113 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008114 if (scope == CDSCOPE_WINDOW)
8115 acmd_fname = (char_u *)"window";
8116 else if (scope == CDSCOPE_TABPAGE)
8117 acmd_fname = (char_u *)"tabpage";
8118 else
8119 acmd_fname = (char_u *)"global";
8120 trigger_DirChangedPre(acmd_fname, new_dir);
8121
8122 if (vim_chdir(new_dir))
8123 {
8124 emsg(_(e_command_failed));
8125 vim_free(pdir);
8126 return FALSE;
8127 }
Richard Doty3d0abad2021-12-29 14:39:08 +00008128 }
zeertzjq73257142022-02-02 13:16:37 +00008129
8130 if (scope == CDSCOPE_WINDOW)
8131 pp = &curwin->w_prevdir;
8132 else if (scope == CDSCOPE_TABPAGE)
8133 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008134 else
zeertzjq73257142022-02-02 13:16:37 +00008135 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008136 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00008137 *pp = pdir;
8138
8139 post_chdir(scope);
8140
8141 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008142 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008143 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00008144 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008145}
Bram Moolenaarea408852005-06-25 22:49:46 +00008146
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008148 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008150 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008151ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01008153 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154
8155 new_dir = eap->arg;
8156#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00008157 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
8158 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008160 ex_pwd(NULL);
8161 return;
8162 }
8163#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008164
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008165 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008166
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008167 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8168 scope = CDSCOPE_WINDOW;
8169 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
8170 scope = CDSCOPE_TABPAGE;
8171
8172 if (changedir_func(new_dir, eap->forceit, scope))
8173 {
8174 // Echo the new current directory if the command was typed.
8175 if (KeyTyped || p_verbose >= 5)
8176 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 }
8178}
8179
8180/*
8181 * ":pwd".
8182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008184ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185{
8186 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8187 {
8188#ifdef BACKSLASH_IN_FILENAME
8189 slash_adjust(NameBuff);
8190#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02008191 if (p_verbose > 0)
8192 {
8193 char *context = "global";
8194
Bram Moolenaar05268152021-11-18 18:53:45 +00008195 if (last_chdir_reason != NULL)
8196 context = last_chdir_reason;
8197 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02008198 context = "window";
8199 else if (curtab->tp_localdir != NULL)
8200 context = "tabpage";
8201 smsg("[%s] %s", context, (char *)NameBuff);
8202 }
8203 else
8204 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 }
8206 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00008207 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208}
8209
8210/*
8211 * ":=".
8212 */
8213 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008214ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008216 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008217 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218}
8219
8220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008221ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008223 int n;
8224 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225
8226 if (cursor_valid())
8227 {
8228 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8229 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00008230 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008232
8233 len = eap->line2;
8234 switch (*eap->arg)
8235 {
8236 case 'm': break;
8237 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008238 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008239 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008240
8241 // Hide the cursor if invoked with !
8242 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243}
8244
8245/*
8246 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008247 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 */
8249 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008250do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251{
Bram Moolenaar52953192019-08-16 10:27:13 +02008252 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008253 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008254# ifdef ELAPSED_FUNC
8255 elapsed_T start_tv;
8256
8257 // Remember at what time we started, so that we know how much longer we
8258 // should wait after waiting for a bit.
8259 ELAPSED_INIT(start_tv);
8260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008262 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008263 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008264 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008265 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008266
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008267 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008268 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008270 wait_now = msec - done > 1000L ? 1000L : msec - done;
8271#ifdef FEAT_TIMERS
8272 {
8273 long due_time = check_due_timer();
8274
8275 if (due_time > 0 && due_time < wait_now)
8276 wait_now = due_time;
8277 }
8278#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008279#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008280 if (has_any_channel() && wait_now > 20L)
8281 wait_now = 20L;
8282#endif
8283#ifdef FEAT_SOUND
8284 if (has_any_sound_callback() && wait_now > 20L)
8285 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008286#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008287 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008288
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008289#ifdef FEAT_JOB_CHANNEL
8290 if (has_any_channel())
8291 ui_breakcheck_force(TRUE);
8292 else
8293#endif
8294 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008295#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008296 // Process the netbeans and clientserver messages that may have been
8297 // received in the call to ui_breakcheck() when the GUI is in use. This
8298 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008299 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008300#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008301
8302# ifdef ELAPSED_FUNC
8303 // actual time passed
8304 done = ELAPSED_FUNC(start_tv);
8305# else
8306 // guestimate time passed (will actually be more)
8307 done += wait_now;
8308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008310
8311 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8312 // input buffer, otherwise a following call to input() fails.
8313 if (got_int)
8314 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008315
8316 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008317 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318}
8319
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320/*
8321 * ":winsize" command (obsolete).
8322 */
8323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008324ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325{
8326 int w, h;
8327 char_u *arg = eap->arg;
8328 char_u *p;
8329
Keith Thompson184f71c2024-01-04 21:19:04 +01008330 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008331 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008332 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008333 return;
8334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 w = getdigits(&arg);
8336 arg = skipwhite(arg);
8337 p = arg;
8338 h = getdigits(&arg);
8339 if (*p != NUL && *arg == NUL)
8340 set_shellsize(w, h, TRUE);
8341 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008342 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343}
8344
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008346ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347{
8348 int xchar = NUL;
8349 char_u *p;
8350
8351 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8352 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008353 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 if (eap->arg[1] == NUL)
8355 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008356 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 return;
8358 }
8359 xchar = eap->arg[1];
8360 p = eap->arg + 2;
8361 }
8362 else
8363 p = eap->arg + 1;
8364
Bram Moolenaar63b91732021-08-05 20:40:03 +02008365 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008367 if (*p != NUL && *p != (
8368#ifdef FEAT_EVAL
8369 in_vim9script() ? '#' :
8370#endif
8371 '"')
8372 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008373 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008374 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008376 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008377 postponed_split_flags = cmdmod.cmod_split;
8378 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8380 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008381 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384
Bram Moolenaar843ee412004-06-30 16:16:41 +00008385#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386/*
8387 * ":winpos".
8388 */
8389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008390ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391{
8392 int x, y;
8393 char_u *arg = eap->arg;
8394 char_u *p;
8395
8396 if (*arg == NUL)
8397 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008398# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008399# ifdef VIMDLL
8400 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8401 mch_get_winpos(&x, &y) != FAIL)
8402# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008404# else
8405 if (mch_get_winpos(&x, &y) != FAIL)
8406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {
8408 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008409 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 }
8411 else
8412# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008413 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 }
8415 else
8416 {
8417 x = getdigits(&arg);
8418 arg = skipwhite(arg);
8419 p = arg;
8420 y = getdigits(&arg);
8421 if (*p == NUL || *arg != NUL)
8422 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008423 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 return;
8425 }
8426# ifdef FEAT_GUI
8427 if (gui.in_use)
8428 gui_mch_set_winpos(x, y);
8429 else if (gui.starting)
8430 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008431 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 gui_win_x = x;
8433 gui_win_y = y;
8434 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008435# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 else
8437# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008438# endif
8439# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008440 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441# endif
8442# ifdef HAVE_TGETENT
8443 if (*T_CWP)
8444 term_set_winpos(x, y);
8445# endif
8446 }
8447}
8448#endif
8449
8450/*
8451 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8452 */
8453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008454ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455{
8456 oparg_T oa;
8457
8458 clear_oparg(&oa);
8459 oa.regname = eap->regname;
8460 oa.start.lnum = eap->line1;
8461 oa.end.lnum = eap->line2;
8462 oa.line_count = eap->line2 - eap->line1 + 1;
8463 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008465 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 {
8467 setpcmark();
8468 curwin->w_cursor.lnum = eap->line1;
8469 beginline(BL_SOL | BL_FIX);
8470 }
8471
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008472 if (VIsual_active)
8473 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008474
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 switch (eap->cmdidx)
8476 {
8477 case CMD_delete:
8478 oa.op_type = OP_DELETE;
8479 op_delete(&oa);
8480 break;
8481
8482 case CMD_yank:
8483 oa.op_type = OP_YANK;
8484 (void)op_yank(&oa, FALSE, TRUE);
8485 break;
8486
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008487 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008488 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008490 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8491#else
8492 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008494 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 oa.op_type = OP_RSHIFT;
8496 else
8497 oa.op_type = OP_LSHIFT;
8498 op_shift(&oa, FALSE, eap->amount);
8499 break;
8500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008502 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503}
8504
8505/*
8506 * ":put".
8507 */
8508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008509ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008511 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 if (eap->line2 == 0)
8513 {
8514 eap->line2 = 1;
8515 eap->forceit = TRUE;
8516 }
8517 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008518 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008519 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008520 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521}
8522
8523/*
8524 * Handle ":copy" and ":move".
8525 */
8526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008527ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528{
8529 long n;
8530
Bram Moolenaar491799b2020-08-01 19:23:43 +02008531#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008532 if (not_in_vim9(eap) == FAIL)
8533 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008534#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008535 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008536 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 {
8538 eap->nextcmd = NULL;
8539 return;
8540 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008541 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542
8543 /*
8544 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8545 */
8546 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8547 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008548 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 return;
8550 }
8551
8552 if (eap->cmdidx == CMD_move)
8553 {
8554 if (do_move(eap->line1, eap->line2, n) == FAIL)
8555 return;
8556 }
8557 else
8558 ex_copy(eap->line1, eap->line2, n);
8559 u_clearline();
8560 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008561 ex_may_print(eap);
8562}
8563
8564/*
8565 * Print the current line if flags were given to the Ex command.
8566 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008567 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008568ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008569{
8570 if (eap->flags != 0)
8571 {
8572 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8573 (eap->flags & EXFLAG_LIST));
8574 ex_no_reprint = TRUE;
8575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576}
8577
8578/*
8579 * ":smagic" and ":snomagic".
8580 */
8581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008582ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008584 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008586 magic_overruled = eap->cmdidx == CMD_smagic
8587 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008588 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008589 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590}
8591
8592/*
8593 * ":join".
8594 */
8595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008596ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
8598 curwin->w_cursor.lnum = eap->line1;
8599 if (eap->line1 == eap->line2)
8600 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008601 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 return;
8603 if (eap->line2 == curbuf->b_ml.ml_line_count)
8604 {
8605 beep_flush();
8606 return;
8607 }
8608 ++eap->line2;
8609 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008610 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008612 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613}
8614
8615/*
8616 * ":[addr]@r" or ":[addr]*r": execute register
8617 */
8618 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008619ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
8621 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008622 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623
8624 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008625 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626
8627#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008628 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629#endif
8630
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008631 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 c = *eap->arg;
8633 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8634 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008635 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008636 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8637 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008640 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008643 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008645 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008647 /*
8648 * Execute from the typeahead buffer.
8649 * Continue until the stuff buffer is empty and all added characters
8650 * have been consumed.
8651 */
8652 while (!stuff_empty() || typebuf.tb_len > prev_len)
8653 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8654
8655 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656}
8657
8658/*
8659 * ":!".
8660 */
8661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008662ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663{
8664 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8665}
8666
8667/*
8668 * ":undo".
8669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008671ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008673 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008674 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008675 else
8676 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677}
8678
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008679#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008681ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008682{
8683 char_u hash[UNDO_HASH_SIZE];
8684
8685 u_compute_hash(hash);
8686 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8687}
8688
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008689 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008690ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008691{
8692 char_u hash[UNDO_HASH_SIZE];
8693
8694 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008695 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008696}
8697#endif
8698
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699/*
8700 * ":redo".
8701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008703ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
8705 u_redo(1);
8706}
8707
8708/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008709 * ":earlier" and ":later".
8710 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008712ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008713{
8714 long count = 0;
8715 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008716 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008717 char_u *p = eap->arg;
8718
8719 if (*p == NUL)
8720 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008721 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008722 {
8723 count = getdigits(&p);
8724 switch (*p)
8725 {
8726 case 's': ++p; sec = TRUE; break;
8727 case 'm': ++p; sec = TRUE; count *= 60; break;
8728 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008729 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8730 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008731 }
8732 }
8733
8734 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008735 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008736 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008737 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8738 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008739}
8740
8741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 * ":redir": start/stop redirection.
8743 */
8744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008745ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746{
8747 char *mode;
8748 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008749 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
Bram Moolenaarba768492016-07-08 15:32:54 +02008751#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008752 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008753 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008754 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008755 return;
8756 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008757#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008758
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 if (STRICMP(eap->arg, "END") == 0)
8760 close_redir();
8761 else
8762 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008763 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008765 ++arg;
8766 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008768 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 mode = "a";
8770 }
8771 else
8772 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008773 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774
8775 close_redir();
8776
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008777 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008778 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 if (fname == NULL)
8780 return;
8781#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008782 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 {
8784 char_u *browseFile;
8785
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008786 browseFile = do_browse(BROWSE_SAVE,
8787 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008788 fname, NULL, NULL,
8789 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008791 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 vim_free(fname);
8793 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008794 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 }
8796#endif
8797
8798 redir_fd = open_exfile(fname, eap->forceit, mode);
8799 vim_free(fname);
8800 }
8801#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008802 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008804 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008806 ++arg;
8807 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008809 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008810 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008812 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008814 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008815 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008816 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008817 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008819 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008820 if (*arg == '>')
8821 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008822 // Make register empty when not using @A-@Z and the
8823 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008824 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008825 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008827 }
8828 if (*arg != NUL)
8829 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008830 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008831 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008832 }
8833 }
8834 else if (*arg == '=' && arg[1] == '>')
8835 {
8836 int append;
8837
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008838 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008839 close_redir();
8840 arg += 2;
8841
8842 if (*arg == '>')
8843 {
8844 ++arg;
8845 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 }
8847 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008848 append = FALSE;
8849
8850 if (var_redir_start(skipwhite(arg), append) == OK)
8851 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 }
8853#endif
8854
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008855 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008858 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008860
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008861 // Make sure redirection is not off. Can happen for cmdline completion
8862 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008863 if (redir_fd != NULL
8864#ifdef FEAT_EVAL
8865 || redir_reg || redir_vname
8866#endif
8867 )
8868 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869}
8870
8871/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008872 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008874 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008875ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008877 redraw_cmd(eap->forceit);
8878}
8879
8880/*
8881 * ":redraw": force redraw, with clear if "clear" is TRUE.
8882 */
8883 void
8884redraw_cmd(int clear)
8885{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008886 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008888
8889 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008891
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008892 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008894 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 if (need_maketitle)
8896 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008897#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8898# ifdef VIMDLL
8899 if (!gui.in_use)
8900# endif
8901 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008902#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008903 RedrawingDisabled = save_RedrawingDisabled;
8904 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905
Bram Moolenaar5017c662022-04-07 18:06:08 +01008906 // After drawing the statusline screen_attr may still be set.
8907 screen_stop_highlight();
8908
Bram Moolenaara2a89732022-08-31 14:46:18 +01008909 // Reset msg_didout, so that a message that's there is overwritten.
8910 msg_didout = FALSE;
8911 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008913 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008914 need_wait_return = FALSE;
8915
Bram Moolenaara653e532022-04-19 11:38:24 +01008916 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008917 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008918 redrawcmdline();
8919
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 out_flush();
8921}
8922
8923/*
8924 * ":redrawstatus": force redraw of status line(s)
8925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008927ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 if (eap->forceit)
8930 status_redraw_all();
8931 else
8932 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008933 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008934 return; // redraw later
8935
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008936 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008937 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008938
8939 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008940 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008941
zeertzjq320d9102022-09-20 17:12:13 +01008942 if (State & MODE_CMDLINE)
8943 redraw_statuslines();
8944 else
8945 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008946 RedrawingDisabled = save_RedrawingDisabled;
8947 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949}
8950
Bram Moolenaare12bab32019-01-08 22:02:56 +01008951/*
8952 * ":redrawtabline": force redraw of the tabline
8953 */
8954 static void
8955ex_redrawtabline(exarg_T *eap UNUSED)
8956{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008957 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008958 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008959
8960 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008961 p_lz = FALSE;
8962
8963 draw_tabline();
8964
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008965 RedrawingDisabled = save_RedrawingDisabled;
8966 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008967 out_flush();
8968}
8969
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008971close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
8973 if (redir_fd != NULL)
8974 {
8975 fclose(redir_fd);
8976 redir_fd = NULL;
8977 }
8978#ifdef FEAT_EVAL
8979 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008980 if (redir_vname)
8981 {
8982 var_redir_stop();
8983 redir_vname = 0;
8984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985#endif
8986}
8987
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008988#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008989 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008990vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008991{
8992 if (vim_mkdir(name, prot) != 0)
8993 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008994 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008995 return FAIL;
8996 }
8997 return OK;
8998}
8999#endif
9000
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001/*
9002 * Open a file for writing for an Ex command, with some checks.
9003 * Return file descriptor, or NULL on failure.
9004 */
9005 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009006open_exfile(
9007 char_u *fname,
9008 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009009 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010{
9011 FILE *fd;
9012
9013#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009014 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 if (mch_isdir(fname))
9016 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01009017 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 return NULL;
9019 }
9020#endif
9021 if (!forceit && *mode != 'a' && vim_fexists(fname))
9022 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00009023 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 return NULL;
9025 }
9026
9027 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00009028 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029
9030 return fd;
9031}
9032
9033/*
9034 * ":mark" and ":k".
9035 */
9036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009037ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
9039 pos_T pos;
9040
Bram Moolenaar10b94212021-02-19 21:42:57 +01009041#ifdef FEAT_EVAL
9042 if (not_in_vim9(eap) == FAIL)
9043 return;
9044#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009045 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009047 emsg(_(e_argument_required));
9048 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009050
9051 if (eap->arg[1] != NUL) // more than one character?
9052 {
9053 semsg(_(e_trailing_characters_str), eap->arg);
9054 return;
9055 }
9056
9057 pos = curwin->w_cursor; // save curwin->w_cursor
9058 curwin->w_cursor.lnum = eap->line2;
9059 beginline(BL_WHITE | BL_FIX);
9060 if (setmark(*eap->arg) == FAIL) // set mark
9061 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
9062 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063}
9064
9065/*
9066 * Update w_topline, w_leftcol and the cursor position.
9067 */
9068 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009069update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009071 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 update_topline();
9073 if (!curwin->w_p_wrap)
9074 validate_cursor();
9075 update_curswant();
9076}
9077
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009079 * Save the current State and go to Normal mode.
9080 * Return TRUE if the typeahead could be saved.
9081 */
9082 int
9083save_current_state(save_state_T *sst)
9084{
9085 sst->save_msg_scroll = msg_scroll;
9086 sst->save_restart_edit = restart_edit;
9087 sst->save_msg_didout = msg_didout;
9088 sst->save_State = State;
9089 sst->save_insertmode = p_im;
9090 sst->save_finish_op = finish_op;
9091 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009092 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009093 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009094
Bram Moolenaar4324d872020-12-01 20:12:24 +01009095 msg_scroll = FALSE; // no msg scrolling in Normal mode
9096 restart_edit = 0; // don't go to Insert mode
9097 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01009098
Bram Moolenaara452b802020-12-01 21:47:59 +01009099 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01009100 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009101
9102 /*
9103 * Save the current typeahead. This is required to allow using ":normal"
9104 * from an event handler and makes sure we don't hang when the argument
9105 * ends with half a command.
9106 */
9107 save_typeahead(&sst->tabuf);
9108 return sst->tabuf.typebuf_valid;
9109}
9110
9111 void
9112restore_current_state(save_state_T *sst)
9113{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009114 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02009115 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009116
9117 msg_scroll = sst->save_msg_scroll;
9118 restart_edit = sst->save_restart_edit;
9119 p_im = sst->save_insertmode;
9120 finish_op = sst->save_finish_op;
9121 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009122 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009123 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009124 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01009125 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009127 // Restore the state (needed when called from a function executed for
9128 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009129 State = sst->save_State;
9130#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009131 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009132#endif
9133}
9134
9135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 * ":normal[!] {commands}": Execute normal mode commands.
9137 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009138 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009139ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009141 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 char_u *arg = NULL;
9143 int l;
9144 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009146 if (ex_normal_lock > 0)
9147 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00009148 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009149 return;
9150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 if (ex_normal_busy >= p_mmd)
9152 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009153 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 return;
9155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 /*
9158 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9159 * this for the K_SPECIAL leading byte, otherwise special keys will not
9160 * work.
9161 */
9162 if (has_mbyte)
9163 {
9164 int len = 0;
9165
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009166 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 for (p = eap->arg; *p != NUL; ++p)
9168 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009169#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009170 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01009172#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009173 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009174 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009175#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 )
9179 len += 2;
9180 }
9181 if (len > 0)
9182 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009183 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 if (arg != NULL)
9185 {
9186 len = 0;
9187 for (p = eap->arg; *p != NUL; ++p)
9188 {
9189 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01009190#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 if (*p == CSI)
9192 {
9193 arg[len++] = KS_EXTRA;
9194 arg[len++] = (int)KE_CSI;
9195 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009196#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009197 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 {
9199 arg[len++] = *++p;
9200 if (*p == K_SPECIAL)
9201 {
9202 arg[len++] = KS_SPECIAL;
9203 arg[len++] = KE_FILLER;
9204 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009205#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 else if (*p == CSI)
9207 {
9208 arg[len++] = KS_EXTRA;
9209 arg[len++] = (int)KE_CSI;
9210 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 }
9213 arg[len] = NUL;
9214 }
9215 }
9216 }
9217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009219 ++ex_normal_busy;
9220 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 {
9222 /*
9223 * Repeat the :normal command for each line in the range. When no
9224 * range given, execute it just once, without positioning the cursor
9225 * first.
9226 */
9227 do
9228 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 if (eap->addr_count != 0)
9230 {
9231 curwin->w_cursor.lnum = eap->line1++;
9232 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02009233 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 }
9235
Bram Moolenaar13505972019-01-24 15:04:48 +01009236 exec_normal_cmd(arg != NULL
9237 ? arg
9238 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 }
9240 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9241 }
9242
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009243 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 update_topline_cursor();
9245
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009246 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009248 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009249#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009250 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009251#endif
9252
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254}
9255
9256/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009257 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 */
9259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009260ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009262 if (eap->forceit)
9263 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009264 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009265 if (!curwin->w_cursor.lnum)
9266 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009267 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009268 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009269#ifdef FEAT_TERMINAL
9270 // Ignore this when running in an active terminal.
9271 if (term_job_running(curbuf->b_term))
9272 return;
9273#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009274
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009275 // Ignore the command when already in Insert mode. Inserting an
9276 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009277 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009278 return;
9279
Bram Moolenaar64969662005-12-14 21:59:55 +00009280 if (eap->cmdidx == CMD_startinsert)
9281 restart_edit = 'a';
9282 else if (eap->cmdidx == CMD_startreplace)
9283 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009285 restart_edit = 'V';
9286
9287 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009289 if (eap->cmdidx == CMD_startinsert)
9290 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009291 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009293
9294 if (VIsual_active)
9295 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296}
9297
9298/*
9299 * ":stopinsert"
9300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009302ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304 restart_edit = 0;
9305 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009306 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009309/*
9310 * Execute normal mode command "cmd".
9311 * "remap" can be REMAP_NONE or REMAP_YES.
9312 */
9313 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009314exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009315{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009316 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009317 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009318 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009319}
Bram Moolenaar25281632016-01-21 23:32:32 +01009320
Bram Moolenaar25281632016-01-21 23:32:32 +01009321/*
9322 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009323 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009324 */
9325 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009326exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009327{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009328 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009329 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009330
Bram Moolenaar905dd902019-04-07 14:21:47 +02009331 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9332 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009333 clear_oparg(&oa);
9334 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009335 while ((!stuff_empty()
9336 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009337 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009338 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009339 {
9340 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009341#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009342 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009343 && oa.op_type == OP_NOP && oa.regname == NUL
9344 && !VIsual_active)
9345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009346 // If terminal_loop() returns OK we got a key that is handled
9347 // in Normal model. With FAIL we first need to position the
9348 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009349 if (terminal_loop(TRUE) == OK)
9350 normal_cmd(&oa, TRUE);
9351 }
9352 else
9353#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009354 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009355 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009356 }
9357}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009358
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359#ifdef FEAT_FIND_ID
9360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009361ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362{
9363 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9364 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009365 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366}
9367
Bram Moolenaar4033c552017-09-16 20:54:51 +02009368#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369/*
9370 * ":psearch"
9371 */
9372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009373ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374{
9375 g_do_tagpreview = p_pvh;
9376 ex_findpat(eap);
9377 g_do_tagpreview = 0;
9378}
9379#endif
9380
9381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009382ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384 int whole = TRUE;
9385 long n;
9386 char_u *p;
9387 int action;
9388
9389 switch (cmdnames[eap->cmdidx].cmd_name[2])
9390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009391 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9393 action = ACTION_GOTO;
9394 else
9395 action = ACTION_SHOW;
9396 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009397 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 action = ACTION_SHOW_ALL;
9399 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009400 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 action = ACTION_GOTO;
9402 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009403 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 action = ACTION_SPLIT;
9405 break;
9406 }
9407
9408 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009409 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 {
9411 n = getdigits(&eap->arg);
9412 eap->arg = skipwhite(eap->arg);
9413 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009414 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 {
9416 whole = FALSE;
9417 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009418 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 if (*p)
9420 {
9421 *p++ = NUL;
9422 p = skipwhite(p);
9423
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009424 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009425 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009426 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009428 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 }
9430 }
9431 if (!eap->skip)
9432 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9433 whole, !eap->forceit,
9434 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009435 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436}
9437#endif
9438
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439
Bram Moolenaar4033c552017-09-16 20:54:51 +02009440#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441/*
9442 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9443 */
9444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009445ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009447 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9449}
9450
9451/*
9452 * ":pedit"
9453 */
9454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009455ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456{
9457 win_T *curwin_save = curwin;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009458 prepare_preview_window();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009460 // Edit the file.
9461 do_exedit(eap, NULL);
9462
9463 back_to_current_window(curwin_save);
9464}
9465
9466/*
9467 * ":pbuffer"
9468 */
9469 static void
9470ex_pbuffer(exarg_T *eap)
9471{
9472 win_T *curwin_save = curwin;
9473 prepare_preview_window();
9474
9475 // Go to the buffer.
9476 do_exbuffer(eap);
9477
9478 back_to_current_window(curwin_save);
9479}
9480
9481 static void
9482prepare_preview_window(void)
9483{
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009484 if (ERROR_IF_ANY_POPUP_WINDOW)
9485 return;
9486
Bram Moolenaar026587b2019-08-17 15:08:00 +02009487 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009489 prepare_tagpreview(TRUE, TRUE, FALSE);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009490}
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009491
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009492 static void
9493back_to_current_window(win_T *curwin_save)
9494{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 if (curwin != curwin_save && win_valid(curwin_save))
9496 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009497 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009499 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 win_enter(curwin_save, TRUE);
9501 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009502# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009503 else if (WIN_IS_POPUP(curwin))
9504 {
9505 // can't keep focus in popup window
9506 win_enter(firstwin, TRUE);
9507 }
9508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 g_do_tagpreview = 0;
9510}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512
9513/*
9514 * ":stag", ":stselect" and ":stjump".
9515 */
9516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009517ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518{
9519 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009520 postponed_split_flags = cmdmod.cmod_split;
9521 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9523 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009524 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526
9527/*
9528 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9529 */
9530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009531ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532{
9533 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9534}
9535
9536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009537ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539 int cmd;
9540
9541 switch (name[1])
9542 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009543 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009545 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009547 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009549 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009551 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009553 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009555 case 'f': // ":tfirst"
9556 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009558 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009560 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009562 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009564 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 return;
9566 }
9567#endif
9568 cmd = DT_TAG;
9569 break;
9570 }
9571
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009572 if (name[0] == 'l')
9573 {
9574#ifndef FEAT_QUICKFIX
9575 ex_ni(eap);
9576 return;
9577#else
9578 cmd = DT_LTAG;
9579#endif
9580 }
9581
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9583 eap->forceit, TRUE);
9584}
9585
John Marriotted908f72024-04-18 22:46:56 +02009586enum {
9587 SPEC_PERC = 0,
9588 SPEC_HASH,
9589 SPEC_CWORD, // cursor word
9590 SPEC_CCWORD, // cursor WORD
9591 SPEC_CEXPR, // expr under cursor
9592 SPEC_CFILE, // cursor path name
9593 SPEC_SFILE, // ":so" file name
9594 SPEC_SLNUM, // ":so" file line number
9595 SPEC_STACK, // call stack
9596 SPEC_SCRIPT, // script file name
9597 SPEC_AFILE, // autocommand file name
9598 SPEC_ABUF, // autocommand buffer number
9599 SPEC_AMATCH, // autocommand match name
9600 SPEC_SFLNUM, // script file line number
9601 SPEC_SID // script ID: <SNR>123_
9602#ifdef FEAT_CLIENTSERVER
9603 , SPEC_CLIENT
9604#endif
9605};
9606
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009608 * Check "str" for starting with a special cmdline variable.
9609 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9610 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9611 */
9612 int
Mike Williams51024bb2024-05-30 07:46:30 +02009613find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009614{
John Marriotted908f72024-04-18 22:46:56 +02009615 // must be sorted by the 'value' field because it is used by bsearch()!
9616 static keyvalue_T spec_str_tab[] = {
9617 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9618 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9619 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9620 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9621 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9622 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9623 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009624#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009625 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009626#endif
John Marriotted908f72024-04-18 22:46:56 +02009627 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9628 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9629 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9630 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9631 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9632 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009633 };
John Marriotted908f72024-04-18 22:46:56 +02009634 keyvalue_T target;
9635 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009636
John Marriotted908f72024-04-18 22:46:56 +02009637 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009638 {
John Marriotted908f72024-04-18 22:46:56 +02009639 case '%':
9640 *usedlen = 1;
9641 return SPEC_PERC;
9642
9643 case '#':
9644 *usedlen = 1;
9645 return SPEC_HASH;
9646
9647 case '<':
9648 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +01009649 target.value.string = src + 1; // skip over '<'
9650 target.value.length = 0; // not used, see cmp_keyvalue_value_n()
John Marriotted908f72024-04-18 22:46:56 +02009651
John Marriott8d4477e2024-11-02 15:59:01 +01009652 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab,
9653 ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]),
9654 cmp_keyvalue_value_n);
John Marriotted908f72024-04-18 22:46:56 +02009655 if (entry == NULL)
9656 return -1;
9657
John Marriott8d4477e2024-11-02 15:59:01 +01009658 *usedlen = entry->value.length + 1;
John Marriotted908f72024-04-18 22:46:56 +02009659 return entry->key;
9660
9661 default:
9662 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009663 }
John Marriotted908f72024-04-18 22:46:56 +02009664
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009665 return -1;
9666}
9667
9668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 * Evaluate cmdline variables.
9670 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009671 * change "%" to curbuf->b_ffname
9672 * "#" to curwin->w_alt_fnum
9673 * "%%" to curwin->w_alt_fnum in Vim9 script
9674 * "<cword>" to word under the cursor
9675 * "<cWORD>" to WORD under the cursor
9676 * "<cexpr>" to C-expression under the cursor
9677 * "<cfile>" to path name under the cursor
9678 * "<sfile>" to sourced file name
9679 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009680 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009681 * "<slnum>" to sourced file line number
9682 * "<afile>" to file name for autocommand
9683 * "<abuf>" to buffer number for autocommand
9684 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 *
9686 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9687 * "" for error without a message) and NULL is returned.
9688 * Returns an allocated string if a valid match was found.
9689 * Returns NULL if no match was found. "usedlen" then still contains the
9690 * number of characters to skip.
9691 */
9692 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009693eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009694 char_u *src, // pointer into commandline
9695 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009696 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009697 linenr_T *lnump, // line number for :e command, or NULL
9698 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009699 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009700 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009701 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
9703 int i;
9704 char_u *s;
9705 char_u *result;
9706 char_u *resultbuf = NULL;
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +01009707 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009709 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009711 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714
9715 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009716 if (escaped != NULL)
9717 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718
9719 /*
9720 * Check if there is something to do.
9721 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009722 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009723 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 {
9725 *usedlen = 1;
9726 return NULL;
9727 }
9728
9729 /*
9730 * Skip when preceded with a backslash "\%" and "\#".
9731 * Note: In "\\%" the % is also not recognized!
9732 */
9733 if (src > srcstart && src[-1] == '\\')
9734 {
9735 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009736 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 return NULL;
9738 }
9739
9740 /*
9741 * word or WORD under cursor
9742 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009743 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9744 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009746 resultlen = find_ident_under_cursor(&result,
9747 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9748 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9749 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 if (resultlen == 0)
9751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009752 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 return NULL;
9754 }
9755 }
9756
9757 /*
9758 * '#': Alternate file name
9759 * '%': Current file name
9760 * File name under the cursor
9761 * File name for autocommand
9762 * and following modifiers
9763 */
9764 else
9765 {
Mike Williams51024bb2024-05-30 07:46:30 +02009766 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009767
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 switch (spec_idx)
9769 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009770 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009771#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009772 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009775 // '%': current file
9776 if (curbuf->b_fname == NULL)
9777 {
9778 result = (char_u *)"";
9779 valid = 0; // Must have ":p:h" to be valid
9780 }
9781 else
9782 {
9783 result = curbuf->b_fname;
9784 tilde_file = STRCMP(result, "~") == 0;
9785 }
9786 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009788#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009789 // "%%" alternate file
9790 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009791#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009792 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009793 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009794 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009796 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 result = arg_all();
9798 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009799 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009800 if (escaped != NULL)
9801 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 break;
9804 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009805 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009806 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009807 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009809 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009810 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009811 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009812 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009814 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009816 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009817 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009818 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009819 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009820 return NULL;
9821 }
9822#ifdef FEAT_EVAL
9823 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9824 (long)i);
9825 if (result == NULL)
9826 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009827 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009828 return NULL;
9829 }
9830#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009831 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 }
9835 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009836 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009837 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9838 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009839 buf = buflist_findnr(i);
9840 if (buf == NULL)
9841 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009842 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009843 return NULL;
9844 }
9845 if (lnump != NULL)
9846 *lnump = ECMD_LAST;
9847 if (buf->b_fname == NULL)
9848 {
9849 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009850 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009851 }
9852 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009853 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009854 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009855 tilde_file = STRCMP(result, "~") == 0;
9856 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 break;
9859
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009860 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009861 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 if (result == NULL)
9863 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009864 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 return NULL;
9866 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009867 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009870 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009872 if (result != NULL && !autocmd_fname_full)
9873 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009874 // Still need to turn the fname into a full path. It is
9875 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009876 autocmd_fname_full = TRUE;
9877 result = FullName_save(autocmd_fname, FALSE);
9878 vim_free(autocmd_fname);
9879 autocmd_fname = result;
9880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 if (result == NULL)
9882 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009883 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 return NULL;
9885 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009886 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 break;
9888
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009889 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 if (autocmd_bufnr <= 0)
9891 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009892 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 return NULL;
9894 }
9895 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9896 result = strbuf;
9897 break;
9898
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009899 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 result = autocmd_match;
9901 if (result == NULL)
9902 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009903 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 return NULL;
9905 }
9906 break;
9907
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009908 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009909 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 if (result == NULL)
9911 {
LemonBoy6013d002022-04-09 21:42:10 +01009912 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9913 return NULL;
9914 }
9915 resultbuf = result; // remember allocated string
9916 break;
9917 case SPEC_STACK: // call stack
9918 result = estack_sfile(ESTACK_STACK);
9919 if (result == NULL)
9920 {
9921 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9922 return NULL;
9923 }
9924 resultbuf = result; // remember allocated string
9925 break;
9926 case SPEC_SCRIPT: // script file name
9927 result = estack_sfile(ESTACK_SCRIPT);
9928 if (result == NULL)
9929 {
9930 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 return NULL;
9932 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009933 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009935
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009936 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009937 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009938 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009939 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009940 return NULL;
9941 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009942 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009943 result = strbuf;
9944 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009945
9946#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009947 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009948 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009949 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009950 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009951 return NULL;
9952 }
9953 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009954 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009955 result = strbuf;
9956 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009957
Bram Moolenaar90944302020-08-01 20:45:11 +02009958 case SPEC_SID:
9959 if (current_sctx.sc_sid <= 0)
9960 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009961 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009962 return NULL;
9963 }
9964 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9965 result = strbuf;
9966 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009967#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009968
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009969#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009970 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009971 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9972 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 result = strbuf;
9974 break;
9975#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009976
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009977 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009978 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 }
9981
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +01009982 resultlen = STRLEN(result); // length of new string
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009983 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 {
9985 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +01009987 resultlen = s - result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 else if (!skip_mod)
9990 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009991 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 &resultlen);
9993 if (result == NULL)
9994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009995 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 return NULL;
9997 }
9998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 }
10000
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010001 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010003 if (empty_is_error)
10004 {
10005 if (valid != VALID_HEAD + VALID_PATH)
10006 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
10007 else
10008 *errormsg = _(e_evaluates_to_an_empty_string);
10009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 result = NULL;
10011 }
10012 else
10013 result = vim_strnsave(result, resultlen);
10014 vim_free(resultbuf);
10015 return result;
10016}
10017
10018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 * Expand the <sfile> string in "arg".
10020 *
10021 * Returns an allocated string, or NULL for any error.
10022 */
10023 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010024expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010026 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +020010028 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +020010030 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +020010032 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +020010033 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 char_u *p;
10035
John Marriotted908f72024-04-18 22:46:56 +020010036 resultlen = STRLEN(arg);
10037 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 if (result == NULL)
10039 return NULL;
10040
10041 for (p = result; *p; )
10042 {
10043 if (STRNCMP(p, "<sfile>", 7) != 0)
10044 ++p;
10045 else
10046 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010047 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +010010048 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 if (errormsg != NULL)
10050 {
10051 if (*errormsg)
10052 emsg(errormsg);
10053 vim_free(result);
10054 return NULL;
10055 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010056 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 {
10058 p += srclen;
10059 continue;
10060 }
John Marriotted908f72024-04-18 22:46:56 +020010061 repllen = STRLEN(repl);
10062 resultlen += repllen - srclen;
10063 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 if (newres == NULL)
10065 {
10066 vim_free(repl);
10067 vim_free(result);
10068 return NULL;
10069 }
John Marriotted908f72024-04-18 22:46:56 +020010070 len = p - result;
10071 mch_memmove(newres, result, len);
10072 STRCPY(newres + len, repl);
10073 len += repllen;
10074 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 vim_free(repl);
10076 vim_free(result);
10077 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010078 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 }
10080 }
10081
10082 return result;
10083}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010086/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010087 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010088 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010091dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 if (fname == NULL)
10094 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010095 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096}
10097#endif
10098
10099/*
10100 * ":behave {mswin,xterm}"
10101 */
10102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010103ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104{
10105 if (STRCMP(eap->arg, "mswin") == 0)
10106 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010107 set_option_value_give_err((char_u *)"selection",
10108 0L, (char_u *)"exclusive", 0);
10109 set_option_value_give_err((char_u *)"selectmode",
10110 0L, (char_u *)"mouse,key", 0);
10111 set_option_value_give_err((char_u *)"mousemodel",
10112 0L, (char_u *)"popup", 0);
10113 set_option_value_give_err((char_u *)"keymodel",
10114 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 }
10116 else if (STRCMP(eap->arg, "xterm") == 0)
10117 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010118 set_option_value_give_err((char_u *)"selection",
10119 0L, (char_u *)"inclusive", 0);
10120 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
10121 set_option_value_give_err((char_u *)"mousemodel",
10122 0L, (char_u *)"extend", 0);
10123 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 }
10125 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010126 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127}
10128
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129static int filetype_detect = FALSE;
10130static int filetype_plugin = FALSE;
10131static int filetype_indent = FALSE;
10132
10133/*
10134 * ":filetype [plugin] [indent] {on,off,detect}"
10135 * on: Load the filetype.vim file to install autocommands for file types.
10136 * off: Load the ftoff.vim file to remove all autocommands for file types.
10137 * plugin on: load filetype.vim and ftplugin.vim
10138 * plugin off: load ftplugof.vim
10139 * indent on: load filetype.vim and indent.vim
10140 * indent off: load indoff.vim
10141 */
10142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010143ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144{
10145 char_u *arg = eap->arg;
10146 int plugin = FALSE;
10147 int indent = FALSE;
10148
10149 if (*eap->arg == NUL)
10150 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010151 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010152 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 filetype_detect ? "ON" : "OFF",
10154 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10155 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10156 return;
10157 }
10158
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010159 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 for (;;)
10161 {
10162 if (STRNCMP(arg, "plugin", 6) == 0)
10163 {
10164 plugin = TRUE;
10165 arg = skipwhite(arg + 6);
10166 continue;
10167 }
10168 if (STRNCMP(arg, "indent", 6) == 0)
10169 {
10170 indent = TRUE;
10171 arg = skipwhite(arg + 6);
10172 continue;
10173 }
10174 break;
10175 }
10176 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10177 {
10178 if (*arg == 'o' || !filetype_detect)
10179 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010180 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 filetype_detect = TRUE;
10182 if (plugin)
10183 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010184 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 filetype_plugin = TRUE;
10186 }
10187 if (indent)
10188 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010189 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 filetype_indent = TRUE;
10191 }
10192 }
10193 if (*arg == 'd')
10194 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010195 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010196 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 }
10198 }
10199 else if (STRCMP(arg, "off") == 0)
10200 {
10201 if (plugin || indent)
10202 {
10203 if (plugin)
10204 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010205 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 filetype_plugin = FALSE;
10207 }
10208 if (indent)
10209 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010210 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 filetype_indent = FALSE;
10212 }
10213 }
10214 else
10215 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010216 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 filetype_detect = FALSE;
10218 }
10219 }
10220 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010221 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222}
10223
10224/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010225 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 */
10227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010228ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229{
zeertzjq5bf6c212024-03-31 18:41:27 +020010230 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010231 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010232
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010233 char_u *arg = eap->arg;
10234 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10235 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010236
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010237 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10238 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +020010239 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010243ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244{
10245#ifdef FEAT_DIGRAPHS
10246 if (*eap->arg != NUL)
10247 putdigraph(eap->arg);
10248 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010249 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250#else
mityu61065042021-07-19 20:07:21 +020010251 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252#endif
10253}
10254
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010255#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10256 void
10257set_no_hlsearch(int flag)
10258{
10259 no_hlsearch = flag;
10260# ifdef FEAT_EVAL
10261 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10262# endif
10263}
10264
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265/*
10266 * ":nohlsearch"
10267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010269ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010271 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +010010272 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274#endif
10275
10276#ifdef FEAT_CRYPT
10277/*
10278 * ":X": Get crypt key
10279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010281ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010283 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010284 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285}
10286#endif
10287
10288#ifdef FEAT_FOLDING
10289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010290ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291{
10292 if (foldManualAllowed(TRUE))
10293 foldCreate(eap->line1, eap->line2);
10294}
10295
10296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010297ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298{
10299 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10300 eap->forceit, FALSE);
10301}
10302
10303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010304ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305{
10306 linenr_T lnum;
10307
Bram Moolenaar4facea32019-10-12 20:17:40 +020010308# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010309 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010310# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010311
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010312 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10314 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10315 ml_setmarked(lnum);
10316
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010317 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010319 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010320# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010321 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323}
10324#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010326#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010327/*
10328 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10329 * instead of a quickfix command.
10330 */
10331 int
10332is_loclist_cmd(int cmdidx)
10333{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010334 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010335 return FALSE;
10336 return cmdnames[cmdidx].cmd_name[0] == 'l';
10337}
10338#endif
10339
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010340 int
10341get_pressedreturn(void)
10342{
10343 return ex_pressedreturn;
10344}
10345
10346 void
10347set_pressedreturn(int val)
10348{
10349 ex_pressedreturn = val;
10350}