blob: a6f5952c1f42153120e4195ce52b92f6c37f1b4b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static 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 +020071static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_cquit(exarg_T *eap);
91static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_close(exarg_T *eap);
93static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
94static void ex_only(exarg_T *eap);
95static void ex_resize(exarg_T *eap);
96static void ex_stag(exarg_T *eap);
97static void ex_tabclose(exarg_T *eap);
98static void ex_tabonly(exarg_T *eap);
99static void ex_tabnext(exarg_T *eap);
100static void ex_tabmove(exarg_T *eap);
101static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200102#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100103static void ex_pclose(exarg_T *eap);
104static void ex_ptag(exarg_T *eap);
105static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#else
107# define ex_pclose ex_ni
108# define ex_ptag ex_ni
109# define ex_pedit ex_ni
110#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100111static void ex_hide(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100112static void ex_exit(exarg_T *eap);
113static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100115static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#else
117# define ex_goto ex_ni
118#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100119static void ex_shell(exarg_T *eap);
120static void ex_preserve(exarg_T *eap);
121static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_mode(exarg_T *eap);
123static void ex_wrongmodifier(exarg_T *eap);
124static void ex_find(exarg_T *eap);
125static void ex_open(exarg_T *eap);
126static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifndef FEAT_GUI
128# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100129static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100131#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#else
134# define ex_tearoff ex_ni
135#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100136#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
137 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100138static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#else
140# define ex_popup ex_ni
141#endif
142#ifndef FEAT_GUI_MSWIN
143# define ex_simalt ex_ni
144#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000145#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146# define gui_mch_find_dialog ex_ni
147# define gui_mch_replace_dialog ex_ni
148#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000149#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150# define ex_helpfind ex_ni
151#endif
152#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100153# define ex_cscope ex_ni
154# define ex_scscope ex_ni
155# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
157#ifndef FEAT_SYN_HL
158# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200159# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000160#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200161#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200162# define ex_syntime ex_ni
163#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000164#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000165# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000166# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000167# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000168# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000169# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000170#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200171#ifndef FEAT_PERSISTENT_UNDO
172# define ex_rundo ex_ni
173# define ex_wundo ex_ni
174#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200175#ifndef FEAT_LUA
176# define ex_lua ex_script_ni
177# define ex_luado ex_ni
178# define ex_luafile ex_ni
179#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000180#ifndef FEAT_MZSCHEME
181# define ex_mzscheme ex_script_ni
182# define ex_mzfile ex_ni
183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#ifndef FEAT_PERL
185# define ex_perl ex_script_ni
186# define ex_perldo ex_ni
187#endif
188#ifndef FEAT_PYTHON
189# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200190# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191# define ex_pyfile ex_ni
192#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200193#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200194# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200195# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196# define ex_py3file ex_ni
197#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100198#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
199# define ex_pyx ex_script_ni
200# define ex_pyxdo ex_ni
201# define ex_pyxfile ex_ni
202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#ifndef FEAT_TCL
204# define ex_tcl ex_script_ni
205# define ex_tcldo ex_ni
206# define ex_tclfile ex_ni
207#endif
208#ifndef FEAT_RUBY
209# define ex_ruby ex_script_ni
210# define ex_rubydo ex_ni
211# define ex_rubyfile ex_ni
212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#ifndef FEAT_KEYMAP
214# define ex_loadkeymap ex_ni
215#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100216static void ex_swapname(exarg_T *eap);
217static void ex_syncbind(exarg_T *eap);
218static void ex_read(exarg_T *eap);
219static void ex_pwd(exarg_T *eap);
220static void ex_equal(exarg_T *eap);
221static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000224#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#else
227# define ex_winpos ex_ni
228#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static void ex_operators(exarg_T *eap);
230static void ex_put(exarg_T *eap);
231static void ex_copymove(exarg_T *eap);
232static void ex_submagic(exarg_T *eap);
233static void ex_join(exarg_T *eap);
234static void ex_at(exarg_T *eap);
235static void ex_bang(exarg_T *eap);
236static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200237#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ex_wundo(exarg_T *eap);
239static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200240#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ex_redo(exarg_T *eap);
242static void ex_later(exarg_T *eap);
243static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100245static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_startinsert(exarg_T *eap);
249static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_checkpath(exarg_T *eap);
252static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#else
254# define ex_findpat ex_ni
255# define ex_checkpath ex_ni
256#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200257#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#else
260# define ex_psearch ex_ni
261#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ex_tag(exarg_T *eap);
263static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200265# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200266# define ex_break ex_ni
267# define ex_breakadd ex_ni
268# define ex_breakdel ex_ni
269# define ex_breaklist ex_ni
270# define ex_call ex_ni
271# define ex_catch ex_ni
272# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200273# define ex_continue ex_ni
274# define ex_debug ex_ni
275# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200277# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# define ex_echo ex_ni
280# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200282# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200283# define ex_endfunction ex_ni
284# define ex_endif ex_ni
285# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
Bram Moolenaar340c59e2021-04-24 20:37:03 +0200289# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200290# define ex_finally ex_ni
291# define ex_finish ex_ni
292# define ex_function ex_ni
293# define ex_if ex_ni
294# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200295# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200296# define ex_lockvar ex_ni
297# define ex_oldfiles ex_ni
298# define ex_options ex_ni
299# define ex_packadd ex_ni
300# define ex_packloadall ex_ni
301# define ex_return ex_ni
302# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_throw ex_ni
304# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200306# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307# define ex_import ex_ni
308# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200310#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# define ex_loadview ex_ni
312#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200313#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# define ex_viminfo ex_ni
315#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_filetype(exarg_T *eap);
318static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000320# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321# define ex_diffpatch ex_ni
322# define ex_diffgetput ex_ni
323# define ex_diffsplit ex_ni
324# define ex_diffthis ex_ni
325# define ex_diffupdate ex_ni
326#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#else
331# define ex_nohlsearch ex_ni
332# define ex_match ex_ni
333#endif
334#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#else
337# define ex_X ex_ni
338#endif
339#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100340static void ex_fold(exarg_T *eap);
341static void ex_foldopen(exarg_T *eap);
342static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_fold ex_ni
345# define ex_foldopen ex_ni
346# define ex_folddo ex_ni
347#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100348#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349# define ex_language ex_ni
350#endif
351#ifndef FEAT_SIGNS
352# define ex_sign ex_ni
353#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaar05159a02005-02-26 23:04:13 +0000360#ifndef FEAT_PROFILE
361# define ex_profile ex_ni
362#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200363#ifndef FEAT_TERMINAL
364# define ex_terminal ex_ni
365#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200366#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
367# define ex_xrestore ex_ni
368#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100369#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200370# define ex_popupclear ex_ni
371#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373/*
374 * Declare cmdnames[].
375 */
376#define DO_DECLARE_EXCMD
377#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200378#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380static char_u dollar_command[2] = {'$', 0};
381
382
383#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100384// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385typedef struct
386{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100387 char_u *line; // command line
388 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389} wcmd_T;
390
391/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000392 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000394 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100398 garray_T *lines_gap; // growarray with line info
399 int current_line; // last read line from growarray
400 int repeating; // TRUE when looping a second time
401 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200402 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 void *cookie;
404};
405
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200406static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100407static int store_loop_line(garray_T *gap, char_u *line);
408static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000409
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100410// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000411struct dbg_stuff
412{
413 int trylevel;
414 int force_abort;
415 except_T *caught_stack;
416 char_u *vv_exception;
417 char_u *vv_throwpoint;
418 int did_emsg;
419 int got_int;
420 int did_throw;
421 int need_rethrow;
422 int check_cstack;
423 except_T *current_exception;
424};
425
Bram Moolenaared203462004-06-16 11:19:22 +0000426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100427save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000428{
429 dsp->trylevel = trylevel; trylevel = 0;
430 dsp->force_abort = force_abort; force_abort = FALSE;
431 dsp->caught_stack = caught_stack; caught_stack = NULL;
432 dsp->vv_exception = v_exception(NULL);
433 dsp->vv_throwpoint = v_throwpoint(NULL);
434
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100435 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000436 dsp->did_emsg = did_emsg; did_emsg = FALSE;
437 dsp->got_int = got_int; got_int = FALSE;
438 dsp->did_throw = did_throw; did_throw = FALSE;
439 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
440 dsp->check_cstack = check_cstack; check_cstack = FALSE;
441 dsp->current_exception = current_exception; current_exception = NULL;
442}
443
444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100445restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000446{
447 suppress_errthrow = FALSE;
448 trylevel = dsp->trylevel;
449 force_abort = dsp->force_abort;
450 caught_stack = dsp->caught_stack;
451 (void)v_exception(dsp->vv_exception);
452 (void)v_throwpoint(dsp->vv_throwpoint);
453 did_emsg = dsp->did_emsg;
454 got_int = dsp->got_int;
455 did_throw = dsp->did_throw;
456 need_rethrow = dsp->need_rethrow;
457 check_cstack = dsp->check_cstack;
458 current_exception = dsp->current_exception;
459}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460#endif
461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462/*
463 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
464 * command is given.
465 */
466 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100467do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100468 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469{
470 int save_msg_scroll;
471 int prev_msg_row;
472 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100473 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000474
475 if (improved)
476 exmode_active = EXMODE_VIM;
477 else
478 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100479 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100480 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000481
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100482 // When using ":global /pat/ visual" and then "Q" we return to continue
483 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000484 if (global_busy)
485 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
487 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100488 ++RedrawingDisabled; // don't redisplay the window
489 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 ++hold_gui_events;
493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
Bram Moolenaar32526b32019-01-19 17:43:09 +0100495 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 while (exmode_active)
497 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100498 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000499 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
500 {
501 exmode_active = FALSE;
502 break;
503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 msg_scroll = TRUE;
505 need_wait_return = FALSE;
506 ex_pressedreturn = FALSE;
507 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100508 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 prev_msg_row = msg_row;
510 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (improved)
512 {
513 cmdline_row = msg_row;
514 do_cmdline(NULL, getexline, NULL, 0);
515 }
516 else
517 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
518 lines_left = Rows - 1;
519
Bram Moolenaardf177f62005-02-22 08:39:57 +0000520 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100521 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000524 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if (ex_pressedreturn)
528 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100529 // go up one line, to overwrite the ":<CR>" line, so the
530 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 msg_row = prev_msg_row;
532 if (prev_msg_row == Rows - 1)
533 msg_row--;
534 }
535 msg_col = 0;
536 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
537 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100540 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000541 {
542 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000543 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000545 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
548
549#ifdef FEAT_GUI
550 --hold_gui_events;
551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 --RedrawingDisabled;
553 --no_wait_return;
554 update_screen(CLEAR);
555 need_wait_return = FALSE;
556 msg_scroll = save_msg_scroll;
557}
558
559/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200560 * Print the executed command for when 'verbose' is set.
561 * When "lnum" is 0 only print the command.
562 */
563 static void
564msg_verbose_cmd(linenr_T lnum, char_u *cmd)
565{
566 ++no_wait_return;
567 verbose_enter_scroll();
568
569 if (lnum == 0)
570 smsg(_("Executing: %s"), cmd);
571 else
572 smsg(_("line %ld: %s"), (long)lnum, cmd);
573 if (msg_silent == 0)
574 msg_puts("\n"); // don't overwrite this
575
576 verbose_leave_scroll();
577 --no_wait_return;
578}
579
580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 * Execute a simple command line. Used for translated commands like "*".
582 */
583 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100584do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 return do_cmdline(cmd, NULL, NULL,
587 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
588}
589
590/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100591 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
592 * This allows for using a range without ":" in Vim9 script.
593 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200594 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100595do_cmd_argument(char_u *cmd)
596{
597 return do_cmdline(cmd, NULL, NULL,
598 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
599}
600
601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 * do_cmdline(): execute one Ex command line
603 *
604 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * 2. Split up in parts separated with '|'.
607 *
608 * This function can be called recursively!
609 *
610 * flags:
611 * DOCMD_VERBOSE - The command will be included in the error message.
612 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100613 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 * DOCMD_KEYTYPED - Don't reset KeyTyped.
615 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
616 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
617 *
618 * return FAIL if cmdline could not be executed, OK otherwise
619 */
620 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100621do_cmdline(
622 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200623 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100625 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100627 char_u *next_cmdline; // next cmd to execute
628 char_u *cmdline_copy = NULL; // copy of cmd line
629 int used_getline = FALSE; // used "fgetline" to obtain command
630 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 int count = 0; // line number count
633 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 int retval = OK;
635#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100636 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100637 garray_T lines_ga; // keep lines for ":while"/":for"
638 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200639 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 char_u *fname = NULL; // function or script name
641 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
642 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
643 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200645 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200646 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100648 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200649 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000651 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000653 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100655# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656# define cmd_cookie cookie
657#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100658 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200659#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100660 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
661 // location for storing error messages to be converted to an exception.
662 // This ensures that the do_errthrow() call in do_one_cmd() does not
663 // combine the messages stored by an earlier invocation of do_one_cmd()
664 // with the command name of the later one. This would happen when
665 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 saved_msg_list = msg_list;
667 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668#endif
669
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100670 // It's possible to create an endless loop with ":execute", catch that
671 // here. The value of 200 allows nested function calls, ":source", etc.
672 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100673 if (call_depth >= 200
674#ifdef FEAT_EVAL
675 && call_depth >= p_mfd
676#endif
677 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000679 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100681 // When converting to an exception, we do not include the command name
682 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100683 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 msg_list = saved_msg_list;
685#endif
686 return FAIL;
687 }
688 ++call_depth;
689
690#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100691 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000693 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100695 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100697 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100698 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ++ex_nesting_level;
701
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100702 // Get the function or script name and the address where the next breakpoint
703 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000704 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 fname = func_name(real_cookie);
707 breakpoint = func_breakpoint(real_cookie);
708 dbg_tick = func_dbg_tick(real_cookie);
709 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100710 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100712 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 breakpoint = source_breakpoint(real_cookie);
714 dbg_tick = source_dbg_tick(real_cookie);
715 }
716
717 /*
718 * Initialize "force_abort" and "suppress_errthrow" at the top level.
719 */
720 if (!recursive)
721 {
722 force_abort = FALSE;
723 suppress_errthrow = FALSE;
724 }
725
726 /*
727 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000728 * exception handling (used when debugging). Otherwise clear it to avoid
729 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000731 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000732 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000733 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200734 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
736 initial_trylevel = trylevel;
737
738 /*
739 * "did_throw" will be set to TRUE when an exception is being thrown.
740 */
741 did_throw = FALSE;
742#endif
743 /*
744 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000745 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * If force_abort is set, we cancel everything.
747 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100748#ifdef FEAT_EVAL
749 did_emsg_cumul += did_emsg;
750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 did_emsg = FALSE;
752
753 /*
754 * KeyTyped is only set when calling vgetc(). Reset it here when not
755 * calling vgetc() (sourced command lines).
756 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100757 if (!(flags & DOCMD_KEYTYPED)
758 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 KeyTyped = FALSE;
760
761 /*
762 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000763 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 * - for multiple commands on one line, separated with '|'
765 * - when repeating until there are no more lines (for ":source")
766 */
767 next_cmdline = cmdline;
768 do
769 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100771 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772#endif
773
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100774 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (next_cmdline == NULL
776#ifdef FEAT_EVAL
777 && !force_abort
778 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100782 {
783#ifdef FEAT_EVAL
784 did_emsg_cumul += did_emsg;
785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000790 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100791 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 * 3. If a line is given: Make a copy, so we can mess with it.
793 */
794
795#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100796 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000797 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100799 // Each '|' separated command is stored separately in lines_ga, to
800 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100801 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100803 // Check if a function has returned or, unless it has an unclosed
804 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000808 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809 func_line_end(real_cookie);
810# endif
811 if (func_has_ended(real_cookie))
812 {
813 retval = FAIL;
814 break;
815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000818 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100819 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820 script_line_end();
821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100823 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {
826 retval = FAIL;
827 break;
828 }
829
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100830 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 if (breakpoint != NULL && dbg_tick != NULL
832 && *dbg_tick != debug_tick)
833 {
834 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100835 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100836 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 *dbg_tick = debug_tick;
838 }
839
840 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100841 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100843 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100845 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100847 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100848 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100850 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100851 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 *dbg_tick = debug_tick;
853 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000855 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856 {
857 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100858 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100859 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860 script_line_start();
861 }
862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#endif
865
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100866 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (next_cmdline == NULL)
868 {
869 /*
870 * Need to set msg_didout for the first line after an ":if",
871 * otherwise the ":if" will be overwritten.
872 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100873 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100875 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#ifdef FEAT_EVAL
877 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
878#else
879 0
880#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100881 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
882 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100884 // Don't call wait_return for aborted command line. The NULL
885 // returned for the end of a sourced file or executed function
886 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (KeyTyped && !(flags & DOCMD_REPEAT))
888 need_wait_return = FALSE;
889 retval = FAIL;
890 break;
891 }
892 used_getline = TRUE;
893
894 /*
895 * Keep the first typed line. Clear it when more lines are typed.
896 */
897 if (flags & DOCMD_KEEPLINE)
898 {
899 vim_free(repeat_cmdline);
900 if (count == 0)
901 repeat_cmdline = vim_strsave(next_cmdline);
902 else
903 repeat_cmdline = NULL;
904 }
905 }
906
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100907 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 else if (cmdline_copy == NULL)
909 {
910 next_cmdline = vim_strsave(next_cmdline);
911 if (next_cmdline == NULL)
912 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200913 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 retval = FAIL;
915 break;
916 }
917 }
918 cmdline_copy = next_cmdline;
919
920#ifdef FEAT_EVAL
921 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200922 * Inside a while/for loop, and when the command looks like a ":while"
923 * or ":for", the line is stored, because we may need it later when
924 * looping.
925 *
926 * When there is a '|' and another command, it is stored separately,
927 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000928 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200929 *
930 * Pass a different "fgetline" function to do_one_cmd() below,
931 * that it stores lines in or reads them from "lines_ga". Makes it
932 * possible to define a function inside a while/for loop and handles
933 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200935 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200937 cmd_getline = get_loop_line;
938 cmd_cookie = (void *)&cmd_loop_cookie;
939 cmd_loop_cookie.lines_gap = &lines_ga;
940 cmd_loop_cookie.current_line = current_line;
941 cmd_loop_cookie.getline = fgetline;
942 cmd_loop_cookie.cookie = cookie;
943 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
944
945 // Save the current line when encountering it the first time.
946 if (current_line == lines_ga.ga_len
947 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
949 retval = FAIL;
950 break;
951 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200952 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200954 else
955 {
956 cmd_getline = fgetline;
957 cmd_cookie = cookie;
958 }
959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 did_endif = FALSE;
961#endif
962
963 if (count++ == 0)
964 {
965 /*
966 * All output from the commands is put below each other, without
967 * waiting for a return. Don't do this when executing commands
968 * from a script or when being called recursive (e.g. for ":e
969 * +command file").
970 */
971 if (!(flags & DOCMD_NOWAIT) && !recursive)
972 {
973 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100974 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100976 msg_scroll = TRUE; // put messages below each other
977 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 ++RedrawingDisabled;
979 did_inc = TRUE;
980 }
981 }
982
Bram Moolenaar823654b2020-05-29 23:03:09 +0200983 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100984 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 /*
987 * 2. Execute one '|' separated command.
988 * do_one_cmd() will return NULL if there is no trailing '|'.
989 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
990 */
991 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100992 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#ifdef FEAT_EVAL
994 &cstack,
995#endif
996 cmd_getline, cmd_cookie);
997 --recursive;
998
999#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001000 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001001 // Use "current_line" from "cmd_loop_cookie", it may have been
1002 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001003 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#endif
1005
1006 if (next_cmdline == NULL)
1007 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001008 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 /*
1011 * If the command was typed, remember it for the ':' register.
1012 * Do this AFTER executing the command to make :@: work.
1013 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 && new_last_cmdline != NULL)
1016 {
1017 vim_free(last_cmdline);
1018 last_cmdline = new_last_cmdline;
1019 new_last_cmdline = NULL;
1020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 }
1022 else
1023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001024 // need to copy the command after the '|' to cmdline_copy, for the
1025 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001026 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 next_cmdline = cmdline_copy;
1028 }
1029
1030
1031#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001032 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001034 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001036 {
1037 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
1043 ++current_line;
1044
1045 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 * An ":endwhile", ":endfor" and ":continue" is handled here.
1047 * If we were executing commands, jump back to the ":while" or
1048 * ":for".
1049 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 // Jump back to the matching ":while" or ":for". Be careful
1056 // not to use a cs_line[] from an entry that isn't a ":while"
1057 // or ":for": It would make "current_line" invalid and can
1058 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (!did_emsg && !got_int && !did_throw
1060 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 && (cstack.cs_flags[cstack.cs_idx]
1062 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 && cstack.cs_line[cstack.cs_idx] >= 0
1064 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1065 {
1066 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001067 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001069 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001071 // Check for the next breakpoint at or after the ":while"
1072 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (breakpoint != NULL)
1074 {
1075 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001076 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 fname,
1078 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1079 *dbg_tick = debug_tick;
1080 }
1081 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001084 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001086 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1087 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089 }
1090
1091 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001097 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 }
1100
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001101 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001102 if (breakpoint != NULL && has_watchexpr())
1103 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001104 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001105 *dbg_tick = debug_tick;
1106 }
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 /*
1109 * When not inside any ":while" loop, clear remembered lines.
1110 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
1113 if (lines_ga.ga_len > 0)
1114 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001115 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1117 free_cmdlines(&lines_ga);
1118 }
1119 current_line = 0;
1120 }
1121
1122 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1124 * being restored at the ":endtry". Reset them here and set the
1125 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1126 * This includes the case where a missing ":endif", ":endwhile" or
1127 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 cstack.cs_lflags &= ~CSL_HAD_FINA;
1132 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1133 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 did_throw ? (void *)current_exception : NULL);
1135 did_emsg = got_int = did_throw = FALSE;
1136 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1137 }
1138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 // Update global "trylevel" for recursive calls to do_cmdline() from
1140 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 trylevel = initial_trylevel + cstack.cs_trylevel;
1142
1143 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001144 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 * files) is aborted because of an error, an interrupt, or an uncaught
1146 * exception, cancel everything. If it is left normally, reset
1147 * force_abort to get the non-EH compatible abortion behavior for
1148 * the rest of the script.
1149 */
1150 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1151 force_abort = FALSE;
1152
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001153 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001155#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
1157 }
1158 /*
1159 * Continue executing command lines when:
1160 * - no CTRL-C typed, no aborting error, no exception thrown or try
1161 * conditionals need to be checked for executing finally clauses or
1162 * catching an interrupt exception
1163 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001164 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 * looping for ":source" command or function call.
1166 */
1167 while (!((got_int
1168#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001169 || (did_emsg && (force_abort || in_vim9script()))
1170 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#endif
1172 )
1173#ifdef FEAT_EVAL
1174 && cstack.cs_trylevel == 0
1175#endif
1176 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001177 && !(did_emsg
1178#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001179 // Keep going when inside try/catch, so that the error can be
1180 // deal with, except when it is a syntax error, it may cause
1181 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001182 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1183#endif
1184 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001185 && (getline_equal(fgetline, cookie, getexmodeline)
1186 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 && (next_cmdline != NULL
1188#ifdef FEAT_EVAL
1189 || cstack.cs_idx >= 0
1190#endif
1191 || (flags & DOCMD_REPEAT)));
1192
1193 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001194 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#ifdef FEAT_EVAL
1196 free_cmdlines(&lines_ga);
1197 ga_clear(&lines_ga);
1198
1199 if (cstack.cs_idx >= 0)
1200 {
1201 /*
1202 * If a sourced file or executed function ran to its end, report the
1203 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001204 * In Vim9 script do not give a second error, executing aborts after
1205 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001207 if (!got_int && !did_throw && !aborting()
1208 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001209 && ((getline_equal(fgetline, cookie, getsourceline)
1210 && !source_finished(fgetline, cookie))
1211 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 && !func_has_ended(real_cookie))))
1213 {
1214 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001215 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001217 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001219 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001221 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223
1224 /*
1225 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1226 * ":endtry" in a sourced file or executed function. If the try
1227 * conditional is in its finally clause, ignore anything pending.
1228 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001229 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 */
1231 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001232 {
1233 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1234
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001235 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001236 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001237 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1238 &cstack.cs_looplevel);
1239 }
1240 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 trylevel = initial_trylevel;
1242 }
1243
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001244 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1245 // lack was reported above and the error message is to be converted to an
1246 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001247 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 ? (char_u *)"endfunction" : (char_u *)NULL);
1249
1250 if (trylevel == 0)
1251 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001252 // Just in case did_throw got set but current_exception wasn't.
1253 if (current_exception == NULL)
1254 did_throw = FALSE;
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 /*
1257 * When an exception is being thrown out of the outermost try
1258 * conditional, discard the uncaught exception, disable the conversion
1259 * of interrupts or errors to exceptions, and ensure that no more
1260 * commands are executed.
1261 */
1262 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001263 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265 /*
1266 * On an interrupt or an aborting error not converted to an exception,
1267 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001268 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 * when force_abort is set and did_emsg unset in case of an interrupt
1270 * from a finally clause after an error.
1271 */
1272 else if (got_int || (did_emsg && force_abort))
1273 suppress_errthrow = TRUE;
1274 }
1275
1276 /*
1277 * The current cstack will be freed when do_cmdline() returns. An uncaught
1278 * exception will have to be rethrown in the previous cstack. If a function
1279 * has just returned or a script file was just finished and the previous
1280 * cstack belongs to the same function or, respectively, script file, it
1281 * will have to be checked for finally clauses to be executed due to the
1282 * ":return" or ":finish". This is done in do_one_cmd().
1283 */
1284 if (did_throw)
1285 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001286 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001288 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 && ex_nesting_level > func_level(real_cookie) + 1))
1290 {
1291 if (!did_throw)
1292 check_cstack = TRUE;
1293 }
1294 else
1295 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001296 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001297 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 --ex_nesting_level;
1299 /*
1300 * Go to debug mode when returning from a function in which we are
1301 * single-stepping.
1302 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001303 if ((getline_equal(fgetline, cookie, getsourceline)
1304 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001306 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 ? (char_u *)_("End of sourced file")
1308 : (char_u *)_("End of function"));
1309 }
1310
1311 /*
1312 * Restore the exception environment (done after returning from the
1313 * debugger).
1314 */
1315 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001316 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317
1318 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001319
1320 // Cleanup if "cs_emsg_silent_list" remains.
1321 if (cstack.cs_emsg_silent_list != NULL)
1322 {
1323 eslist_T *elem, *temp;
1324
1325 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1326 {
1327 temp = elem->next;
1328 vim_free(elem);
1329 }
1330 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001331#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
1333 /*
1334 * If there was too much output to fit on the command line, ask the user to
1335 * hit return before redrawing the screen. With the ":global" command we do
1336 * this only once after the command is finished.
1337 */
1338 if (did_inc)
1339 {
1340 --RedrawingDisabled;
1341 --no_wait_return;
1342 msg_scroll = FALSE;
1343
1344 /*
1345 * When just finished an ":if"-":else" which was typed, no need to
1346 * wait for hit-return. Also for an error situation.
1347 */
1348 if (retval == FAIL
1349#ifdef FEAT_EVAL
1350 || (did_endif && KeyTyped && !did_emsg)
1351#endif
1352 )
1353 {
1354 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001355 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
1357 else if (need_wait_return)
1358 {
1359 /*
1360 * The msg_start() above clears msg_didout. The wait_return we do
1361 * here should not overwrite the command that may be shown before
1362 * doing that.
1363 */
1364 msg_didout |= msg_didout_before_start;
1365 wait_return(FALSE);
1366 }
1367 }
1368
Bram Moolenaar2a942252012-11-28 23:03:07 +01001369#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001370 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001371#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 /*
1373 * Reset if_level, in case a sourced script file contains more ":if" than
1374 * ":endif" (could be ":if x | foo | endif").
1375 */
1376 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001377#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 --call_depth;
1380 return retval;
1381}
1382
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001383#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001384/*
1385 * Handle when "did_throw" is set after executing commands.
1386 */
1387 void
1388handle_did_throw()
1389{
1390 char *p = NULL;
1391 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001392 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001393
1394 /*
1395 * If the uncaught exception is a user exception, report it as an
1396 * error. If it is an error exception, display the saved error
1397 * message now. For an interrupt exception, do nothing; the
1398 * interrupt message is given elsewhere.
1399 */
1400 switch (current_exception->type)
1401 {
1402 case ET_USER:
1403 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001404 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001405 current_exception->value);
1406 p = (char *)vim_strsave(IObuff);
1407 break;
1408 case ET_ERROR:
1409 messages = current_exception->messages;
1410 current_exception->messages = NULL;
1411 break;
1412 case ET_INTERRUPT:
1413 break;
1414 }
1415
1416 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1417 current_exception->throw_lnum);
1418 ESTACK_CHECK_SETUP
1419 current_exception->throw_name = NULL;
1420
1421 discard_current_exception(); // uses IObuff if 'verbose'
1422 suppress_errthrow = TRUE;
1423 force_abort = TRUE;
1424
1425 if (messages != NULL)
1426 {
1427 do
1428 {
1429 msglist_T *next = messages->next;
1430 int save_compiling = estack_compiling;
1431
1432 estack_compiling = messages->msg_compiling;
1433 emsg(messages->msg);
1434 vim_free(messages->msg);
1435 vim_free(messages->sfile);
1436 vim_free(messages);
1437 messages = next;
1438 estack_compiling = save_compiling;
1439 }
1440 while (messages != NULL);
1441 }
1442 else if (p != NULL)
1443 {
1444 emsg(p);
1445 vim_free(p);
1446 }
1447 vim_free(SOURCING_NAME);
1448 ESTACK_CHECK_NOW
1449 estack_pop();
1450}
1451
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001453 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 */
1455 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001456get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001458 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 wcmd_T *wp;
1460 char_u *line;
1461
1462 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1463 {
1464 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001465 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001467 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (cp->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001469 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001471 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001472 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 ++cp->current_line;
1474
1475 return line;
1476 }
1477
1478 KeyTyped = FALSE;
1479 ++cp->current_line;
1480 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001481 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 return vim_strsave(wp->line);
1483}
1484
1485/*
1486 * Store a line in "gap" so that a ":while" loop can execute it again.
1487 */
1488 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001489store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 if (ga_grow(gap, 1) == FAIL)
1492 return FAIL;
1493 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001494 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 return OK;
1497}
1498
1499/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001500 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
1502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001503free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 while (gap->ga_len > 0)
1506 {
1507 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1508 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 }
1510}
1511#endif
1512
1513/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001514 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1515 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001518getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001519 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001520 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001521 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001524 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001527 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1528 // function that's originally used to obtain the lines. This may be
1529 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001530 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001531 cp = (struct loop_cookie *)cookie;
1532 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
1534 gp = cp->getline;
1535 cp = cp->cookie;
1536 }
1537 return gp == func;
1538#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001539 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540#endif
1541}
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001544 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 * getline function. Otherwise return "cookie".
1546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001548getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001549 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001550 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
Bram Moolenaar13505972019-01-24 15:04:48 +01001552#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001553 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001554 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001556 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1557 // cookie that's originally used to obtain the lines. This may be nested
1558 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001559 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560 cp = (struct loop_cookie *)cookie;
1561 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 {
1563 gp = cp->getline;
1564 cp = cp->cookie;
1565 }
1566 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001567#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaard5053d02020-06-28 15:51:16 +02001572#if defined(FEAT_EVAL) || defined(PROT)
1573/*
1574 * Get the next line source line without advancing.
1575 */
1576 char_u *
1577getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001578 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001579 void *cookie) // argument for fgetline()
1580{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001581 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001582 struct loop_cookie *cp;
1583 wcmd_T *wp;
1584
1585 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1586 // cookie that's originally used to obtain the lines. This may be nested
1587 // several levels.
1588 gp = fgetline;
1589 cp = (struct loop_cookie *)cookie;
1590 while (gp == get_loop_line)
1591 {
1592 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1593 {
1594 // executing lines a second time, use the stored copy
1595 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1596 return wp->line;
1597 }
1598 gp = cp->getline;
1599 cp = cp->cookie;
1600 }
1601 if (gp == getsourceline)
1602 return source_nextline(cp);
1603 return NULL;
1604}
1605#endif
1606
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607
1608/*
1609 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1610 * ":bwipeout", etc.
1611 * Returns the buffer number.
1612 */
1613 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001614compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615{
1616 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001617 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001618 int count = offset;
1619
1620 buf = firstbuf;
1621 while (buf->b_next != NULL && buf->b_fnum < lnum)
1622 buf = buf->b_next;
1623 while (count != 0)
1624 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001625 count += (offset < 0) ? 1 : -1;
1626 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1627 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001629 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001630 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001631 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001632 while (buf->b_ml.ml_mfp == NULL)
1633 {
1634 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1635 if (nextbuf == NULL)
1636 break;
1637 buf = nextbuf;
1638 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001639 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001640 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001641 if (addr_type == ADDR_LOADED_BUFFERS)
1642 while (buf->b_ml.ml_mfp == NULL)
1643 {
1644 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1645 if (nextbuf == NULL)
1646 break;
1647 buf = nextbuf;
1648 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001649 return buf->b_fnum;
1650}
1651
Bram Moolenaarb7316892019-05-01 18:08:42 +02001652/*
1653 * Return the window number of "win".
1654 * When "win" is NULL return the number of windows.
1655 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001656 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001657current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001658{
1659 win_T *wp;
1660 int nr = 0;
1661
Bram Moolenaar29323592016-07-24 22:04:11 +02001662 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001663 {
1664 ++nr;
1665 if (wp == win)
1666 break;
1667 }
1668 return nr;
1669}
1670
1671 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001672current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001673{
1674 tabpage_T *tp;
1675 int nr = 0;
1676
Bram Moolenaar29323592016-07-24 22:04:11 +02001677 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001678 {
1679 ++nr;
1680 if (tp == tab)
1681 break;
1682 }
1683 return nr;
1684}
1685
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001686 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001687comment_start(char_u *p, int starts_with_colon UNUSED)
1688{
1689#ifdef FEAT_EVAL
1690 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001691 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001692#endif
1693 return *p == '"';
1694}
1695
Bram Moolenaarf240e182014-11-27 18:33:02 +01001696# define CURRENT_WIN_NR current_win_nr(curwin)
1697# define LAST_WIN_NR current_win_nr(NULL)
1698# define CURRENT_TAB_NR current_tab_nr(curtab)
1699# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701/*
1702 * Execute one Ex command.
1703 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001704 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1705 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 *
1707 * 1. skip comment lines and leading space
1708 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001709 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001710 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001711 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001712 * 6. parse arguments
1713 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001715 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 *
1717 * This function may be called recursively!
1718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001720do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001721 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001722 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001724 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001726 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001727 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001729 char_u *p;
1730 linenr_T lnum;
1731 long n;
1732 char *errormsg = NULL; // error message
1733 char_u *after_modifier = NULL;
1734 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001735 cmdmod_T save_cmdmod;
1736 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001737 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001738 int ni; // set when Not Implemented
1739 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001740 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001741#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001742 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001743 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001744 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001745#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001746 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaara80faa82020-04-12 19:37:17 +02001748 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 ea.line1 = 1;
1750 ea.line2 = 1;
1751#ifdef FEAT_EVAL
1752 ++ex_nesting_level;
1753#endif
1754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001755 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 if (quitmore
1757#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001758 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001759 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001760#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001761 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001762 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 --quitmore;
1764
1765 /*
1766 * Reset browse, confirm, etc.. They are restored when returning, for
1767 * recursive calls.
1768 */
1769 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001771 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001772 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1773 goto doend;
1774
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001775/*
1776 * 1. Skip comment lines and leading white space and colons.
1777 * 2. Handle command modifiers.
1778 */
1779 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001781 ea.cmdlinep = cmdlinep;
1782 ea.getline = fgetline;
1783 ea.cookie = cookie;
1784#ifdef FEAT_EVAL
1785 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001786 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001788 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001789 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001790 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001791#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001792 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001793#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001794 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796#ifdef FEAT_EVAL
1797 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1798 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1799#else
1800 ea.skip = (if_level > 0);
1801#endif
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001804 * 3. Skip over the range to find the command. Let "p" point to after it.
1805 *
1806 * We need the command to know what kind of range it uses.
1807 */
1808 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001809#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001810 // In Vim9 script a colon is required before the range. This may also be
1811 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001812 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001813 {
1814 may_have_range = FALSE;
1815 for (p = ea.cmd; p >= *cmdlinep; --p)
1816 {
1817 if (*p == ':')
1818 may_have_range = TRUE;
1819 if (p < ea.cmd && !VIM_ISWHITE(*p))
1820 break;
1821 }
1822 }
1823 else
1824 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001825 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001826#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001827 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828
1829#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001830 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001831 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001832 if (ea.cmd == cmd + 1 && *cmd == '$')
1833 // should be "$VAR = val"
1834 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001835 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001836 if (ea.cmdidx == CMD_SIZE)
1837 {
1838 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1839
1840 // If a ':' before the range is missing, give a clearer error
1841 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001842 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001843 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001844 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001845 goto doend;
1846 }
1847 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 else
1850#endif
1851 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001852
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001853#ifdef FEAT_EVAL
1854# ifdef FEAT_PROFILE
1855 // Count this line for profiling if skip is TRUE.
1856 if (do_profiling == PROF_YES
1857 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1858 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1859 {
1860 int skip = did_emsg || got_int || did_throw;
1861
1862 if (ea.cmdidx == CMD_catch)
1863 skip = !skip && !(cstack->cs_idx >= 0
1864 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1865 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1866 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1867 skip = skip || !(cstack->cs_idx >= 0
1868 && !(cstack->cs_flags[cstack->cs_idx]
1869 & (CSF_ACTIVE | CSF_TRUE)));
1870 else if (ea.cmdidx == CMD_finally)
1871 skip = FALSE;
1872 else if (ea.cmdidx != CMD_endif
1873 && ea.cmdidx != CMD_endfor
1874 && ea.cmdidx != CMD_endtry
1875 && ea.cmdidx != CMD_endwhile)
1876 skip = ea.skip;
1877
1878 if (!skip)
1879 {
1880 if (getline_equal(fgetline, cookie, get_func_line))
1881 func_line_exec(getline_cookie(fgetline, cookie));
1882 else if (getline_equal(fgetline, cookie, getsourceline))
1883 script_line_exec();
1884 }
1885 }
1886# endif
1887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001888 // May go to debug mode. If this happens and the ">quit" debug command is
1889 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001890 dbg_check_breakpoint(&ea);
1891 if (!ea.skip && got_int)
1892 {
1893 ea.skip = TRUE;
1894 (void)do_intthrow(cstack);
1895 }
1896#endif
1897
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001898/*
1899 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 *
1901 * where 'addr' is:
1902 *
1903 * % (entire file)
1904 * $ [+-NUM]
1905 * 'x [+-NUM] (where x denotes a currently defined mark)
1906 * . [+-NUM]
1907 * [+-NUM]..
1908 * NUM
1909 *
1910 * The ea.cmd pointer is updated to point to the first character following the
1911 * range spec. If an initial address is found, but no second, the upper bound
1912 * is equal to the lower.
1913 */
1914
Bram Moolenaar25190db2019-05-04 15:05:28 +02001915 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001916 if (!IS_USER_CMDIDX(ea.cmdidx))
1917 {
1918 if (ea.cmdidx != CMD_SIZE)
1919 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1920 else
1921 ea.addr_type = ADDR_LINES;
1922
Bram Moolenaar25190db2019-05-04 15:05:28 +02001923 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001924 if (ea.cmdidx == CMD_wincmd && p != NULL)
1925 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001926#ifdef FEAT_QUICKFIX
1927 // :.cc in quickfix window uses line number
1928 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1929 ea.addr_type = ADDR_OTHER;
1930#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001931 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001932
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001933 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001934#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001935 if (!may_have_range)
1936 ea.line1 = ea.line2 = default_address(&ea);
1937 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001938#endif
1939 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1940 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001943 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 */
1945
1946 /*
1947 * Skip ':' and any white space
1948 */
1949 ea.cmd = skipwhite(ea.cmd);
1950 while (*ea.cmd == ':')
1951 ea.cmd = skipwhite(ea.cmd + 1);
1952
1953 /*
1954 * If we got a line, but no command, then go to the line.
1955 * If we find a '|' or '\n' we set ea.nextcmd.
1956 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001957 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1958 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
1960 /*
1961 * strange vi behaviour:
1962 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001963 * ":3|..." prints line 3 (not in Vim9 script)
1964 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001966 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001968 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 goto doend;
1970 }
1971
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001972 // If this looks like an undefined user command and there are CmdUndefined
1973 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001974 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1975 && ASCII_ISUPPER(*ea.cmd)
1976 && has_cmdundefined())
1977 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001978 int ret;
1979
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001980 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001981 while (ASCII_ISALNUM(*p))
1982 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001983 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001984 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1985 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001986 // If the autocommands did something and didn't cause an error, try
1987 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001988 p = (ret
1989#ifdef FEAT_EVAL
1990 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001991#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001993 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (p == NULL)
1996 {
1997 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001998 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 goto doend;
2000 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002001 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002002 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2003 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 {
2005 errormsg = uc_fun_cmd();
2006 goto doend;
2007 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (ea.cmdidx == CMD_SIZE)
2010 {
2011 if (!ea.skip)
2012 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002013 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002015 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002016 // If the modifier was parsed OK the error must be in the
2017 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002018 if (after_modifier != NULL)
2019 append_command(after_modifier);
2020 else
2021 append_command(*cmdlinep);
2022 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002023 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002024 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
2026 goto doend;
2027 }
2028
Bram Moolenaar958636c2014-10-21 20:01:58 +02002029 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2030 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002031#ifdef HAVE_EX_SCRIPT_NI
2032 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2033#endif
2034 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036#ifndef FEAT_EVAL
2037 /*
2038 * When the expression evaluation is disabled, recognize the ":if" and
2039 * ":endif" commands and ignore everything in between it.
2040 */
2041 if (ea.cmdidx == CMD_if)
2042 ++if_level;
2043 if (if_level)
2044 {
2045 if (ea.cmdidx == CMD_endif)
2046 --if_level;
2047 goto doend;
2048 }
2049
2050#endif
2051
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002052 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002053 if (*p == '!' && ea.cmdidx != CMD_substitute
2054 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056 ++p;
2057 ea.forceit = TRUE;
2058 }
2059 else
2060 ea.forceit = FALSE;
2061
2062/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002063 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002065 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002066 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 if (!ea.skip)
2069 {
2070#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002071 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002073 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002074 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 goto doend;
2076 }
2077#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002078 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002079 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002080 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002081 goto doend;
2082 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002083 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002085 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002086 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 goto doend;
2088 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002089
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002090 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002092#ifdef FEAT_CMDWIN
2093 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2094 {
2095 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002096 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002097 goto doend;
2098 }
2099#endif
2100 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2101 {
2102 // Command not allowed when text is locked
2103 errormsg = _(get_text_locked_msg());
2104 goto doend;
2105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002107
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 // Disallow editing another buffer when "curbuf_lock" is set.
2109 // Do allow ":checktime" (it is postponed).
2110 // Do allow ":edit" (check for an argument later).
2111 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002112 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002113 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002114 && ea.cmdidx != CMD_edit
2115 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002116 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002117 && curbuf_locked())
2118 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002120 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002122 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 goto doend;
2124 }
2125 }
2126
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002127 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002129 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 goto doend;
2131 }
2132
2133 /*
2134 * Don't complain about the range if it is not used
2135 * (could happen if line_count is accidentally set to 0).
2136 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002137 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
2139 /*
2140 * If the range is backwards, ask for confirmation and, if given, swap
2141 * ea.line1 & ea.line2 so it's forwards again.
2142 * When global command is busy, don't ask, will fail below.
2143 */
2144 if (!global_busy && ea.line1 > ea.line2)
2145 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002146 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002148 if (sourcing || exmode_active)
2149 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002150 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002151 goto doend;
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (ask_yesno((char_u *)
2154 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002155 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 }
2157 lnum = ea.line1;
2158 ea.line1 = ea.line2;
2159 ea.line2 = lnum;
2160 }
2161 if ((errormsg = invalid_range(&ea)) != NULL)
2162 goto doend;
2163 }
2164
Bram Moolenaarb7316892019-05-01 18:08:42 +02002165 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2166 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 ea.line2 = 1;
2168
2169 correct_range(&ea);
2170
2171#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002172 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002173 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002175 // Put the first line at the start of a closed fold, put the last line
2176 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 (void)hasFolding(ea.line1, &ea.line1, NULL);
2178 (void)hasFolding(ea.line2, NULL, &ea.line2);
2179 }
2180#endif
2181
2182#ifdef FEAT_QUICKFIX
2183 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002184 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 * option here, so things like % get expanded.
2186 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002187 p = replace_makeprg(&ea, p, cmdlinep);
2188 if (p == NULL)
2189 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190#endif
2191
2192 /*
2193 * Skip to start of argument.
2194 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2195 */
2196 if (ea.cmdidx == CMD_bang)
2197 ea.arg = p;
2198 else
2199 ea.arg = skipwhite(p);
2200
Bram Moolenaar379fb762018-08-30 15:58:28 +02002201 // ":file" cannot be run with an argument when "curbuf_lock" is set
2202 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2203 goto doend;
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 /*
2206 * Check for "++opt=val" argument.
2207 * Must be first, allow ":w ++enc=utf8 !cmd"
2208 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002209 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2211 if (getargopt(&ea) == FAIL && !ni)
2212 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002213 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 goto doend;
2215 }
2216
2217 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002219 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002221 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002223 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 goto doend;
2225 }
2226 ea.arg = skipwhite(ea.arg + 1);
2227 ea.append = TRUE;
2228 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002229 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
2231 ++ea.arg;
2232 ea.usefilter = TRUE;
2233 }
2234 }
2235
2236 if (ea.cmdidx == CMD_read)
2237 {
2238 if (ea.forceit)
2239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002240 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 ea.forceit = FALSE;
2242 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002243 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 {
2245 ++ea.arg;
2246 ea.usefilter = TRUE;
2247 }
2248 }
2249
2250 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2251 {
2252 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002253 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
2255 ++ea.arg;
2256 ++ea.amount;
2257 }
2258 ea.arg = skipwhite(ea.arg);
2259 }
2260
2261 /*
2262 * Check for "+command" argument, before checking for next command.
2263 * Don't do this for ":read !cmd" and ":write !cmd".
2264 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002265 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2267
2268 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002269 * For commands that do not use '|' inside their argument: Check for '|' to
2270 * separate commands and '"' or '#' to start comments.
2271 *
2272 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002273 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002274 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002275 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002277 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2278 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002279 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002280 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002281 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002282 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002283 || ea.cmdidx == CMD_global
2284 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002285 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002286#ifdef FEAT_EVAL
2287 || inside_block(&ea)
2288#endif
2289 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 for (p = ea.arg; *p; ++p)
2292 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002293 // Remove one backslash before a newline, so that it's possible to
2294 // pass a newline to the shell and also a newline that is preceded
2295 // with a backslash. This makes it impossible to end a shell
2296 // command in a backslash, but that doesn't appear useful.
2297 // Halving the number of backslashes is incompatible with previous
2298 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002300 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002301 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 ea.nextcmd = p + 1;
2304 *p = NUL;
2305 break;
2306 }
2307 }
2308 }
2309
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002310 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002311 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002313 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002314 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002316 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002317 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002318 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002320#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002321 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002322 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002324 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002325 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002328 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2329 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002330 {
2331 ea.regname = *ea.arg++;
2332#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002333 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002334 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2335 {
kuuote08d7b1c2021-10-04 22:17:36 +01002336 if (!ea.skip)
2337 {
2338 set_expr_line(vim_strsave(ea.arg), &ea);
2339 did_set_expr_line = TRUE;
2340 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002341 ea.arg += STRLEN(ea.arg);
2342 }
2343#endif
2344 ea.arg = skipwhite(ea.arg);
2345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
2347
2348 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002349 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 * count, it's a buffer name.
2351 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002352 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002353 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002354 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002356 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002358 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002360 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 goto doend;
2362 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002363 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
2365 ea.line2 = n;
2366 if (ea.addr_count == 0)
2367 ea.addr_count = 1;
2368 }
2369 else
2370 {
2371 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002372 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002373 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002374 else
2375 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 ++ea.addr_count;
2377 /*
2378 * Be vi compatible: no error message for out of range.
2379 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002380 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 ea.line2 = curbuf->b_ml.ml_line_count;
2382 }
2383 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002384
2385 /*
2386 * Check for flags: 'l', 'p' and '#'.
2387 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002388 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002389 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002390 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2391 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002393 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002394 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 goto doend;
2396 }
2397
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002398 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002400 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 goto doend;
2402 }
2403
2404#ifdef FEAT_EVAL
2405 /*
2406 * Skip the command when it's not going to be executed.
2407 * The commands like :if, :endif, etc. always need to be executed.
2408 * Also make an exception for commands that handle a trailing command
2409 * themselves.
2410 */
2411 if (ea.skip)
2412 {
2413 switch (ea.cmdidx)
2414 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002415 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 case CMD_while:
2417 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002418 case CMD_for:
2419 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 case CMD_if:
2421 case CMD_elseif:
2422 case CMD_else:
2423 case CMD_endif:
2424 case CMD_try:
2425 case CMD_catch:
2426 case CMD_finally:
2427 case CMD_endtry:
2428 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002429 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 break;
2431
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002432 // Commands that handle '|' themselves. Check: A command should
2433 // either have the EX_TRLBAR flag, appear in this list or appear in
2434 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 case CMD_aboveleft:
2436 case CMD_and:
2437 case CMD_belowright:
2438 case CMD_botright:
2439 case CMD_browse:
2440 case CMD_call:
2441 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002442 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_delfunction:
2444 case CMD_djump:
2445 case CMD_dlist:
2446 case CMD_dsearch:
2447 case CMD_dsplit:
2448 case CMD_echo:
2449 case CMD_echoerr:
2450 case CMD_echomsg:
2451 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002452 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002454 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002455 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 case CMD_help:
2457 case CMD_hide:
2458 case CMD_ijump:
2459 case CMD_ilist:
2460 case CMD_isearch:
2461 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002462 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 case CMD_keepjumps:
2464 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002465 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case CMD_leftabove:
2467 case CMD_let:
2468 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002469 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002470 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002472 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002473 case CMD_noautocmd:
2474 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 case CMD_perl:
2476 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002477 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002478 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002479 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 case CMD_return:
2481 case CMD_rightbelow:
2482 case CMD_ruby:
2483 case CMD_silent:
2484 case CMD_smagic:
2485 case CMD_snomagic:
2486 case CMD_substitute:
2487 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002488 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 case CMD_tcl:
2490 case CMD_throw:
2491 case CMD_tilde:
2492 case CMD_topleft:
2493 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002494 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002495 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 case CMD_verbose:
2497 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002498 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 break;
2500
2501 default: goto doend;
2502 }
2503 }
2504#endif
2505
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002506 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2509 goto doend;
2510 }
2511
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 /*
2513 * Accept buffer name. Cannot be used at the same time with a buffer
2514 * number. Don't do this for a user command.
2515 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002516 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002517 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
2519 /*
2520 * :bdelete, :bwipeout and :bunload take several arguments, separated
2521 * by spaces: find next space (skipping over escaped characters).
2522 * The others take one argument: ignore trailing spaces.
2523 */
2524 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2525 || ea.cmdidx == CMD_bunload)
2526 p = skiptowhite_esc(ea.arg);
2527 else
2528 {
2529 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002530 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 --p;
2532 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002533 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002534 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002535 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 goto doend;
2537 ea.addr_count = 1;
2538 ea.arg = skipwhite(p);
2539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002541 // The :try command saves the emsg_silent flag, reset it here when
2542 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002543 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002544 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002545 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002546 if (emsg_silent < 0)
2547 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002548 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002549 }
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002552 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
Bram Moolenaar958636c2014-10-21 20:01:58 +02002555 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
2557 /*
2558 * Execute a user-defined command.
2559 */
2560 do_ucmd(&ea);
2561 }
2562 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
2564 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002565 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 */
2567 ea.errmsg = NULL;
2568 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2569 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002570 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
2572
2573#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002575 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002576 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002577 && current_sctx.sc_sid > 0
2578 && ea.cmdidx != CMD_endif
2579 && (cstack->cs_idx < 0
2580 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002581 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 /*
2584 * If the command just executed called do_cmdline(), any throw or ":return"
2585 * or ":finish" encountered there must also check the cstack of the still
2586 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2587 * exception, or reanimate a returned function or finished script file and
2588 * return or finish it again.
2589 */
2590 if (need_rethrow)
2591 do_throw(cstack);
2592 else if (check_cstack)
2593 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002594 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002596 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 && current_func_returned())
2598 do_return(&ea, TRUE, FALSE, NULL);
2599 }
2600 need_rethrow = check_cstack = FALSE;
2601#endif
2602
2603doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002604 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002607 curwin->w_cursor.col = 0;
2608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
2610 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2611 {
Bram Moolenaarbed34f02022-01-19 20:48:37 +00002612 if (sourcing || !KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002614 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002617 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002619 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 }
2621 emsg(errormsg);
2622 }
2623#ifdef FEAT_EVAL
2624 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002625 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2626 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002627
2628 if (did_set_expr_line)
2629 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630#endif
2631
Bram Moolenaare1004402020-10-24 20:49:43 +02002632 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002634 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002635 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002637 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 ea.nextcmd = NULL;
2639
2640#ifdef FEAT_EVAL
2641 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002642 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643#endif
2644
2645 return ea.nextcmd;
2646}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002648static char ex_error_buf[MSG_BUF_LEN];
2649
2650/*
2651 * Return an error message with argument included.
2652 * Uses a static buffer, only the last error will be kept.
2653 * "msg" will be translated, caller should use N_().
2654 */
2655 char *
2656ex_errmsg(char *msg, char_u *arg)
2657{
2658 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2659 return ex_error_buf;
2660}
2661
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002663 * Handle a range without a command.
2664 * Returns an error message on failure.
2665 */
2666 char *
2667ex_range_without_command(exarg_T *eap)
2668{
2669 char *errormsg = NULL;
2670
2671 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2672#ifdef FEAT_EVAL
2673 && !in_vim9script()
2674#endif
2675 )
2676 {
2677 eap->cmdidx = CMD_print;
2678 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2679 if ((errormsg = invalid_range(eap)) == NULL)
2680 {
2681 correct_range(eap);
2682 ex_print(eap);
2683 }
2684 }
2685 else if (eap->addr_count != 0)
2686 {
2687 if (eap->line2 > curbuf->b_ml.ml_line_count)
2688 {
2689 // With '-' in 'cpoptions' a line number past the file is an
2690 // error, otherwise put it at the end of the file.
2691 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2692 eap->line2 = -1;
2693 else
2694 eap->line2 = curbuf->b_ml.ml_line_count;
2695 }
2696
2697 if (eap->line2 < 0)
2698 errormsg = _(e_invalid_range);
2699 else
2700 {
2701 if (eap->line2 == 0)
2702 curwin->w_cursor.lnum = 1;
2703 else
2704 curwin->w_cursor.lnum = eap->line2;
2705 beginline(BL_SOL | BL_FIX);
2706 }
2707 }
2708 return errormsg;
2709}
2710
2711/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002712 * Check for an Ex command with optional tail.
2713 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002714 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002715 */
2716 static int
2717checkforcmd_opt(
2718 char_u **pp, // start of command
2719 char *cmd, // name of command
2720 int len, // required length
2721 int noparen)
2722{
2723 int i;
2724
2725 for (i = 0; cmd[i] != NUL; ++i)
2726 if (((char_u *)cmd)[i] != (*pp)[i])
2727 break;
Bram Moolenaar68854a82022-01-31 18:59:13 +00002728 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_'
2729 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002730 {
2731 *pp = skipwhite(*pp + i);
2732 return TRUE;
2733 }
2734 return FALSE;
2735}
2736
2737/*
2738 * Check for an Ex command with optional tail.
2739 * If there is a match advance "pp" to the argument and return TRUE.
2740 */
2741 int
2742checkforcmd(
2743 char_u **pp, // start of command
2744 char *cmd, // name of command
2745 int len) // required length
2746{
2747 return checkforcmd_opt(pp, cmd, len, FALSE);
2748}
2749
2750/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002751 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002752 * If there is a match advance "pp" to the argument and return TRUE.
2753 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002754 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002755checkforcmd_noparen(
2756 char_u **pp, // start of command
2757 char *cmd, // name of command
2758 int len) // required length
2759{
2760 return checkforcmd_opt(pp, cmd, len, TRUE);
2761}
2762
2763/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002764 * Parse and skip over command modifiers:
2765 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002766 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002767 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002768 * When "skip_only" is TRUE the global variables are not changed, except for
2769 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002770 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2771 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002772 * Call apply_cmdmod() to get the side effects of the modifiers:
2773 * - Increment "sandbox" for ":sandbox"
2774 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002775 * - set msg_silent for ":silent"
2776 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 * Return FAIL when the command is not to be executed.
2778 * May set "errormsg" to an error message.
2779 */
2780 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002781parse_command_modifiers(
2782 exarg_T *eap,
2783 char **errormsg,
2784 cmdmod_T *cmod,
2785 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002786{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002787 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002788 char_u *cmd_start = NULL;
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002789 int did_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002790 char_u *p;
2791 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002792 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002793 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002794
Bram Moolenaare1004402020-10-24 20:49:43 +02002795 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002796 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002797
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002798 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2799 {
2800 // The automatically inserted Visual area range is skipped, so that
2801 // typing ":cmdmod cmd" in Visual mode works without having to move the
2802 // range to after the modififiers.
2803 eap->cmd += 5;
2804 cmd_start = eap->cmd;
2805 has_visual_range = TRUE;
2806 }
2807
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002808 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002809 for (;;)
2810 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002812 {
2813 if (*eap->cmd == ':')
2814 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002815 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002816 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002817
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002818 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 if (*eap->cmd == NUL && exmode_active
2820 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2821 || getline_equal(eap->getline, eap->cookie, getexline))
2822 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2823 {
2824 eap->cmd = (char_u *)"+";
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002825 did_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002826 if (!skip_only)
2827 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002828 }
2829
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002830 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002831 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002832 {
2833 // a comment ends at a NL
2834 if (eap->nextcmd == NULL)
2835 {
2836 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2837 if (eap->nextcmd != NULL)
2838 ++eap->nextcmd;
2839 }
Bram Moolenaarbc510062022-02-14 21:19:04 +00002840 if (vim9script && has_cmdmod(cmod, FALSE))
2841 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002843 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002844 if (*eap->cmd == NUL)
2845 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002846 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002847 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002848 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002849 if (vim9script && has_cmdmod(cmod, FALSE))
2850 *errormsg = _(e_command_modifier_without_command);
2851 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002852 return FAIL;
2853 }
2854
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002855 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002856
2857 // In Vim9 script a variable can shadow a command modifier:
2858 // verbose = 123
2859 // verbose += 123
2860 // silent! verbose = func()
2861 // verbose.member = 2
2862 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002863 // But not:
2864 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002865 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002866 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002867 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002868
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002869 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002870 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002871 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002872 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2873 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002874 break;
2875 }
2876
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 switch (*p)
2878 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002879 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002880 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002881 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002882 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002883 continue;
2884
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002885 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002887 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002888 continue;
2889 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002890 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002891 {
2892#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002893 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002894#endif
2895 continue;
2896 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002897 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002898 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002899 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002900 continue;
2901
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002902 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002903 break;
2904#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002905 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002906#endif
2907 continue;
2908
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002909 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002910 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002911 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002912 continue;
2913 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002914 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002915 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002916 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002917 continue;
2918 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002919 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002920 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002921 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002922 continue;
2923 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002924 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002925 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002926 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002927 continue;
2928
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002929 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002930 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002931 char_u *reg_pat;
2932 char_u *nulp = NULL;
2933 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002934
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002935 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002936 || *p == NUL
2937 || (ends_excmd(*p)
2938#ifdef FEAT_EVAL
2939 // in ":filter #pat# cmd" # does not
2940 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002941 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002942#endif
2943 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002944 break;
2945 if (*p == '!')
2946 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002947 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002948 p = skipwhite(p + 1);
2949 if (*p == NUL || ends_excmd(*p))
2950 break;
2951 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002952#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002953 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002954 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002955 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002956#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002957 if (skip_only)
2958 p = skip_vimgrep_pat(p, NULL, NULL);
2959 else
2960 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002961 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2962 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 if (p == NULL || *p == NUL)
2964 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002965 if (!skip_only)
2966 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002967 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002968 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002969 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002970 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002971 // restore the character overwritten by NUL
2972 if (nulp != NULL)
2973 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002974 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002975 eap->cmd = p;
2976 continue;
2977 }
2978
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002979 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002980 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002981 || *p == NUL || ends_excmd(*p))
2982 break;
2983 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002984 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002985 continue;
2986
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002987 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002988 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002989 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002990 continue;
2991 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002992 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2993 {
2994 if (ends_excmd2(p, eap->cmd))
2995 {
2996 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02002997 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002998 return FAIL;
2999 }
3000 cmod->cmod_flags |= CMOD_LEGACY;
3001 continue;
3002 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003003
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003004 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003005 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003006 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 continue;
3008
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003009 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003010 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003011 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003012 continue;
3013 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003014 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003015 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003016 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003017 continue;
3018
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003019 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003020 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003021 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003022 continue;
3023
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003024 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003025 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003026 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003027 continue;
3028 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003029 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003030 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003031 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003032 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3033 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003034 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003035 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003036 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003037 }
3038 continue;
3039
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003040 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003041 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003042 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003043 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003044 long tabnr = get_address(eap, &eap->cmd,
3045 ADDR_TABS, eap->skip,
3046 skip_only, FALSE, 1);
3047 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003048 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003049 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003050 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003051 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3052 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003053 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003054 return FAIL;
3055 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003056 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003057 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003058 }
3059 eap->cmd = p;
3060 continue;
3061 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003062 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003063 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003064 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003065 continue;
3066
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003067 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003068 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003069 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003070 continue;
3071
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003072 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003073 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003074 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003075 continue;
3076 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003077 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003078 {
3079 if (ends_excmd2(p, eap->cmd))
3080 {
3081 *errormsg =
3082 _(e_vim9cmd_must_be_followed_by_command);
3083 return FAIL;
3084 }
3085 cmod->cmod_flags |= CMOD_VIM9CMD;
3086 continue;
3087 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003088 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003089 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003090 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003091 {
zeertzjqcd749632022-06-14 13:30:35 +01003092 // zero means not set, one is verbose == 0, etc.
3093 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003094 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003095 else
zeertzjqcd749632022-06-14 13:30:35 +01003096 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003097 eap->cmd = p;
3098 continue;
3099 }
3100 break;
3101 }
3102
Bram Moolenaar1501b632022-03-27 16:56:21 +01003103 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003104 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003105 if (eap->cmd > cmd_start)
3106 {
3107 // Move the '<,'> range to after the modifiers and insert a colon.
3108 // Since the modifiers have been parsed put the colon on top of the
3109 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3110 // Put eap->cmd after the colon.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003111 if (did_plus_cmd)
3112 {
3113 size_t len = STRLEN(cmd_start);
3114
3115 // Special case: empty command may have been changed to "+":
3116 // "'<,'>mod" -> "mod'<,'>+
3117 mch_memmove(orig_cmd, cmd_start, len);
3118 STRCPY(orig_cmd + len, "'<,'>+");
3119 }
3120 else
3121 {
3122 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3123 eap->cmd -= 5;
3124 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3125 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003126 }
3127 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003128 // No modifiers, move the pointer back.
3129 // Special case: empty command may have been changed to "+".
3130 if (did_plus_cmd)
3131 eap->cmd = (char_u *)"'<,'>+";
3132 else
3133 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003134 }
3135
Bram Moolenaareffed932018-08-14 13:38:17 +02003136 return OK;
3137}
3138
3139/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003140 * Return TRUE if "cmod" has anything set.
3141 */
3142 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003143has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003144{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003145 return (cmod->cmod_flags != 0 && (!ignore_silent
3146 || (cmod->cmod_flags
3147 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003148 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003149 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003150 || cmod->cmod_tab != 0
3151 || cmod->cmod_filter_regmatch.regprog != NULL;
3152}
3153
Bram Moolenaar8991be22022-02-14 21:51:46 +00003154#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003155/*
3156 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3157 */
3158 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003159cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003160{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003161 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003162 {
3163 emsg(_(e_misplaced_command_modifier));
3164 return TRUE;
3165 }
3166 return FALSE;
3167}
Dominique Pelle748b3082022-01-08 12:41:16 +00003168#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003169
3170/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003171 * Apply the command modifiers. Saves current state in "cmdmod", call
3172 * undo_cmdmod() later.
3173 */
3174 void
3175apply_cmdmod(cmdmod_T *cmod)
3176{
3177#ifdef HAVE_SANDBOX
3178 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3179 {
3180 ++sandbox;
3181 cmod->cmod_did_sandbox = TRUE;
3182 }
3183#endif
zeertzjqcd749632022-06-14 13:30:35 +01003184 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003185 {
3186 if (cmod->cmod_verbose_save == 0)
3187 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003188 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003189 }
3190
3191 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3192 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003193 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003194 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003195 cmod->cmod_save_msg_scroll = msg_scroll;
3196 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003197 if (cmod->cmod_flags & CMOD_SILENT)
3198 ++msg_silent;
3199 if (cmod->cmod_flags & CMOD_UNSILENT)
3200 msg_silent = 0;
3201
3202 if (cmod->cmod_flags & CMOD_ERRSILENT)
3203 {
3204 ++emsg_silent;
3205 ++cmod->cmod_did_esilent;
3206 }
3207
Bram Moolenaare1004402020-10-24 20:49:43 +02003208 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003209 {
3210 // Set 'eventignore' to "all".
3211 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003212 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003213 set_string_option_direct((char_u *)"ei", -1,
3214 (char_u *)"all", OPT_FREE, SID_NONE);
3215 }
3216}
3217
3218/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003219 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003220 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003221 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003222undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003223{
Bram Moolenaare1004402020-10-24 20:49:43 +02003224 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003225 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003226 p_verbose = cmod->cmod_verbose_save - 1;
3227 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003228 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003229
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003230#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003231 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003232 {
3233 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003234 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003235 }
3236#endif
3237
Bram Moolenaare1004402020-10-24 20:49:43 +02003238 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003240 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003241 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3242 OPT_FREE, SID_NONE);
3243 free_string_option(cmod->cmod_save_ei);
3244 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003245 }
3246
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003247 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003248
Bram Moolenaare1004402020-10-24 20:49:43 +02003249 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003250 {
3251 // messages could be enabled for a serious error, need to check if the
3252 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003253 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3254 msg_silent = cmod->cmod_save_msg_silent - 1;
3255 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003256 if (emsg_silent < 0)
3257 emsg_silent = 0;
3258 // Restore msg_scroll, it's set by file I/O commands, even when no
3259 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003260 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003261
3262 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3263 // somewhere in the line. Put it back in the first column.
3264 if (redirecting())
3265 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003266
Bram Moolenaare1004402020-10-24 20:49:43 +02003267 cmod->cmod_save_msg_silent = 0;
3268 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003269 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003270}
3271
3272/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003273 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003274 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003275 * Return FAIL and set "errormsg" or return OK.
3276 */
3277 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003278parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003279{
3280 int address_count = 1;
3281 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003282 int need_check_cursor = FALSE;
3283 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003284
3285 // Repeat for all ',' or ';' separated addresses.
3286 for (;;)
3287 {
3288 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003289 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003290 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003291 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003292 eap->addr_count == 0, address_count++);
3293 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003294 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003295 if (lnum == MAXLNUM)
3296 {
3297 if (*eap->cmd == '%') // '%' - all lines
3298 {
3299 ++eap->cmd;
3300 switch (eap->addr_type)
3301 {
3302 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003303 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003304 eap->line1 = 1;
3305 eap->line2 = curbuf->b_ml.ml_line_count;
3306 break;
3307 case ADDR_LOADED_BUFFERS:
3308 {
3309 buf_T *buf = firstbuf;
3310
3311 while (buf->b_next != NULL
3312 && buf->b_ml.ml_mfp == NULL)
3313 buf = buf->b_next;
3314 eap->line1 = buf->b_fnum;
3315 buf = lastbuf;
3316 while (buf->b_prev != NULL
3317 && buf->b_ml.ml_mfp == NULL)
3318 buf = buf->b_prev;
3319 eap->line2 = buf->b_fnum;
3320 break;
3321 }
3322 case ADDR_BUFFERS:
3323 eap->line1 = firstbuf->b_fnum;
3324 eap->line2 = lastbuf->b_fnum;
3325 break;
3326 case ADDR_WINDOWS:
3327 case ADDR_TABS:
3328 if (IS_USER_CMDIDX(eap->cmdidx))
3329 {
3330 eap->line1 = 1;
3331 eap->line2 = eap->addr_type == ADDR_WINDOWS
3332 ? LAST_WIN_NR : LAST_TAB_NR;
3333 }
3334 else
3335 {
3336 // there is no Vim command which uses '%' and
3337 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003338 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003339 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003340 }
3341 break;
3342 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003343 case ADDR_UNSIGNED:
3344 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003345 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003346 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003347 case ADDR_ARGUMENTS:
3348 if (ARGCOUNT == 0)
3349 eap->line1 = eap->line2 = 0;
3350 else
3351 {
3352 eap->line1 = 1;
3353 eap->line2 = ARGCOUNT;
3354 }
3355 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003356 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003357#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003358 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003359 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003360 if (eap->line2 == 0)
3361 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003362#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003363 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003364 case ADDR_NONE:
3365 // Will give an error later if a range is found.
3366 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003367 }
3368 ++eap->addr_count;
3369 }
3370 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3371 {
3372 pos_T *fp;
3373
3374 // '*' - visual area
3375 if (eap->addr_type != ADDR_LINES)
3376 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003377 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003378 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003379 }
3380
3381 ++eap->cmd;
3382 if (!eap->skip)
3383 {
3384 fp = getmark('<', FALSE);
3385 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003386 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003387 eap->line1 = fp->lnum;
3388 fp = getmark('>', FALSE);
3389 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003390 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003391 eap->line2 = fp->lnum;
3392 ++eap->addr_count;
3393 }
3394 }
3395 }
3396 else
3397 eap->line2 = lnum;
3398 eap->addr_count++;
3399
3400 if (*eap->cmd == ';')
3401 {
3402 if (!eap->skip)
3403 {
3404 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003405
Bram Moolenaar0e717042020-04-27 19:29:01 +02003406 // Don't leave the cursor on an illegal line or column, but do
3407 // accept zero as address, so 0;/PATTERN/ works correctly.
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003408 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003409 if (eap->line2 > 0)
3410 check_cursor();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003411 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003412 }
3413 }
3414 else if (*eap->cmd != ',')
3415 break;
3416 ++eap->cmd;
3417 }
3418
3419 // One address given: set start and end lines.
3420 if (eap->addr_count == 1)
3421 {
3422 eap->line1 = eap->line2;
3423 // ... but only implicit: really no address given
3424 if (lnum == MAXLNUM)
3425 eap->addr_count = 0;
3426 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003427 ret = OK;
3428
3429theend:
3430 if (need_check_cursor)
3431 check_cursor();
3432 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003433}
3434
3435/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003436 * Append "cmd" to the error message in IObuff.
3437 * Takes care of limiting the length and handling 0xa0, which would be
3438 * invisible otherwise.
3439 */
3440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003441append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003442{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003443 size_t len = STRLEN(IObuff);
3444 char_u *s = cmd;
3445 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003446
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003447 if (len > IOSIZE - 100)
3448 {
3449 // Not enough space, truncate and put in "...".
3450 d = IObuff + IOSIZE - 100;
3451 d -= mb_head_off(IObuff, d);
3452 STRCPY(d, "...");
3453 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003454 STRCAT(IObuff, ": ");
3455 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003456 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003457 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003458 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003459 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003460 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003461 STRCPY(d, "<a0>");
3462 d += 4;
3463 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003464 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3465 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003466 else
3467 MB_COPY_CHAR(s, d);
3468 }
3469 *d = NUL;
3470}
3471
Dominique Pelle748b3082022-01-08 12:41:16 +00003472#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003473/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003474 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3475 * the name. Otherwise just return "start".
3476 */
3477 char_u *
3478skip_option_env_lead(char_u *start)
3479{
3480 char_u *name = start;
3481
3482 if (*start == '&')
3483 {
3484 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3485 name += 3;
3486 else
3487 name += 1;
3488 }
3489 else if (*start == '$')
3490 name += 1;
3491 return name;
3492}
Dominique Pelle748b3082022-01-08 12:41:16 +00003493#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003494
3495/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003496 * Return TRUE and set "*idx" if "p" points to a one letter command.
3497 * If not in Vim9 script:
3498 * - The 'k' command can directly be followed by any character.
3499 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3500 * but :sre[wind] is another command, as are :scr[iptnames],
3501 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3502 */
3503 static int
3504one_letter_cmd(char_u *p, cmdidx_T *idx)
3505{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003506 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003507 return FALSE;
3508 if (*p == 'k')
3509 {
3510 *idx = CMD_k;
3511 return TRUE;
3512 }
3513 if (p[0] == 's'
3514 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3515 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3516 || p[1] == 'g'
3517 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3518 || p[1] == 'I'
3519 || (p[1] == 'r' && p[2] != 'e')))
3520 {
3521 *idx = CMD_substitute;
3522 return TRUE;
3523 }
3524 return FALSE;
3525}
3526
3527/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003528 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3529 * call.
3530 */
3531 int
3532number_method(char_u *cmd)
3533{
3534 char_u *p = skipdigits(cmd);
3535
3536 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3537}
3538
3539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003541 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003542 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003543 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 *
3545 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3546 * assignment without "let". Sets eap->cmdidx to the command while returning
3547 * "eap->cmd".
3548 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 * Returns NULL for an ambiguous user command.
3550 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 char_u *
3552find_ex_command(
3553 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003554 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003555 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003556 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557{
3558 int len;
3559 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003560 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003561#ifndef FEAT_EVAL
3562 int vim9 = FALSE;
3563#else
3564 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 /*
3567 * Recognize a Vim9 script function/method call and assignment:
3568 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3569 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003570 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003571 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003573 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003575 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003576 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3577 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003579 int oplen;
3580 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003581 char_u *swp;
3582
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003583 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003584 || (eap->cmd[0] == '$'
3585 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003586 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003587 && (valid_yank_reg(eap->cmd[1], FALSE)
3588 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003589 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003590 if (*eap->cmd == '&')
3591 {
3592 p = eap->cmd + 1;
3593 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3594 p += 2;
3595 p = to_name_end(p, FALSE);
3596 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003597 else if (*eap->cmd == '$')
3598 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003599 else
3600 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003601 if (ends_excmd(*skipwhite(p)))
3602 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003603 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3604 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003605 eap->cmdidx = CMD_eval;
3606 return eap->cmd;
3607 }
3608 // "&option" can be followed by "->" or "=", check below
3609 }
3610
3611 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003613 if (
3614 // "(..." is an expression.
3615 // "funcname(" is always a function call.
3616 *p == '('
3617 || (p == eap->cmd
3618 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003619 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003620 *eap->cmd == '{'
3621 // "'string'->func()" is an expression.
3622 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003623 // '"string"->func()' is an expression.
3624 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003625 // '$"string"->func()' is an expression.
3626 // "$'string'->func()" is an expression.
3627 || (eap->cmd[0] == '$'
3628 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3629 // '0z1234->func()' is an expression.
3630 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003631 // "g:varname" is an expression.
3632 || eap->cmd[1] == ':'
3633 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003634 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003635 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003636 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003637 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3638 {
3639 // "{" by itself is the start of a block.
3640 eap->cmdidx = CMD_block;
3641 return eap->cmd + 1;
3642 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003643 eap->cmdidx = CMD_eval;
3644 return eap->cmd;
3645 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003646
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003647 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003648 // "varname[]" is an expression.
3649 *p == '['
3650 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003651 || (*p == '.'
3652 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3653 // g:[key] is an expression
3654 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003655 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003656 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003657
3658 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003659 // Skip over the whole thing, it can be:
3660 // name.member = val
3661 // name[a : b] = val
3662 // name[idx] = val
3663 // name[idx].member = val
3664 // etc.
3665 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003666 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003667 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003668 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003669 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003670 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003671 || (after[0] == '.' && after[1] == '.'
3672 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003673 eap->cmdidx = CMD_var;
3674 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003675 --emsg_silent;
3676 return eap->cmd;
3677 }
3678
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003679 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3680 // an assignment.
3681 // If there is no line break inside the "[...]" then "p" is
3682 // advanced to after the "]" by to_name_const_end(): check if a "="
3683 // follows.
3684 // If "[...]" has a line break "p" still points at the "[" and it
3685 // can't be an assignment.
3686 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003687 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003688 char_u *eq;
3689
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003690 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003691 if (p == eap->cmd && *p == '[')
3692 {
3693 int count = 0;
3694 int semicolon = FALSE;
3695
3696 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3697 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003698 eq = p;
3699 if (eq != NULL)
3700 {
3701 eq = skipwhite(eq);
3702 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3703 ++eq;
3704 }
3705 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003706 {
3707 eap->cmdidx = CMD_eval;
3708 return eap->cmd;
3709 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003710 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003711 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003712 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003713 return eap->cmd;
3714 }
3715 }
3716
3717 // Recognize an assignment if we recognize the variable name:
3718 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003719 // "@r = expr"
3720 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003721 // "var = expr" where "var" is a variable name or we are skipping
3722 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003723 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003724 oplen = assignment_len(skipwhite(p), &heredoc);
3725 if (oplen > 0)
3726 {
3727 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3728 || *eap->cmd == '&'
3729 || *eap->cmd == '$'
3730 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003731 || (eap->skip && IS_WHITE_OR_NUL(
3732 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003733 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003734 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003735 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003736 return eap->cmd;
3737 }
3738 }
3739
3740 // Recognize using a type for a w:, b:, t: or g: variable:
3741 // "w:varname: number = 123".
3742 if (eap->cmd[1] == ':' && *p == ':')
3743 {
3744 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003745 return eap->cmd;
3746 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003747 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003748
Bram Moolenaar31d99482022-05-26 22:24:43 +01003749 // 1234->func() is a method call
3750 if (number_method(eap->cmd))
3751 {
3752 eap->cmdidx = CMD_eval;
3753 return eap->cmd;
3754 }
3755
Bram Moolenaar7b829262021-10-13 15:04:34 +01003756 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3757 // an expression. A global/substitute/list command needs to use a
3758 // longer name.
3759 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3760 {
3761 eap->cmdidx = CMD_eval;
3762 return eap->cmd;
3763 }
3764
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003765 // If it is an ID it might be a variable with an operator on the next
3766 // line, if the variable exists it can't be an Ex command.
3767 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003768 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003769 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3770 {
3771 eap->cmdidx = CMD_eval;
3772 return eap->cmd;
3773 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003774
3775 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003776 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3777 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003778 {
3779 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3780 return eap->cmd + 2;
3781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 }
3783#endif
3784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 /*
3786 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 */
3788 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003789 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 ++p;
3792 }
3793 else
3794 {
3795 while (ASCII_ISALPHA(*p))
3796 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003797 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003798 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003800 while (ASCII_ISALNUM(*p))
3801 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 }
3803 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3804 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003805 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 ++p;
3807 while (ASCII_ISALPHA(*p))
3808 ++p;
3809 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003810
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003811 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003812 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 ++p;
3814 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003815 // The "d" command can directly be followed by 'l' or 'p' flag, when
3816 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003817 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003818 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003819 // Check for ":dl", ":dell", etc. to ":deletel": that's
3820 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003821 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003822 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003823 break;
3824 if (i == len - 1)
3825 {
3826 --len;
3827 if (p[-1] == 'l')
3828 eap->flags |= EXFLAG_LIST;
3829 else
3830 eap->flags |= EXFLAG_PRINT;
3831 }
3832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003834 if (ASCII_ISLOWER(eap->cmd[0]))
3835 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003836 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003837 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003838
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003839 if (command_count != (int)CMD_SIZE)
3840 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003841 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003842 getout(1);
3843 }
3844
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003845 // Use a precomputed index for fast look-up in cmdnames[]
3846 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003847 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3848 if (ASCII_ISLOWER(c2))
3849 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3850 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003851 else if (ASCII_ISUPPER(eap->cmd[0]))
3852 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003854 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
3856 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3857 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3858 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3859 (size_t)len) == 0)
3860 {
3861#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003862 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 *full = TRUE;
3864#endif
3865 break;
3866 }
3867
Bram Moolenaar204852a2022-03-05 12:56:44 +00003868 // :Print and :mode are not supported in Vim9 script.
3869 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003870 if (vim9 && eap->cmdidx != CMD_SIZE)
3871 {
3872 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3873 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003874 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003875 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003876 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003877 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003878 eap->cmdidx = CMD_SIZE;
3879 }
3880 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003881
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003882 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003883 // 'cpoptions'.
3884 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3885 p = eap->cmd;
3886
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003887 // Look for a user defined command as a last resort. Let ":Print" be
3888 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003889 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3890 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003892 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 while (ASCII_ISALNUM(*p))
3894 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003895 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003897 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 eap->cmdidx = CMD_SIZE;
3899 }
3900
Bram Moolenaar204852a2022-03-05 12:56:44 +00003901 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3902 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003903 eap->cmdidx = CMD_finally;
3904
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003905#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003906 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003907 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003908 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003909 && (eap->cmdidx < 0 ||
3910 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003911 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003912 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3913
3914 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003915 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003916 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003917 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003918#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 return p;
3921}
3922
3923#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003924static struct cmdmod
3925{
3926 char *name;
3927 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003928 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003929} cmdmods[] = {
3930 {"aboveleft", 3, FALSE},
3931 {"belowright", 3, FALSE},
3932 {"botright", 2, FALSE},
3933 {"browse", 3, FALSE},
3934 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003935 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003936 {"hide", 3, FALSE},
3937 {"keepalt", 5, FALSE},
3938 {"keepjumps", 5, FALSE},
3939 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003940 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003941 {"leftabove", 5, FALSE},
3942 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003943 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003944 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003945 {"rightbelow", 6, FALSE},
3946 {"sandbox", 3, FALSE},
3947 {"silent", 3, FALSE},
3948 {"tab", 3, TRUE},
3949 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003950 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003951 {"verbose", 4, TRUE},
3952 {"vertical", 4, FALSE},
3953};
3954
3955/*
3956 * Return length of a command modifier (including optional count).
3957 * Return zero when it's not a modifier.
3958 */
3959 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003960modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003961{
3962 int i, j;
3963 char_u *p = cmd;
3964
3965 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003966 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003967 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003968 {
3969 for (j = 0; p[j] != NUL; ++j)
3970 if (p[j] != cmdmods[i].name[j])
3971 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003972 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003973 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003974 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003975 }
3976 return 0;
3977}
3978
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979/*
3980 * Return > 0 if an Ex command "name" exists.
3981 * Return 2 if there is an exact match.
3982 * Return 3 if there is an ambiguous match.
3983 */
3984 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003985cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
3987 exarg_T ea;
3988 int full = FALSE;
3989 int i;
3990 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003991 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003993 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003994 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 {
3996 for (j = 0; name[j] != NUL; ++j)
3997 if (name[j] != cmdmods[i].name[j])
3998 break;
3999 if (name[j] == NUL && j >= cmdmods[i].minlen)
4000 return (cmdmods[i].name[j] == NUL ? 2 : 1);
4001 }
4002
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004003 // Check built-in commands and user defined commands.
4004 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004005 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004007 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004009 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004011 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4012 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004013 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004014 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4016}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004017
4018/*
4019 * "fullcommand" function
4020 */
4021 void
4022f_fullcommand(typval_T *argvars, typval_T *rettv)
4023{
4024 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004025 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004026 char_u *p;
4027
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004028 rettv->v_type = VAR_STRING;
4029 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004030
4031 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
4032 return;
4033
4034 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004035 if (name == NULL)
4036 return;
4037
zeertzjqc024ed92022-01-04 16:22:52 +00004038 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004039 name++;
4040 name = skip_range(name, TRUE, NULL);
4041
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004042 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4043 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004044 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004045 p = find_ex_command(&ea, NULL, NULL, NULL);
4046 if (p == NULL || ea.cmdidx == CMD_SIZE)
4047 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004048 if (in_vim9script())
4049 {
4050 int res;
4051
4052 ++emsg_silent;
4053 res = not_in_vim9(&ea);
4054 --emsg_silent;
4055
4056 if (res == FAIL)
4057 return;
4058 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004059
4060 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004061 ? get_user_command_name(ea.useridx, ea.cmdidx)
4062 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004063}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064#endif
4065
Bram Moolenaard0190392019-08-23 21:17:35 +02004066 cmdidx_T
4067excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068{
Bram Moolenaard0190392019-08-23 21:17:35 +02004069 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004071 if (!one_letter_cmd(cmd, &idx))
4072 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4073 idx = (cmdidx_T)((int)idx + 1))
4074 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4075 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
Bram Moolenaard0190392019-08-23 21:17:35 +02004077 return idx;
4078}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079
Bram Moolenaard0190392019-08-23 21:17:35 +02004080 long
4081excmd_get_argt(cmdidx_T idx)
4082{
4083 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084}
4085
4086/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004087 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 *
4089 * Backslashed delimiters after / or ? will be skipped, and commands will
4090 * not be expanded between /'s and ?'s or after "'".
4091 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004092 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 * Returns the "cmd" pointer advanced to beyond the range.
4094 */
4095 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004096skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004097 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004098 int skip_star, // skip "*" used for Visual range
4099 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004101 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004102 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004104 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004106 if (*cmd == '\\')
4107 {
4108 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4109 ++cmd;
4110 else
4111 break;
4112 }
4113 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004115 char_u *p = cmd;
4116
4117 // a quote is only valid at the start or after a separator
4118 while (p > cmd_start)
4119 {
4120 --p;
4121 if (!VIM_ISWHITE(*p))
4122 break;
4123 }
4124 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4125 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 if (*++cmd == NUL && ctx != NULL)
4127 *ctx = EXPAND_NOTHING;
4128 }
4129 else if (*cmd == '/' || *cmd == '?')
4130 {
4131 delim = *cmd++;
4132 while (*cmd != NUL && *cmd != delim)
4133 if (*cmd++ == '\\' && *cmd != NUL)
4134 ++cmd;
4135 if (*cmd == NUL && ctx != NULL)
4136 *ctx = EXPAND_NOTHING;
4137 }
4138 if (*cmd != NUL)
4139 ++cmd;
4140 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004141
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004142 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004143 while (*cmd == ':')
4144 cmd = skipwhite(cmd + 1);
4145
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004146 // Skip "*" used for Visual range.
4147 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4148 cmd = skipwhite(cmd + 1);
4149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 return cmd;
4151}
4152
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004153 static void
4154addr_error(cmd_addr_T addr_type)
4155{
4156 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004157 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004158 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004159 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004160}
4161
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004163 * Return the default address for an address type.
4164 */
4165 static linenr_T
4166default_address(exarg_T *eap)
4167{
4168 linenr_T lnum = 0;
4169
4170 switch (eap->addr_type)
4171 {
4172 case ADDR_LINES:
4173 case ADDR_OTHER:
4174 // Default is the cursor line number. Avoid using an invalid
4175 // line number though.
4176 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4177 lnum = curbuf->b_ml.ml_line_count;
4178 else
4179 lnum = curwin->w_cursor.lnum;
4180 break;
4181 case ADDR_WINDOWS:
4182 lnum = CURRENT_WIN_NR;
4183 break;
4184 case ADDR_ARGUMENTS:
4185 lnum = curwin->w_arg_idx + 1;
4186 if (lnum > ARGCOUNT)
4187 lnum = ARGCOUNT;
4188 break;
4189 case ADDR_LOADED_BUFFERS:
4190 case ADDR_BUFFERS:
4191 lnum = curbuf->b_fnum;
4192 break;
4193 case ADDR_TABS:
4194 lnum = CURRENT_TAB_NR;
4195 break;
4196 case ADDR_TABS_RELATIVE:
4197 case ADDR_UNSIGNED:
4198 lnum = 1;
4199 break;
4200 case ADDR_QUICKFIX:
4201#ifdef FEAT_QUICKFIX
4202 lnum = qf_get_cur_idx(eap);
4203#endif
4204 break;
4205 case ADDR_QUICKFIX_VALID:
4206#ifdef FEAT_QUICKFIX
4207 lnum = qf_get_cur_valid_idx(eap);
4208#endif
4209 break;
4210 case ADDR_NONE:
4211 // Will give an error later if a range is found.
4212 break;
4213 }
4214 return lnum;
4215}
4216
4217/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004218 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 *
4220 * Set ptr to the next character after the part that was interpreted.
4221 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004222 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 *
4224 * Return MAXLNUM when no Ex address was found.
4225 */
4226 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004227get_address(
4228 exarg_T *eap UNUSED,
4229 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004230 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004231 int skip, // only skip the address, don't use it
4232 int silent, // no errors or side effects
4233 int to_other_file, // flag: may jump to other file
4234 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
4236 int c;
4237 int i;
4238 long n;
4239 char_u *cmd;
4240 pos_T pos;
4241 pos_T *fp;
4242 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004243 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 cmd = skipwhite(*ptr);
4246 lnum = MAXLNUM;
4247 do
4248 {
4249 switch (*cmd)
4250 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004251 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004252 ++cmd;
4253 switch (addr_type)
4254 {
4255 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004256 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 lnum = curwin->w_cursor.lnum;
4258 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004259 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004260 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004261 break;
4262 case ADDR_ARGUMENTS:
4263 lnum = curwin->w_arg_idx + 1;
4264 break;
4265 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004266 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004267 lnum = curbuf->b_fnum;
4268 break;
4269 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004270 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004272 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004273 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004274 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004275 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004276 cmd = NULL;
4277 goto error;
4278 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004279 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004280#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004281 lnum = qf_get_cur_idx(eap);
4282#endif
4283 break;
4284 case ADDR_QUICKFIX_VALID:
4285#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004286 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004287#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004288 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004289 }
4290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004292 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004293 ++cmd;
4294 switch (addr_type)
4295 {
4296 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004297 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 lnum = curbuf->b_ml.ml_line_count;
4299 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004300 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004301 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004302 break;
4303 case ADDR_ARGUMENTS:
4304 lnum = ARGCOUNT;
4305 break;
4306 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004307 buf = lastbuf;
4308 while (buf->b_ml.ml_mfp == NULL)
4309 {
4310 if (buf->b_prev == NULL)
4311 break;
4312 buf = buf->b_prev;
4313 }
4314 lnum = buf->b_fnum;
4315 break;
4316 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004317 lnum = lastbuf->b_fnum;
4318 break;
4319 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004320 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004321 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004322 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004323 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004324 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004325 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004326 cmd = NULL;
4327 goto error;
4328 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004329 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004330#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004331 lnum = qf_get_size(eap);
4332 if (lnum == 0)
4333 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004334#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004335 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004336 case ADDR_QUICKFIX_VALID:
4337#ifdef FEAT_QUICKFIX
4338 lnum = qf_get_valid_size(eap);
4339 if (lnum == 0)
4340 lnum = 1;
4341#endif
4342 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004343 }
4344 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004346 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004347 if (*++cmd == NUL)
4348 {
4349 cmd = NULL;
4350 goto error;
4351 }
4352 if (addr_type != ADDR_LINES)
4353 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004354 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004355 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004356 goto error;
4357 }
4358 if (skip)
4359 ++cmd;
4360 else
4361 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004362 // Only accept a mark in another file when it is
4363 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004364 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4365 ++cmd;
4366 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004367 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004368 lnum = curwin->w_cursor.lnum;
4369 else
4370 {
4371 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
4373 cmd = NULL;
4374 goto error;
4375 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004376 lnum = fp->lnum;
4377 }
4378 }
4379 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
4381 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004382 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004383 c = *cmd++;
4384 if (addr_type != ADDR_LINES)
4385 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004386 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004387 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004388 goto error;
4389 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004390 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004391 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004392 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004393 if (*cmd == c)
4394 ++cmd;
4395 }
4396 else
4397 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004398 int flags;
4399
4400 pos = curwin->w_cursor; // save curwin->w_cursor
4401
4402 // When '/' or '?' follows another address, start from
4403 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004404 if (lnum > 0 && lnum != MAXLNUM)
4405 curwin->w_cursor.lnum =
4406 lnum > curbuf->b_ml.ml_line_count
4407 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004408
4409 // Start a forward search at the end of the line (unless
4410 // before the first line).
4411 // Start a backward search at the start of the line.
4412 // This makes sure we never match in the current
4413 // line, and can match anywhere in the
4414 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004415 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004416 curwin->w_cursor.col = MAXCOL;
4417 else
4418 curwin->w_cursor.col = 0;
4419 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004420 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004421 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004422 {
4423 curwin->w_cursor = pos;
4424 cmd = NULL;
4425 goto error;
4426 }
4427 lnum = curwin->w_cursor.lnum;
4428 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004429 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004430 cmd += searchcmdlen;
4431 }
4432 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004434 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 ++cmd;
4436 if (addr_type != ADDR_LINES)
4437 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004438 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004439 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004440 goto error;
4441 }
4442 if (*cmd == '&')
4443 i = RE_SUBST;
4444 else if (*cmd == '?' || *cmd == '/')
4445 i = RE_SEARCH;
4446 else
4447 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004448 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 cmd = NULL;
4450 goto error;
4451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 if (!skip)
4454 {
4455 /*
4456 * When search follows another address, start from
4457 * there.
4458 */
4459 if (lnum != MAXLNUM)
4460 pos.lnum = lnum;
4461 else
4462 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004464 /*
4465 * Start the search just like for the above
4466 * do_search().
4467 */
4468 if (*cmd != '?')
4469 pos.col = MAXCOL;
4470 else
4471 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004472 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004473 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004474 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004475 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 lnum = pos.lnum;
4477 else
4478 {
4479 cmd = NULL;
4480 goto error;
4481 }
4482 }
4483 ++cmd;
4484 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
4486 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004487 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004488 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490
4491 for (;;)
4492 {
4493 cmd = skipwhite(cmd);
4494 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4495 break;
4496
4497 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004498 {
4499 switch (addr_type)
4500 {
4501 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004502 case ADDR_OTHER:
4503 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004504 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 break;
4506 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004507 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004508 break;
4509 case ADDR_ARGUMENTS:
4510 lnum = curwin->w_arg_idx + 1;
4511 break;
4512 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004513 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 lnum = curbuf->b_fnum;
4515 break;
4516 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004517 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004519 case ADDR_TABS_RELATIVE:
4520 lnum = 1;
4521 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004522 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004523#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004524 lnum = qf_get_cur_idx(eap);
4525#endif
4526 break;
4527 case ADDR_QUICKFIX_VALID:
4528#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004529 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004530#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004531 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004532 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004533 case ADDR_UNSIGNED:
4534 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004535 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004536 }
4537 }
4538
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004540 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 else
4542 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004543 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 n = 1;
4545 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004548 if (n == MAXLNUM)
4549 {
4550 emsg(_(e_line_number_out_of_range));
4551 goto error;
4552 }
4553 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004554
4555 if (addr_type == ADDR_TABS_RELATIVE)
4556 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004557 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004558 cmd = NULL;
4559 goto error;
4560 }
4561 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004562 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004563 lnum = compute_buffer_local_count(
4564 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004566 {
4567#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004568 // Relative line addressing, need to adjust for folded lines
4569 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004570 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004571 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004572 (void)hasFolding(lnum, NULL, &lnum);
4573#endif
4574 if (i == '-')
4575 lnum -= n;
4576 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004577 {
4578 if (n >= LONG_MAX - lnum)
4579 {
4580 emsg(_(e_line_number_out_of_range));
4581 goto error;
4582 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004583 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004584 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 }
4587 } while (*cmd == '/' || *cmd == '?');
4588
4589error:
4590 *ptr = cmd;
4591 return lnum;
4592}
4593
4594/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004595 * Set eap->line1 and eap->line2 to the whole range.
4596 * Used for commands with the EX_DFLALL flag and no range given.
4597 */
4598 static void
4599address_default_all(exarg_T *eap)
4600{
4601 eap->line1 = 1;
4602 switch (eap->addr_type)
4603 {
4604 case ADDR_LINES:
4605 case ADDR_OTHER:
4606 eap->line2 = curbuf->b_ml.ml_line_count;
4607 break;
4608 case ADDR_LOADED_BUFFERS:
4609 {
4610 buf_T *buf = firstbuf;
4611
4612 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4613 buf = buf->b_next;
4614 eap->line1 = buf->b_fnum;
4615 buf = lastbuf;
4616 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4617 buf = buf->b_prev;
4618 eap->line2 = buf->b_fnum;
4619 }
4620 break;
4621 case ADDR_BUFFERS:
4622 eap->line1 = firstbuf->b_fnum;
4623 eap->line2 = lastbuf->b_fnum;
4624 break;
4625 case ADDR_WINDOWS:
4626 eap->line2 = LAST_WIN_NR;
4627 break;
4628 case ADDR_TABS:
4629 eap->line2 = LAST_TAB_NR;
4630 break;
4631 case ADDR_TABS_RELATIVE:
4632 eap->line2 = 1;
4633 break;
4634 case ADDR_ARGUMENTS:
4635 if (ARGCOUNT == 0)
4636 eap->line1 = eap->line2 = 0;
4637 else
4638 eap->line2 = ARGCOUNT;
4639 break;
4640 case ADDR_QUICKFIX_VALID:
4641#ifdef FEAT_QUICKFIX
4642 eap->line2 = qf_get_valid_size(eap);
4643 if (eap->line2 == 0)
4644 eap->line2 = 1;
4645#endif
4646 break;
4647 case ADDR_NONE:
4648 case ADDR_UNSIGNED:
4649 case ADDR_QUICKFIX:
4650 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4651 break;
4652 }
4653}
4654
4655
4656/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004657 * Get flags from an Ex command argument.
4658 */
4659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004660get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004661{
4662 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4663 {
4664 if (*eap->arg == 'l')
4665 eap->flags |= EXFLAG_LIST;
4666 else if (*eap->arg == 'p')
4667 eap->flags |= EXFLAG_PRINT;
4668 else
4669 eap->flags |= EXFLAG_NR;
4670 eap->arg = skipwhite(eap->arg + 1);
4671 }
4672}
4673
4674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 * Function called for command which is Not Implemented. NI!
4676 */
4677 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004678ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
4680 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004681 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004682 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683}
4684
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004685#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686/*
4687 * Function called for script command which is Not Implemented. NI!
4688 * Skips over ":perl <<EOF" constructs.
4689 */
4690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004691ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
4693 if (!eap->skip)
4694 ex_ni(eap);
4695 else
4696 vim_free(script_get(eap, eap->arg));
4697}
4698#endif
4699
4700/*
4701 * Check range in Ex command for validity.
4702 * Return NULL when valid, error message when invalid.
4703 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004704 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004705invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004707 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004708
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 if ( eap->line1 < 0
4710 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004711 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004712 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004713
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004714 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004715 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004716 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004717 {
4718 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004719 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004720#ifdef FEAT_DIFF
4721 + (eap->cmdidx == CMD_diffget)
4722#endif
4723 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004724 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004725 break;
4726 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004727 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004728 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004729 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004730 break;
4731 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004732 // Only a boundary check, not whether the buffers actually
4733 // exist.
4734 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004735 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004736 break;
4737 case ADDR_LOADED_BUFFERS:
4738 buf = firstbuf;
4739 while (buf->b_ml.ml_mfp == NULL)
4740 {
4741 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004742 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004743 buf = buf->b_next;
4744 }
4745 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004746 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004747 buf = lastbuf;
4748 while (buf->b_ml.ml_mfp == NULL)
4749 {
4750 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004751 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004752 buf = buf->b_prev;
4753 }
4754 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004755 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004756 break;
4757 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004758 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004759 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004760 break;
4761 case ADDR_TABS:
4762 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004763 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004764 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004765 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004766 case ADDR_OTHER:
4767 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004768 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004769 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004770#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004771 // No error for value that is too big, will use the last entry.
4772 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004773 {
4774 if (eap->addr_count == 0)
4775 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004776 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004777 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004778#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004779 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004780 case ADDR_QUICKFIX_VALID:
4781#ifdef FEAT_QUICKFIX
4782 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4783 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004784 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004785#endif
4786 break;
4787 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004788 case ADDR_NONE:
4789 // Will give an error elsewhere.
4790 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004791 }
4792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 return NULL;
4794}
4795
4796/*
4797 * Correct the range for zero line number, if required.
4798 */
4799 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004800correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004802 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
4804 if (eap->line1 == 0)
4805 eap->line1 = 1;
4806 if (eap->line2 == 0)
4807 eap->line2 = 1;
4808 }
4809}
4810
Bram Moolenaar748bf032005-02-02 23:04:36 +00004811#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004812/*
4813 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4814 * pattern. Otherwise return eap->arg.
4815 */
4816 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004817skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004818{
4819 char_u *p = eap->arg;
4820
Bram Moolenaara37420f2006-02-04 22:37:47 +00004821 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4822 || eap->cmdidx == CMD_vimgrepadd
4823 || eap->cmdidx == CMD_lvimgrepadd
4824 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004825 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004826 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004827 if (p == NULL)
4828 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004829 }
4830 return p;
4831}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004832
4833/*
4834 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4835 * in the command line, so that things like % get expanded.
4836 */
4837 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004838replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004839{
4840 char_u *new_cmdline;
4841 char_u *program;
4842 char_u *pos;
4843 char_u *ptr;
4844 int len;
4845 int i;
4846
4847 /*
4848 * Don't do it when ":vimgrep" is used for ":grep".
4849 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004850 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4851 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4852 || eap->cmdidx == CMD_grepadd
4853 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004854 && !grep_internal(eap->cmdidx))
4855 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004856 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4857 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004858 {
4859 if (*curbuf->b_p_gp == NUL)
4860 program = p_gp;
4861 else
4862 program = curbuf->b_p_gp;
4863 }
4864 else
4865 {
4866 if (*curbuf->b_p_mp == NUL)
4867 program = p_mp;
4868 else
4869 program = curbuf->b_p_mp;
4870 }
4871
4872 p = skipwhite(p);
4873
4874 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4875 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004876 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004877 i = 1;
4878 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4879 ++i;
4880 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004881 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004882 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004883 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004884 ptr = new_cmdline;
4885 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4886 {
4887 i = (int)(pos - program);
4888 STRNCPY(ptr, program, i);
4889 STRCPY(ptr += i, p);
4890 ptr += len;
4891 program = pos + 2;
4892 }
4893 STRCPY(ptr, program);
4894 }
4895 else
4896 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004897 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004898 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004899 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004900 STRCPY(new_cmdline, program);
4901 STRCAT(new_cmdline, " ");
4902 STRCAT(new_cmdline, p);
4903 }
4904 msg_make(p);
4905
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004906 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004907 vim_free(*cmdlinep);
4908 *cmdlinep = new_cmdline;
4909 p = new_cmdline;
4910 }
4911 return p;
4912}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004913#endif
4914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915/*
4916 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004917 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 * Return FAIL for failure, OK otherwise.
4919 */
4920 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004921expand_filename(
4922 exarg_T *eap,
4923 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004924 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004926 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 char_u *repl;
4928 int srclen;
4929 char_u *p;
4930 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004931 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
Bram Moolenaar748bf032005-02-02 23:04:36 +00004933#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004934 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004935 p = skip_grep_pat(eap);
4936#else
4937 p = eap->arg;
4938#endif
4939
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 /*
4941 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4942 * the file name contains a wildcard it should not cause expanding.
4943 * (it will be expanded anyway if there is a wildcard before replacing).
4944 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004945 has_wildcards = mch_has_wildcard(p);
4946 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004948#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004949 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004950 if (p[0] == '`' && p[1] == '=')
4951 {
4952 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004953 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004954 if (*p == '`')
4955 ++p;
4956 continue;
4957 }
4958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 /*
4960 * Quick check if this cannot be the start of a special string.
4961 * Also removes backslash before '%', '#' and '<'.
4962 */
4963 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4964 {
4965 ++p;
4966 continue;
4967 }
4968
4969 /*
4970 * Try to find a match at this position.
4971 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004972 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01004973 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004974 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004976 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
4978 p += srclen;
4979 continue;
4980 }
4981
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004982 // Wildcards won't be expanded below, the replacement is taken
4983 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004984 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4985 {
4986 char_u *l = repl;
4987
4988 repl = expand_env_save(repl);
4989 vim_free(l);
4990 }
4991
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004992 // Need to escape white space et al. with a backslash.
4993 // Don't do this for:
4994 // - replacement that already has been escaped: "##"
4995 // - shell commands (may have to use quotes instead).
4996 // - non-unix systems when there is a single argument (spaces don't
4997 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004999 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 && eap->cmdidx != CMD_grep
5002 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005003 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005004 && eap->cmdidx != CMD_lgrep
5005 && eap->cmdidx != CMD_lgrepadd
5006 && eap->cmdidx != CMD_lmake
5007 && eap->cmdidx != CMD_make
5008 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005010 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011#endif
5012 )
5013 {
5014 char_u *l;
5015#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005016 // Don't escape a backslash here, because rem_backslash() doesn't
5017 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 static char_u *nobslash = (char_u *)" \t\"|";
5019# define ESCAPE_CHARS nobslash
5020#else
5021# define ESCAPE_CHARS escape_chars
5022#endif
5023
5024 for (l = repl; *l; ++l)
5025 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5026 {
5027 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5028 if (l != NULL)
5029 {
5030 vim_free(repl);
5031 repl = l;
5032 }
5033 break;
5034 }
5035 }
5036
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005037 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005038 if ((eap->usefilter || eap->cmdidx == CMD_bang
5039 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005040 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 {
5042 char_u *l;
5043
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005044 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (l != NULL)
5046 {
5047 vim_free(repl);
5048 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
5050 }
5051
5052 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5053 vim_free(repl);
5054 if (p == NULL)
5055 return FAIL;
5056 }
5057
5058 /*
5059 * One file argument: Expand wildcards.
5060 * Don't do this with ":r !command" or ":w !command".
5061 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005062 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 /*
5065 * May do this twice:
5066 * 1. Replace environment variables.
5067 * 2. Replace any other wildcards, remove backslashes.
5068 */
5069 for (n = 1; n <= 2; ++n)
5070 {
5071 if (n == 2)
5072 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 /*
5074 * Halve the number of backslashes (this is Vi compatible).
5075 * For Unix and OS/2, when wildcards are expanded, this is
5076 * done by ExpandOne() below.
5077 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005078#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 if (!has_wildcards)
5080#endif
5081 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
5083
5084 if (has_wildcards)
5085 {
5086 if (n == 1)
5087 {
5088 /*
5089 * First loop: May expand environment variables. This
5090 * can be done much faster with expand_env() than with
5091 * something else (e.g., calling a shell).
5092 * After expanding environment variables, check again
5093 * if there are still wildcards present.
5094 */
5095 if (vim_strchr(eap->arg, '$') != NULL
5096 || vim_strchr(eap->arg, '~') != NULL)
5097 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005098 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005099 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 has_wildcards = mch_has_wildcard(NameBuff);
5101 p = NameBuff;
5102 }
5103 else
5104 p = NULL;
5105 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005106 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
5108 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005109 int options = WILD_LIST_NOTFOUND
5110 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
5112 ExpandInit(&xpc);
5113 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005114 if (p_wic)
5115 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005117 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (p == NULL)
5119 return FAIL;
5120 }
5121 if (p != NULL)
5122 {
5123 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5124 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005125 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 vim_free(p);
5127 }
5128 }
5129 }
5130 }
5131 return OK;
5132}
5133
5134/*
5135 * Replace part of the command line, keeping eap->cmd, eap->arg and
5136 * eap->nextcmd correct.
5137 * "src" points to the part that is to be replaced, of length "srclen".
5138 * "repl" is the replacement string.
5139 * Returns a pointer to the character after the replaced string.
5140 * Returns NULL for failure.
5141 */
5142 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005143repl_cmdline(
5144 exarg_T *eap,
5145 char_u *src,
5146 int srclen,
5147 char_u *repl,
5148 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149{
5150 int len;
5151 int i;
5152 char_u *new_cmdline;
5153
5154 /*
5155 * The new command line is build in new_cmdline[].
5156 * First allocate it.
5157 * Careful: a "+cmd" argument may have been NUL terminated.
5158 */
5159 len = (int)STRLEN(repl);
5160 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005161 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005162 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005163 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005164 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
5166 /*
5167 * Copy the stuff before the expanded part.
5168 * Copy the expanded stuff.
5169 * Copy what came after the expanded part.
5170 * Copy the next commands, if there are any.
5171 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005172 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005176 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005178 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005180 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
5182 i = (int)STRLEN(new_cmdline) + 1;
5183 STRCPY(new_cmdline + i, eap->nextcmd);
5184 eap->nextcmd = new_cmdline + i;
5185 }
5186 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5187 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5188 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5189 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5190 vim_free(*cmdlinep);
5191 *cmdlinep = new_cmdline;
5192
5193 return src;
5194}
5195
5196/*
5197 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005198 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 */
5200 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005201separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202{
5203 char_u *p;
5204
Bram Moolenaar86b68352004-12-27 21:59:20 +00005205#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005206 p = skip_grep_pat(eap);
5207#else
5208 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005209#endif
5210
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005211 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 {
5213 if (*p == Ctrl_V)
5214 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005215 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005216 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005218 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005219 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005220 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 break;
5222 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005223
5224#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005225 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005226 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005227 {
5228 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005229 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005230 if (*p == NUL) // stop at NUL after CTRL-V
5231 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005232 }
5233#endif
5234
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005235 // Check for '"': start of comment or '|': next command
5236 // :@" and :*" do not start a comment!
5237 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005238 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005239#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005240 && !in_vim9script()
5241#endif
5242 && !(eap->argt & EX_NOTRLCOM)
5243 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5244 || p != eap->arg)
5245 && (eap->cmdidx != CMD_redir
5246 || p != eap->arg + 1 || p[-1] != '@'))
5247#ifdef FEAT_EVAL
5248 || (*p == '#'
5249 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005250 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005251 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 || *p == '|' || *p == '\n')
5254 {
5255 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005256 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 * AND 'b' is present in 'cpoptions'.
5258 */
5259 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005260 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005262 if (!keep_backslash)
5263 {
5264 STRMOVE(p - 1, p); // remove the '\'
5265 --p;
5266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 }
5268 else
5269 {
5270 eap->nextcmd = check_nextcmd(p);
5271 *p = NUL;
5272 break;
5273 }
5274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005276
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005277 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 del_trailing_spaces(eap->arg);
5279}
5280
5281/*
5282 * get + command from ex argument
5283 */
5284 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005285getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286{
5287 char_u *arg = *argp;
5288 char_u *command = NULL;
5289
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005290 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
5292 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005293 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 command = dollar_command;
5295 else
5296 {
5297 command = arg;
5298 arg = skip_cmd_arg(command, TRUE);
5299 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005300 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
5302
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005303 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 *argp = arg;
5305 }
5306 return command;
5307}
5308
5309/*
5310 * Find end of "+command" argument. Skip over "\ " and "\\".
5311 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005312 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005313skip_cmd_arg(
5314 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005315 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 while (*p && !vim_isspace(*p))
5318 {
5319 if (*p == '\\' && p[1] != NUL)
5320 {
5321 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005322 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 else
5324 ++p;
5325 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005326 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 }
5328 return p;
5329}
5330
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005331 int
5332get_bad_opt(char_u *p, exarg_T *eap)
5333{
5334 if (STRICMP(p, "keep") == 0)
5335 eap->bad_char = BAD_KEEP;
5336 else if (STRICMP(p, "drop") == 0)
5337 eap->bad_char = BAD_DROP;
5338 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5339 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005340 else
5341 return FAIL;
5342 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005343}
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345/*
5346 * Get "++opt=arg" argument.
5347 * Return FAIL or OK.
5348 */
5349 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005350getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351{
5352 char_u *arg = eap->arg + 2;
5353 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005354 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005357 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5359 {
5360 if (*arg == 'n')
5361 {
5362 arg += 2;
5363 eap->force_bin = FORCE_NOBIN;
5364 }
5365 else
5366 eap->force_bin = FORCE_BIN;
5367 if (!checkforcmd(&arg, "binary", 3))
5368 return FAIL;
5369 eap->arg = skipwhite(arg);
5370 return OK;
5371 }
5372
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005373 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005374 if (STRNCMP(arg, "edit", 4) == 0)
5375 {
5376 eap->read_edit = TRUE;
5377 eap->arg = skipwhite(arg + 4);
5378 return OK;
5379 }
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 if (STRNCMP(arg, "ff", 2) == 0)
5382 {
5383 arg += 2;
5384 pp = &eap->force_ff;
5385 }
5386 else if (STRNCMP(arg, "fileformat", 10) == 0)
5387 {
5388 arg += 10;
5389 pp = &eap->force_ff;
5390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 else if (STRNCMP(arg, "enc", 3) == 0)
5392 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005393 if (STRNCMP(arg, "encoding", 8) == 0)
5394 arg += 8;
5395 else
5396 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 pp = &eap->force_enc;
5398 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005399 else if (STRNCMP(arg, "bad", 3) == 0)
5400 {
5401 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005402 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404
5405 if (pp == NULL || *arg != '=')
5406 return FAIL;
5407
5408 ++arg;
5409 *pp = (int)(arg - eap->cmd);
5410 arg = skip_cmd_arg(arg, FALSE);
5411 eap->arg = skipwhite(arg);
5412 *arg = NUL;
5413
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 if (pp == &eap->force_ff)
5415 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5417 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005418 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005420 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005422 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5424 *p = TOLOWER_ASC(*p);
5425 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005426 else
5427 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005428 // Check ++bad= argument. Must be a single-byte character, "keep" or
5429 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005430 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005431 return FAIL;
5432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433
5434 return OK;
5435}
5436
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005438ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
5440 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005441 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 * directory for security reasons.
5443 */
5444 if (secure)
5445 {
5446 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005447 eap->errmsg =
5448 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 }
5450 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005451 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 else
5453 do_augroup(eap->arg, eap->forceit);
5454}
5455
5456/*
5457 * ":doautocmd": Apply the automatic commands to the current buffer.
5458 */
5459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005460ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005462 char_u *arg = eap->arg;
5463 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005464 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005465
Bram Moolenaar1610d052016-06-09 22:53:01 +02005466 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005467 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005468 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005469 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472/*
5473 * :[N]bunload[!] [N] [bufname] unload buffer
5474 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5475 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5476 */
5477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005478ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005480 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005481 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 eap->errmsg = do_bufdel(
5483 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5484 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5485 : DOBUF_UNLOAD, eap->arg,
5486 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5487}
5488
5489/*
5490 * :[N]buffer [N] to buffer N
5491 * :[N]sbuffer [N] to buffer N
5492 */
5493 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005494ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005496 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005497 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005499 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 else
5501 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005502 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5504 else
5505 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005506 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005507 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 }
5509}
5510
5511/*
5512 * :[N]bmodified [N] to next mod. buffer
5513 * :[N]sbmodified [N] to next mod. buffer
5514 */
5515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005516ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
5518 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005519 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005520 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521}
5522
5523/*
5524 * :[N]bnext [N] to next buffer
5525 * :[N]sbnext [N] split and to next buffer
5526 */
5527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005528ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005530 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005531 return;
5532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005534 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005535 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536}
5537
5538/*
5539 * :[N]bNext [N] to previous buffer
5540 * :[N]bprevious [N] to previous buffer
5541 * :[N]sbNext [N] split and to previous buffer
5542 * :[N]sbprevious [N] split and to previous buffer
5543 */
5544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005545ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005547 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005548 return;
5549
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005551 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005552 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553}
5554
5555/*
5556 * :brewind to first buffer
5557 * :bfirst to first buffer
5558 * :sbrewind split and to first buffer
5559 * :sbfirst split and to first buffer
5560 */
5561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005562ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005564 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005565 return;
5566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005568 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005569 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570}
5571
5572/*
5573 * :blast to last buffer
5574 * :sblast split and to last buffer
5575 */
5576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005579 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005580 return;
5581
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005583 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005584 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
Bram Moolenaar7a092242020-04-16 22:10:49 +02005587/*
5588 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005589 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005592ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005594 int comment_char = '"';
5595
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005596#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005597 if (in_vim9script())
5598 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005599#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005600 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005601}
5602
5603/*
5604 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5605 * to "cmd_start" or has a white space character before it.
5606 */
5607 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005608ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005609{
5610 int c = *cmd;
5611
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005612 if (c == NUL || c == '|' || c == '\n')
5613 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005614#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005615 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005616 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5617 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005618 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005619#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005620 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621}
5622
5623#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5624 || defined(PROTO)
5625/*
5626 * Return the next command, after the first '|' or '\n'.
5627 * Return NULL if not found.
5628 */
5629 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005630find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 while (*p != '|' && *p != '\n')
5633 {
5634 if (*p == NUL)
5635 return NULL;
5636 ++p;
5637 }
5638 return (p + 1);
5639}
5640#endif
5641
5642/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005643 * Check if *p is a separator between Ex commands, skipping over white space.
5644 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 */
5646 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005647check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005649 char_u *s = skipwhite(p);
5650
5651 if (*s == '|' || *s == '\n')
5652 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 else
5654 return NULL;
5655}
5656
5657/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005658 * If "eap->nextcmd" is not set, check for a next command at "p".
5659 */
5660 void
5661set_nextcmd(exarg_T *eap, char_u *arg)
5662{
5663 char_u *p = check_nextcmd(arg);
5664
5665 if (eap->nextcmd == NULL)
5666 eap->nextcmd = p;
5667 else if (p != NULL)
5668 // cannot use "| command" inside a {} block
5669 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5670}
5671
5672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 * - if there are more files to edit
5674 * - and this is the last window
5675 * - and forceit not used
5676 * - and not repeated twice on a row
5677 * return FAIL and give error message if 'message' TRUE
5678 * return OK otherwise
5679 */
5680 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005681check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005682 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005683 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684{
5685 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5686
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005687 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005688 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
5690 if (message)
5691 {
5692#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005693 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5694 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005696 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005698 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5699 NGETTEXT("%d more file to edit. Quit anyway?",
5700 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5702 return OK;
5703 return FAIL;
5704 }
5705#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005706 semsg(NGETTEXT(e_nr_more_file_to_edit,
5707 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005708 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
5710 return FAIL;
5711 }
5712 return OK;
5713}
5714
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715/*
5716 * Function given to ExpandGeneric() to obtain the list of command names.
5717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005719get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
5721 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005722 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 return cmdnames[idx].cmd_name;
5724}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005727ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
Bram Moolenaare6850792010-05-14 15:28:44 +02005729 if (*eap->arg == NUL)
5730 {
5731#ifdef FEAT_EVAL
5732 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5733 char_u *p = NULL;
5734
5735 if (expr != NULL)
5736 {
5737 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005738 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005739 --emsg_off;
5740 vim_free(expr);
5741 }
5742 if (p != NULL)
5743 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005744 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005745 vim_free(p);
5746 }
5747 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005748 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005749#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005750 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005751#endif
5752 }
5753 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005754 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005755
5756#ifdef FEAT_VTP
5757 else if (has_vtp_working())
5758 {
5759 // background color change requires clear + redraw
5760 update_screen(CLEAR);
5761 redrawcmd();
5762 }
5763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764}
5765
5766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005767ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768{
5769 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005770 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 do_highlight(eap->arg, eap->forceit, FALSE);
5772}
5773
5774
5775/*
5776 * Call this function if we thought we were going to exit, but we won't
5777 * (because of an error). May need to restore the terminal mode.
5778 */
5779 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005780not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
5782 exiting = FALSE;
5783 settmode(TMODE_RAW);
5784}
5785
Bram Moolenaar3552e742021-05-29 12:21:58 +02005786 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005787before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5788{
5789 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5790
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005791 // Bail out when autocommands closed the window.
5792 // Refuse to quit when the buffer in the last window is being closed (can
5793 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005794 if (!win_valid(wp)
5795 || curbuf_locked()
5796 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5797 return TRUE;
5798
5799 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5800 {
5801 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005802 // Refuse to quit when locked or when the window was closed or the
5803 // buffer in the last window is being closed (can only happen in
5804 // autocommands).
5805 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005806 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5807 return TRUE;
5808 }
5809
5810 return FALSE;
5811}
5812
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005814 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005815 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005816 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005818 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005819ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005821 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005822
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823#ifdef FEAT_CMDWIN
5824 if (cmdwin_type != 0)
5825 {
5826 cmdwin_result = Ctrl_C;
5827 return;
5828 }
5829#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005830 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005831 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005832 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005833 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005834 return;
5835 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005836 if (eap->addr_count > 0)
5837 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005838 int wnr = eap->line2;
5839
5840 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5841 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005842 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005843 }
5844 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005845 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005846
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005847 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005848 if (curbuf_locked())
5849 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005850
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005851 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005852 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005853 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854
5855#ifdef FEAT_NETBEANS_INTG
5856 netbeansForcedQuit = eap->forceit;
5857#endif
5858
5859 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005860 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 */
5862 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5863 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005864 if ((!buf_hide(wp->w_buffer)
5865 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005866 | (eap->forceit ? CCGD_FORCEIT : 0)
5867 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005869 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 {
5871 not_exiting();
5872 }
5873 else
5874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005875 // quit last window
5876 // Note: only_one_window() returns true, even so a help window is
5877 // still open. In that case only quit, if no address has been
5878 // specified. Example:
5879 // :h|wincmd w|1q - don't quit
5880 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005881 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005883 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005884#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005886#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005887 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005888 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 }
5890}
5891
5892/*
5893 * ":cquit".
5894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005896ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005898 // this does not always pass on the exit code to the Manx compiler. why?
5899 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900}
5901
5902/*
5903 * ":qall": try to quit all windows
5904 */
5905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005906ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907{
5908# ifdef FEAT_CMDWIN
5909 if (cmdwin_type != 0)
5910 {
5911 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005912 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 else
5914 cmdwin_result = K_XF2;
5915 return;
5916 }
5917# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005918
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005919 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005920 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005921 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005922 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005923 return;
5924 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005925
5926 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005927 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005928
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005930 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 getout(0);
5932 not_exiting();
5933}
5934
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935/*
5936 * ":close": close current window, unless it is the last one
5937 */
5938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005939ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005941 win_T *win;
5942 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005943#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005945 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005947#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005948 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005949 {
5950 if (eap->addr_count == 0)
5951 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005952 else
5953 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005954 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005955 {
5956 winnr++;
5957 if (winnr == eap->line2)
5958 break;
5959 }
5960 if (win == NULL)
5961 win = lastwin;
5962 ex_win_close(eap->forceit, win, NULL);
5963 }
5964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965}
5966
Bram Moolenaar4033c552017-09-16 20:54:51 +02005967#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005968/*
5969 * ":pclose": Close any preview window.
5970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005972ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005973{
5974 win_T *win;
5975
Bram Moolenaar79648732019-07-18 21:43:07 +02005976 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005977 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005978 if (win->w_p_pvw)
5979 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005980 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005981 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005982 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005983# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005984 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005985 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005986# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005987}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005988#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005989
Bram Moolenaarf740b292006-02-16 22:11:02 +00005990/*
5991 * Close window "win" and take care of handling closing the last window for a
5992 * modified buffer.
5993 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005994 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005995ex_win_close(
5996 int forceit,
5997 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005998 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999{
6000 int need_hide;
6001 buf_T *buf = win->w_buffer;
6002
Bram Moolenaarcf844172020-06-26 19:44:06 +02006003 // Never close the autocommand window.
6004 if (win == aucmd_win)
6005 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006006 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006007 return;
6008 }
6009
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006011 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006013#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006014 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006016 bufref_T bufref;
6017
6018 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006020 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 return;
6022 need_hide = FALSE;
6023 }
6024 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006027 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 return;
6029 }
6030 }
6031
Bram Moolenaar4033c552017-09-16 20:54:51 +02006032#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006034#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006035
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006036 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006037 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006038 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006039 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006040 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041}
6042
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006044 * Handle the argument for a tabpage related ex command.
6045 * Returns a tabpage number.
6046 * When an error is encountered then eap->errmsg is set.
6047 */
6048 static int
6049get_tabpage_arg(exarg_T *eap)
6050{
6051 int tab_number;
6052 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6053
6054 if (eap->arg && *eap->arg != NUL)
6055 {
6056 char_u *p = eap->arg;
6057 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006058 int relative = 0; // argument +N/-N means: go to N places to the
6059 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006060
6061 if (*p == '-')
6062 {
6063 relative = -1;
6064 p++;
6065 }
6066 else if (*p == '+')
6067 {
6068 relative = 1;
6069 p++;
6070 }
6071
6072 p_save = p;
6073 tab_number = getdigits(&p);
6074
6075 if (relative == 0)
6076 {
6077 if (STRCMP(p, "$") == 0)
6078 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006079 else if (STRCMP(p, "#") == 0)
6080 if (valid_tabpage(lastused_tabpage))
6081 tab_number = tabpage_index(lastused_tabpage);
6082 else
6083 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006084 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006085 tab_number = 0;
6086 goto theend;
6087 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006088 else if (p == p_save || *p_save == '-' || *p != NUL
6089 || tab_number > LAST_TAB_NR)
6090 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006091 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006092 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006093 goto theend;
6094 }
6095 }
6096 else
6097 {
6098 if (*p_save == NUL)
6099 tab_number = 1;
6100 else if (p == p_save || *p_save == '-' || *p != NUL
6101 || tab_number == 0)
6102 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006104 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006105 goto theend;
6106 }
6107 tab_number = tab_number * relative + tabpage_index(curtab);
6108 if (!unaccept_arg0 && relative == -1)
6109 --tab_number;
6110 }
6111 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006112 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006113 }
6114 else if (eap->addr_count > 0)
6115 {
6116 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006117 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006118 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006119 tab_number = 0;
6120 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006121 else
6122 {
6123 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006124 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006125 {
6126 --tab_number;
6127 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006128 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006129 }
6130 }
6131 }
6132 else
6133 {
6134 switch (eap->cmdidx)
6135 {
6136 case CMD_tabnext:
6137 tab_number = tabpage_index(curtab) + 1;
6138 if (tab_number > LAST_TAB_NR)
6139 tab_number = 1;
6140 break;
6141 case CMD_tabmove:
6142 tab_number = LAST_TAB_NR;
6143 break;
6144 default:
6145 tab_number = tabpage_index(curtab);
6146 }
6147 }
6148
6149theend:
6150 return tab_number;
6151}
6152
6153/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006154 * ":tabclose": close current tab page, unless it is the last one.
6155 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 */
6157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006158ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006160 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006161 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006162
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006163# ifdef FEAT_CMDWIN
6164 if (cmdwin_type != 0)
6165 cmdwin_result = K_IGNORE;
6166 else
6167# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006168 if (first_tabpage->tp_next == NULL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00006169 emsg(_(e_cannot_close_last_tab_page));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006170 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006172 tab_number = get_tabpage_arg(eap);
6173 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006174 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006175 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006176 if (tp == NULL)
6177 {
6178 beep_flush();
6179 return;
6180 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006181 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006182 {
6183 tabpage_close_other(tp, eap->forceit);
6184 return;
6185 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006186 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006187 tabpage_close(eap->forceit);
6188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006190}
6191
6192/*
6193 * ":tabonly": close all tab pages except the current one
6194 */
6195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006196ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006197{
6198 tabpage_T *tp;
6199 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006200 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006201
6202# ifdef FEAT_CMDWIN
6203 if (cmdwin_type != 0)
6204 cmdwin_result = K_IGNORE;
6205 else
6206# endif
6207 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006208 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006209 else
6210 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006211 tab_number = get_tabpage_arg(eap);
6212 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006213 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006214 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006215 // Repeat this up to a 1000 times, because autocommands may
6216 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006217 for (done = 0; done < 1000; ++done)
6218 {
6219 FOR_ALL_TABPAGES(tp)
6220 if (tp->tp_topframe != topframe)
6221 {
6222 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006223 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006224 if (valid_tabpage(tp))
6225 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006226 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006227 break;
6228 }
6229 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006230 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006231 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006232 }
6233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
6236/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006237 * Close the current tab page.
6238 */
6239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006240tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006241{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006242 // First close all the windows but the current one. If that worked then
6243 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006244 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006245 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006246 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006247 ex_win_close(forceit, curwin, NULL);
6248# ifdef FEAT_GUI
6249 need_mouse_correct = TRUE;
6250# endif
6251}
6252
6253/*
6254 * Close tab page "tp", which is not the current tab page.
6255 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006256 * Also takes care of the tab pages line disappearing when closing the
6257 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006258 */
6259 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006260tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006261{
6262 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006263 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006264 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006265
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006266 // Limit to 1000 windows, autocommands may add a window while we close
6267 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006268 while (++done < 1000)
6269 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006270 wp = tp->tp_firstwin;
6271 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006272
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006273 // Autocommands may delete the tab page under our fingers and we may
6274 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006275 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006276 break;
6277 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006278
Bram Moolenaar29323592016-07-24 22:04:11 +02006279 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006280
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006281 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006282 if (h != tabline_height())
6283 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006284}
6285
6286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 * ":only".
6288 */
6289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006290ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006292 win_T *wp;
6293 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294# ifdef FEAT_GUI
6295 need_mouse_correct = TRUE;
6296# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006297 if (eap->addr_count > 0)
6298 {
6299 wnr = eap->line2;
6300 for (wp = firstwin; --wnr > 0; )
6301 {
6302 if (wp->w_next == NULL)
6303 break;
6304 else
6305 wp = wp->w_next;
6306 }
6307 win_goto(wp);
6308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 close_others(TRUE, eap->forceit);
6310}
6311
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006313ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006315 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006316 if (!eap->skip)
6317 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006318#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006319 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006320#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006321 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006322 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006323 else
6324 {
6325 int winnr = 0;
6326 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006327
Bram Moolenaar2256c992016-11-15 21:17:07 +01006328 FOR_ALL_WINDOWS(win)
6329 {
6330 winnr++;
6331 if (winnr == eap->line2)
6332 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006333 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006334 if (win == NULL)
6335 win = lastwin;
6336 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 }
6339}
6340
6341/*
6342 * ":stop" and ":suspend": Suspend Vim.
6343 */
dbivolaruab16ad32021-12-29 19:41:47 +00006344 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006345ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
6347 /*
6348 * Disallow suspending for "rvim".
6349 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006350 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 {
6352 if (!eap->forceit)
6353 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006354 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 windgoto((int)Rows - 1, 0);
6356 out_char('\n');
6357 out_flush();
6358 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006359 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006360 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006361 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006363 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006365 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006367 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006368 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 }
6370}
6371
6372/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006373 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 */
6375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006376ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006378#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006379 if (not_in_vim9(eap) == FAIL)
6380 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382#ifdef FEAT_CMDWIN
6383 if (cmdwin_type != 0)
6384 {
6385 cmdwin_result = Ctrl_C;
6386 return;
6387 }
6388#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006389 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006390 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006391 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006392 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006393 return;
6394 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006395
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006397 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 */
6399 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6400 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006401
6402 // Write the buffer for ":wq" or when it was changed.
6403 // Trigger QuitPre and ExitPre.
6404 // Check if we can exit now, after autocommands have changed things.
6405 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6406 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006408 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 {
6410 not_exiting();
6411 }
6412 else
6413 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006414 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006416 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417# ifdef FEAT_GUI
6418 need_mouse_correct = TRUE;
6419# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006420 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006421 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423}
6424
6425/*
6426 * ":print", ":list", ":number".
6427 */
6428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006429ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006431 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006432 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006433 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006435 for ( ;!got_int; ui_breakcheck())
6436 {
6437 print_line(eap->line1,
6438 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6439 || (eap->flags & EXFLAG_NR)),
6440 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6441 if (++eap->line1 > eap->line2)
6442 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006443 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006444 }
6445 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006446 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006447 curwin->w_cursor.lnum = eap->line2;
6448 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 }
6450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452}
6453
6454#ifdef FEAT_BYTEOFF
6455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006456ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
6458 goto_byte(eap->line2);
6459}
6460#endif
6461
6462/*
6463 * ":shell".
6464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006466ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
6468 do_shell(NULL, 0);
6469}
6470
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006471#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006473static int drop_busy = FALSE;
6474static int drop_filec;
6475static char_u **drop_filev = NULL;
6476static int drop_split;
6477static void (*drop_callback)(void *);
6478static void *drop_cookie;
6479
6480 static void
6481handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
6483 exarg_T ea;
6484 int save_msg_scroll = msg_scroll;
6485
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006486 // Setting the argument list may cause screen updates and being called
6487 // recursively. Avoid that by setting drop_busy.
6488 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006490 // Check whether the current buffer is changed. If so, we will need
6491 // to split the current window or data could be lost.
6492 // We don't need to check if the 'hidden' option is set, as in this
6493 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006494 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 {
6496 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006497 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 --emsg_off;
6499 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006500 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 if (win_split(0, 0) == FAIL)
6503 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006504 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006506 // When splitting the window, create a new alist. Otherwise the
6507 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 alist_unlink(curwin->w_alist);
6509 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 }
6511
6512 /*
6513 * Set up the new argument list.
6514 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006515 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516
6517 /*
6518 * Move to the first file.
6519 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006520 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006521 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 ea.cmd = (char_u *)"next";
6523 do_argfile(&ea, 0);
6524
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006525 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6526 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 need_start_insertmode = FALSE;
6528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006529 // Restore msg_scroll, otherwise a following command may cause scrolling
6530 // unexpectedly. The screen will be redrawn by the caller, thus
6531 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006533
6534 if (drop_callback != NULL)
6535 drop_callback(drop_cookie);
6536
6537 drop_filev = NULL;
6538 drop_busy = FALSE;
6539}
6540
6541/*
6542 * Handle a file drop. The code is here because a drop is *nearly* like an
6543 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006544 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006545 * code is very similar to :args and hence needs access to a lot of the static
6546 * functions in this file.
6547 *
6548 * The "filev" list must have been allocated using alloc(), as should each item
6549 * in the list. This function takes over responsibility for freeing the "filev"
6550 * list.
6551 */
6552 void
6553handle_drop(
6554 int filec, // the number of files dropped
6555 char_u **filev, // the list of files dropped
6556 int split, // force splitting the window
6557 void (*callback)(void *), // to be called after setting the argument
6558 // list
6559 void *cookie) // argument for "callback" (allocated)
6560{
6561 // Cannot handle recursive drops, finish the pending one.
6562 if (drop_busy)
6563 {
6564 FreeWild(filec, filev);
6565 vim_free(cookie);
6566 return;
6567 }
6568
6569 // When calling handle_drop() more than once in a row we only use the last
6570 // one.
6571 if (drop_filev != NULL)
6572 {
6573 FreeWild(drop_filec, drop_filev);
6574 vim_free(drop_cookie);
6575 }
6576
6577 drop_filec = filec;
6578 drop_filev = filev;
6579 drop_split = split;
6580 drop_callback = callback;
6581 drop_cookie = cookie;
6582
6583 // Postpone this when:
6584 // - editing the command line
6585 // - not possible to change the current buffer
6586 // - updating the screen
6587 // As it may change buffers and window structures that are in use and cause
6588 // freed memory to be used.
6589 if (text_locked() || curbuf_locked() || updating_screen)
6590 return;
6591
6592 handle_drop_internal();
6593}
6594
6595/*
6596 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6597 * reset: Handle a postponed drop.
6598 */
6599 void
6600handle_any_postponed_drop(void)
6601{
6602 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006603 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006604 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605}
6606#endif
6607
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 * ":preserve".
6610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006612ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006614 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 ml_preserve(curbuf, TRUE);
6616}
6617
6618/*
6619 * ":recover".
6620 */
6621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006622ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006624 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006626 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6627 | CCGD_MULTWIN
6628 | (eap->forceit ? CCGD_FORCEIT : 0)
6629 | CCGD_EXCMD)
6630
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 && (*eap->arg == NUL
6632 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006633 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 recoverymode = FALSE;
6635}
6636
6637/*
6638 * Command modifier used in a wrong way.
6639 */
6640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006641ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006643 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644}
6645
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646/*
6647 * :sview [+command] file split window with new file, read-only
6648 * :split [[+command] file] split window with current or new file
6649 * :vsplit [[+command] file] split window vertically with current or new file
6650 * :new [[+command] file] split window with no or new file
6651 * :vnew [[+command] file] split vertically window with no or new file
6652 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006653 *
6654 * :tabedit open new Tab page with empty window
6655 * :tabedit [+command] file open new Tab page and edit "file"
6656 * :tabnew [[+command] file] just like :tabedit
6657 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 */
6659 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006660ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006662 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006663#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006665#endif
6666#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006667 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006668 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006669#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006670 int use_tab = eap->cmdidx == CMD_tabedit
6671 || eap->cmdidx == CMD_tabfind
6672 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006674 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006675 return;
6676
Bram Moolenaar4033c552017-09-16 20:54:51 +02006677#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
Bram Moolenaar4033c552017-09-16 20:54:51 +02006681#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006682 // A ":split" in the quickfix window works like ":new". Don't want two
6683 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006684 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 {
6686 if (eap->cmdidx == CMD_split)
6687 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 if (eap->cmdidx == CMD_vsplit)
6689 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
Bram Moolenaar4033c552017-09-16 20:54:51 +02006693#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006694 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 {
6696 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6697 FNAME_MESS, TRUE, curbuf->b_ffname);
6698 if (fname == NULL)
6699 goto theend;
6700 eap->arg = fname;
6701 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006702# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006703 else
6704# endif
6705#endif
6706#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006707 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 && eap->cmdidx != CMD_new)
6710 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006711 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006712# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006713 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006714# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006715 au_has_group((char_u *)"FileExplorer"))
6716 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006717 // No browsing supported but we do have the file explorer:
6718 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006719 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006720 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006721 }
6722 else
6723 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006724 fname = do_browse(0, (char_u *)(use_tab
6725 ? _("Edit File in new tab page")
6726 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006728 if (fname == NULL)
6729 goto theend;
6730 eap->arg = fname;
6731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006733 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006736 /*
6737 * Either open new tab page or split the window.
6738 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006739 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006740 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006741 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006742 : eap->addr_count == 0 ? 0
6743 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006744 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006745 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006746
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006747 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006748 if (curwin != old_curwin
6749 && win_valid(old_curwin)
6750 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006751 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006752 old_curwin->w_alt_fnum = curbuf->b_fnum;
6753 }
6754 }
6755 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6757 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006758 // Reset 'scrollbind' when editing another file, but keep it when
6759 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006760 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006761 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 else
6763 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 do_exedit(eap, old_curwin);
6765 }
6766
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006767# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006768 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006771# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772theend:
6773 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006776
6777/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006778 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006779 */
6780 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006781tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006782{
6783 exarg_T ea;
6784
Bram Moolenaara80faa82020-04-12 19:37:17 +02006785 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006786 ea.cmdidx = CMD_tabnew;
6787 ea.cmd = (char_u *)"tabn";
6788 ea.arg = (char_u *)"";
6789 ex_splitview(&ea);
6790}
6791
6792/*
6793 * :tabnext command
6794 */
6795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006796ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006797{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006798 int tab_number;
6799
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006800 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006801 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006802 switch (eap->cmdidx)
6803 {
6804 case CMD_tabfirst:
6805 case CMD_tabrewind:
6806 goto_tabpage(1);
6807 break;
6808 case CMD_tablast:
6809 goto_tabpage(9999);
6810 break;
6811 case CMD_tabprevious:
6812 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006813 if (eap->arg && *eap->arg != NUL)
6814 {
6815 char_u *p = eap->arg;
6816 char_u *p_save = p;
6817
6818 tab_number = getdigits(&p);
6819 if (p == p_save || *p_save == '-' || *p != NUL
6820 || tab_number == 0)
6821 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006822 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006823 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006824 return;
6825 }
6826 }
6827 else
6828 {
6829 if (eap->addr_count == 0)
6830 tab_number = 1;
6831 else
6832 {
6833 tab_number = eap->line2;
6834 if (tab_number < 1)
6835 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006836 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006837 return;
6838 }
6839 }
6840 }
6841 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006842 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006843 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006844 tab_number = get_tabpage_arg(eap);
6845 if (eap->errmsg == NULL)
6846 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006847 break;
6848 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006849}
6850
6851/*
6852 * :tabmove command
6853 */
6854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006855ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006856{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006857 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006858
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006859 tab_number = get_tabpage_arg(eap);
6860 if (eap->errmsg == NULL)
6861 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006862}
6863
6864/*
6865 * :tabs command: List tabs and their contents.
6866 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006868ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006869{
6870 tabpage_T *tp;
6871 win_T *wp;
6872 int tabcount = 1;
6873
6874 msg_start();
6875 msg_scroll = TRUE;
6876 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6877 {
6878 msg_putchar('\n');
6879 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006880 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006881 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006882 ui_breakcheck();
6883
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006884 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006885 wp = firstwin;
6886 else
6887 wp = tp->tp_firstwin;
6888 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6889 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006890 msg_putchar('\n');
6891 msg_putchar(wp == curwin ? '>' : ' ');
6892 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006893 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6894 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006895 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006896 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006897 else
6898 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6899 IObuff, IOSIZE, TRUE);
6900 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006901 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006902 ui_breakcheck();
6903 }
6904 }
6905}
6906
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907/*
6908 * ":mode": Set screen mode.
6909 * If no argument given, just get the screen size and redraw.
6910 */
6911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006912ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
6914 if (*eap->arg == NUL)
6915 shell_resized();
6916 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006917 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918}
6919
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920/*
6921 * ":resize".
6922 * set, increment or decrement current window height
6923 */
6924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006925ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926{
6927 int n;
6928 win_T *wp = curwin;
6929
6930 if (eap->addr_count > 0)
6931 {
6932 n = eap->line2;
6933 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6934 ;
6935 }
6936
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006937# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006941 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 {
6943 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006944 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006945 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006947 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 }
6949 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 {
6951 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006952 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006953 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006955 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 }
6957}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
6959/*
6960 * ":find [+command] <file>" command.
6961 */
6962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006963ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
6965#ifdef FEAT_SEARCHPATH
6966 char_u *fname;
6967 int count;
6968
6969 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6970 TRUE, curbuf->b_ffname);
6971 if (eap->addr_count > 0)
6972 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006973 // Repeat finding the file "count" times. This matters when it
6974 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 count = eap->line2;
6976 while (fname != NULL && --count > 0)
6977 {
6978 vim_free(fname);
6979 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6980 FALSE, curbuf->b_ffname);
6981 }
6982 }
6983
6984 if (fname != NULL)
6985 {
6986 eap->arg = fname;
6987#endif
6988 do_exedit(eap, NULL);
6989#ifdef FEAT_SEARCHPATH
6990 vim_free(fname);
6991 }
6992#endif
6993}
6994
6995/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006996 * ":open" simulation: for now just work like ":visual".
6997 */
6998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006999ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007000{
7001 regmatch_T regmatch;
7002 char_u *p;
7003
Bram Moolenaar65088802021-03-13 21:07:21 +01007004#ifdef FEAT_EVAL
7005 if (not_in_vim9(eap) == FAIL)
7006 return;
7007#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007008 curwin->w_cursor.lnum = eap->line2;
7009 beginline(BL_SOL | BL_FIX);
7010 if (*eap->arg == '/')
7011 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007012 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007013 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007014 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007015 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007016 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007017 if (regmatch.regprog != NULL)
7018 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007019 // make a copy of the line, when searching for a mark it might be
7020 // flushed
7021 char_u *line = vim_strsave(ml_get_curline());
7022
Bram Moolenaardf177f62005-02-22 08:39:57 +00007023 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007024 if (vim_regexec(&regmatch, line, (colnr_T)0))
7025 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007026 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007027 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007028 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007029 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007030 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007031 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007032 eap->arg += STRLEN(eap->arg);
7033 }
7034 check_cursor();
7035
7036 eap->cmdidx = CMD_visual;
7037 do_exedit(eap, NULL);
7038}
7039
7040/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007041 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 */
7043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007044ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 do_exedit(eap, NULL);
7047}
7048
7049/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007050 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007053do_exedit(
7054 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007055 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
7057 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007059 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007061 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7062 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007063 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 /*
7065 * ":vi" command ends Ex mode.
7066 */
7067 if (exmode_active && (eap->cmdidx == CMD_visual
7068 || eap->cmdidx == CMD_view))
7069 {
7070 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007071 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007074 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007075 if (global_busy)
7076 {
7077 int rd = RedrawingDisabled;
7078 int nwr = no_wait_return;
7079 int ms = msg_scroll;
7080#ifdef FEAT_GUI
7081 int he = hold_gui_events;
7082#endif
7083
7084 if (eap->nextcmd != NULL)
7085 {
7086 stuffReadbuff(eap->nextcmd);
7087 eap->nextcmd = NULL;
7088 }
7089
7090 if (exmode_was != EXMODE_VIM)
7091 settmode(TMODE_RAW);
7092 RedrawingDisabled = 0;
7093 no_wait_return = 0;
7094 need_wait_return = FALSE;
7095 msg_scroll = 0;
7096#ifdef FEAT_GUI
7097 hold_gui_events = 0;
7098#endif
7099 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007100 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007101
7102 main_loop(FALSE, TRUE);
7103
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007104 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007105 RedrawingDisabled = rd;
7106 no_wait_return = nwr;
7107 msg_scroll = ms;
7108#ifdef FEAT_GUI
7109 hold_gui_events = he;
7110#endif
7111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 }
7115
7116 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007117 || eap->cmdidx == CMD_tabnew
7118 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007119 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007121 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 setpcmark();
7123 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007124 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7125 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007127 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 || *eap->arg != NUL
7129#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007130 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131#endif
7132 )
7133 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007134 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
7135 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007136 if (*eap->arg != NUL && curbuf_locked())
7137 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007138
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 n = readonlymode;
7140 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7141 readonlymode = TRUE;
7142 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007143 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7144 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007145 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7146 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7148 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007149 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7151 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7152 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007153 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007155 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007156 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007157 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7158 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007159 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007161 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 if (old_curwin != NULL)
7163 {
7164 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007165 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007167#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007168 cleanup_T cs;
7169
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007170 // Reset the error/interrupt/exception state here so that
7171 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007172 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007173#endif
7174#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007176#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007177 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007178
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007179#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007180 // Restore the error/interrupt/exception state if not
7181 // discarded by a new aborting error, interrupt, or
7182 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007183 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 }
7186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 }
7188 else if (readonlymode && curbuf->b_nwindows == 1)
7189 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007190 // When editing an already visited buffer, 'readonly' won't be set
7191 // but the previous value is kept. With ":view" and ":sview" we
7192 // want the file to be readonly, except when another window is
7193 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 curbuf->b_p_ro = TRUE;
7195 }
7196 readonlymode = n;
7197 }
7198 else
7199 {
7200 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007201 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 if (n != curwin->w_arg_idx_invalid)
7205 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 }
7207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 /*
7209 * if ":split file" worked, set alternate file name in old window to new
7210 * file
7211 */
7212 if (old_curwin != NULL
7213 && *eap->arg != NUL
7214 && curwin != old_curwin
7215 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007216 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007217 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
7220 ex_no_reprint = TRUE;
7221}
7222
7223#ifndef FEAT_GUI
7224/*
7225 * ":gui" and ":gvim" when there is no GUI.
7226 */
7227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007228ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007230 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231}
7232#endif
7233
Bram Moolenaar4f974752019-02-17 17:44:42 +01007234#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007236ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
7238 gui_make_tearoff(eap->arg);
7239}
7240#endif
7241
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007242#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7243 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007245ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007247# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7248 if (gui.in_use)
7249 gui_make_popup(eap->arg, eap->forceit);
7250# ifdef FEAT_TERM_POPUP_MENU
7251 else
7252# endif
7253# endif
7254# ifdef FEAT_TERM_POPUP_MENU
7255 pum_make_popup(eap->arg, eap->forceit);
7256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257}
7258#endif
7259
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007261ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262{
7263 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007264 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007266 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267}
7268
7269/*
7270 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7271 * offset.
7272 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007275ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007278 win_T *save_curwin = curwin;
7279 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 long topline;
7281 long y;
7282 linenr_T old_linenr = curwin->w_cursor.lnum;
7283
7284 setpcmark();
7285
7286 /*
7287 * determine max topline
7288 */
7289 if (curwin->w_p_scb)
7290 {
7291 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007292 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
7294 if (wp->w_p_scb && wp->w_buffer)
7295 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007296 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 if (topline > y)
7298 topline = y;
7299 }
7300 }
7301 if (topline < 1)
7302 topline = 1;
7303 }
7304 else
7305 {
7306 topline = 1;
7307 }
7308
7309
7310 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007311 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007313 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 {
7315 if (curwin->w_p_scb)
7316 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007317 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 y = topline - curwin->w_topline;
7319 if (y > 0)
7320 scrollup(y, TRUE);
7321 else
7322 scrolldown(-y, TRUE);
7323 curwin->w_scbind_pos = topline;
7324 redraw_later(VALID);
7325 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 }
7328 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007329 curwin = save_curwin;
7330 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 if (curwin->w_p_scb)
7332 {
7333 did_syncbind = TRUE;
7334 checkpcmark();
7335 if (old_linenr != curwin->w_cursor.lnum)
7336 {
7337 char_u ctrl_o[2];
7338
7339 ctrl_o[0] = Ctrl_O;
7340 ctrl_o[1] = 0;
7341 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7342 }
7343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344}
7345
7346
7347 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007348ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007350 int i;
7351 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7352 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007354 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 do_bang(1, eap, FALSE, FALSE, TRUE);
7356 else
7357 {
7358 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7359 return;
7360
7361#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007362 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 {
7364 char_u *browseFile;
7365
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007366 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 NULL, NULL, NULL, curbuf);
7368 if (browseFile != NULL)
7369 {
7370 i = readfile(browseFile, NULL,
7371 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7372 vim_free(browseFile);
7373 }
7374 else
7375 i = OK;
7376 }
7377 else
7378#endif
7379 if (*eap->arg == NUL)
7380 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007381 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 return;
7383 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7384 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7385 }
7386 else
7387 {
7388 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7389 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7390 i = readfile(eap->arg, NULL,
7391 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7392
7393 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007394 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007396#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 if (!aborting())
7398#endif
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007399 semsg(_(e_cant_open_file_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 }
7401 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007402 {
7403 if (empty && exmode_active)
7404 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007405 // Delete the empty line that remains. Historically ex does
7406 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007407 if (eap->line2 == 0)
7408 lnum = curbuf->b_ml.ml_line_count;
7409 else
7410 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007411 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007412 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007413 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007414 if (curwin->w_cursor.lnum > 1
7415 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007416 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007417 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007418 }
7419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 }
7423}
7424
Bram Moolenaarea408852005-06-25 22:49:46 +00007425static char_u *prev_dir = NULL;
7426
7427#if defined(EXITFREE) || defined(PROTO)
7428 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007429free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007430{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007431 VIM_CLEAR(prev_dir);
7432 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007433}
7434#endif
7435
Bram Moolenaarf4258302013-06-02 18:20:17 +02007436/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007437 * Get the previous directory for the given chdir scope.
7438 */
7439 static char_u *
7440get_prevdir(cdscope_T scope)
7441{
7442 if (scope == CDSCOPE_WINDOW)
7443 return curwin->w_prevdir;
7444 else if (scope == CDSCOPE_TABPAGE)
7445 return curtab->tp_prevdir;
7446 return prev_dir;
7447}
7448
7449/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007450 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007451 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7452 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007453 */
7454 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007455post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007456{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007457 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007458 // Clear tab local directory for both :cd and :tcd
7459 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007460 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007461 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007462 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007463 char_u *pdir = get_prevdir(scope);
7464
7465 // If still in the global directory, need to remember current
7466 // directory as the global directory.
7467 if (globaldir == NULL && pdir != NULL)
7468 globaldir = vim_strsave(pdir);
7469
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007470 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007471 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007472 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007473 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007474 curtab->tp_localdir = vim_strsave(NameBuff);
7475 else
7476 curwin->w_localdir = vim_strsave(NameBuff);
7477 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007478 }
7479 else
7480 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007481 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007482 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007483 }
7484
zeertzjq64be6aa2021-11-19 11:59:08 +00007485 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007486 shorten_fnames(TRUE);
7487}
7488
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007489/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007490 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7491 */
7492 void
7493trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7494{
7495#ifdef FEAT_EVAL
7496 dict_T *v_event;
7497 save_v_event_T save_v_event;
7498
7499 v_event = get_v_event(&save_v_event);
7500 (void)dict_add_string(v_event, "directory", new_dir);
7501 dict_set_items_ro(v_event);
7502#endif
7503 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7504#ifdef FEAT_EVAL
7505 restore_v_event(v_event, &save_v_event);
7506#endif
7507}
7508
7509/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007510 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007511 * chdir() function.
7512 * scope == CDSCOPE_WINDOW: changes the window-local directory
7513 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7514 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007515 * Returns TRUE if the directory is successfully changed.
7516 */
7517 int
7518changedir_func(
7519 char_u *new_dir,
7520 int forceit,
7521 cdscope_T scope)
7522{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007523 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007524 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007525 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007526 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007527 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007528
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007529 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007530 return FALSE;
7531
7532 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7533 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007534 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007535 return FALSE;
7536 }
7537
7538 // ":cd -": Change to previous directory
7539 if (STRCMP(new_dir, "-") == 0)
7540 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007541 pdir = get_prevdir(scope);
7542 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007543 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007544 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007545 return FALSE;
7546 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007547 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007548 }
7549
7550 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007551 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007552 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007553 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007554 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007555
Bakudankun29f3a452021-12-11 12:28:08 +00007556 // For UNIX ":cd" means: go to home directory.
7557 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007558#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007559 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007560#else
7561 if (*new_dir == NUL && p_cdh)
7562#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007563 {
7564 // use NameBuff for home directory name
7565# ifdef VMS
7566 char_u *p;
7567
7568 p = mch_getenv((char_u *)"SYS$LOGIN");
7569 if (p == NULL || *p == NUL) // empty is the same as not set
7570 NameBuff[0] = NUL;
7571 else
7572 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7573# else
7574 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7575# endif
7576 new_dir = NameBuff;
7577 }
zeertzjqf38aad82021-12-30 13:45:57 +00007578 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007579 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7580 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007581 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007582 if (scope == CDSCOPE_WINDOW)
7583 acmd_fname = (char_u *)"window";
7584 else if (scope == CDSCOPE_TABPAGE)
7585 acmd_fname = (char_u *)"tabpage";
7586 else
7587 acmd_fname = (char_u *)"global";
7588 trigger_DirChangedPre(acmd_fname, new_dir);
7589
7590 if (vim_chdir(new_dir))
7591 {
7592 emsg(_(e_command_failed));
7593 vim_free(pdir);
7594 return FALSE;
7595 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007596 }
zeertzjq73257142022-02-02 13:16:37 +00007597
7598 if (scope == CDSCOPE_WINDOW)
7599 pp = &curwin->w_prevdir;
7600 else if (scope == CDSCOPE_TABPAGE)
7601 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007602 else
zeertzjq73257142022-02-02 13:16:37 +00007603 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007604 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007605 *pp = pdir;
7606
7607 post_chdir(scope);
7608
7609 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007610 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007611 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007612 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007613}
Bram Moolenaarea408852005-06-25 22:49:46 +00007614
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007616 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007618 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007619ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007621 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622
7623 new_dir = eap->arg;
7624#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007625 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7626 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 ex_pwd(NULL);
7628 else
7629#endif
7630 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007631 cdscope_T scope = CDSCOPE_GLOBAL;
7632
7633 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7634 scope = CDSCOPE_WINDOW;
7635 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7636 scope = CDSCOPE_TABPAGE;
7637
7638 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007639 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007640 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007641 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 ex_pwd(eap);
7643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 }
7645}
7646
7647/*
7648 * ":pwd".
7649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007651ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652{
7653 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7654 {
7655#ifdef BACKSLASH_IN_FILENAME
7656 slash_adjust(NameBuff);
7657#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007658 if (p_verbose > 0)
7659 {
7660 char *context = "global";
7661
Bram Moolenaar05268152021-11-18 18:53:45 +00007662 if (last_chdir_reason != NULL)
7663 context = last_chdir_reason;
7664 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007665 context = "window";
7666 else if (curtab->tp_localdir != NULL)
7667 context = "tabpage";
7668 smsg("[%s] %s", context, (char *)NameBuff);
7669 }
7670 else
7671 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 }
7673 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007674 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675}
7676
7677/*
7678 * ":=".
7679 */
7680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007681ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007683 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007684 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685}
7686
7687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007688ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007690 int n;
7691 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692
7693 if (cursor_valid())
7694 {
7695 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7696 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007697 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007699
7700 len = eap->line2;
7701 switch (*eap->arg)
7702 {
7703 case 'm': break;
7704 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007705 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007706 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007707
7708 // Hide the cursor if invoked with !
7709 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710}
7711
7712/*
7713 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007714 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 */
7716 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007717do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
Bram Moolenaar52953192019-08-16 10:27:13 +02007719 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007720 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007721# ifdef ELAPSED_FUNC
7722 elapsed_T start_tv;
7723
7724 // Remember at what time we started, so that we know how much longer we
7725 // should wait after waiting for a bit.
7726 ELAPSED_INIT(start_tv);
7727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007729 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007730 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007731 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007732 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007733
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007734 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007735 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007737 wait_now = msec - done > 1000L ? 1000L : msec - done;
7738#ifdef FEAT_TIMERS
7739 {
7740 long due_time = check_due_timer();
7741
7742 if (due_time > 0 && due_time < wait_now)
7743 wait_now = due_time;
7744 }
7745#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007746#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007747 if (has_any_channel() && wait_now > 20L)
7748 wait_now = 20L;
7749#endif
7750#ifdef FEAT_SOUND
7751 if (has_any_sound_callback() && wait_now > 20L)
7752 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007753#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007754 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007755
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007756#ifdef FEAT_JOB_CHANNEL
7757 if (has_any_channel())
7758 ui_breakcheck_force(TRUE);
7759 else
7760#endif
7761 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007762#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007763 // Process the netbeans and clientserver messages that may have been
7764 // received in the call to ui_breakcheck() when the GUI is in use. This
7765 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007766 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007767#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007768
7769# ifdef ELAPSED_FUNC
7770 // actual time passed
7771 done = ELAPSED_FUNC(start_tv);
7772# else
7773 // guestimate time passed (will actually be more)
7774 done += wait_now;
7775# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007777
7778 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7779 // input buffer, otherwise a following call to input() fails.
7780 if (got_int)
7781 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007782
7783 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007784 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785}
7786
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787/*
7788 * ":winsize" command (obsolete).
7789 */
7790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007791ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 int w, h;
7794 char_u *arg = eap->arg;
7795 char_u *p;
7796
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007797 if (!isdigit(*arg))
7798 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007799 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007800 return;
7801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 w = getdigits(&arg);
7803 arg = skipwhite(arg);
7804 p = arg;
7805 h = getdigits(&arg);
7806 if (*p != NUL && *arg == NUL)
7807 set_shellsize(w, h, TRUE);
7808 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007809 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810}
7811
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007813ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
7815 int xchar = NUL;
7816 char_u *p;
7817
7818 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7819 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007820 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 if (eap->arg[1] == NUL)
7822 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007823 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 return;
7825 }
7826 xchar = eap->arg[1];
7827 p = eap->arg + 2;
7828 }
7829 else
7830 p = eap->arg + 1;
7831
Bram Moolenaar63b91732021-08-05 20:40:03 +02007832 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007834 if (*p != NUL && *p != (
7835#ifdef FEAT_EVAL
7836 in_vim9script() ? '#' :
7837#endif
7838 '"')
7839 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007840 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007841 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007843 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007844 postponed_split_flags = cmdmod.cmod_split;
7845 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7847 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007848 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 }
7850}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851
Bram Moolenaar843ee412004-06-30 16:16:41 +00007852#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853/*
7854 * ":winpos".
7855 */
7856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007857ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859 int x, y;
7860 char_u *arg = eap->arg;
7861 char_u *p;
7862
7863 if (*arg == NUL)
7864 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007865# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007866# ifdef VIMDLL
7867 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7868 mch_get_winpos(&x, &y) != FAIL)
7869# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007871# else
7872 if (mch_get_winpos(&x, &y) != FAIL)
7873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {
7875 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007876 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 }
7878 else
7879# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007880 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 }
7882 else
7883 {
7884 x = getdigits(&arg);
7885 arg = skipwhite(arg);
7886 p = arg;
7887 y = getdigits(&arg);
7888 if (*p == NUL || *arg != NUL)
7889 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007890 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 return;
7892 }
7893# ifdef FEAT_GUI
7894 if (gui.in_use)
7895 gui_mch_set_winpos(x, y);
7896 else if (gui.starting)
7897 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007898 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 gui_win_x = x;
7900 gui_win_y = y;
7901 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007902# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 else
7904# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007905# endif
7906# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007907 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908# endif
7909# ifdef HAVE_TGETENT
7910 if (*T_CWP)
7911 term_set_winpos(x, y);
7912# endif
7913 }
7914}
7915#endif
7916
7917/*
7918 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7919 */
7920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007921ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922{
7923 oparg_T oa;
7924
7925 clear_oparg(&oa);
7926 oa.regname = eap->regname;
7927 oa.start.lnum = eap->line1;
7928 oa.end.lnum = eap->line2;
7929 oa.line_count = eap->line2 - eap->line1 + 1;
7930 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007932 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {
7934 setpcmark();
7935 curwin->w_cursor.lnum = eap->line1;
7936 beginline(BL_SOL | BL_FIX);
7937 }
7938
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007939 if (VIsual_active)
7940 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007941
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 switch (eap->cmdidx)
7943 {
7944 case CMD_delete:
7945 oa.op_type = OP_DELETE;
7946 op_delete(&oa);
7947 break;
7948
7949 case CMD_yank:
7950 oa.op_type = OP_YANK;
7951 (void)op_yank(&oa, FALSE, TRUE);
7952 break;
7953
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007954 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007955 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007957 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7958#else
7959 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007961 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 oa.op_type = OP_RSHIFT;
7963 else
7964 oa.op_type = OP_LSHIFT;
7965 op_shift(&oa, FALSE, eap->amount);
7966 break;
7967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007969 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970}
7971
7972/*
7973 * ":put".
7974 */
7975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007976ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007978 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 if (eap->line2 == 0)
7980 {
7981 eap->line2 = 1;
7982 eap->forceit = TRUE;
7983 }
7984 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00007985 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007986 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007987 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988}
7989
7990/*
7991 * Handle ":copy" and ":move".
7992 */
7993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007994ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995{
7996 long n;
7997
Bram Moolenaar491799b2020-08-01 19:23:43 +02007998#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007999 if (not_in_vim9(eap) == FAIL)
8000 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008001#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008002 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008003 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {
8005 eap->nextcmd = NULL;
8006 return;
8007 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008008 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009
8010 /*
8011 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8012 */
8013 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8014 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008015 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 return;
8017 }
8018
8019 if (eap->cmdidx == CMD_move)
8020 {
8021 if (do_move(eap->line1, eap->line2, n) == FAIL)
8022 return;
8023 }
8024 else
8025 ex_copy(eap->line1, eap->line2, n);
8026 u_clearline();
8027 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008028 ex_may_print(eap);
8029}
8030
8031/*
8032 * Print the current line if flags were given to the Ex command.
8033 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008034 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008035ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008036{
8037 if (eap->flags != 0)
8038 {
8039 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8040 (eap->flags & EXFLAG_LIST));
8041 ex_no_reprint = TRUE;
8042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043}
8044
8045/*
8046 * ":smagic" and ":snomagic".
8047 */
8048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008049ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008051 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008053 magic_overruled = eap->cmdidx == CMD_smagic
8054 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008055 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008056 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057}
8058
8059/*
8060 * ":join".
8061 */
8062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008063ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064{
8065 curwin->w_cursor.lnum = eap->line1;
8066 if (eap->line1 == eap->line2)
8067 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008068 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 return;
8070 if (eap->line2 == curbuf->b_ml.ml_line_count)
8071 {
8072 beep_flush();
8073 return;
8074 }
8075 ++eap->line2;
8076 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008077 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008079 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080}
8081
8082/*
8083 * ":[addr]@r" or ":[addr]*r": execute register
8084 */
8085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008086ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
8088 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008089 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090
8091 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008092 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093
8094#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008095 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096#endif
8097
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008098 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 c = *eap->arg;
8100 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8101 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008102 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008103 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8104 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 else
8109 {
8110 int save_efr = exec_from_reg;
8111
8112 exec_from_reg = TRUE;
8113
8114 /*
8115 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008116 * Continue until the stuff buffer is empty and all added characters
8117 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008119 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8121
8122 exec_from_reg = save_efr;
8123 }
8124}
8125
8126/*
8127 * ":!".
8128 */
8129 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008130ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131{
8132 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8133}
8134
8135/*
8136 * ":undo".
8137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008139ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008141 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008142 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008143 else
8144 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145}
8146
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008147#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008149ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008150{
8151 char_u hash[UNDO_HASH_SIZE];
8152
8153 u_compute_hash(hash);
8154 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8155}
8156
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008158ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008159{
8160 char_u hash[UNDO_HASH_SIZE];
8161
8162 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008163 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008164}
8165#endif
8166
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167/*
8168 * ":redo".
8169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008171ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172{
8173 u_redo(1);
8174}
8175
8176/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008177 * ":earlier" and ":later".
8178 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008180ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008181{
8182 long count = 0;
8183 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008184 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008185 char_u *p = eap->arg;
8186
8187 if (*p == NUL)
8188 count = 1;
8189 else if (isdigit(*p))
8190 {
8191 count = getdigits(&p);
8192 switch (*p)
8193 {
8194 case 's': ++p; sec = TRUE; break;
8195 case 'm': ++p; sec = TRUE; count *= 60; break;
8196 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008197 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8198 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008199 }
8200 }
8201
8202 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008203 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008204 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008205 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8206 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008207}
8208
8209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 * ":redir": start/stop redirection.
8211 */
8212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008213ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214{
8215 char *mode;
8216 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008217 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218
Bram Moolenaarba768492016-07-08 15:32:54 +02008219#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008220 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008221 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008222 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008223 return;
8224 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008225#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008226
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 if (STRICMP(eap->arg, "END") == 0)
8228 close_redir();
8229 else
8230 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008231 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008233 ++arg;
8234 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008236 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 mode = "a";
8238 }
8239 else
8240 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008241 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242
8243 close_redir();
8244
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008245 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008246 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 if (fname == NULL)
8248 return;
8249#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008250 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {
8252 char_u *browseFile;
8253
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008254 browseFile = do_browse(BROWSE_SAVE,
8255 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008256 fname, NULL, NULL,
8257 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008259 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 vim_free(fname);
8261 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008262 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 }
8264#endif
8265
8266 redir_fd = open_exfile(fname, eap->forceit, mode);
8267 vim_free(fname);
8268 }
8269#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008270 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008272 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008274 ++arg;
8275 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008277 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008278 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008280 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008282 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008283 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008284 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008285 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008287 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008288 if (*arg == '>')
8289 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008290 // Make register empty when not using @A-@Z and the
8291 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008292 if (*arg == NUL && !isupper(redir_reg))
8293 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008295 }
8296 if (*arg != NUL)
8297 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008298 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008299 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008300 }
8301 }
8302 else if (*arg == '=' && arg[1] == '>')
8303 {
8304 int append;
8305
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008306 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008307 close_redir();
8308 arg += 2;
8309
8310 if (*arg == '>')
8311 {
8312 ++arg;
8313 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 }
8315 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008316 append = FALSE;
8317
8318 if (var_redir_start(skipwhite(arg), append) == OK)
8319 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 }
8321#endif
8322
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008323 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008326 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008329 // Make sure redirection is not off. Can happen for cmdline completion
8330 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008331 if (redir_fd != NULL
8332#ifdef FEAT_EVAL
8333 || redir_reg || redir_vname
8334#endif
8335 )
8336 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337}
8338
8339/*
8340 * ":redraw": force redraw
8341 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008342 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008343ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344{
8345 int r = RedrawingDisabled;
8346 int p = p_lz;
8347
8348 RedrawingDisabled = 0;
8349 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008350 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008352 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 if (need_maketitle)
8354 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008355#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8356# ifdef VIMDLL
8357 if (!gui.in_use)
8358# endif
8359 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 RedrawingDisabled = r;
8362 p_lz = p;
8363
Bram Moolenaar5017c662022-04-07 18:06:08 +01008364 // After drawing the statusline screen_attr may still be set.
8365 screen_stop_highlight();
8366
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008367 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 msg_didout = FALSE;
8369 msg_col = 0;
8370
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008371 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008372 need_wait_return = FALSE;
8373
Bram Moolenaara653e532022-04-19 11:38:24 +01008374 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008375 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008376 redrawcmdline();
8377
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 out_flush();
8379}
8380
8381/*
8382 * ":redrawstatus": force redraw of status line(s)
8383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008385ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 int r = RedrawingDisabled;
8388 int p = p_lz;
8389
8390 RedrawingDisabled = 0;
8391 p_lz = FALSE;
8392 if (eap->forceit)
8393 status_redraw_all();
8394 else
8395 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008396 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 RedrawingDisabled = r;
8398 p_lz = p;
8399 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400}
8401
Bram Moolenaare12bab32019-01-08 22:02:56 +01008402/*
8403 * ":redrawtabline": force redraw of the tabline
8404 */
8405 static void
8406ex_redrawtabline(exarg_T *eap UNUSED)
8407{
8408 int r = RedrawingDisabled;
8409 int p = p_lz;
8410
8411 RedrawingDisabled = 0;
8412 p_lz = FALSE;
8413
8414 draw_tabline();
8415
8416 RedrawingDisabled = r;
8417 p_lz = p;
8418 out_flush();
8419}
8420
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008422close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423{
8424 if (redir_fd != NULL)
8425 {
8426 fclose(redir_fd);
8427 redir_fd = NULL;
8428 }
8429#ifdef FEAT_EVAL
8430 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008431 if (redir_vname)
8432 {
8433 var_redir_stop();
8434 redir_vname = 0;
8435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436#endif
8437}
8438
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008439#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008440 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008441vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008442{
8443 if (vim_mkdir(name, prot) != 0)
8444 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008445 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008446 return FAIL;
8447 }
8448 return OK;
8449}
8450#endif
8451
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452/*
8453 * Open a file for writing for an Ex command, with some checks.
8454 * Return file descriptor, or NULL on failure.
8455 */
8456 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008457open_exfile(
8458 char_u *fname,
8459 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008460 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
8462 FILE *fd;
8463
8464#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008465 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 if (mch_isdir(fname))
8467 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008468 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 return NULL;
8470 }
8471#endif
8472 if (!forceit && *mode != 'a' && vim_fexists(fname))
8473 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008474 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 return NULL;
8476 }
8477
8478 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008479 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480
8481 return fd;
8482}
8483
8484/*
8485 * ":mark" and ":k".
8486 */
8487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008488ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489{
8490 pos_T pos;
8491
Bram Moolenaar10b94212021-02-19 21:42:57 +01008492#ifdef FEAT_EVAL
8493 if (not_in_vim9(eap) == FAIL)
8494 return;
8495#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008496 if (*eap->arg == NUL) // No argument?
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008497 emsg(_(e_argument_required));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008498 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar74409f62022-01-01 15:58:22 +00008499 semsg(_(e_trailing_characters_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 else
8501 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008502 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 curwin->w_cursor.lnum = eap->line2;
8504 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008505 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaar6d057012021-12-31 18:49:43 +00008506 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008507 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 }
8509}
8510
8511/*
8512 * Update w_topline, w_leftcol and the cursor position.
8513 */
8514 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008515update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008517 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 update_topline();
8519 if (!curwin->w_p_wrap)
8520 validate_cursor();
8521 update_curswant();
8522}
8523
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008525 * Save the current State and go to Normal mode.
8526 * Return TRUE if the typeahead could be saved.
8527 */
8528 int
8529save_current_state(save_state_T *sst)
8530{
8531 sst->save_msg_scroll = msg_scroll;
8532 sst->save_restart_edit = restart_edit;
8533 sst->save_msg_didout = msg_didout;
8534 sst->save_State = State;
8535 sst->save_insertmode = p_im;
8536 sst->save_finish_op = finish_op;
8537 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008538 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008539 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008540
Bram Moolenaar4324d872020-12-01 20:12:24 +01008541 msg_scroll = FALSE; // no msg scrolling in Normal mode
8542 restart_edit = 0; // don't go to Insert mode
8543 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008544
Bram Moolenaara452b802020-12-01 21:47:59 +01008545 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008546 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008547
8548 /*
8549 * Save the current typeahead. This is required to allow using ":normal"
8550 * from an event handler and makes sure we don't hang when the argument
8551 * ends with half a command.
8552 */
8553 save_typeahead(&sst->tabuf);
8554 return sst->tabuf.typebuf_valid;
8555}
8556
8557 void
8558restore_current_state(save_state_T *sst)
8559{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008560 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008561 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008562
8563 msg_scroll = sst->save_msg_scroll;
8564 restart_edit = sst->save_restart_edit;
8565 p_im = sst->save_insertmode;
8566 finish_op = sst->save_finish_op;
8567 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008568 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008569 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008570 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008571 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008572
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008573 // Restore the state (needed when called from a function executed for
8574 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008575 State = sst->save_State;
8576#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008577 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008578#endif
8579}
8580
8581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 * ":normal[!] {commands}": Execute normal mode commands.
8583 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008584 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008585ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008587 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 char_u *arg = NULL;
8589 int l;
8590 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008592 if (ex_normal_lock > 0)
8593 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008594 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008595 return;
8596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 if (ex_normal_busy >= p_mmd)
8598 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008599 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 return;
8601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 /*
8604 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8605 * this for the K_SPECIAL leading byte, otherwise special keys will not
8606 * work.
8607 */
8608 if (has_mbyte)
8609 {
8610 int len = 0;
8611
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008612 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 for (p = eap->arg; *p != NUL; ++p)
8614 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008615#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008616 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008618#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008619 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008620 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008621#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 )
8625 len += 2;
8626 }
8627 if (len > 0)
8628 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008629 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 if (arg != NULL)
8631 {
8632 len = 0;
8633 for (p = eap->arg; *p != NUL; ++p)
8634 {
8635 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008636#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 if (*p == CSI)
8638 {
8639 arg[len++] = KS_EXTRA;
8640 arg[len++] = (int)KE_CSI;
8641 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008642#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008643 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 {
8645 arg[len++] = *++p;
8646 if (*p == K_SPECIAL)
8647 {
8648 arg[len++] = KS_SPECIAL;
8649 arg[len++] = KE_FILLER;
8650 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008651#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 else if (*p == CSI)
8653 {
8654 arg[len++] = KS_EXTRA;
8655 arg[len++] = (int)KE_CSI;
8656 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 }
8659 arg[len] = NUL;
8660 }
8661 }
8662 }
8663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008665 ++ex_normal_busy;
8666 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 {
8668 /*
8669 * Repeat the :normal command for each line in the range. When no
8670 * range given, execute it just once, without positioning the cursor
8671 * first.
8672 */
8673 do
8674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 if (eap->addr_count != 0)
8676 {
8677 curwin->w_cursor.lnum = eap->line1++;
8678 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008679 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 }
8681
Bram Moolenaar13505972019-01-24 15:04:48 +01008682 exec_normal_cmd(arg != NULL
8683 ? arg
8684 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 }
8686 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8687 }
8688
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008689 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 update_topline_cursor();
8691
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008692 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008694 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008695#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008696 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008697#endif
8698
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700}
8701
8702/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008703 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 */
8705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008706ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008708 if (eap->forceit)
8709 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008710 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008711 if (!curwin->w_cursor.lnum)
8712 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008713 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008714 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008715#ifdef FEAT_TERMINAL
8716 // Ignore this when running in an active terminal.
8717 if (term_job_running(curbuf->b_term))
8718 return;
8719#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008720
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008721 // Ignore the command when already in Insert mode. Inserting an
8722 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01008723 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00008724 return;
8725
Bram Moolenaar64969662005-12-14 21:59:55 +00008726 if (eap->cmdidx == CMD_startinsert)
8727 restart_edit = 'a';
8728 else if (eap->cmdidx == CMD_startreplace)
8729 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008731 restart_edit = 'V';
8732
8733 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008735 if (eap->cmdidx == CMD_startinsert)
8736 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008737 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008739
8740 if (VIsual_active)
8741 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742}
8743
8744/*
8745 * ":stopinsert"
8746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008748ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749{
8750 restart_edit = 0;
8751 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008752 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008755/*
8756 * Execute normal mode command "cmd".
8757 * "remap" can be REMAP_NONE or REMAP_YES.
8758 */
8759 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008760exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008761{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008762 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008763 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008764 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008765}
Bram Moolenaar25281632016-01-21 23:32:32 +01008766
Bram Moolenaar25281632016-01-21 23:32:32 +01008767/*
8768 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008769 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008770 */
8771 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008772exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008773{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008774 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008775 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008776
Bram Moolenaar905dd902019-04-07 14:21:47 +02008777 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8778 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008779 clear_oparg(&oa);
8780 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008781 while ((!stuff_empty()
8782 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008783 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008784 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008785 {
8786 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008787#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008788 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008789 && oa.op_type == OP_NOP && oa.regname == NUL
8790 && !VIsual_active)
8791 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008792 // If terminal_loop() returns OK we got a key that is handled
8793 // in Normal model. With FAIL we first need to position the
8794 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008795 if (terminal_loop(TRUE) == OK)
8796 normal_cmd(&oa, TRUE);
8797 }
8798 else
8799#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008800 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008801 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008802 }
8803}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008804
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805#ifdef FEAT_FIND_ID
8806 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008807ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808{
8809 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8810 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8811 (linenr_T)1, (linenr_T)MAXLNUM);
8812}
8813
Bram Moolenaar4033c552017-09-16 20:54:51 +02008814#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815/*
8816 * ":psearch"
8817 */
8818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008819ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 g_do_tagpreview = p_pvh;
8822 ex_findpat(eap);
8823 g_do_tagpreview = 0;
8824}
8825#endif
8826
8827 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008828ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829{
8830 int whole = TRUE;
8831 long n;
8832 char_u *p;
8833 int action;
8834
8835 switch (cmdnames[eap->cmdidx].cmd_name[2])
8836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008837 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8839 action = ACTION_GOTO;
8840 else
8841 action = ACTION_SHOW;
8842 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008843 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 action = ACTION_SHOW_ALL;
8845 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008846 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 action = ACTION_GOTO;
8848 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008849 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 action = ACTION_SPLIT;
8851 break;
8852 }
8853
8854 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008855 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 {
8857 n = getdigits(&eap->arg);
8858 eap->arg = skipwhite(eap->arg);
8859 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008860 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 {
8862 whole = FALSE;
8863 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008864 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 if (*p)
8866 {
8867 *p++ = NUL;
8868 p = skipwhite(p);
8869
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008870 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008871 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008872 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008874 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 }
8876 }
8877 if (!eap->skip)
8878 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8879 whole, !eap->forceit,
8880 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8881 n, action, eap->line1, eap->line2);
8882}
8883#endif
8884
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885
Bram Moolenaar4033c552017-09-16 20:54:51 +02008886#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887/*
8888 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8889 */
8890 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008891ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008893 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8895}
8896
8897/*
8898 * ":pedit"
8899 */
8900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008901ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
8903 win_T *curwin_save = curwin;
8904
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008905 if (ERROR_IF_ANY_POPUP_WINDOW)
8906 return;
8907
Bram Moolenaar026587b2019-08-17 15:08:00 +02008908 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008910 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008911
Bram Moolenaar026587b2019-08-17 15:08:00 +02008912 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008914
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 if (curwin != curwin_save && win_valid(curwin_save))
8916 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008917 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 validate_cursor();
8919 redraw_later(VALID);
8920 win_enter(curwin_save, TRUE);
8921 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008922# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008923 else if (WIN_IS_POPUP(curwin))
8924 {
8925 // can't keep focus in popup window
8926 win_enter(firstwin, TRUE);
8927 }
8928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 g_do_tagpreview = 0;
8930}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932
8933/*
8934 * ":stag", ":stselect" and ":stjump".
8935 */
8936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008937ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938{
8939 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008940 postponed_split_flags = cmdmod.cmod_split;
8941 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8943 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008944 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946
8947/*
8948 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8949 */
8950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008951ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952{
8953 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8954}
8955
8956 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008957ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
8959 int cmd;
8960
8961 switch (name[1])
8962 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008963 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008965 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008967 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008969 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008971 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008973 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008975 case 'f': // ":tfirst"
8976 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008978 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008980 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008982 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008984 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 return;
8986 }
8987#endif
8988 cmd = DT_TAG;
8989 break;
8990 }
8991
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008992 if (name[0] == 'l')
8993 {
8994#ifndef FEAT_QUICKFIX
8995 ex_ni(eap);
8996 return;
8997#else
8998 cmd = DT_LTAG;
8999#endif
9000 }
9001
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9003 eap->forceit, TRUE);
9004}
9005
9006/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009007 * Check "str" for starting with a special cmdline variable.
9008 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9009 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9010 */
9011 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009012find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009013{
9014 int len;
9015 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009016 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009017 "%",
9018#define SPEC_PERC 0
9019 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009020#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009021 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02009022#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009023 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02009024#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009025 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02009026#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009027 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009028#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009029 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009030#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009031 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02009032#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02009033 "<stack>", // call stack
9034#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01009035 "<script>", // script file name
9036#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009037 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01009038#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009039 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009040#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009041 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009042#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009043 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009044#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02009045 "<SID>", // script ID: <SNR>123_
9046#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009047#ifdef FEAT_CLIENTSERVER
9048 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02009049# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009050#endif
9051 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009052
K.Takataeeec2542021-06-02 13:28:16 +02009053 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009054 {
9055 len = (int)STRLEN(spec_str[i]);
9056 if (STRNCMP(src, spec_str[i], len) == 0)
9057 {
9058 *usedlen = len;
9059 return i;
9060 }
9061 }
9062 return -1;
9063}
9064
9065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 * Evaluate cmdline variables.
9067 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009068 * change "%" to curbuf->b_ffname
9069 * "#" to curwin->w_alt_fnum
9070 * "%%" to curwin->w_alt_fnum in Vim9 script
9071 * "<cword>" to word under the cursor
9072 * "<cWORD>" to WORD under the cursor
9073 * "<cexpr>" to C-expression under the cursor
9074 * "<cfile>" to path name under the cursor
9075 * "<sfile>" to sourced file name
9076 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009077 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009078 * "<slnum>" to sourced file line number
9079 * "<afile>" to file name for autocommand
9080 * "<abuf>" to buffer number for autocommand
9081 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 *
9083 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9084 * "" for error without a message) and NULL is returned.
9085 * Returns an allocated string if a valid match was found.
9086 * Returns NULL if no match was found. "usedlen" then still contains the
9087 * number of characters to skip.
9088 */
9089 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009090eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009091 char_u *src, // pointer into commandline
9092 char_u *srcstart, // beginning of valid memory for src
9093 int *usedlen, // characters after src that are used
9094 linenr_T *lnump, // line number for :e command, or NULL
9095 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009096 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009097 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009098 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099{
9100 int i;
9101 char_u *s;
9102 char_u *result;
9103 char_u *resultbuf = NULL;
9104 int resultlen;
9105 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009106 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009108 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111
9112 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009113 if (escaped != NULL)
9114 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115
9116 /*
9117 * Check if there is something to do.
9118 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009119 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009120 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 {
9122 *usedlen = 1;
9123 return NULL;
9124 }
9125
9126 /*
9127 * Skip when preceded with a backslash "\%" and "\#".
9128 * Note: In "\\%" the % is also not recognized!
9129 */
9130 if (src > srcstart && src[-1] == '\\')
9131 {
9132 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009133 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 return NULL;
9135 }
9136
9137 /*
9138 * word or WORD under cursor
9139 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009140 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9141 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009143 resultlen = find_ident_under_cursor(&result,
9144 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9145 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9146 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 if (resultlen == 0)
9148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009149 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 return NULL;
9151 }
9152 }
9153
9154 /*
9155 * '#': Alternate file name
9156 * '%': Current file name
9157 * File name under the cursor
9158 * File name for autocommand
9159 * and following modifiers
9160 */
9161 else
9162 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009163 int off = 0;
9164
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 switch (spec_idx)
9166 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009167 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009168#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009169 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009172 // '%': current file
9173 if (curbuf->b_fname == NULL)
9174 {
9175 result = (char_u *)"";
9176 valid = 0; // Must have ":p:h" to be valid
9177 }
9178 else
9179 {
9180 result = curbuf->b_fname;
9181 tilde_file = STRCMP(result, "~") == 0;
9182 }
9183 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009185#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009186 // "%%" alternate file
9187 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009188#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009189 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009190 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009191 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009193 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 result = arg_all();
9195 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009196 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009197 if (escaped != NULL)
9198 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 break;
9201 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009202 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009203 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009204 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009206 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009207 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009208 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009209 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009211 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009213 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009214 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009215 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009216 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009217 return NULL;
9218 }
9219#ifdef FEAT_EVAL
9220 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9221 (long)i);
9222 if (result == NULL)
9223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009224 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009225 return NULL;
9226 }
9227#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009228 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 }
9232 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009233 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009234 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9235 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009236 buf = buflist_findnr(i);
9237 if (buf == NULL)
9238 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009239 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009240 return NULL;
9241 }
9242 if (lnump != NULL)
9243 *lnump = ECMD_LAST;
9244 if (buf->b_fname == NULL)
9245 {
9246 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009247 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009248 }
9249 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009250 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009251 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009252 tilde_file = STRCMP(result, "~") == 0;
9253 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 break;
9256
9257#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009258 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009259 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 if (result == NULL)
9261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009262 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 return NULL;
9264 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009265 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 break;
9267#endif
9268
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009269 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009271 if (result != NULL && !autocmd_fname_full)
9272 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009273 // Still need to turn the fname into a full path. It is
9274 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009275 autocmd_fname_full = TRUE;
9276 result = FullName_save(autocmd_fname, FALSE);
9277 vim_free(autocmd_fname);
9278 autocmd_fname = result;
9279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 if (result == NULL)
9281 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009282 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 return NULL;
9284 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009285 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 break;
9287
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009288 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 if (autocmd_bufnr <= 0)
9290 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009291 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 return NULL;
9293 }
9294 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9295 result = strbuf;
9296 break;
9297
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009298 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 result = autocmd_match;
9300 if (result == NULL)
9301 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009302 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 return NULL;
9304 }
9305 break;
9306
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009307 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009308 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 if (result == NULL)
9310 {
LemonBoy6013d002022-04-09 21:42:10 +01009311 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9312 return NULL;
9313 }
9314 resultbuf = result; // remember allocated string
9315 break;
9316 case SPEC_STACK: // call stack
9317 result = estack_sfile(ESTACK_STACK);
9318 if (result == NULL)
9319 {
9320 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9321 return NULL;
9322 }
9323 resultbuf = result; // remember allocated string
9324 break;
9325 case SPEC_SCRIPT: // script file name
9326 result = estack_sfile(ESTACK_SCRIPT);
9327 if (result == NULL)
9328 {
9329 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 return NULL;
9331 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009332 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009334
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009335 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009336 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009337 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009338 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009339 return NULL;
9340 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009341 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009342 result = strbuf;
9343 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009344
9345#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009346 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009347 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009348 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009349 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009350 return NULL;
9351 }
9352 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009353 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009354 result = strbuf;
9355 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009356
Bram Moolenaar90944302020-08-01 20:45:11 +02009357 case SPEC_SID:
9358 if (current_sctx.sc_sid <= 0)
9359 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009360 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009361 return NULL;
9362 }
9363 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9364 result = strbuf;
9365 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009366#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009367
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009368#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009369 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009370 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9371 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 result = strbuf;
9373 break;
9374#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009375
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009376 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009377 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009378 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 }
9380
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009381 resultlen = (int)STRLEN(result); // length of new string
9382 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 {
9384 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 resultlen = (int)(s - result);
9387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 else if (!skip_mod)
9389 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009390 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 &resultlen);
9392 if (result == NULL)
9393 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009394 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 return NULL;
9396 }
9397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 }
9399
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009400 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009402 if (empty_is_error)
9403 {
9404 if (valid != VALID_HEAD + VALID_PATH)
9405 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9406 else
9407 *errormsg = _(e_evaluates_to_an_empty_string);
9408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 result = NULL;
9410 }
9411 else
9412 result = vim_strnsave(result, resultlen);
9413 vim_free(resultbuf);
9414 return result;
9415}
9416
9417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 * Expand the <sfile> string in "arg".
9419 *
9420 * Returns an allocated string, or NULL for any error.
9421 */
9422 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009423expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009425 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 int len;
9427 char_u *result;
9428 char_u *newres;
9429 char_u *repl;
9430 int srclen;
9431 char_u *p;
9432
9433 result = vim_strsave(arg);
9434 if (result == NULL)
9435 return NULL;
9436
9437 for (p = result; *p; )
9438 {
9439 if (STRNCMP(p, "<sfile>", 7) != 0)
9440 ++p;
9441 else
9442 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009443 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009444 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 if (errormsg != NULL)
9446 {
9447 if (*errormsg)
9448 emsg(errormsg);
9449 vim_free(result);
9450 return NULL;
9451 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009452 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 {
9454 p += srclen;
9455 continue;
9456 }
9457 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9458 newres = alloc(len);
9459 if (newres == NULL)
9460 {
9461 vim_free(repl);
9462 vim_free(result);
9463 return NULL;
9464 }
9465 mch_memmove(newres, result, (size_t)(p - result));
9466 STRCPY(newres + (p - result), repl);
9467 len = (int)STRLEN(newres);
9468 STRCAT(newres, p + srclen);
9469 vim_free(repl);
9470 vim_free(result);
9471 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009472 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 }
9474 }
9475
9476 return result;
9477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009480/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009481 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009482 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009485dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 if (fname == NULL)
9488 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009489 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490}
9491#endif
9492
9493/*
9494 * ":behave {mswin,xterm}"
9495 */
9496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009497ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
9499 if (STRCMP(eap->arg, "mswin") == 0)
9500 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009501 set_option_value_give_err((char_u *)"selection",
9502 0L, (char_u *)"exclusive", 0);
9503 set_option_value_give_err((char_u *)"selectmode",
9504 0L, (char_u *)"mouse,key", 0);
9505 set_option_value_give_err((char_u *)"mousemodel",
9506 0L, (char_u *)"popup", 0);
9507 set_option_value_give_err((char_u *)"keymodel",
9508 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 }
9510 else if (STRCMP(eap->arg, "xterm") == 0)
9511 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009512 set_option_value_give_err((char_u *)"selection",
9513 0L, (char_u *)"inclusive", 0);
9514 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9515 set_option_value_give_err((char_u *)"mousemodel",
9516 0L, (char_u *)"extend", 0);
9517 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 }
9519 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009520 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521}
9522
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523static int filetype_detect = FALSE;
9524static int filetype_plugin = FALSE;
9525static int filetype_indent = FALSE;
9526
9527/*
9528 * ":filetype [plugin] [indent] {on,off,detect}"
9529 * on: Load the filetype.vim file to install autocommands for file types.
9530 * off: Load the ftoff.vim file to remove all autocommands for file types.
9531 * plugin on: load filetype.vim and ftplugin.vim
9532 * plugin off: load ftplugof.vim
9533 * indent on: load filetype.vim and indent.vim
9534 * indent off: load indoff.vim
9535 */
9536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009537ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539 char_u *arg = eap->arg;
9540 int plugin = FALSE;
9541 int indent = FALSE;
9542
9543 if (*eap->arg == NUL)
9544 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009545 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009546 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 filetype_detect ? "ON" : "OFF",
9548 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9549 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9550 return;
9551 }
9552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009553 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 for (;;)
9555 {
9556 if (STRNCMP(arg, "plugin", 6) == 0)
9557 {
9558 plugin = TRUE;
9559 arg = skipwhite(arg + 6);
9560 continue;
9561 }
9562 if (STRNCMP(arg, "indent", 6) == 0)
9563 {
9564 indent = TRUE;
9565 arg = skipwhite(arg + 6);
9566 continue;
9567 }
9568 break;
9569 }
9570 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9571 {
9572 if (*arg == 'o' || !filetype_detect)
9573 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009574 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 filetype_detect = TRUE;
9576 if (plugin)
9577 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009578 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 filetype_plugin = TRUE;
9580 }
9581 if (indent)
9582 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009583 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 filetype_indent = TRUE;
9585 }
9586 }
9587 if (*arg == 'd')
9588 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009589 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009590 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 }
9592 }
9593 else if (STRCMP(arg, "off") == 0)
9594 {
9595 if (plugin || indent)
9596 {
9597 if (plugin)
9598 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009599 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 filetype_plugin = FALSE;
9601 }
9602 if (indent)
9603 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009604 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 filetype_indent = FALSE;
9606 }
9607 }
9608 else
9609 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009610 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 filetype_detect = FALSE;
9612 }
9613 }
9614 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009615 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616}
9617
9618/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009619 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 */
9621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009622ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009625 {
9626 char_u *arg = eap->arg;
9627
9628 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9629 arg += 9;
9630
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009631 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
Bram Moolenaar3e545692017-06-04 19:00:32 +02009632 if (arg != eap->arg)
9633 did_filetype = FALSE;
9634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009638ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639{
9640#ifdef FEAT_DIGRAPHS
9641 if (*eap->arg != NUL)
9642 putdigraph(eap->arg);
9643 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009644 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645#else
mityu61065042021-07-19 20:07:21 +02009646 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647#endif
9648}
9649
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009650#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9651 void
9652set_no_hlsearch(int flag)
9653{
9654 no_hlsearch = flag;
9655# ifdef FEAT_EVAL
9656 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9657# endif
9658}
9659
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660/*
9661 * ":nohlsearch"
9662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009664ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009666 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009667 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669#endif
9670
9671#ifdef FEAT_CRYPT
9672/*
9673 * ":X": Get crypt key
9674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009676ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009678 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009679 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680}
9681#endif
9682
9683#ifdef FEAT_FOLDING
9684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009685ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686{
9687 if (foldManualAllowed(TRUE))
9688 foldCreate(eap->line1, eap->line2);
9689}
9690
9691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009692ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693{
9694 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9695 eap->forceit, FALSE);
9696}
9697
9698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009699ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700{
9701 linenr_T lnum;
9702
Bram Moolenaar4facea32019-10-12 20:17:40 +02009703# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009704 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009705# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009706
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009707 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9709 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9710 ml_setmarked(lnum);
9711
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009712 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009714 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009715# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009716 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009717# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718}
9719#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009721#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009722/*
9723 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9724 * instead of a quickfix command.
9725 */
9726 int
9727is_loclist_cmd(int cmdidx)
9728{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009729 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009730 return FALSE;
9731 return cmdnames[cmdidx].cmd_name[0] == 'l';
9732}
9733#endif
9734
Bram Moolenaar4facea32019-10-12 20:17:40 +02009735#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009736 int
9737get_pressedreturn(void)
9738{
9739 return ex_pressedreturn;
9740}
9741
9742 void
9743set_pressedreturn(int val)
9744{
9745 ex_pressedreturn = val;
9746}
9747#endif