blob: ceb500dc131b38e0c08e2fdec732c2f050a50ba2 [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;
479 State = 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 Moolenaar227c58a2021-04-28 20:40:44 +02001207 if (!got_int && !did_throw && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001208 && ((getline_equal(fgetline, cookie, getsourceline)
1209 && !source_finished(fgetline, cookie))
1210 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 && !func_has_ended(real_cookie))))
1212 {
1213 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001214 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001216 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001218 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001220 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
1222
1223 /*
1224 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1225 * ":endtry" in a sourced file or executed function. If the try
1226 * conditional is in its finally clause, ignore anything pending.
1227 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001228 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 */
1230 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001231 {
1232 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1233
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001234 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001235 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001236 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1237 &cstack.cs_looplevel);
1238 }
1239 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 trylevel = initial_trylevel;
1241 }
1242
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001243 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1244 // lack was reported above and the error message is to be converted to an
1245 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001246 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 ? (char_u *)"endfunction" : (char_u *)NULL);
1248
1249 if (trylevel == 0)
1250 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001251 // Just in case did_throw got set but current_exception wasn't.
1252 if (current_exception == NULL)
1253 did_throw = FALSE;
1254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 /*
1256 * When an exception is being thrown out of the outermost try
1257 * conditional, discard the uncaught exception, disable the conversion
1258 * of interrupts or errors to exceptions, and ensure that no more
1259 * commands are executed.
1260 */
1261 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001262 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264 /*
1265 * On an interrupt or an aborting error not converted to an exception,
1266 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001267 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 * when force_abort is set and did_emsg unset in case of an interrupt
1269 * from a finally clause after an error.
1270 */
1271 else if (got_int || (did_emsg && force_abort))
1272 suppress_errthrow = TRUE;
1273 }
1274
1275 /*
1276 * The current cstack will be freed when do_cmdline() returns. An uncaught
1277 * exception will have to be rethrown in the previous cstack. If a function
1278 * has just returned or a script file was just finished and the previous
1279 * cstack belongs to the same function or, respectively, script file, it
1280 * will have to be checked for finally clauses to be executed due to the
1281 * ":return" or ":finish". This is done in do_one_cmd().
1282 */
1283 if (did_throw)
1284 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001285 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001287 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 && ex_nesting_level > func_level(real_cookie) + 1))
1289 {
1290 if (!did_throw)
1291 check_cstack = TRUE;
1292 }
1293 else
1294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001295 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001296 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 --ex_nesting_level;
1298 /*
1299 * Go to debug mode when returning from a function in which we are
1300 * single-stepping.
1301 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001302 if ((getline_equal(fgetline, cookie, getsourceline)
1303 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001305 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 ? (char_u *)_("End of sourced file")
1307 : (char_u *)_("End of function"));
1308 }
1309
1310 /*
1311 * Restore the exception environment (done after returning from the
1312 * debugger).
1313 */
1314 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001315 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
1317 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001318
1319 // Cleanup if "cs_emsg_silent_list" remains.
1320 if (cstack.cs_emsg_silent_list != NULL)
1321 {
1322 eslist_T *elem, *temp;
1323
1324 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1325 {
1326 temp = elem->next;
1327 vim_free(elem);
1328 }
1329 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001330#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
1332 /*
1333 * If there was too much output to fit on the command line, ask the user to
1334 * hit return before redrawing the screen. With the ":global" command we do
1335 * this only once after the command is finished.
1336 */
1337 if (did_inc)
1338 {
1339 --RedrawingDisabled;
1340 --no_wait_return;
1341 msg_scroll = FALSE;
1342
1343 /*
1344 * When just finished an ":if"-":else" which was typed, no need to
1345 * wait for hit-return. Also for an error situation.
1346 */
1347 if (retval == FAIL
1348#ifdef FEAT_EVAL
1349 || (did_endif && KeyTyped && !did_emsg)
1350#endif
1351 )
1352 {
1353 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001354 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 else if (need_wait_return)
1357 {
1358 /*
1359 * The msg_start() above clears msg_didout. The wait_return we do
1360 * here should not overwrite the command that may be shown before
1361 * doing that.
1362 */
1363 msg_didout |= msg_didout_before_start;
1364 wait_return(FALSE);
1365 }
1366 }
1367
Bram Moolenaar2a942252012-11-28 23:03:07 +01001368#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001369 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001370#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 /*
1372 * Reset if_level, in case a sourced script file contains more ":if" than
1373 * ":endif" (could be ":if x | foo | endif").
1374 */
1375 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001376#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 --call_depth;
1379 return retval;
1380}
1381
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001382#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001383/*
1384 * Handle when "did_throw" is set after executing commands.
1385 */
1386 void
1387handle_did_throw()
1388{
1389 char *p = NULL;
1390 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001391 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001392
1393 /*
1394 * If the uncaught exception is a user exception, report it as an
1395 * error. If it is an error exception, display the saved error
1396 * message now. For an interrupt exception, do nothing; the
1397 * interrupt message is given elsewhere.
1398 */
1399 switch (current_exception->type)
1400 {
1401 case ET_USER:
1402 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001403 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001404 current_exception->value);
1405 p = (char *)vim_strsave(IObuff);
1406 break;
1407 case ET_ERROR:
1408 messages = current_exception->messages;
1409 current_exception->messages = NULL;
1410 break;
1411 case ET_INTERRUPT:
1412 break;
1413 }
1414
1415 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1416 current_exception->throw_lnum);
1417 ESTACK_CHECK_SETUP
1418 current_exception->throw_name = NULL;
1419
1420 discard_current_exception(); // uses IObuff if 'verbose'
1421 suppress_errthrow = TRUE;
1422 force_abort = TRUE;
1423
1424 if (messages != NULL)
1425 {
1426 do
1427 {
1428 msglist_T *next = messages->next;
1429 int save_compiling = estack_compiling;
1430
1431 estack_compiling = messages->msg_compiling;
1432 emsg(messages->msg);
1433 vim_free(messages->msg);
1434 vim_free(messages->sfile);
1435 vim_free(messages);
1436 messages = next;
1437 estack_compiling = save_compiling;
1438 }
1439 while (messages != NULL);
1440 }
1441 else if (p != NULL)
1442 {
1443 emsg(p);
1444 vim_free(p);
1445 }
1446 vim_free(SOURCING_NAME);
1447 ESTACK_CHECK_NOW
1448 estack_pop();
1449}
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001452 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 */
1454 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001455get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001457 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 wcmd_T *wp;
1459 char_u *line;
1460
1461 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1462 {
1463 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001464 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001466 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 if (cp->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001468 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001470 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001471 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 ++cp->current_line;
1473
1474 return line;
1475 }
1476
1477 KeyTyped = FALSE;
1478 ++cp->current_line;
1479 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001480 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 return vim_strsave(wp->line);
1482}
1483
1484/*
1485 * Store a line in "gap" so that a ":while" loop can execute it again.
1486 */
1487 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001488store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 if (ga_grow(gap, 1) == FAIL)
1491 return FAIL;
1492 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001493 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 return OK;
1496}
1497
1498/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 */
1501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001502free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 while (gap->ga_len > 0)
1505 {
1506 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1507 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
1509}
1510#endif
1511
1512/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001513 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1514 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001517getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001518 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001519 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001520 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
1522#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001523 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001526 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1527 // function that's originally used to obtain the lines. This may be
1528 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001529 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001530 cp = (struct loop_cookie *)cookie;
1531 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
1533 gp = cp->getline;
1534 cp = cp->cookie;
1535 }
1536 return gp == func;
1537#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001538 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539#endif
1540}
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001543 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 * getline function. Otherwise return "cookie".
1545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001547getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001548 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001549 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550{
Bram Moolenaar13505972019-01-24 15:04:48 +01001551#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001552 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001553 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001555 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1556 // cookie that's originally used to obtain the lines. This may be nested
1557 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001558 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001559 cp = (struct loop_cookie *)cookie;
1560 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
1562 gp = cp->getline;
1563 cp = cp->cookie;
1564 }
1565 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001566#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
Bram Moolenaard5053d02020-06-28 15:51:16 +02001571#if defined(FEAT_EVAL) || defined(PROT)
1572/*
1573 * Get the next line source line without advancing.
1574 */
1575 char_u *
1576getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001577 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001578 void *cookie) // argument for fgetline()
1579{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001580 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001581 struct loop_cookie *cp;
1582 wcmd_T *wp;
1583
1584 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1585 // cookie that's originally used to obtain the lines. This may be nested
1586 // several levels.
1587 gp = fgetline;
1588 cp = (struct loop_cookie *)cookie;
1589 while (gp == get_loop_line)
1590 {
1591 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1592 {
1593 // executing lines a second time, use the stored copy
1594 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1595 return wp->line;
1596 }
1597 gp = cp->getline;
1598 cp = cp->cookie;
1599 }
1600 if (gp == getsourceline)
1601 return source_nextline(cp);
1602 return NULL;
1603}
1604#endif
1605
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001606
1607/*
1608 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1609 * ":bwipeout", etc.
1610 * Returns the buffer number.
1611 */
1612 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001613compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614{
1615 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001616 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617 int count = offset;
1618
1619 buf = firstbuf;
1620 while (buf->b_next != NULL && buf->b_fnum < lnum)
1621 buf = buf->b_next;
1622 while (count != 0)
1623 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001624 count += (offset < 0) ? 1 : -1;
1625 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1626 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001627 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001628 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001629 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001630 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001631 while (buf->b_ml.ml_mfp == NULL)
1632 {
1633 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1634 if (nextbuf == NULL)
1635 break;
1636 buf = nextbuf;
1637 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001638 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001639 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001640 if (addr_type == ADDR_LOADED_BUFFERS)
1641 while (buf->b_ml.ml_mfp == NULL)
1642 {
1643 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1644 if (nextbuf == NULL)
1645 break;
1646 buf = nextbuf;
1647 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001648 return buf->b_fnum;
1649}
1650
Bram Moolenaarb7316892019-05-01 18:08:42 +02001651/*
1652 * Return the window number of "win".
1653 * When "win" is NULL return the number of windows.
1654 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001655 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001656current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001657{
1658 win_T *wp;
1659 int nr = 0;
1660
Bram Moolenaar29323592016-07-24 22:04:11 +02001661 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001662 {
1663 ++nr;
1664 if (wp == win)
1665 break;
1666 }
1667 return nr;
1668}
1669
1670 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001671current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001672{
1673 tabpage_T *tp;
1674 int nr = 0;
1675
Bram Moolenaar29323592016-07-24 22:04:11 +02001676 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001677 {
1678 ++nr;
1679 if (tp == tab)
1680 break;
1681 }
1682 return nr;
1683}
1684
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001685 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001686comment_start(char_u *p, int starts_with_colon UNUSED)
1687{
1688#ifdef FEAT_EVAL
1689 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001690 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001691#endif
1692 return *p == '"';
1693}
1694
Bram Moolenaarf240e182014-11-27 18:33:02 +01001695# define CURRENT_WIN_NR current_win_nr(curwin)
1696# define LAST_WIN_NR current_win_nr(NULL)
1697# define CURRENT_TAB_NR current_tab_nr(curtab)
1698# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700/*
1701 * Execute one Ex command.
1702 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001703 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1704 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 *
1706 * 1. skip comment lines and leading space
1707 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001708 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001709 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001710 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001711 * 6. parse arguments
1712 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001714 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 *
1716 * This function may be called recursively!
1717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001719do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001720 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001721 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001723 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001725 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001726 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001728 char_u *p;
1729 linenr_T lnum;
1730 long n;
1731 char *errormsg = NULL; // error message
1732 char_u *after_modifier = NULL;
1733 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001734 cmdmod_T save_cmdmod;
1735 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001736 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001737 int ni; // set when Not Implemented
1738 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001739 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001740#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001741 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001742 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001743 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001744#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001745 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaara80faa82020-04-12 19:37:17 +02001747 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 ea.line1 = 1;
1749 ea.line2 = 1;
1750#ifdef FEAT_EVAL
1751 ++ex_nesting_level;
1752#endif
1753
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001754 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (quitmore
1756#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001757 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001759#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001760 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001761 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 --quitmore;
1763
1764 /*
1765 * Reset browse, confirm, etc.. They are restored when returning, for
1766 * recursive calls.
1767 */
1768 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001770 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001771 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1772 goto doend;
1773
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001774/*
1775 * 1. Skip comment lines and leading white space and colons.
1776 * 2. Handle command modifiers.
1777 */
1778 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001780 ea.cmdlinep = cmdlinep;
1781 ea.getline = fgetline;
1782 ea.cookie = cookie;
1783#ifdef FEAT_EVAL
1784 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001785 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001787 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001788 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001789 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001790#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001791 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001792#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001793 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795#ifdef FEAT_EVAL
1796 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1797 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1798#else
1799 ea.skip = (if_level > 0);
1800#endif
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001803 * 3. Skip over the range to find the command. Let "p" point to after it.
1804 *
1805 * We need the command to know what kind of range it uses.
1806 */
1807 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001808#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001809 // In Vim9 script a colon is required before the range. This may also be
1810 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001811 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001812 {
1813 may_have_range = FALSE;
1814 for (p = ea.cmd; p >= *cmdlinep; --p)
1815 {
1816 if (*p == ':')
1817 may_have_range = TRUE;
1818 if (p < ea.cmd && !VIM_ISWHITE(*p))
1819 break;
1820 }
1821 }
1822 else
1823 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001824 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001825#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001826 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827
1828#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001829 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001830 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001831 if (ea.cmd == cmd + 1 && *cmd == '$')
1832 // should be "$VAR = val"
1833 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001834 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001835 if (ea.cmdidx == CMD_SIZE)
1836 {
1837 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1838
1839 // If a ':' before the range is missing, give a clearer error
1840 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001841 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001842 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001843 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001844 goto doend;
1845 }
1846 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 else
1849#endif
1850 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001851
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001852#ifdef FEAT_EVAL
1853# ifdef FEAT_PROFILE
1854 // Count this line for profiling if skip is TRUE.
1855 if (do_profiling == PROF_YES
1856 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1857 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1858 {
1859 int skip = did_emsg || got_int || did_throw;
1860
1861 if (ea.cmdidx == CMD_catch)
1862 skip = !skip && !(cstack->cs_idx >= 0
1863 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1864 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1865 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1866 skip = skip || !(cstack->cs_idx >= 0
1867 && !(cstack->cs_flags[cstack->cs_idx]
1868 & (CSF_ACTIVE | CSF_TRUE)));
1869 else if (ea.cmdidx == CMD_finally)
1870 skip = FALSE;
1871 else if (ea.cmdidx != CMD_endif
1872 && ea.cmdidx != CMD_endfor
1873 && ea.cmdidx != CMD_endtry
1874 && ea.cmdidx != CMD_endwhile)
1875 skip = ea.skip;
1876
1877 if (!skip)
1878 {
1879 if (getline_equal(fgetline, cookie, get_func_line))
1880 func_line_exec(getline_cookie(fgetline, cookie));
1881 else if (getline_equal(fgetline, cookie, getsourceline))
1882 script_line_exec();
1883 }
1884 }
1885# endif
1886
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001887 // May go to debug mode. If this happens and the ">quit" debug command is
1888 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001889 dbg_check_breakpoint(&ea);
1890 if (!ea.skip && got_int)
1891 {
1892 ea.skip = TRUE;
1893 (void)do_intthrow(cstack);
1894 }
1895#endif
1896
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001897/*
1898 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 *
1900 * where 'addr' is:
1901 *
1902 * % (entire file)
1903 * $ [+-NUM]
1904 * 'x [+-NUM] (where x denotes a currently defined mark)
1905 * . [+-NUM]
1906 * [+-NUM]..
1907 * NUM
1908 *
1909 * The ea.cmd pointer is updated to point to the first character following the
1910 * range spec. If an initial address is found, but no second, the upper bound
1911 * is equal to the lower.
1912 */
1913
Bram Moolenaar25190db2019-05-04 15:05:28 +02001914 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001915 if (!IS_USER_CMDIDX(ea.cmdidx))
1916 {
1917 if (ea.cmdidx != CMD_SIZE)
1918 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1919 else
1920 ea.addr_type = ADDR_LINES;
1921
Bram Moolenaar25190db2019-05-04 15:05:28 +02001922 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001923 if (ea.cmdidx == CMD_wincmd && p != NULL)
1924 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001925#ifdef FEAT_QUICKFIX
1926 // :.cc in quickfix window uses line number
1927 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1928 ea.addr_type = ADDR_OTHER;
1929#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001930 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001931
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001932 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001933#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001934 if (!may_have_range)
1935 ea.line1 = ea.line2 = default_address(&ea);
1936 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001937#endif
1938 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1939 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001942 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 */
1944
1945 /*
1946 * Skip ':' and any white space
1947 */
1948 ea.cmd = skipwhite(ea.cmd);
1949 while (*ea.cmd == ':')
1950 ea.cmd = skipwhite(ea.cmd + 1);
1951
1952 /*
1953 * If we got a line, but no command, then go to the line.
1954 * If we find a '|' or '\n' we set ea.nextcmd.
1955 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001956 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1957 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 /*
1960 * strange vi behaviour:
1961 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001962 * ":3|..." prints line 3 (not in Vim9 script)
1963 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001965 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001967 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 goto doend;
1969 }
1970
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001971 // If this looks like an undefined user command and there are CmdUndefined
1972 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001973 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1974 && ASCII_ISUPPER(*ea.cmd)
1975 && has_cmdundefined())
1976 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001977 int ret;
1978
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001979 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001980 while (ASCII_ISALNUM(*p))
1981 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001982 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001983 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1984 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001985 // If the autocommands did something and didn't cause an error, try
1986 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001987 p = (ret
1988#ifdef FEAT_EVAL
1989 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001990#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001992 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (p == NULL)
1995 {
1996 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001997 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 goto doend;
1999 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002000 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002001 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2002 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 {
2004 errormsg = uc_fun_cmd();
2005 goto doend;
2006 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002007
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (ea.cmdidx == CMD_SIZE)
2009 {
2010 if (!ea.skip)
2011 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002012 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002014 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002015 // If the modifier was parsed OK the error must be in the
2016 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002017 if (after_modifier != NULL)
2018 append_command(after_modifier);
2019 else
2020 append_command(*cmdlinep);
2021 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002022 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002023 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025 goto doend;
2026 }
2027
Bram Moolenaar958636c2014-10-21 20:01:58 +02002028 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2029 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002030#ifdef HAVE_EX_SCRIPT_NI
2031 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2032#endif
2033 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
2035#ifndef FEAT_EVAL
2036 /*
2037 * When the expression evaluation is disabled, recognize the ":if" and
2038 * ":endif" commands and ignore everything in between it.
2039 */
2040 if (ea.cmdidx == CMD_if)
2041 ++if_level;
2042 if (if_level)
2043 {
2044 if (ea.cmdidx == CMD_endif)
2045 --if_level;
2046 goto doend;
2047 }
2048
2049#endif
2050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002051 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002052 if (*p == '!' && ea.cmdidx != CMD_substitute
2053 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
2055 ++p;
2056 ea.forceit = TRUE;
2057 }
2058 else
2059 ea.forceit = FALSE;
2060
2061/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002062 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002064 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002065 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
2067 if (!ea.skip)
2068 {
2069#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002070 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002072 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002073 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 goto doend;
2075 }
2076#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002077 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002078 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002079 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002080 goto doend;
2081 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002082 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002084 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002085 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 goto doend;
2087 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002088
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002089 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002091#ifdef FEAT_CMDWIN
2092 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2093 {
2094 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002095 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002096 goto doend;
2097 }
2098#endif
2099 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2100 {
2101 // Command not allowed when text is locked
2102 errormsg = _(get_text_locked_msg());
2103 goto doend;
2104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002106
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002107 // Disallow editing another buffer when "curbuf_lock" is set.
2108 // Do allow ":checktime" (it is postponed).
2109 // Do allow ":edit" (check for an argument later).
2110 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002111 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002112 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002113 && ea.cmdidx != CMD_edit
2114 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002115 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002116 && curbuf_locked())
2117 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002119 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002121 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 goto doend;
2123 }
2124 }
2125
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002126 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002128 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 goto doend;
2130 }
2131
2132 /*
2133 * Don't complain about the range if it is not used
2134 * (could happen if line_count is accidentally set to 0).
2135 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002136 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
2138 /*
2139 * If the range is backwards, ask for confirmation and, if given, swap
2140 * ea.line1 & ea.line2 so it's forwards again.
2141 * When global command is busy, don't ask, will fail below.
2142 */
2143 if (!global_busy && ea.line1 > ea.line2)
2144 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002145 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002147 if (sourcing || exmode_active)
2148 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002149 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002150 goto doend;
2151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (ask_yesno((char_u *)
2153 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002154 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 lnum = ea.line1;
2157 ea.line1 = ea.line2;
2158 ea.line2 = lnum;
2159 }
2160 if ((errormsg = invalid_range(&ea)) != NULL)
2161 goto doend;
2162 }
2163
Bram Moolenaarb7316892019-05-01 18:08:42 +02002164 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2165 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 ea.line2 = 1;
2167
2168 correct_range(&ea);
2169
2170#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002171 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002172 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002174 // Put the first line at the start of a closed fold, put the last line
2175 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 (void)hasFolding(ea.line1, &ea.line1, NULL);
2177 (void)hasFolding(ea.line2, NULL, &ea.line2);
2178 }
2179#endif
2180
2181#ifdef FEAT_QUICKFIX
2182 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002183 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 * option here, so things like % get expanded.
2185 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002186 p = replace_makeprg(&ea, p, cmdlinep);
2187 if (p == NULL)
2188 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189#endif
2190
2191 /*
2192 * Skip to start of argument.
2193 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2194 */
2195 if (ea.cmdidx == CMD_bang)
2196 ea.arg = p;
2197 else
2198 ea.arg = skipwhite(p);
2199
Bram Moolenaar379fb762018-08-30 15:58:28 +02002200 // ":file" cannot be run with an argument when "curbuf_lock" is set
2201 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2202 goto doend;
2203
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 /*
2205 * Check for "++opt=val" argument.
2206 * Must be first, allow ":w ++enc=utf8 !cmd"
2207 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002208 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2210 if (getargopt(&ea) == FAIL && !ni)
2211 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002212 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 goto doend;
2214 }
2215
2216 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2217 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002218 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002220 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002222 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 goto doend;
2224 }
2225 ea.arg = skipwhite(ea.arg + 1);
2226 ea.append = TRUE;
2227 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002228 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
2230 ++ea.arg;
2231 ea.usefilter = TRUE;
2232 }
2233 }
2234
2235 if (ea.cmdidx == CMD_read)
2236 {
2237 if (ea.forceit)
2238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002239 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 ea.forceit = FALSE;
2241 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002242 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 ++ea.arg;
2245 ea.usefilter = TRUE;
2246 }
2247 }
2248
2249 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2250 {
2251 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002252 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
2254 ++ea.arg;
2255 ++ea.amount;
2256 }
2257 ea.arg = skipwhite(ea.arg);
2258 }
2259
2260 /*
2261 * Check for "+command" argument, before checking for next command.
2262 * Don't do this for ":read !cmd" and ":write !cmd".
2263 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002264 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2266
2267 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002268 * For commands that do not use '|' inside their argument: Check for '|' to
2269 * separate commands and '"' or '#' to start comments.
2270 *
2271 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002272 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002273 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002274 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002276 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2277 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002278 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002279 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002280 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002281 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002282 || ea.cmdidx == CMD_global
2283 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002284 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002285#ifdef FEAT_EVAL
2286 || inside_block(&ea)
2287#endif
2288 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 for (p = ea.arg; *p; ++p)
2291 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 // Remove one backslash before a newline, so that it's possible to
2293 // pass a newline to the shell and also a newline that is preceded
2294 // with a backslash. This makes it impossible to end a shell
2295 // command in a backslash, but that doesn't appear useful.
2296 // Halving the number of backslashes is incompatible with previous
2297 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002299 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002300 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 ea.nextcmd = p + 1;
2303 *p = NUL;
2304 break;
2305 }
2306 }
2307 }
2308
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002309 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002310 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002312 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002313 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002315 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002316 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002317 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002319#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002320 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002321 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002323 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002324 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
2326#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002327 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2328 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002329 {
2330 ea.regname = *ea.arg++;
2331#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002332 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002333 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2334 {
kuuote08d7b1c2021-10-04 22:17:36 +01002335 if (!ea.skip)
2336 {
2337 set_expr_line(vim_strsave(ea.arg), &ea);
2338 did_set_expr_line = TRUE;
2339 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002340 ea.arg += STRLEN(ea.arg);
2341 }
2342#endif
2343 ea.arg = skipwhite(ea.arg);
2344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346
2347 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002348 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 * count, it's a buffer name.
2350 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002351 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002352 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002353 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002355 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002357 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002359 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 goto doend;
2361 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002362 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
2364 ea.line2 = n;
2365 if (ea.addr_count == 0)
2366 ea.addr_count = 1;
2367 }
2368 else
2369 {
2370 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002371 if (ea.line2 >= LONG_MAX - (n - 1))
2372 ea.line2 = LONG_MAX; // avoid overflow
2373 else
2374 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 ++ea.addr_count;
2376 /*
2377 * Be vi compatible: no error message for out of range.
2378 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002379 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 ea.line2 = curbuf->b_ml.ml_line_count;
2381 }
2382 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002383
2384 /*
2385 * Check for flags: 'l', 'p' and '#'.
2386 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002387 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002388 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002389 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2390 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002392 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002393 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 goto doend;
2395 }
2396
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002397 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002399 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 goto doend;
2401 }
2402
2403#ifdef FEAT_EVAL
2404 /*
2405 * Skip the command when it's not going to be executed.
2406 * The commands like :if, :endif, etc. always need to be executed.
2407 * Also make an exception for commands that handle a trailing command
2408 * themselves.
2409 */
2410 if (ea.skip)
2411 {
2412 switch (ea.cmdidx)
2413 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002414 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 case CMD_while:
2416 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002417 case CMD_for:
2418 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_if:
2420 case CMD_elseif:
2421 case CMD_else:
2422 case CMD_endif:
2423 case CMD_try:
2424 case CMD_catch:
2425 case CMD_finally:
2426 case CMD_endtry:
2427 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002428 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 break;
2430
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002431 // Commands that handle '|' themselves. Check: A command should
2432 // either have the EX_TRLBAR flag, appear in this list or appear in
2433 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 case CMD_aboveleft:
2435 case CMD_and:
2436 case CMD_belowright:
2437 case CMD_botright:
2438 case CMD_browse:
2439 case CMD_call:
2440 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002441 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 case CMD_delfunction:
2443 case CMD_djump:
2444 case CMD_dlist:
2445 case CMD_dsearch:
2446 case CMD_dsplit:
2447 case CMD_echo:
2448 case CMD_echoerr:
2449 case CMD_echomsg:
2450 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002451 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002453 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002454 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 case CMD_help:
2456 case CMD_hide:
2457 case CMD_ijump:
2458 case CMD_ilist:
2459 case CMD_isearch:
2460 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002461 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_keepjumps:
2463 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002464 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_leftabove:
2466 case CMD_let:
2467 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002468 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002469 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002471 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002472 case CMD_noautocmd:
2473 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 case CMD_perl:
2475 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002476 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002477 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002478 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_return:
2480 case CMD_rightbelow:
2481 case CMD_ruby:
2482 case CMD_silent:
2483 case CMD_smagic:
2484 case CMD_snomagic:
2485 case CMD_substitute:
2486 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002487 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 case CMD_tcl:
2489 case CMD_throw:
2490 case CMD_tilde:
2491 case CMD_topleft:
2492 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002493 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002494 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 case CMD_verbose:
2496 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002497 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 break;
2499
2500 default: goto doend;
2501 }
2502 }
2503#endif
2504
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002505 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2508 goto doend;
2509 }
2510
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 /*
2512 * Accept buffer name. Cannot be used at the same time with a buffer
2513 * number. Don't do this for a user command.
2514 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002515 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002516 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
2518 /*
2519 * :bdelete, :bwipeout and :bunload take several arguments, separated
2520 * by spaces: find next space (skipping over escaped characters).
2521 * The others take one argument: ignore trailing spaces.
2522 */
2523 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2524 || ea.cmdidx == CMD_bunload)
2525 p = skiptowhite_esc(ea.arg);
2526 else
2527 {
2528 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002529 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 --p;
2531 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002532 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002533 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002534 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 goto doend;
2536 ea.addr_count = 1;
2537 ea.arg = skipwhite(p);
2538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002540 // The :try command saves the emsg_silent flag, reset it here when
2541 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002542 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002543 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002544 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002545 if (emsg_silent < 0)
2546 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002547 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002548 }
2549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002551 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaar958636c2014-10-21 20:01:58 +02002554 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
2556 /*
2557 * Execute a user-defined command.
2558 */
2559 do_ucmd(&ea);
2560 }
2561 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
2563 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002564 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 */
2566 ea.errmsg = NULL;
2567 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2568 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002569 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571
2572#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002574 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002575 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002576 && current_sctx.sc_sid > 0
2577 && ea.cmdidx != CMD_endif
2578 && (cstack->cs_idx < 0
2579 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002580 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 /*
2583 * If the command just executed called do_cmdline(), any throw or ":return"
2584 * or ":finish" encountered there must also check the cstack of the still
2585 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2586 * exception, or reanimate a returned function or finished script file and
2587 * return or finish it again.
2588 */
2589 if (need_rethrow)
2590 do_throw(cstack);
2591 else if (check_cstack)
2592 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002593 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002595 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 && current_func_returned())
2597 do_return(&ea, TRUE, FALSE, NULL);
2598 }
2599 need_rethrow = check_cstack = FALSE;
2600#endif
2601
2602doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002603 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002606 curwin->w_cursor.col = 0;
2607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
2609 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2610 {
Bram Moolenaarbed34f02022-01-19 20:48:37 +00002611 if (sourcing || !KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002613 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002618 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620 emsg(errormsg);
2621 }
2622#ifdef FEAT_EVAL
2623 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002624 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2625 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002626
2627 if (did_set_expr_line)
2628 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629#endif
2630
Bram Moolenaare1004402020-10-24 20:49:43 +02002631 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002633 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002634 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002636 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 ea.nextcmd = NULL;
2638
2639#ifdef FEAT_EVAL
2640 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002641 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642#endif
2643
2644 return ea.nextcmd;
2645}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002647static char ex_error_buf[MSG_BUF_LEN];
2648
2649/*
2650 * Return an error message with argument included.
2651 * Uses a static buffer, only the last error will be kept.
2652 * "msg" will be translated, caller should use N_().
2653 */
2654 char *
2655ex_errmsg(char *msg, char_u *arg)
2656{
2657 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2658 return ex_error_buf;
2659}
2660
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002662 * Handle a range without a command.
2663 * Returns an error message on failure.
2664 */
2665 char *
2666ex_range_without_command(exarg_T *eap)
2667{
2668 char *errormsg = NULL;
2669
2670 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2671#ifdef FEAT_EVAL
2672 && !in_vim9script()
2673#endif
2674 )
2675 {
2676 eap->cmdidx = CMD_print;
2677 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2678 if ((errormsg = invalid_range(eap)) == NULL)
2679 {
2680 correct_range(eap);
2681 ex_print(eap);
2682 }
2683 }
2684 else if (eap->addr_count != 0)
2685 {
2686 if (eap->line2 > curbuf->b_ml.ml_line_count)
2687 {
2688 // With '-' in 'cpoptions' a line number past the file is an
2689 // error, otherwise put it at the end of the file.
2690 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2691 eap->line2 = -1;
2692 else
2693 eap->line2 = curbuf->b_ml.ml_line_count;
2694 }
2695
2696 if (eap->line2 < 0)
2697 errormsg = _(e_invalid_range);
2698 else
2699 {
2700 if (eap->line2 == 0)
2701 curwin->w_cursor.lnum = 1;
2702 else
2703 curwin->w_cursor.lnum = eap->line2;
2704 beginline(BL_SOL | BL_FIX);
2705 }
2706 }
2707 return errormsg;
2708}
2709
2710/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002711 * Check for an Ex command with optional tail.
2712 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002713 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002714 */
2715 static int
2716checkforcmd_opt(
2717 char_u **pp, // start of command
2718 char *cmd, // name of command
2719 int len, // required length
2720 int noparen)
2721{
2722 int i;
2723
2724 for (i = 0; cmd[i] != NUL; ++i)
2725 if (((char_u *)cmd)[i] != (*pp)[i])
2726 break;
Bram Moolenaar68854a82022-01-31 18:59:13 +00002727 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_'
2728 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002729 {
2730 *pp = skipwhite(*pp + i);
2731 return TRUE;
2732 }
2733 return FALSE;
2734}
2735
2736/*
2737 * Check for an Ex command with optional tail.
2738 * If there is a match advance "pp" to the argument and return TRUE.
2739 */
2740 int
2741checkforcmd(
2742 char_u **pp, // start of command
2743 char *cmd, // name of command
2744 int len) // required length
2745{
2746 return checkforcmd_opt(pp, cmd, len, FALSE);
2747}
2748
2749/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002750 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002751 * If there is a match advance "pp" to the argument and return TRUE.
2752 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002753 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002754checkforcmd_noparen(
2755 char_u **pp, // start of command
2756 char *cmd, // name of command
2757 int len) // required length
2758{
2759 return checkforcmd_opt(pp, cmd, len, TRUE);
2760}
2761
2762/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 * Parse and skip over command modifiers:
2764 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002765 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002766 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002767 * When "skip_only" is TRUE the global variables are not changed, except for
2768 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002769 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2770 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002771 * Call apply_cmdmod() to get the side effects of the modifiers:
2772 * - Increment "sandbox" for ":sandbox"
2773 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002774 * - set msg_silent for ":silent"
2775 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002776 * Return FAIL when the command is not to be executed.
2777 * May set "errormsg" to an error message.
2778 */
2779 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002780parse_command_modifiers(
2781 exarg_T *eap,
2782 char **errormsg,
2783 cmdmod_T *cmod,
2784 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002785{
Bram Moolenaar565d1272022-03-27 18:11:05 +01002786 char_u *cmd_start = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002787 char_u *p;
2788 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002789 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002790 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002791
Bram Moolenaare1004402020-10-24 20:49:43 +02002792 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002793 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002794
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002795 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2796 {
2797 // The automatically inserted Visual area range is skipped, so that
2798 // typing ":cmdmod cmd" in Visual mode works without having to move the
2799 // range to after the modififiers.
2800 eap->cmd += 5;
2801 cmd_start = eap->cmd;
2802 has_visual_range = TRUE;
2803 }
2804
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002805 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002806 for (;;)
2807 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002808 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002809 {
2810 if (*eap->cmd == ':')
2811 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002812 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002813 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002814
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002815 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 if (*eap->cmd == NUL && exmode_active
2817 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2818 || getline_equal(eap->getline, eap->cookie, getexline))
2819 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2820 {
2821 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002822 if (!skip_only)
2823 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002824 }
2825
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002826 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002827 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002828 {
2829 // a comment ends at a NL
2830 if (eap->nextcmd == NULL)
2831 {
2832 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2833 if (eap->nextcmd != NULL)
2834 ++eap->nextcmd;
2835 }
Bram Moolenaarbc510062022-02-14 21:19:04 +00002836 if (vim9script && has_cmdmod(cmod, FALSE))
2837 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002839 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 if (*eap->cmd == NUL)
2841 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002842 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002843 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002844 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002845 if (vim9script && has_cmdmod(cmod, FALSE))
2846 *errormsg = _(e_command_modifier_without_command);
2847 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002848 return FAIL;
2849 }
2850
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002851 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002852
2853 // In Vim9 script a variable can shadow a command modifier:
2854 // verbose = 123
2855 // verbose += 123
2856 // silent! verbose = func()
2857 // verbose.member = 2
2858 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002859 // But not:
2860 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002861 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002862 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002863 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002864
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002865 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002866 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002867 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002868 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2869 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002870 break;
2871 }
2872
Bram Moolenaareffed932018-08-14 13:38:17 +02002873 switch (*p)
2874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002875 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002876 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002878 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002879 continue;
2880
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002881 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002883 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 continue;
2885 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002886 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002887 {
2888#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002889 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002890#endif
2891 continue;
2892 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002893 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002895 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002896 continue;
2897
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002898 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002899 break;
2900#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002901 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002902#endif
2903 continue;
2904
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002905 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002906 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002907 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002908 continue;
2909 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002910 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002911 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002912 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002913 continue;
2914 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002915 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002916 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002917 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002918 continue;
2919 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002920 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002922 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002923 continue;
2924
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002925 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002926 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002927 char_u *reg_pat;
2928 char_u *nulp = NULL;
2929 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002930
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002931 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002932 || *p == NUL
2933 || (ends_excmd(*p)
2934#ifdef FEAT_EVAL
2935 // in ":filter #pat# cmd" # does not
2936 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002937 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002938#endif
2939 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002940 break;
2941 if (*p == '!')
2942 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002943 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002944 p = skipwhite(p + 1);
2945 if (*p == NUL || ends_excmd(*p))
2946 break;
2947 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002948#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002949 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002950 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002951 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002952#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002953 if (skip_only)
2954 p = skip_vimgrep_pat(p, NULL, NULL);
2955 else
2956 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002957 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2958 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002959 if (p == NULL || *p == NUL)
2960 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002961 if (!skip_only)
2962 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002963 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002964 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002965 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002966 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002967 // restore the character overwritten by NUL
2968 if (nulp != NULL)
2969 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002970 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002971 eap->cmd = p;
2972 continue;
2973 }
2974
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002975 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002976 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002977 || *p == NUL || ends_excmd(*p))
2978 break;
2979 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002980 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002981 continue;
2982
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002983 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002984 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002985 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002986 continue;
2987 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002988 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2989 {
2990 if (ends_excmd2(p, eap->cmd))
2991 {
2992 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02002993 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002994 return FAIL;
2995 }
2996 cmod->cmod_flags |= CMOD_LEGACY;
2997 continue;
2998 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002999
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003000 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003001 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003002 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003003 continue;
3004
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003005 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003006 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003007 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003008 continue;
3009 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003010 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003011 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003012 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003013 continue;
3014
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003015 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003016 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003017 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003018 continue;
3019
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003020 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003022 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003023 continue;
3024 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003025 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003026 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003027 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003028 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3029 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003030 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003031 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003032 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003033 }
3034 continue;
3035
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003036 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003037 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003038 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003039 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003040 long tabnr = get_address(eap, &eap->cmd,
3041 ADDR_TABS, eap->skip,
3042 skip_only, FALSE, 1);
3043 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003044 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003045 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003046 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003047 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3048 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003049 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003050 return FAIL;
3051 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003052 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003053 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003054 }
3055 eap->cmd = p;
3056 continue;
3057 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003058 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003059 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003060 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003061 continue;
3062
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003063 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003064 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003065 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003066 continue;
3067
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003068 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003069 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003070 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003071 continue;
3072 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003073 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003074 {
3075 if (ends_excmd2(p, eap->cmd))
3076 {
3077 *errormsg =
3078 _(e_vim9cmd_must_be_followed_by_command);
3079 return FAIL;
3080 }
3081 cmod->cmod_flags |= CMOD_VIM9CMD;
3082 continue;
3083 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003084 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003085 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003086 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003087 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003088 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar60895f32022-04-12 14:23:19 +01003089 if (cmod->cmod_verbose == 0)
3090 cmod->cmod_verbose = -1;
3091 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003092 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003093 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003094 eap->cmd = p;
3095 continue;
3096 }
3097 break;
3098 }
3099
Bram Moolenaar1501b632022-03-27 16:56:21 +01003100 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003101 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003102 if (eap->cmd > cmd_start)
3103 {
3104 // Move the '<,'> range to after the modifiers and insert a colon.
3105 // Since the modifiers have been parsed put the colon on top of the
3106 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3107 // Put eap->cmd after the colon.
3108 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3109 eap->cmd -= 5;
3110 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3111 }
3112 else
3113 // no modifiers, move the pointer back
3114 eap->cmd -= 5;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003115 }
3116
Bram Moolenaareffed932018-08-14 13:38:17 +02003117 return OK;
3118}
3119
3120/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003121 * Return TRUE if "cmod" has anything set.
3122 */
3123 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003124has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003125{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003126 return (cmod->cmod_flags != 0 && (!ignore_silent
3127 || (cmod->cmod_flags
3128 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003129 || cmod->cmod_split != 0
3130 || cmod->cmod_verbose != 0
3131 || cmod->cmod_tab != 0
3132 || cmod->cmod_filter_regmatch.regprog != NULL;
3133}
3134
Bram Moolenaar8991be22022-02-14 21:51:46 +00003135#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003136/*
3137 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3138 */
3139 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003140cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003141{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003142 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003143 {
3144 emsg(_(e_misplaced_command_modifier));
3145 return TRUE;
3146 }
3147 return FALSE;
3148}
Dominique Pelle748b3082022-01-08 12:41:16 +00003149#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003150
3151/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003152 * Apply the command modifiers. Saves current state in "cmdmod", call
3153 * undo_cmdmod() later.
3154 */
3155 void
3156apply_cmdmod(cmdmod_T *cmod)
3157{
3158#ifdef HAVE_SANDBOX
3159 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3160 {
3161 ++sandbox;
3162 cmod->cmod_did_sandbox = TRUE;
3163 }
3164#endif
Bram Moolenaar60895f32022-04-12 14:23:19 +01003165 if (cmod->cmod_verbose != 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003166 {
3167 if (cmod->cmod_verbose_save == 0)
3168 cmod->cmod_verbose_save = p_verbose + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003169 p_verbose = cmod->cmod_verbose < 0 ? 0 : cmod->cmod_verbose;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003170 }
3171
3172 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3173 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003174 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003175 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003176 cmod->cmod_save_msg_scroll = msg_scroll;
3177 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003178 if (cmod->cmod_flags & CMOD_SILENT)
3179 ++msg_silent;
3180 if (cmod->cmod_flags & CMOD_UNSILENT)
3181 msg_silent = 0;
3182
3183 if (cmod->cmod_flags & CMOD_ERRSILENT)
3184 {
3185 ++emsg_silent;
3186 ++cmod->cmod_did_esilent;
3187 }
3188
Bram Moolenaare1004402020-10-24 20:49:43 +02003189 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003190 {
3191 // Set 'eventignore' to "all".
3192 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003193 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003194 set_string_option_direct((char_u *)"ei", -1,
3195 (char_u *)"all", OPT_FREE, SID_NONE);
3196 }
3197}
3198
3199/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003200 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003201 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003202 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003203undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003204{
Bram Moolenaare1004402020-10-24 20:49:43 +02003205 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003206 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003207 p_verbose = cmod->cmod_verbose_save - 1;
3208 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003209 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003210
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003211#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003212 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003213 {
3214 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003215 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003216 }
3217#endif
3218
Bram Moolenaare1004402020-10-24 20:49:43 +02003219 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003221 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003222 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3223 OPT_FREE, SID_NONE);
3224 free_string_option(cmod->cmod_save_ei);
3225 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003226 }
3227
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003228 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003229
Bram Moolenaare1004402020-10-24 20:49:43 +02003230 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003231 {
3232 // messages could be enabled for a serious error, need to check if the
3233 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003234 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3235 msg_silent = cmod->cmod_save_msg_silent - 1;
3236 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003237 if (emsg_silent < 0)
3238 emsg_silent = 0;
3239 // Restore msg_scroll, it's set by file I/O commands, even when no
3240 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003241 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003242
3243 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3244 // somewhere in the line. Put it back in the first column.
3245 if (redirecting())
3246 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003247
Bram Moolenaare1004402020-10-24 20:49:43 +02003248 cmod->cmod_save_msg_silent = 0;
3249 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003250 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003251}
3252
3253/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003254 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003255 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003256 * Return FAIL and set "errormsg" or return OK.
3257 */
3258 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003259parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003260{
3261 int address_count = 1;
3262 linenr_T lnum;
3263
3264 // Repeat for all ',' or ';' separated addresses.
3265 for (;;)
3266 {
3267 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003268 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003269 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003270 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003271 eap->addr_count == 0, address_count++);
3272 if (eap->cmd == NULL) // error detected
3273 return FAIL;
3274 if (lnum == MAXLNUM)
3275 {
3276 if (*eap->cmd == '%') // '%' - all lines
3277 {
3278 ++eap->cmd;
3279 switch (eap->addr_type)
3280 {
3281 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003282 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003283 eap->line1 = 1;
3284 eap->line2 = curbuf->b_ml.ml_line_count;
3285 break;
3286 case ADDR_LOADED_BUFFERS:
3287 {
3288 buf_T *buf = firstbuf;
3289
3290 while (buf->b_next != NULL
3291 && buf->b_ml.ml_mfp == NULL)
3292 buf = buf->b_next;
3293 eap->line1 = buf->b_fnum;
3294 buf = lastbuf;
3295 while (buf->b_prev != NULL
3296 && buf->b_ml.ml_mfp == NULL)
3297 buf = buf->b_prev;
3298 eap->line2 = buf->b_fnum;
3299 break;
3300 }
3301 case ADDR_BUFFERS:
3302 eap->line1 = firstbuf->b_fnum;
3303 eap->line2 = lastbuf->b_fnum;
3304 break;
3305 case ADDR_WINDOWS:
3306 case ADDR_TABS:
3307 if (IS_USER_CMDIDX(eap->cmdidx))
3308 {
3309 eap->line1 = 1;
3310 eap->line2 = eap->addr_type == ADDR_WINDOWS
3311 ? LAST_WIN_NR : LAST_TAB_NR;
3312 }
3313 else
3314 {
3315 // there is no Vim command which uses '%' and
3316 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003317 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003318 return FAIL;
3319 }
3320 break;
3321 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003322 case ADDR_UNSIGNED:
3323 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003324 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003325 return FAIL;
3326 case ADDR_ARGUMENTS:
3327 if (ARGCOUNT == 0)
3328 eap->line1 = eap->line2 = 0;
3329 else
3330 {
3331 eap->line1 = 1;
3332 eap->line2 = ARGCOUNT;
3333 }
3334 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003335 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003336#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003337 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003338 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003339 if (eap->line2 == 0)
3340 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003341#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003342 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003343 case ADDR_NONE:
3344 // Will give an error later if a range is found.
3345 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003346 }
3347 ++eap->addr_count;
3348 }
3349 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3350 {
3351 pos_T *fp;
3352
3353 // '*' - visual area
3354 if (eap->addr_type != ADDR_LINES)
3355 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003356 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003357 return FAIL;
3358 }
3359
3360 ++eap->cmd;
3361 if (!eap->skip)
3362 {
3363 fp = getmark('<', FALSE);
3364 if (check_mark(fp) == FAIL)
3365 return FAIL;
3366 eap->line1 = fp->lnum;
3367 fp = getmark('>', FALSE);
3368 if (check_mark(fp) == FAIL)
3369 return FAIL;
3370 eap->line2 = fp->lnum;
3371 ++eap->addr_count;
3372 }
3373 }
3374 }
3375 else
3376 eap->line2 = lnum;
3377 eap->addr_count++;
3378
3379 if (*eap->cmd == ';')
3380 {
3381 if (!eap->skip)
3382 {
3383 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003384 // Don't leave the cursor on an illegal line or column, but do
3385 // accept zero as address, so 0;/PATTERN/ works correctly.
3386 if (eap->line2 > 0)
3387 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003388 }
3389 }
3390 else if (*eap->cmd != ',')
3391 break;
3392 ++eap->cmd;
3393 }
3394
3395 // One address given: set start and end lines.
3396 if (eap->addr_count == 1)
3397 {
3398 eap->line1 = eap->line2;
3399 // ... but only implicit: really no address given
3400 if (lnum == MAXLNUM)
3401 eap->addr_count = 0;
3402 }
3403 return OK;
3404}
3405
3406/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003407 * Append "cmd" to the error message in IObuff.
3408 * Takes care of limiting the length and handling 0xa0, which would be
3409 * invisible otherwise.
3410 */
3411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003412append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003413{
3414 char_u *s = cmd;
3415 char_u *d;
3416
3417 STRCAT(IObuff, ": ");
3418 d = IObuff + STRLEN(IObuff);
3419 while (*s != NUL && d - IObuff < IOSIZE - 7)
3420 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003421 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003422 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003423 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003424 STRCPY(d, "<a0>");
3425 d += 4;
3426 }
3427 else
3428 MB_COPY_CHAR(s, d);
3429 }
3430 *d = NUL;
3431}
3432
Dominique Pelle748b3082022-01-08 12:41:16 +00003433#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003434/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003435 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3436 * the name. Otherwise just return "start".
3437 */
3438 char_u *
3439skip_option_env_lead(char_u *start)
3440{
3441 char_u *name = start;
3442
3443 if (*start == '&')
3444 {
3445 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3446 name += 3;
3447 else
3448 name += 1;
3449 }
3450 else if (*start == '$')
3451 name += 1;
3452 return name;
3453}
Dominique Pelle748b3082022-01-08 12:41:16 +00003454#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003455
3456/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003457 * Return TRUE and set "*idx" if "p" points to a one letter command.
3458 * If not in Vim9 script:
3459 * - The 'k' command can directly be followed by any character.
3460 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3461 * but :sre[wind] is another command, as are :scr[iptnames],
3462 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3463 */
3464 static int
3465one_letter_cmd(char_u *p, cmdidx_T *idx)
3466{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003467 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003468 return FALSE;
3469 if (*p == 'k')
3470 {
3471 *idx = CMD_k;
3472 return TRUE;
3473 }
3474 if (p[0] == 's'
3475 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3476 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3477 || p[1] == 'g'
3478 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3479 || p[1] == 'I'
3480 || (p[1] == 'r' && p[2] != 'e')))
3481 {
3482 *idx = CMD_substitute;
3483 return TRUE;
3484 }
3485 return FALSE;
3486}
3487
3488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003490 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003491 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003492 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 *
3494 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3495 * assignment without "let". Sets eap->cmdidx to the command while returning
3496 * "eap->cmd".
3497 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 * Returns NULL for an ambiguous user command.
3499 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 char_u *
3501find_ex_command(
3502 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003503 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003504 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003505 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506{
3507 int len;
3508 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003509 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003510#ifndef FEAT_EVAL
3511 int vim9 = FALSE;
3512#else
3513 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 /*
3516 * Recognize a Vim9 script function/method call and assignment:
3517 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3518 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003519 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003520 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003522 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003524 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003525 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3526 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003528 int oplen;
3529 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003530 char_u *swp;
3531
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003532 if (*eap->cmd == '&'
3533 || *eap->cmd == '$'
3534 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003535 && (valid_yank_reg(eap->cmd[1], FALSE)
3536 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003537 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003538 if (*eap->cmd == '&')
3539 {
3540 p = eap->cmd + 1;
3541 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3542 p += 2;
3543 p = to_name_end(p, FALSE);
3544 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003545 else if (*eap->cmd == '$')
3546 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003547 else
3548 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003549 if (ends_excmd(*skipwhite(p)))
3550 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003551 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3552 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003553 eap->cmdidx = CMD_eval;
3554 return eap->cmd;
3555 }
3556 // "&option" can be followed by "->" or "=", check below
3557 }
3558
3559 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003561 if (
3562 // "(..." is an expression.
3563 // "funcname(" is always a function call.
3564 *p == '('
3565 || (p == eap->cmd
3566 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003567 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003568 *eap->cmd == '{'
3569 // "'string'->func()" is an expression.
3570 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003571 // '"string"->func()' is an expression.
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003572 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
3573 // '"string"->func()' is an expression.
Bram Moolenaar10409562020-07-29 20:00:38 +02003574 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003575 // "g:varname" is an expression.
3576 || eap->cmd[1] == ':'
3577 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003578 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003579 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003580 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003581 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3582 {
3583 // "{" by itself is the start of a block.
3584 eap->cmdidx = CMD_block;
3585 return eap->cmd + 1;
3586 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003587 eap->cmdidx = CMD_eval;
3588 return eap->cmd;
3589 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003590
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003591 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003592 // "varname[]" is an expression.
3593 *p == '['
3594 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003595 || (*p == '.'
3596 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3597 // g:[key] is an expression
3598 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003599 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003600 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003601
3602 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003603 // Skip over the whole thing, it can be:
3604 // name.member = val
3605 // name[a : b] = val
3606 // name[idx] = val
3607 // name[idx].member = val
3608 // etc.
3609 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003610 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003611 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003612 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003613 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003614 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003615 || (after[0] == '.' && after[1] == '.'
3616 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003617 eap->cmdidx = CMD_var;
3618 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003619 --emsg_silent;
3620 return eap->cmd;
3621 }
3622
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003623 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3624 // an assignment.
3625 // If there is no line break inside the "[...]" then "p" is
3626 // advanced to after the "]" by to_name_const_end(): check if a "="
3627 // follows.
3628 // If "[...]" has a line break "p" still points at the "[" and it
3629 // can't be an assignment.
3630 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003631 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003632 char_u *eq;
3633
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003634 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003635 if (p == eap->cmd && *p == '[')
3636 {
3637 int count = 0;
3638 int semicolon = FALSE;
3639
3640 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3641 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003642 eq = p;
3643 if (eq != NULL)
3644 {
3645 eq = skipwhite(eq);
3646 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3647 ++eq;
3648 }
3649 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003650 {
3651 eap->cmdidx = CMD_eval;
3652 return eap->cmd;
3653 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003654 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003655 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003656 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003657 return eap->cmd;
3658 }
3659 }
3660
3661 // Recognize an assignment if we recognize the variable name:
3662 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003663 // "@r = expr"
3664 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003665 // "var = expr" where "var" is a variable name or we are skipping
3666 // (variable declaration might have been skipped).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003667 oplen = assignment_len(skipwhite(p), &heredoc);
3668 if (oplen > 0)
3669 {
3670 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3671 || *eap->cmd == '&'
3672 || *eap->cmd == '$'
3673 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003674 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003675 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003676 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003677 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003678 return eap->cmd;
3679 }
3680 }
3681
3682 // Recognize using a type for a w:, b:, t: or g: variable:
3683 // "w:varname: number = 123".
3684 if (eap->cmd[1] == ':' && *p == ':')
3685 {
3686 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003687 return eap->cmd;
3688 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003689 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003690
Bram Moolenaar7b829262021-10-13 15:04:34 +01003691 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3692 // an expression. A global/substitute/list command needs to use a
3693 // longer name.
3694 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3695 {
3696 eap->cmdidx = CMD_eval;
3697 return eap->cmd;
3698 }
3699
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003700 // If it is an ID it might be a variable with an operator on the next
3701 // line, if the variable exists it can't be an Ex command.
3702 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003703 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003704 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3705 {
3706 eap->cmdidx = CMD_eval;
3707 return eap->cmd;
3708 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003709
3710 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003711 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3712 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003713 {
3714 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3715 return eap->cmd + 2;
3716 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 }
3718#endif
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 /*
3721 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 */
3723 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003724 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 ++p;
3727 }
3728 else
3729 {
3730 while (ASCII_ISALPHA(*p))
3731 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003732 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003733 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003735 while (ASCII_ISALNUM(*p))
3736 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 }
3738 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3739 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003740 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 ++p;
3742 while (ASCII_ISALPHA(*p))
3743 ++p;
3744 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003745
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003746 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003747 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 ++p;
3749 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003750 // The "d" command can directly be followed by 'l' or 'p' flag, when
3751 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003752 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003753 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003754 // Check for ":dl", ":dell", etc. to ":deletel": that's
3755 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003756 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003757 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003758 break;
3759 if (i == len - 1)
3760 {
3761 --len;
3762 if (p[-1] == 'l')
3763 eap->flags |= EXFLAG_LIST;
3764 else
3765 eap->flags |= EXFLAG_PRINT;
3766 }
3767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003769 if (ASCII_ISLOWER(eap->cmd[0]))
3770 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003771 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003772 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003773
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003774 if (command_count != (int)CMD_SIZE)
3775 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003776 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003777 getout(1);
3778 }
3779
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003780 // Use a precomputed index for fast look-up in cmdnames[]
3781 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003782 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3783 if (ASCII_ISLOWER(c2))
3784 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3785 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003786 else if (ASCII_ISUPPER(eap->cmd[0]))
3787 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003789 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
3791 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3792 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3793 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3794 (size_t)len) == 0)
3795 {
3796#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003797 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 *full = TRUE;
3799#endif
3800 break;
3801 }
3802
Bram Moolenaar204852a2022-03-05 12:56:44 +00003803 // :Print and :mode are not supported in Vim9 script.
3804 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003805 if (vim9 && eap->cmdidx != CMD_SIZE)
3806 {
3807 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3808 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003809 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003810 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003811 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003812 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003813 eap->cmdidx = CMD_SIZE;
3814 }
3815 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003816
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003817 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003818 // 'cpoptions'.
3819 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3820 p = eap->cmd;
3821
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003822 // Look for a user defined command as a last resort. Let ":Print" be
3823 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003824 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3825 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003827 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 while (ASCII_ISALNUM(*p))
3829 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003830 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003832 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 eap->cmdidx = CMD_SIZE;
3834 }
3835
Bram Moolenaar204852a2022-03-05 12:56:44 +00003836 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3837 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003838 eap->cmdidx = CMD_finally;
3839
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003840#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003841 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003842 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003843 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003844 && (eap->cmdidx < 0 ||
3845 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003846 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003847 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3848
3849 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003850 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003851 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003852 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003853#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 return p;
3856}
3857
3858#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003859static struct cmdmod
3860{
3861 char *name;
3862 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003863 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003864} cmdmods[] = {
3865 {"aboveleft", 3, FALSE},
3866 {"belowright", 3, FALSE},
3867 {"botright", 2, FALSE},
3868 {"browse", 3, FALSE},
3869 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003870 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003871 {"hide", 3, FALSE},
3872 {"keepalt", 5, FALSE},
3873 {"keepjumps", 5, FALSE},
3874 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003875 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003876 {"leftabove", 5, FALSE},
3877 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003878 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003879 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003880 {"rightbelow", 6, FALSE},
3881 {"sandbox", 3, FALSE},
3882 {"silent", 3, FALSE},
3883 {"tab", 3, TRUE},
3884 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003885 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003886 {"verbose", 4, TRUE},
3887 {"vertical", 4, FALSE},
3888};
3889
3890/*
3891 * Return length of a command modifier (including optional count).
3892 * Return zero when it's not a modifier.
3893 */
3894 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003895modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003896{
3897 int i, j;
3898 char_u *p = cmd;
3899
3900 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003901 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003902 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003903 {
3904 for (j = 0; p[j] != NUL; ++j)
3905 if (p[j] != cmdmods[i].name[j])
3906 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003907 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003908 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003909 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003910 }
3911 return 0;
3912}
3913
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914/*
3915 * Return > 0 if an Ex command "name" exists.
3916 * Return 2 if there is an exact match.
3917 * Return 3 if there is an ambiguous match.
3918 */
3919 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003920cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921{
3922 exarg_T ea;
3923 int full = FALSE;
3924 int i;
3925 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003926 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003928 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003929 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 {
3931 for (j = 0; name[j] != NUL; ++j)
3932 if (name[j] != cmdmods[i].name[j])
3933 break;
3934 if (name[j] == NUL && j >= cmdmods[i].minlen)
3935 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3936 }
3937
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003938 // Check built-in commands and user defined commands.
3939 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003940 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00003942 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003944 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003946 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3947 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003948 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003949 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3951}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003952
3953/*
3954 * "fullcommand" function
3955 */
3956 void
3957f_fullcommand(typval_T *argvars, typval_T *rettv)
3958{
3959 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003960 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003961 char_u *p;
3962
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003963 rettv->v_type = VAR_STRING;
3964 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003965
3966 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3967 return;
3968
3969 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003970 if (name == NULL)
3971 return;
3972
zeertzjqc024ed92022-01-04 16:22:52 +00003973 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003974 name++;
3975 name = skip_range(name, TRUE, NULL);
3976
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003977 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3978 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003979 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003980 p = find_ex_command(&ea, NULL, NULL, NULL);
3981 if (p == NULL || ea.cmdidx == CMD_SIZE)
3982 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003983 if (in_vim9script())
3984 {
3985 int res;
3986
3987 ++emsg_silent;
3988 res = not_in_vim9(&ea);
3989 --emsg_silent;
3990
3991 if (res == FAIL)
3992 return;
3993 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003994
3995 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02003996 ? get_user_command_name(ea.useridx, ea.cmdidx)
3997 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003998}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999#endif
4000
Bram Moolenaard0190392019-08-23 21:17:35 +02004001 cmdidx_T
4002excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003{
Bram Moolenaard0190392019-08-23 21:17:35 +02004004 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004006 if (!one_letter_cmd(cmd, &idx))
4007 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4008 idx = (cmdidx_T)((int)idx + 1))
4009 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4010 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
Bram Moolenaard0190392019-08-23 21:17:35 +02004012 return idx;
4013}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014
Bram Moolenaard0190392019-08-23 21:17:35 +02004015 long
4016excmd_get_argt(cmdidx_T idx)
4017{
4018 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019}
4020
4021/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004022 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 *
4024 * Backslashed delimiters after / or ? will be skipped, and commands will
4025 * not be expanded between /'s and ?'s or after "'".
4026 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004027 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 * Returns the "cmd" pointer advanced to beyond the range.
4029 */
4030 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004031skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004032 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004033 int skip_star, // skip "*" used for Visual range
4034 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004036 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004037 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004039 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004041 if (*cmd == '\\')
4042 {
4043 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4044 ++cmd;
4045 else
4046 break;
4047 }
4048 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004050 char_u *p = cmd;
4051
4052 // a quote is only valid at the start or after a separator
4053 while (p > cmd_start)
4054 {
4055 --p;
4056 if (!VIM_ISWHITE(*p))
4057 break;
4058 }
4059 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4060 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 if (*++cmd == NUL && ctx != NULL)
4062 *ctx = EXPAND_NOTHING;
4063 }
4064 else if (*cmd == '/' || *cmd == '?')
4065 {
4066 delim = *cmd++;
4067 while (*cmd != NUL && *cmd != delim)
4068 if (*cmd++ == '\\' && *cmd != NUL)
4069 ++cmd;
4070 if (*cmd == NUL && ctx != NULL)
4071 *ctx = EXPAND_NOTHING;
4072 }
4073 if (*cmd != NUL)
4074 ++cmd;
4075 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004076
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004077 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004078 while (*cmd == ':')
4079 cmd = skipwhite(cmd + 1);
4080
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004081 // Skip "*" used for Visual range.
4082 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4083 cmd = skipwhite(cmd + 1);
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 return cmd;
4086}
4087
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004088 static void
4089addr_error(cmd_addr_T addr_type)
4090{
4091 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004092 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004093 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004094 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004095}
4096
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004098 * Return the default address for an address type.
4099 */
4100 static linenr_T
4101default_address(exarg_T *eap)
4102{
4103 linenr_T lnum = 0;
4104
4105 switch (eap->addr_type)
4106 {
4107 case ADDR_LINES:
4108 case ADDR_OTHER:
4109 // Default is the cursor line number. Avoid using an invalid
4110 // line number though.
4111 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4112 lnum = curbuf->b_ml.ml_line_count;
4113 else
4114 lnum = curwin->w_cursor.lnum;
4115 break;
4116 case ADDR_WINDOWS:
4117 lnum = CURRENT_WIN_NR;
4118 break;
4119 case ADDR_ARGUMENTS:
4120 lnum = curwin->w_arg_idx + 1;
4121 if (lnum > ARGCOUNT)
4122 lnum = ARGCOUNT;
4123 break;
4124 case ADDR_LOADED_BUFFERS:
4125 case ADDR_BUFFERS:
4126 lnum = curbuf->b_fnum;
4127 break;
4128 case ADDR_TABS:
4129 lnum = CURRENT_TAB_NR;
4130 break;
4131 case ADDR_TABS_RELATIVE:
4132 case ADDR_UNSIGNED:
4133 lnum = 1;
4134 break;
4135 case ADDR_QUICKFIX:
4136#ifdef FEAT_QUICKFIX
4137 lnum = qf_get_cur_idx(eap);
4138#endif
4139 break;
4140 case ADDR_QUICKFIX_VALID:
4141#ifdef FEAT_QUICKFIX
4142 lnum = qf_get_cur_valid_idx(eap);
4143#endif
4144 break;
4145 case ADDR_NONE:
4146 // Will give an error later if a range is found.
4147 break;
4148 }
4149 return lnum;
4150}
4151
4152/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004153 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 *
4155 * Set ptr to the next character after the part that was interpreted.
4156 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004157 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 *
4159 * Return MAXLNUM when no Ex address was found.
4160 */
4161 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004162get_address(
4163 exarg_T *eap UNUSED,
4164 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004165 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004166 int skip, // only skip the address, don't use it
4167 int silent, // no errors or side effects
4168 int to_other_file, // flag: may jump to other file
4169 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 int c;
4172 int i;
4173 long n;
4174 char_u *cmd;
4175 pos_T pos;
4176 pos_T *fp;
4177 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004178 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
4180 cmd = skipwhite(*ptr);
4181 lnum = MAXLNUM;
4182 do
4183 {
4184 switch (*cmd)
4185 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004186 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004187 ++cmd;
4188 switch (addr_type)
4189 {
4190 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004191 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 lnum = curwin->w_cursor.lnum;
4193 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004194 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004195 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004196 break;
4197 case ADDR_ARGUMENTS:
4198 lnum = curwin->w_arg_idx + 1;
4199 break;
4200 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004201 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004202 lnum = curbuf->b_fnum;
4203 break;
4204 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004205 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004206 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004207 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004208 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004209 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004210 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004211 cmd = NULL;
4212 goto error;
4213 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004214 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004215#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004216 lnum = qf_get_cur_idx(eap);
4217#endif
4218 break;
4219 case ADDR_QUICKFIX_VALID:
4220#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004221 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004222#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004223 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004224 }
4225 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004227 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004228 ++cmd;
4229 switch (addr_type)
4230 {
4231 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004232 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 lnum = curbuf->b_ml.ml_line_count;
4234 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004235 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004236 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004237 break;
4238 case ADDR_ARGUMENTS:
4239 lnum = ARGCOUNT;
4240 break;
4241 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004242 buf = lastbuf;
4243 while (buf->b_ml.ml_mfp == NULL)
4244 {
4245 if (buf->b_prev == NULL)
4246 break;
4247 buf = buf->b_prev;
4248 }
4249 lnum = buf->b_fnum;
4250 break;
4251 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004252 lnum = lastbuf->b_fnum;
4253 break;
4254 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004255 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004256 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004257 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004258 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004259 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004260 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004261 cmd = NULL;
4262 goto error;
4263 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004264 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004265#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004266 lnum = qf_get_size(eap);
4267 if (lnum == 0)
4268 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004269#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004270 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004271 case ADDR_QUICKFIX_VALID:
4272#ifdef FEAT_QUICKFIX
4273 lnum = qf_get_valid_size(eap);
4274 if (lnum == 0)
4275 lnum = 1;
4276#endif
4277 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004278 }
4279 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004281 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004282 if (*++cmd == NUL)
4283 {
4284 cmd = NULL;
4285 goto error;
4286 }
4287 if (addr_type != ADDR_LINES)
4288 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004289 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004290 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004291 goto error;
4292 }
4293 if (skip)
4294 ++cmd;
4295 else
4296 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004297 // Only accept a mark in another file when it is
4298 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004299 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4300 ++cmd;
4301 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004302 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004303 lnum = curwin->w_cursor.lnum;
4304 else
4305 {
4306 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
4308 cmd = NULL;
4309 goto error;
4310 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004311 lnum = fp->lnum;
4312 }
4313 }
4314 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004317 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004318 c = *cmd++;
4319 if (addr_type != ADDR_LINES)
4320 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004321 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004322 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004323 goto error;
4324 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004325 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004326 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004327 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004328 if (*cmd == c)
4329 ++cmd;
4330 }
4331 else
4332 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004333 int flags;
4334
4335 pos = curwin->w_cursor; // save curwin->w_cursor
4336
4337 // When '/' or '?' follows another address, start from
4338 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004339 if (lnum > 0 && lnum != MAXLNUM)
4340 curwin->w_cursor.lnum =
4341 lnum > curbuf->b_ml.ml_line_count
4342 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004343
4344 // Start a forward search at the end of the line (unless
4345 // before the first line).
4346 // Start a backward search at the start of the line.
4347 // This makes sure we never match in the current
4348 // line, and can match anywhere in the
4349 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004350 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004351 curwin->w_cursor.col = MAXCOL;
4352 else
4353 curwin->w_cursor.col = 0;
4354 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004355 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004356 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004357 {
4358 curwin->w_cursor = pos;
4359 cmd = NULL;
4360 goto error;
4361 }
4362 lnum = curwin->w_cursor.lnum;
4363 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004364 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004365 cmd += searchcmdlen;
4366 }
4367 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004369 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004370 ++cmd;
4371 if (addr_type != ADDR_LINES)
4372 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004373 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004374 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004375 goto error;
4376 }
4377 if (*cmd == '&')
4378 i = RE_SUBST;
4379 else if (*cmd == '?' || *cmd == '/')
4380 i = RE_SEARCH;
4381 else
4382 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004383 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004384 cmd = NULL;
4385 goto error;
4386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004388 if (!skip)
4389 {
4390 /*
4391 * When search follows another address, start from
4392 * there.
4393 */
4394 if (lnum != MAXLNUM)
4395 pos.lnum = lnum;
4396 else
4397 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004399 /*
4400 * Start the search just like for the above
4401 * do_search().
4402 */
4403 if (*cmd != '?')
4404 pos.col = MAXCOL;
4405 else
4406 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004407 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004408 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004409 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004410 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004411 lnum = pos.lnum;
4412 else
4413 {
4414 cmd = NULL;
4415 goto error;
4416 }
4417 }
4418 ++cmd;
4419 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
4421 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004422 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425
4426 for (;;)
4427 {
4428 cmd = skipwhite(cmd);
4429 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4430 break;
4431
4432 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 {
4434 switch (addr_type)
4435 {
4436 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004437 case ADDR_OTHER:
4438 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004439 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004440 break;
4441 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004442 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004443 break;
4444 case ADDR_ARGUMENTS:
4445 lnum = curwin->w_arg_idx + 1;
4446 break;
4447 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004448 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 lnum = curbuf->b_fnum;
4450 break;
4451 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004452 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004454 case ADDR_TABS_RELATIVE:
4455 lnum = 1;
4456 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004457 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004458#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004459 lnum = qf_get_cur_idx(eap);
4460#endif
4461 break;
4462 case ADDR_QUICKFIX_VALID:
4463#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004464 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004465#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004466 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004467 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004468 case ADDR_UNSIGNED:
4469 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004470 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004471 }
4472 }
4473
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004475 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 else
4477 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004478 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 n = 1;
4480 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004481 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004483 if (n == MAXLNUM)
4484 {
4485 emsg(_(e_line_number_out_of_range));
4486 goto error;
4487 }
4488 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004489
4490 if (addr_type == ADDR_TABS_RELATIVE)
4491 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004492 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004493 cmd = NULL;
4494 goto error;
4495 }
4496 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004497 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004498 lnum = compute_buffer_local_count(
4499 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004501 {
4502#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004503 // Relative line addressing, need to adjust for folded lines
4504 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004505 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004506 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004507 (void)hasFolding(lnum, NULL, &lnum);
4508#endif
4509 if (i == '-')
4510 lnum -= n;
4511 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004512 {
4513 if (n >= LONG_MAX - lnum)
4514 {
4515 emsg(_(e_line_number_out_of_range));
4516 goto error;
4517 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004518 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004519 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
4522 } while (*cmd == '/' || *cmd == '?');
4523
4524error:
4525 *ptr = cmd;
4526 return lnum;
4527}
4528
4529/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004530 * Set eap->line1 and eap->line2 to the whole range.
4531 * Used for commands with the EX_DFLALL flag and no range given.
4532 */
4533 static void
4534address_default_all(exarg_T *eap)
4535{
4536 eap->line1 = 1;
4537 switch (eap->addr_type)
4538 {
4539 case ADDR_LINES:
4540 case ADDR_OTHER:
4541 eap->line2 = curbuf->b_ml.ml_line_count;
4542 break;
4543 case ADDR_LOADED_BUFFERS:
4544 {
4545 buf_T *buf = firstbuf;
4546
4547 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4548 buf = buf->b_next;
4549 eap->line1 = buf->b_fnum;
4550 buf = lastbuf;
4551 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4552 buf = buf->b_prev;
4553 eap->line2 = buf->b_fnum;
4554 }
4555 break;
4556 case ADDR_BUFFERS:
4557 eap->line1 = firstbuf->b_fnum;
4558 eap->line2 = lastbuf->b_fnum;
4559 break;
4560 case ADDR_WINDOWS:
4561 eap->line2 = LAST_WIN_NR;
4562 break;
4563 case ADDR_TABS:
4564 eap->line2 = LAST_TAB_NR;
4565 break;
4566 case ADDR_TABS_RELATIVE:
4567 eap->line2 = 1;
4568 break;
4569 case ADDR_ARGUMENTS:
4570 if (ARGCOUNT == 0)
4571 eap->line1 = eap->line2 = 0;
4572 else
4573 eap->line2 = ARGCOUNT;
4574 break;
4575 case ADDR_QUICKFIX_VALID:
4576#ifdef FEAT_QUICKFIX
4577 eap->line2 = qf_get_valid_size(eap);
4578 if (eap->line2 == 0)
4579 eap->line2 = 1;
4580#endif
4581 break;
4582 case ADDR_NONE:
4583 case ADDR_UNSIGNED:
4584 case ADDR_QUICKFIX:
4585 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4586 break;
4587 }
4588}
4589
4590
4591/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004592 * Get flags from an Ex command argument.
4593 */
4594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004595get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004596{
4597 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4598 {
4599 if (*eap->arg == 'l')
4600 eap->flags |= EXFLAG_LIST;
4601 else if (*eap->arg == 'p')
4602 eap->flags |= EXFLAG_PRINT;
4603 else
4604 eap->flags |= EXFLAG_NR;
4605 eap->arg = skipwhite(eap->arg + 1);
4606 }
4607}
4608
4609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 * Function called for command which is Not Implemented. NI!
4611 */
4612 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004613ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614{
4615 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004616 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004617 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618}
4619
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004620#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621/*
4622 * Function called for script command which is Not Implemented. NI!
4623 * Skips over ":perl <<EOF" constructs.
4624 */
4625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004626ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627{
4628 if (!eap->skip)
4629 ex_ni(eap);
4630 else
4631 vim_free(script_get(eap, eap->arg));
4632}
4633#endif
4634
4635/*
4636 * Check range in Ex command for validity.
4637 * Return NULL when valid, error message when invalid.
4638 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004639 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004640invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004642 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004643
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if ( eap->line1 < 0
4645 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004646 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004647 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004648
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004649 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004650 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004651 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004652 {
4653 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004654 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004655#ifdef FEAT_DIFF
4656 + (eap->cmdidx == CMD_diffget)
4657#endif
4658 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004659 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004660 break;
4661 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004662 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004663 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004664 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004665 break;
4666 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004667 // Only a boundary check, not whether the buffers actually
4668 // exist.
4669 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004670 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004671 break;
4672 case ADDR_LOADED_BUFFERS:
4673 buf = firstbuf;
4674 while (buf->b_ml.ml_mfp == NULL)
4675 {
4676 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004677 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004678 buf = buf->b_next;
4679 }
4680 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004681 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004682 buf = lastbuf;
4683 while (buf->b_ml.ml_mfp == NULL)
4684 {
4685 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004686 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004687 buf = buf->b_prev;
4688 }
4689 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004690 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004691 break;
4692 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004693 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004694 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004695 break;
4696 case ADDR_TABS:
4697 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004698 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004699 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004700 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004701 case ADDR_OTHER:
4702 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004703 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004704 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004705#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004706 // No error for value that is too big, will use the last entry.
4707 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004708 {
4709 if (eap->addr_count == 0)
4710 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004711 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004712 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004713#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004714 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004715 case ADDR_QUICKFIX_VALID:
4716#ifdef FEAT_QUICKFIX
4717 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4718 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004719 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004720#endif
4721 break;
4722 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004723 case ADDR_NONE:
4724 // Will give an error elsewhere.
4725 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004726 }
4727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 return NULL;
4729}
4730
4731/*
4732 * Correct the range for zero line number, if required.
4733 */
4734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004735correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004737 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
4739 if (eap->line1 == 0)
4740 eap->line1 = 1;
4741 if (eap->line2 == 0)
4742 eap->line2 = 1;
4743 }
4744}
4745
Bram Moolenaar748bf032005-02-02 23:04:36 +00004746#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004747/*
4748 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4749 * pattern. Otherwise return eap->arg.
4750 */
4751 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004752skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004753{
4754 char_u *p = eap->arg;
4755
Bram Moolenaara37420f2006-02-04 22:37:47 +00004756 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4757 || eap->cmdidx == CMD_vimgrepadd
4758 || eap->cmdidx == CMD_lvimgrepadd
4759 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004760 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004761 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004762 if (p == NULL)
4763 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004764 }
4765 return p;
4766}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004767
4768/*
4769 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4770 * in the command line, so that things like % get expanded.
4771 */
4772 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004773replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004774{
4775 char_u *new_cmdline;
4776 char_u *program;
4777 char_u *pos;
4778 char_u *ptr;
4779 int len;
4780 int i;
4781
4782 /*
4783 * Don't do it when ":vimgrep" is used for ":grep".
4784 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004785 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4786 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4787 || eap->cmdidx == CMD_grepadd
4788 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004789 && !grep_internal(eap->cmdidx))
4790 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004791 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4792 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004793 {
4794 if (*curbuf->b_p_gp == NUL)
4795 program = p_gp;
4796 else
4797 program = curbuf->b_p_gp;
4798 }
4799 else
4800 {
4801 if (*curbuf->b_p_mp == NUL)
4802 program = p_mp;
4803 else
4804 program = curbuf->b_p_mp;
4805 }
4806
4807 p = skipwhite(p);
4808
4809 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4810 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004811 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004812 i = 1;
4813 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4814 ++i;
4815 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004816 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004817 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004818 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004819 ptr = new_cmdline;
4820 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4821 {
4822 i = (int)(pos - program);
4823 STRNCPY(ptr, program, i);
4824 STRCPY(ptr += i, p);
4825 ptr += len;
4826 program = pos + 2;
4827 }
4828 STRCPY(ptr, program);
4829 }
4830 else
4831 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004832 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004833 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004834 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004835 STRCPY(new_cmdline, program);
4836 STRCAT(new_cmdline, " ");
4837 STRCAT(new_cmdline, p);
4838 }
4839 msg_make(p);
4840
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004841 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004842 vim_free(*cmdlinep);
4843 *cmdlinep = new_cmdline;
4844 p = new_cmdline;
4845 }
4846 return p;
4847}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004848#endif
4849
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850/*
4851 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004852 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 * Return FAIL for failure, OK otherwise.
4854 */
4855 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004856expand_filename(
4857 exarg_T *eap,
4858 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004859 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004861 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 char_u *repl;
4863 int srclen;
4864 char_u *p;
4865 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004866 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
Bram Moolenaar748bf032005-02-02 23:04:36 +00004868#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004869 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004870 p = skip_grep_pat(eap);
4871#else
4872 p = eap->arg;
4873#endif
4874
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 /*
4876 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4877 * the file name contains a wildcard it should not cause expanding.
4878 * (it will be expanded anyway if there is a wildcard before replacing).
4879 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004880 has_wildcards = mch_has_wildcard(p);
4881 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004883#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004884 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004885 if (p[0] == '`' && p[1] == '=')
4886 {
4887 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004888 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004889 if (*p == '`')
4890 ++p;
4891 continue;
4892 }
4893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 /*
4895 * Quick check if this cannot be the start of a special string.
4896 * Also removes backslash before '%', '#' and '<'.
4897 */
4898 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4899 {
4900 ++p;
4901 continue;
4902 }
4903
4904 /*
4905 * Try to find a match at this position.
4906 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004907 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4908 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004909 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004911 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
4913 p += srclen;
4914 continue;
4915 }
4916
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004917 // Wildcards won't be expanded below, the replacement is taken
4918 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004919 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4920 {
4921 char_u *l = repl;
4922
4923 repl = expand_env_save(repl);
4924 vim_free(l);
4925 }
4926
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004927 // Need to escape white space et al. with a backslash.
4928 // Don't do this for:
4929 // - replacement that already has been escaped: "##"
4930 // - shell commands (may have to use quotes instead).
4931 // - non-unix systems when there is a single argument (spaces don't
4932 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004934 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 && eap->cmdidx != CMD_grep
4937 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004938 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004939 && eap->cmdidx != CMD_lgrep
4940 && eap->cmdidx != CMD_lgrepadd
4941 && eap->cmdidx != CMD_lmake
4942 && eap->cmdidx != CMD_make
4943 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004945 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946#endif
4947 )
4948 {
4949 char_u *l;
4950#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004951 // Don't escape a backslash here, because rem_backslash() doesn't
4952 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 static char_u *nobslash = (char_u *)" \t\"|";
4954# define ESCAPE_CHARS nobslash
4955#else
4956# define ESCAPE_CHARS escape_chars
4957#endif
4958
4959 for (l = repl; *l; ++l)
4960 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4961 {
4962 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4963 if (l != NULL)
4964 {
4965 vim_free(repl);
4966 repl = l;
4967 }
4968 break;
4969 }
4970 }
4971
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004972 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004973 if ((eap->usefilter || eap->cmdidx == CMD_bang
4974 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004975 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 char_u *l;
4978
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004979 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 if (l != NULL)
4981 {
4982 vim_free(repl);
4983 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985 }
4986
4987 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4988 vim_free(repl);
4989 if (p == NULL)
4990 return FAIL;
4991 }
4992
4993 /*
4994 * One file argument: Expand wildcards.
4995 * Don't do this with ":r !command" or ":w !command".
4996 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004997 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
4999 /*
5000 * May do this twice:
5001 * 1. Replace environment variables.
5002 * 2. Replace any other wildcards, remove backslashes.
5003 */
5004 for (n = 1; n <= 2; ++n)
5005 {
5006 if (n == 2)
5007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 /*
5009 * Halve the number of backslashes (this is Vi compatible).
5010 * For Unix and OS/2, when wildcards are expanded, this is
5011 * done by ExpandOne() below.
5012 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005013#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 if (!has_wildcards)
5015#endif
5016 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018
5019 if (has_wildcards)
5020 {
5021 if (n == 1)
5022 {
5023 /*
5024 * First loop: May expand environment variables. This
5025 * can be done much faster with expand_env() than with
5026 * something else (e.g., calling a shell).
5027 * After expanding environment variables, check again
5028 * if there are still wildcards present.
5029 */
5030 if (vim_strchr(eap->arg, '$') != NULL
5031 || vim_strchr(eap->arg, '~') != NULL)
5032 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005033 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005034 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 has_wildcards = mch_has_wildcard(NameBuff);
5036 p = NameBuff;
5037 }
5038 else
5039 p = NULL;
5040 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005041 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 {
5043 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005044 int options = WILD_LIST_NOTFOUND
5045 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046
5047 ExpandInit(&xpc);
5048 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005049 if (p_wic)
5050 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005052 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 if (p == NULL)
5054 return FAIL;
5055 }
5056 if (p != NULL)
5057 {
5058 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5059 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005060 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 vim_free(p);
5062 }
5063 }
5064 }
5065 }
5066 return OK;
5067}
5068
5069/*
5070 * Replace part of the command line, keeping eap->cmd, eap->arg and
5071 * eap->nextcmd correct.
5072 * "src" points to the part that is to be replaced, of length "srclen".
5073 * "repl" is the replacement string.
5074 * Returns a pointer to the character after the replaced string.
5075 * Returns NULL for failure.
5076 */
5077 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005078repl_cmdline(
5079 exarg_T *eap,
5080 char_u *src,
5081 int srclen,
5082 char_u *repl,
5083 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
5085 int len;
5086 int i;
5087 char_u *new_cmdline;
5088
5089 /*
5090 * The new command line is build in new_cmdline[].
5091 * First allocate it.
5092 * Careful: a "+cmd" argument may have been NUL terminated.
5093 */
5094 len = (int)STRLEN(repl);
5095 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005096 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005097 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005098 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005099 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
5101 /*
5102 * Copy the stuff before the expanded part.
5103 * Copy the expanded stuff.
5104 * Copy what came after the expanded part.
5105 * Copy the next commands, if there are any.
5106 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005107 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005109
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005111 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005113 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005115 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
5117 i = (int)STRLEN(new_cmdline) + 1;
5118 STRCPY(new_cmdline + i, eap->nextcmd);
5119 eap->nextcmd = new_cmdline + i;
5120 }
5121 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5122 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5123 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5124 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5125 vim_free(*cmdlinep);
5126 *cmdlinep = new_cmdline;
5127
5128 return src;
5129}
5130
5131/*
5132 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005133 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 */
5135 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005136separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137{
5138 char_u *p;
5139
Bram Moolenaar86b68352004-12-27 21:59:20 +00005140#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005141 p = skip_grep_pat(eap);
5142#else
5143 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005144#endif
5145
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005146 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 if (*p == Ctrl_V)
5149 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005150 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005151 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005153 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005154 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005155 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 break;
5157 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005158
5159#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005160 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005161 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005162 {
5163 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005164 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005165 if (*p == NUL) // stop at NUL after CTRL-V
5166 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005167 }
5168#endif
5169
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005170 // Check for '"': start of comment or '|': next command
5171 // :@" and :*" do not start a comment!
5172 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005173 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005174#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005175 && !in_vim9script()
5176#endif
5177 && !(eap->argt & EX_NOTRLCOM)
5178 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5179 || p != eap->arg)
5180 && (eap->cmdidx != CMD_redir
5181 || p != eap->arg + 1 || p[-1] != '@'))
5182#ifdef FEAT_EVAL
5183 || (*p == '#'
5184 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005185 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005186 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 || *p == '|' || *p == '\n')
5189 {
5190 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005191 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 * AND 'b' is present in 'cpoptions'.
5193 */
5194 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005195 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005197 if (!keep_backslash)
5198 {
5199 STRMOVE(p - 1, p); // remove the '\'
5200 --p;
5201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
5203 else
5204 {
5205 eap->nextcmd = check_nextcmd(p);
5206 *p = NUL;
5207 break;
5208 }
5209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005211
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005212 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 del_trailing_spaces(eap->arg);
5214}
5215
5216/*
5217 * get + command from ex argument
5218 */
5219 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005220getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221{
5222 char_u *arg = *argp;
5223 char_u *command = NULL;
5224
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005225 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
5227 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005228 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 command = dollar_command;
5230 else
5231 {
5232 command = arg;
5233 arg = skip_cmd_arg(command, TRUE);
5234 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005235 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 }
5237
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005238 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 *argp = arg;
5240 }
5241 return command;
5242}
5243
5244/*
5245 * Find end of "+command" argument. Skip over "\ " and "\\".
5246 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005247 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005248skip_cmd_arg(
5249 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005250 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
5252 while (*p && !vim_isspace(*p))
5253 {
5254 if (*p == '\\' && p[1] != NUL)
5255 {
5256 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005257 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 else
5259 ++p;
5260 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005261 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 }
5263 return p;
5264}
5265
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005266 int
5267get_bad_opt(char_u *p, exarg_T *eap)
5268{
5269 if (STRICMP(p, "keep") == 0)
5270 eap->bad_char = BAD_KEEP;
5271 else if (STRICMP(p, "drop") == 0)
5272 eap->bad_char = BAD_DROP;
5273 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5274 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005275 else
5276 return FAIL;
5277 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005278}
5279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280/*
5281 * Get "++opt=arg" argument.
5282 * Return FAIL or OK.
5283 */
5284 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005285getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286{
5287 char_u *arg = eap->arg + 2;
5288 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005289 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005292 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5294 {
5295 if (*arg == 'n')
5296 {
5297 arg += 2;
5298 eap->force_bin = FORCE_NOBIN;
5299 }
5300 else
5301 eap->force_bin = FORCE_BIN;
5302 if (!checkforcmd(&arg, "binary", 3))
5303 return FAIL;
5304 eap->arg = skipwhite(arg);
5305 return OK;
5306 }
5307
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005308 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005309 if (STRNCMP(arg, "edit", 4) == 0)
5310 {
5311 eap->read_edit = TRUE;
5312 eap->arg = skipwhite(arg + 4);
5313 return OK;
5314 }
5315
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 if (STRNCMP(arg, "ff", 2) == 0)
5317 {
5318 arg += 2;
5319 pp = &eap->force_ff;
5320 }
5321 else if (STRNCMP(arg, "fileformat", 10) == 0)
5322 {
5323 arg += 10;
5324 pp = &eap->force_ff;
5325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 else if (STRNCMP(arg, "enc", 3) == 0)
5327 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005328 if (STRNCMP(arg, "encoding", 8) == 0)
5329 arg += 8;
5330 else
5331 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 pp = &eap->force_enc;
5333 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005334 else if (STRNCMP(arg, "bad", 3) == 0)
5335 {
5336 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005337 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
5340 if (pp == NULL || *arg != '=')
5341 return FAIL;
5342
5343 ++arg;
5344 *pp = (int)(arg - eap->cmd);
5345 arg = skip_cmd_arg(arg, FALSE);
5346 eap->arg = skipwhite(arg);
5347 *arg = NUL;
5348
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 if (pp == &eap->force_ff)
5350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5352 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005353 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005355 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005357 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5359 *p = TOLOWER_ASC(*p);
5360 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005361 else
5362 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005363 // Check ++bad= argument. Must be a single-byte character, "keep" or
5364 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005365 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005366 return FAIL;
5367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
5369 return OK;
5370}
5371
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005373ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374{
5375 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005376 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 * directory for security reasons.
5378 */
5379 if (secure)
5380 {
5381 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005382 eap->errmsg =
5383 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 }
5385 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005386 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 else
5388 do_augroup(eap->arg, eap->forceit);
5389}
5390
5391/*
5392 * ":doautocmd": Apply the automatic commands to the current buffer.
5393 */
5394 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005395ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005397 char_u *arg = eap->arg;
5398 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005399 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005400
Bram Moolenaar1610d052016-06-09 22:53:01 +02005401 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005402 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005403 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005404 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407/*
5408 * :[N]bunload[!] [N] [bufname] unload buffer
5409 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5410 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5411 */
5412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005413ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005415 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005416 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 eap->errmsg = do_bufdel(
5418 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5419 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5420 : DOBUF_UNLOAD, eap->arg,
5421 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5422}
5423
5424/*
5425 * :[N]buffer [N] to buffer N
5426 * :[N]sbuffer [N] to buffer N
5427 */
5428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005429ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005431 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005432 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005434 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 else
5436 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005437 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5439 else
5440 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005441 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005442 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
5444}
5445
5446/*
5447 * :[N]bmodified [N] to next mod. buffer
5448 * :[N]sbmodified [N] to next mod. buffer
5449 */
5450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005451ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
5453 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005454 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005455 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456}
5457
5458/*
5459 * :[N]bnext [N] to next buffer
5460 * :[N]sbnext [N] split and to next buffer
5461 */
5462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005463ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005465 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005466 return;
5467
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005469 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005470 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471}
5472
5473/*
5474 * :[N]bNext [N] to previous buffer
5475 * :[N]bprevious [N] to previous buffer
5476 * :[N]sbNext [N] split and to previous buffer
5477 * :[N]sbprevious [N] split and to previous buffer
5478 */
5479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005480ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005482 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005483 return;
5484
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005486 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005487 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488}
5489
5490/*
5491 * :brewind to first buffer
5492 * :bfirst to first buffer
5493 * :sbrewind split and to first buffer
5494 * :sbfirst split and to first buffer
5495 */
5496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005497ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005499 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005500 return;
5501
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005503 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005504 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505}
5506
5507/*
5508 * :blast to last buffer
5509 * :sblast split and to last buffer
5510 */
5511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005514 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005515 return;
5516
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005518 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005519 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521
Bram Moolenaar7a092242020-04-16 22:10:49 +02005522/*
5523 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005524 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005527ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005529 int comment_char = '"';
5530
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005531#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005532 if (in_vim9script())
5533 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005534#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005535 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005536}
5537
5538/*
5539 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5540 * to "cmd_start" or has a white space character before it.
5541 */
5542 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005543ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005544{
5545 int c = *cmd;
5546
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005547 if (c == NUL || c == '|' || c == '\n')
5548 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005549#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005550 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005551 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5552 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005553 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005554#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005555 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556}
5557
5558#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5559 || defined(PROTO)
5560/*
5561 * Return the next command, after the first '|' or '\n'.
5562 * Return NULL if not found.
5563 */
5564 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005565find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
5567 while (*p != '|' && *p != '\n')
5568 {
5569 if (*p == NUL)
5570 return NULL;
5571 ++p;
5572 }
5573 return (p + 1);
5574}
5575#endif
5576
5577/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005578 * Check if *p is a separator between Ex commands, skipping over white space.
5579 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 */
5581 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005582check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005584 char_u *s = skipwhite(p);
5585
5586 if (*s == '|' || *s == '\n')
5587 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 else
5589 return NULL;
5590}
5591
5592/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005593 * If "eap->nextcmd" is not set, check for a next command at "p".
5594 */
5595 void
5596set_nextcmd(exarg_T *eap, char_u *arg)
5597{
5598 char_u *p = check_nextcmd(arg);
5599
5600 if (eap->nextcmd == NULL)
5601 eap->nextcmd = p;
5602 else if (p != NULL)
5603 // cannot use "| command" inside a {} block
5604 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5605}
5606
5607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 * - if there are more files to edit
5609 * - and this is the last window
5610 * - and forceit not used
5611 * - and not repeated twice on a row
5612 * return FAIL and give error message if 'message' TRUE
5613 * return OK otherwise
5614 */
5615 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005616check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005617 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005618 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619{
5620 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5621
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005622 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005623 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 {
5625 if (message)
5626 {
5627#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005628 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5629 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005631 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005633 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5634 NGETTEXT("%d more file to edit. Quit anyway?",
5635 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5637 return OK;
5638 return FAIL;
5639 }
5640#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005641 semsg(NGETTEXT(e_nr_more_file_to_edit,
5642 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005643 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 }
5645 return FAIL;
5646 }
5647 return OK;
5648}
5649
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650/*
5651 * Function given to ExpandGeneric() to obtain the list of command names.
5652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005654get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
5656 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005657 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 return cmdnames[idx].cmd_name;
5659}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005662ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
Bram Moolenaare6850792010-05-14 15:28:44 +02005664 if (*eap->arg == NUL)
5665 {
5666#ifdef FEAT_EVAL
5667 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5668 char_u *p = NULL;
5669
5670 if (expr != NULL)
5671 {
5672 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005673 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005674 --emsg_off;
5675 vim_free(expr);
5676 }
5677 if (p != NULL)
5678 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005679 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005680 vim_free(p);
5681 }
5682 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005683 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005684#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005685 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005686#endif
5687 }
5688 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005689 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005690
5691#ifdef FEAT_VTP
5692 else if (has_vtp_working())
5693 {
5694 // background color change requires clear + redraw
5695 update_screen(CLEAR);
5696 redrawcmd();
5697 }
5698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699}
5700
5701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005702ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
5704 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005705 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 do_highlight(eap->arg, eap->forceit, FALSE);
5707}
5708
5709
5710/*
5711 * Call this function if we thought we were going to exit, but we won't
5712 * (because of an error). May need to restore the terminal mode.
5713 */
5714 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005715not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
5717 exiting = FALSE;
5718 settmode(TMODE_RAW);
5719}
5720
Bram Moolenaar3552e742021-05-29 12:21:58 +02005721 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005722before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5723{
5724 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5725
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005726 // Bail out when autocommands closed the window.
5727 // Refuse to quit when the buffer in the last window is being closed (can
5728 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005729 if (!win_valid(wp)
5730 || curbuf_locked()
5731 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5732 return TRUE;
5733
5734 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5735 {
5736 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005737 // Refuse to quit when locked or when the window was closed or the
5738 // buffer in the last window is being closed (can only happen in
5739 // autocommands).
5740 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005741 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5742 return TRUE;
5743 }
5744
5745 return FALSE;
5746}
5747
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005749 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005750 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005751 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005753 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005754ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005756 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005757
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758#ifdef FEAT_CMDWIN
5759 if (cmdwin_type != 0)
5760 {
5761 cmdwin_result = Ctrl_C;
5762 return;
5763 }
5764#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005765 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005766 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005767 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005768 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005769 return;
5770 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005771 if (eap->addr_count > 0)
5772 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005773 int wnr = eap->line2;
5774
5775 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5776 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005777 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005778 }
5779 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005780 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005781
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005782 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005783 if (curbuf_locked())
5784 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005785
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005786 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005787 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005788 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789
5790#ifdef FEAT_NETBEANS_INTG
5791 netbeansForcedQuit = eap->forceit;
5792#endif
5793
5794 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005795 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 */
5797 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5798 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005799 if ((!buf_hide(wp->w_buffer)
5800 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005801 | (eap->forceit ? CCGD_FORCEIT : 0)
5802 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005804 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 {
5806 not_exiting();
5807 }
5808 else
5809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005810 // quit last window
5811 // Note: only_one_window() returns true, even so a help window is
5812 // still open. In that case only quit, if no address has been
5813 // specified. Example:
5814 // :h|wincmd w|1q - don't quit
5815 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005816 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005818 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005819#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005821#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005822 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005823 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
5825}
5826
5827/*
5828 * ":cquit".
5829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005831ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005833 // this does not always pass on the exit code to the Manx compiler. why?
5834 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835}
5836
5837/*
5838 * ":qall": try to quit all windows
5839 */
5840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005841ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
5843# ifdef FEAT_CMDWIN
5844 if (cmdwin_type != 0)
5845 {
5846 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005847 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 else
5849 cmdwin_result = K_XF2;
5850 return;
5851 }
5852# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005853
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005854 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005855 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005856 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005857 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005858 return;
5859 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005860
5861 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005862 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005863
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005865 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 getout(0);
5867 not_exiting();
5868}
5869
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870/*
5871 * ":close": close current window, unless it is the last one
5872 */
5873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005874ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005876 win_T *win;
5877 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005878#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005880 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005882#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005883 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005884 {
5885 if (eap->addr_count == 0)
5886 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005887 else
5888 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005889 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005890 {
5891 winnr++;
5892 if (winnr == eap->line2)
5893 break;
5894 }
5895 if (win == NULL)
5896 win = lastwin;
5897 ex_win_close(eap->forceit, win, NULL);
5898 }
5899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900}
5901
Bram Moolenaar4033c552017-09-16 20:54:51 +02005902#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005903/*
5904 * ":pclose": Close any preview window.
5905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005907ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005908{
5909 win_T *win;
5910
Bram Moolenaar79648732019-07-18 21:43:07 +02005911 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005912 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005913 if (win->w_p_pvw)
5914 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005915 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005916 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005917 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005918# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005919 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005920 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005921# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005922}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005923#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005924
Bram Moolenaarf740b292006-02-16 22:11:02 +00005925/*
5926 * Close window "win" and take care of handling closing the last window for a
5927 * modified buffer.
5928 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005930ex_win_close(
5931 int forceit,
5932 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005933 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 int need_hide;
5936 buf_T *buf = win->w_buffer;
5937
Bram Moolenaarcf844172020-06-26 19:44:06 +02005938 // Never close the autocommand window.
5939 if (win == aucmd_win)
5940 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00005941 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02005942 return;
5943 }
5944
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005946 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005948#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005949 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005951 bufref_T bufref;
5952
5953 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005955 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 return;
5957 need_hide = FALSE;
5958 }
5959 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005962 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 return;
5964 }
5965 }
5966
Bram Moolenaar4033c552017-09-16 20:54:51 +02005967#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005969#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005970
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005971 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005972 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005973 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005974 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005975 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976}
5977
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005979 * Handle the argument for a tabpage related ex command.
5980 * Returns a tabpage number.
5981 * When an error is encountered then eap->errmsg is set.
5982 */
5983 static int
5984get_tabpage_arg(exarg_T *eap)
5985{
5986 int tab_number;
5987 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5988
5989 if (eap->arg && *eap->arg != NUL)
5990 {
5991 char_u *p = eap->arg;
5992 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005993 int relative = 0; // argument +N/-N means: go to N places to the
5994 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005995
5996 if (*p == '-')
5997 {
5998 relative = -1;
5999 p++;
6000 }
6001 else if (*p == '+')
6002 {
6003 relative = 1;
6004 p++;
6005 }
6006
6007 p_save = p;
6008 tab_number = getdigits(&p);
6009
6010 if (relative == 0)
6011 {
6012 if (STRCMP(p, "$") == 0)
6013 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006014 else if (STRCMP(p, "#") == 0)
6015 if (valid_tabpage(lastused_tabpage))
6016 tab_number = tabpage_index(lastused_tabpage);
6017 else
6018 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006019 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006020 tab_number = 0;
6021 goto theend;
6022 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006023 else if (p == p_save || *p_save == '-' || *p != NUL
6024 || tab_number > LAST_TAB_NR)
6025 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006026 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006027 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006028 goto theend;
6029 }
6030 }
6031 else
6032 {
6033 if (*p_save == NUL)
6034 tab_number = 1;
6035 else if (p == p_save || *p_save == '-' || *p != NUL
6036 || tab_number == 0)
6037 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006038 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006039 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006040 goto theend;
6041 }
6042 tab_number = tab_number * relative + tabpage_index(curtab);
6043 if (!unaccept_arg0 && relative == -1)
6044 --tab_number;
6045 }
6046 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006047 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006048 }
6049 else if (eap->addr_count > 0)
6050 {
6051 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006052 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006053 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006054 tab_number = 0;
6055 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006056 else
6057 {
6058 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006059 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006060 {
6061 --tab_number;
6062 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006063 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006064 }
6065 }
6066 }
6067 else
6068 {
6069 switch (eap->cmdidx)
6070 {
6071 case CMD_tabnext:
6072 tab_number = tabpage_index(curtab) + 1;
6073 if (tab_number > LAST_TAB_NR)
6074 tab_number = 1;
6075 break;
6076 case CMD_tabmove:
6077 tab_number = LAST_TAB_NR;
6078 break;
6079 default:
6080 tab_number = tabpage_index(curtab);
6081 }
6082 }
6083
6084theend:
6085 return tab_number;
6086}
6087
6088/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006089 * ":tabclose": close current tab page, unless it is the last one.
6090 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 */
6092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006093ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006095 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006096 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006097
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006098# ifdef FEAT_CMDWIN
6099 if (cmdwin_type != 0)
6100 cmdwin_result = K_IGNORE;
6101 else
6102# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006103 if (first_tabpage->tp_next == NULL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00006104 emsg(_(e_cannot_close_last_tab_page));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006107 tab_number = get_tabpage_arg(eap);
6108 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006109 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006110 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006111 if (tp == NULL)
6112 {
6113 beep_flush();
6114 return;
6115 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006116 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006117 {
6118 tabpage_close_other(tp, eap->forceit);
6119 return;
6120 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006121 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006122 tabpage_close(eap->forceit);
6123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006125}
6126
6127/*
6128 * ":tabonly": close all tab pages except the current one
6129 */
6130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006131ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006132{
6133 tabpage_T *tp;
6134 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006135 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006136
6137# ifdef FEAT_CMDWIN
6138 if (cmdwin_type != 0)
6139 cmdwin_result = K_IGNORE;
6140 else
6141# endif
6142 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006143 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006144 else
6145 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006146 tab_number = get_tabpage_arg(eap);
6147 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006148 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006149 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006150 // Repeat this up to a 1000 times, because autocommands may
6151 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006152 for (done = 0; done < 1000; ++done)
6153 {
6154 FOR_ALL_TABPAGES(tp)
6155 if (tp->tp_topframe != topframe)
6156 {
6157 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006158 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006159 if (valid_tabpage(tp))
6160 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006161 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006162 break;
6163 }
6164 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006165 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006166 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006167 }
6168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170
6171/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006172 * Close the current tab page.
6173 */
6174 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006175tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006176{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006177 // First close all the windows but the current one. If that worked then
6178 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006179 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006180 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006181 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006182 ex_win_close(forceit, curwin, NULL);
6183# ifdef FEAT_GUI
6184 need_mouse_correct = TRUE;
6185# endif
6186}
6187
6188/*
6189 * Close tab page "tp", which is not the current tab page.
6190 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006191 * Also takes care of the tab pages line disappearing when closing the
6192 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006193 */
6194 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006195tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006196{
6197 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006198 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006199 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006200
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006201 // Limit to 1000 windows, autocommands may add a window while we close
6202 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006203 while (++done < 1000)
6204 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006205 wp = tp->tp_firstwin;
6206 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006207
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006208 // Autocommands may delete the tab page under our fingers and we may
6209 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006210 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006211 break;
6212 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006213
Bram Moolenaar29323592016-07-24 22:04:11 +02006214 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006215
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006216 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006217 if (h != tabline_height())
6218 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006219}
6220
6221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 * ":only".
6223 */
6224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006225ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006227 win_T *wp;
6228 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229# ifdef FEAT_GUI
6230 need_mouse_correct = TRUE;
6231# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006232 if (eap->addr_count > 0)
6233 {
6234 wnr = eap->line2;
6235 for (wp = firstwin; --wnr > 0; )
6236 {
6237 if (wp->w_next == NULL)
6238 break;
6239 else
6240 wp = wp->w_next;
6241 }
6242 win_goto(wp);
6243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 close_others(TRUE, eap->forceit);
6245}
6246
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006248ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006250 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006251 if (!eap->skip)
6252 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006253#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006254 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006255#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006256 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006257 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006258 else
6259 {
6260 int winnr = 0;
6261 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006262
Bram Moolenaar2256c992016-11-15 21:17:07 +01006263 FOR_ALL_WINDOWS(win)
6264 {
6265 winnr++;
6266 if (winnr == eap->line2)
6267 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006268 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006269 if (win == NULL)
6270 win = lastwin;
6271 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 }
6274}
6275
6276/*
6277 * ":stop" and ":suspend": Suspend Vim.
6278 */
dbivolaruab16ad32021-12-29 19:41:47 +00006279 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006280ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281{
6282 /*
6283 * Disallow suspending for "rvim".
6284 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006285 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 {
6287 if (!eap->forceit)
6288 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006289 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 windgoto((int)Rows - 1, 0);
6291 out_char('\n');
6292 out_flush();
6293 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006294 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006295 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006296 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006298 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006300 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006302 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006303 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 }
6305}
6306
6307/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006308 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 */
6310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006311ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006313#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006314 if (not_in_vim9(eap) == FAIL)
6315 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317#ifdef FEAT_CMDWIN
6318 if (cmdwin_type != 0)
6319 {
6320 cmdwin_result = Ctrl_C;
6321 return;
6322 }
6323#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006324 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006325 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006326 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006327 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006328 return;
6329 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006330
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006332 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 */
6334 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6335 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006336
6337 // Write the buffer for ":wq" or when it was changed.
6338 // Trigger QuitPre and ExitPre.
6339 // Check if we can exit now, after autocommands have changed things.
6340 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6341 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006343 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 {
6345 not_exiting();
6346 }
6347 else
6348 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006349 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006351 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352# ifdef FEAT_GUI
6353 need_mouse_correct = TRUE;
6354# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006355 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006356 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 }
6358}
6359
6360/*
6361 * ":print", ":list", ":number".
6362 */
6363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006364ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006366 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006367 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006368 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006370 for ( ;!got_int; ui_breakcheck())
6371 {
6372 print_line(eap->line1,
6373 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6374 || (eap->flags & EXFLAG_NR)),
6375 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6376 if (++eap->line1 > eap->line2)
6377 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006378 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006379 }
6380 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006381 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006382 curwin->w_cursor.lnum = eap->line2;
6383 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
6385
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387}
6388
6389#ifdef FEAT_BYTEOFF
6390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006391ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392{
6393 goto_byte(eap->line2);
6394}
6395#endif
6396
6397/*
6398 * ":shell".
6399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006401ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 do_shell(NULL, 0);
6404}
6405
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006406#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006408static int drop_busy = FALSE;
6409static int drop_filec;
6410static char_u **drop_filev = NULL;
6411static int drop_split;
6412static void (*drop_callback)(void *);
6413static void *drop_cookie;
6414
6415 static void
6416handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417{
6418 exarg_T ea;
6419 int save_msg_scroll = msg_scroll;
6420
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006421 // Setting the argument list may cause screen updates and being called
6422 // recursively. Avoid that by setting drop_busy.
6423 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006425 // Check whether the current buffer is changed. If so, we will need
6426 // to split the current window or data could be lost.
6427 // We don't need to check if the 'hidden' option is set, as in this
6428 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006429 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
6431 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006432 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 --emsg_off;
6434 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006435 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 if (win_split(0, 0) == FAIL)
6438 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006439 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006441 // When splitting the window, create a new alist. Otherwise the
6442 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 alist_unlink(curwin->w_alist);
6444 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 }
6446
6447 /*
6448 * Set up the new argument list.
6449 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006450 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451
6452 /*
6453 * Move to the first file.
6454 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006455 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006456 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 ea.cmd = (char_u *)"next";
6458 do_argfile(&ea, 0);
6459
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006460 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6461 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 need_start_insertmode = FALSE;
6463
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006464 // Restore msg_scroll, otherwise a following command may cause scrolling
6465 // unexpectedly. The screen will be redrawn by the caller, thus
6466 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006468
6469 if (drop_callback != NULL)
6470 drop_callback(drop_cookie);
6471
6472 drop_filev = NULL;
6473 drop_busy = FALSE;
6474}
6475
6476/*
6477 * Handle a file drop. The code is here because a drop is *nearly* like an
6478 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006479 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006480 * code is very similar to :args and hence needs access to a lot of the static
6481 * functions in this file.
6482 *
6483 * The "filev" list must have been allocated using alloc(), as should each item
6484 * in the list. This function takes over responsibility for freeing the "filev"
6485 * list.
6486 */
6487 void
6488handle_drop(
6489 int filec, // the number of files dropped
6490 char_u **filev, // the list of files dropped
6491 int split, // force splitting the window
6492 void (*callback)(void *), // to be called after setting the argument
6493 // list
6494 void *cookie) // argument for "callback" (allocated)
6495{
6496 // Cannot handle recursive drops, finish the pending one.
6497 if (drop_busy)
6498 {
6499 FreeWild(filec, filev);
6500 vim_free(cookie);
6501 return;
6502 }
6503
6504 // When calling handle_drop() more than once in a row we only use the last
6505 // one.
6506 if (drop_filev != NULL)
6507 {
6508 FreeWild(drop_filec, drop_filev);
6509 vim_free(drop_cookie);
6510 }
6511
6512 drop_filec = filec;
6513 drop_filev = filev;
6514 drop_split = split;
6515 drop_callback = callback;
6516 drop_cookie = cookie;
6517
6518 // Postpone this when:
6519 // - editing the command line
6520 // - not possible to change the current buffer
6521 // - updating the screen
6522 // As it may change buffers and window structures that are in use and cause
6523 // freed memory to be used.
6524 if (text_locked() || curbuf_locked() || updating_screen)
6525 return;
6526
6527 handle_drop_internal();
6528}
6529
6530/*
6531 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6532 * reset: Handle a postponed drop.
6533 */
6534 void
6535handle_any_postponed_drop(void)
6536{
6537 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006538 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006539 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540}
6541#endif
6542
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 * ":preserve".
6545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006547ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006549 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 ml_preserve(curbuf, TRUE);
6551}
6552
6553/*
6554 * ":recover".
6555 */
6556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006557ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006559 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006561 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6562 | CCGD_MULTWIN
6563 | (eap->forceit ? CCGD_FORCEIT : 0)
6564 | CCGD_EXCMD)
6565
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 && (*eap->arg == NUL
6567 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006568 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 recoverymode = FALSE;
6570}
6571
6572/*
6573 * Command modifier used in a wrong way.
6574 */
6575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006576ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006578 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579}
6580
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581/*
6582 * :sview [+command] file split window with new file, read-only
6583 * :split [[+command] file] split window with current or new file
6584 * :vsplit [[+command] file] split window vertically with current or new file
6585 * :new [[+command] file] split window with no or new file
6586 * :vnew [[+command] file] split vertically window with no or new file
6587 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006588 *
6589 * :tabedit open new Tab page with empty window
6590 * :tabedit [+command] file open new Tab page and edit "file"
6591 * :tabnew [[+command] file] just like :tabedit
6592 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 */
6594 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006595ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006597 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006598#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006600#endif
6601#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006602 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006603 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006604#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006605 int use_tab = eap->cmdidx == CMD_tabedit
6606 || eap->cmdidx == CMD_tabfind
6607 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006609 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006610 return;
6611
Bram Moolenaar4033c552017-09-16 20:54:51 +02006612#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
Bram Moolenaar4033c552017-09-16 20:54:51 +02006616#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006617 // A ":split" in the quickfix window works like ":new". Don't want two
6618 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006619 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 {
6621 if (eap->cmdidx == CMD_split)
6622 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 if (eap->cmdidx == CMD_vsplit)
6624 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627
Bram Moolenaar4033c552017-09-16 20:54:51 +02006628#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006629 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 {
6631 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6632 FNAME_MESS, TRUE, curbuf->b_ffname);
6633 if (fname == NULL)
6634 goto theend;
6635 eap->arg = fname;
6636 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006637# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006638 else
6639# endif
6640#endif
6641#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006642 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 && eap->cmdidx != CMD_new)
6645 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006646 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006647# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006648 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006649# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006650 au_has_group((char_u *)"FileExplorer"))
6651 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006652 // No browsing supported but we do have the file explorer:
6653 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006654 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006655 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006656 }
6657 else
6658 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006659 fname = do_browse(0, (char_u *)(use_tab
6660 ? _("Edit File in new tab page")
6661 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006663 if (fname == NULL)
6664 goto theend;
6665 eap->arg = fname;
6666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006668 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006671 /*
6672 * Either open new tab page or split the window.
6673 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006674 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006675 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006676 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006677 : eap->addr_count == 0 ? 0
6678 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006679 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006680 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006681
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006682 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006683 if (curwin != old_curwin
6684 && win_valid(old_curwin)
6685 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006686 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006687 old_curwin->w_alt_fnum = curbuf->b_fnum;
6688 }
6689 }
6690 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6692 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006693 // Reset 'scrollbind' when editing another file, but keep it when
6694 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006695 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006696 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 else
6698 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 do_exedit(eap, old_curwin);
6700 }
6701
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006702# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006703 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006706# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707theend:
6708 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006711
6712/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006713 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006714 */
6715 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006716tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006717{
6718 exarg_T ea;
6719
Bram Moolenaara80faa82020-04-12 19:37:17 +02006720 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006721 ea.cmdidx = CMD_tabnew;
6722 ea.cmd = (char_u *)"tabn";
6723 ea.arg = (char_u *)"";
6724 ex_splitview(&ea);
6725}
6726
6727/*
6728 * :tabnext command
6729 */
6730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006731ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006732{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006733 int tab_number;
6734
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006735 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006736 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006737 switch (eap->cmdidx)
6738 {
6739 case CMD_tabfirst:
6740 case CMD_tabrewind:
6741 goto_tabpage(1);
6742 break;
6743 case CMD_tablast:
6744 goto_tabpage(9999);
6745 break;
6746 case CMD_tabprevious:
6747 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006748 if (eap->arg && *eap->arg != NUL)
6749 {
6750 char_u *p = eap->arg;
6751 char_u *p_save = p;
6752
6753 tab_number = getdigits(&p);
6754 if (p == p_save || *p_save == '-' || *p != NUL
6755 || tab_number == 0)
6756 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006757 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006758 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006759 return;
6760 }
6761 }
6762 else
6763 {
6764 if (eap->addr_count == 0)
6765 tab_number = 1;
6766 else
6767 {
6768 tab_number = eap->line2;
6769 if (tab_number < 1)
6770 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006771 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006772 return;
6773 }
6774 }
6775 }
6776 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006777 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006778 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006779 tab_number = get_tabpage_arg(eap);
6780 if (eap->errmsg == NULL)
6781 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006782 break;
6783 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006784}
6785
6786/*
6787 * :tabmove command
6788 */
6789 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006790ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006791{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006792 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006793
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006794 tab_number = get_tabpage_arg(eap);
6795 if (eap->errmsg == NULL)
6796 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006797}
6798
6799/*
6800 * :tabs command: List tabs and their contents.
6801 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006803ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006804{
6805 tabpage_T *tp;
6806 win_T *wp;
6807 int tabcount = 1;
6808
6809 msg_start();
6810 msg_scroll = TRUE;
6811 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6812 {
6813 msg_putchar('\n');
6814 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006815 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006816 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006817 ui_breakcheck();
6818
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006819 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006820 wp = firstwin;
6821 else
6822 wp = tp->tp_firstwin;
6823 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6824 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006825 msg_putchar('\n');
6826 msg_putchar(wp == curwin ? '>' : ' ');
6827 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006828 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6829 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006830 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006831 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006832 else
6833 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6834 IObuff, IOSIZE, TRUE);
6835 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006836 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006837 ui_breakcheck();
6838 }
6839 }
6840}
6841
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842/*
6843 * ":mode": Set screen mode.
6844 * If no argument given, just get the screen size and redraw.
6845 */
6846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006847ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849 if (*eap->arg == NUL)
6850 shell_resized();
6851 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006852 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853}
6854
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855/*
6856 * ":resize".
6857 * set, increment or decrement current window height
6858 */
6859 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006860ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
6862 int n;
6863 win_T *wp = curwin;
6864
6865 if (eap->addr_count > 0)
6866 {
6867 n = eap->line2;
6868 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6869 ;
6870 }
6871
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006872# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006874# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006876 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {
6878 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006879 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006880 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006882 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 }
6884 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
6886 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006887 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006888 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006890 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
6892}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893
6894/*
6895 * ":find [+command] <file>" command.
6896 */
6897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006898ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899{
6900#ifdef FEAT_SEARCHPATH
6901 char_u *fname;
6902 int count;
6903
6904 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6905 TRUE, curbuf->b_ffname);
6906 if (eap->addr_count > 0)
6907 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006908 // Repeat finding the file "count" times. This matters when it
6909 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 count = eap->line2;
6911 while (fname != NULL && --count > 0)
6912 {
6913 vim_free(fname);
6914 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6915 FALSE, curbuf->b_ffname);
6916 }
6917 }
6918
6919 if (fname != NULL)
6920 {
6921 eap->arg = fname;
6922#endif
6923 do_exedit(eap, NULL);
6924#ifdef FEAT_SEARCHPATH
6925 vim_free(fname);
6926 }
6927#endif
6928}
6929
6930/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006931 * ":open" simulation: for now just work like ":visual".
6932 */
6933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006934ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006935{
6936 regmatch_T regmatch;
6937 char_u *p;
6938
Bram Moolenaar65088802021-03-13 21:07:21 +01006939#ifdef FEAT_EVAL
6940 if (not_in_vim9(eap) == FAIL)
6941 return;
6942#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006943 curwin->w_cursor.lnum = eap->line2;
6944 beginline(BL_SOL | BL_FIX);
6945 if (*eap->arg == '/')
6946 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006947 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006948 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006949 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006950 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006951 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006952 if (regmatch.regprog != NULL)
6953 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00006954 // make a copy of the line, when searching for a mark it might be
6955 // flushed
6956 char_u *line = vim_strsave(ml_get_curline());
6957
Bram Moolenaardf177f62005-02-22 08:39:57 +00006958 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00006959 if (vim_regexec(&regmatch, line, (colnr_T)0))
6960 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006961 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006962 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02006963 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00006964 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006965 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006966 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006967 eap->arg += STRLEN(eap->arg);
6968 }
6969 check_cursor();
6970
6971 eap->cmdidx = CMD_visual;
6972 do_exedit(eap, NULL);
6973}
6974
6975/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006976 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 */
6978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006979ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980{
6981 do_exedit(eap, NULL);
6982}
6983
6984/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006985 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006988do_exedit(
6989 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006990 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
6992 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006994 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006996 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6997 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006998 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 /*
7000 * ":vi" command ends Ex mode.
7001 */
7002 if (exmode_active && (eap->cmdidx == CMD_visual
7003 || eap->cmdidx == CMD_view))
7004 {
7005 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007006 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007008 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007009 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007010 if (global_busy)
7011 {
7012 int rd = RedrawingDisabled;
7013 int nwr = no_wait_return;
7014 int ms = msg_scroll;
7015#ifdef FEAT_GUI
7016 int he = hold_gui_events;
7017#endif
7018
7019 if (eap->nextcmd != NULL)
7020 {
7021 stuffReadbuff(eap->nextcmd);
7022 eap->nextcmd = NULL;
7023 }
7024
7025 if (exmode_was != EXMODE_VIM)
7026 settmode(TMODE_RAW);
7027 RedrawingDisabled = 0;
7028 no_wait_return = 0;
7029 need_wait_return = FALSE;
7030 msg_scroll = 0;
7031#ifdef FEAT_GUI
7032 hold_gui_events = 0;
7033#endif
7034 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007035 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007036
7037 main_loop(FALSE, TRUE);
7038
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007039 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007040 RedrawingDisabled = rd;
7041 no_wait_return = nwr;
7042 msg_scroll = ms;
7043#ifdef FEAT_GUI
7044 hold_gui_events = he;
7045#endif
7046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 }
7050
7051 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007052 || eap->cmdidx == CMD_tabnew
7053 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007054 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007056 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 setpcmark();
7058 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007059 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7060 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007062 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 || *eap->arg != NUL
7064#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007065 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066#endif
7067 )
7068 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007069 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
7070 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007071 if (*eap->arg != NUL && curbuf_locked())
7072 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007073
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 n = readonlymode;
7075 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7076 readonlymode = TRUE;
7077 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007078 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7079 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007080 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7081 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7083 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007084 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7086 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7087 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007088 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007090 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007091 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007092 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7093 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007094 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007096 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 if (old_curwin != NULL)
7098 {
7099 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007100 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007102#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007103 cleanup_T cs;
7104
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007105 // Reset the error/interrupt/exception state here so that
7106 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007107 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007108#endif
7109#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007111#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007112 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007113
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007114#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007115 // Restore the error/interrupt/exception state if not
7116 // discarded by a new aborting error, interrupt, or
7117 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007118 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 }
7121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 }
7123 else if (readonlymode && curbuf->b_nwindows == 1)
7124 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007125 // When editing an already visited buffer, 'readonly' won't be set
7126 // but the previous value is kept. With ":view" and ":sview" we
7127 // want the file to be readonly, except when another window is
7128 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 curbuf->b_p_ro = TRUE;
7130 }
7131 readonlymode = n;
7132 }
7133 else
7134 {
7135 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007136 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 if (n != curwin->w_arg_idx_invalid)
7140 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 }
7142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 /*
7144 * if ":split file" worked, set alternate file name in old window to new
7145 * file
7146 */
7147 if (old_curwin != NULL
7148 && *eap->arg != NUL
7149 && curwin != old_curwin
7150 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007151 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007152 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
7155 ex_no_reprint = TRUE;
7156}
7157
7158#ifndef FEAT_GUI
7159/*
7160 * ":gui" and ":gvim" when there is no GUI.
7161 */
7162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007163ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007165 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166}
7167#endif
7168
Bram Moolenaar4f974752019-02-17 17:44:42 +01007169#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007171ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 gui_make_tearoff(eap->arg);
7174}
7175#endif
7176
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007177#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7178 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007180ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007182# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7183 if (gui.in_use)
7184 gui_make_popup(eap->arg, eap->forceit);
7185# ifdef FEAT_TERM_POPUP_MENU
7186 else
7187# endif
7188# endif
7189# ifdef FEAT_TERM_POPUP_MENU
7190 pum_make_popup(eap->arg, eap->forceit);
7191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192}
7193#endif
7194
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007196ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007199 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007201 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202}
7203
7204/*
7205 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7206 * offset.
7207 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007210ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007213 win_T *save_curwin = curwin;
7214 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 long topline;
7216 long y;
7217 linenr_T old_linenr = curwin->w_cursor.lnum;
7218
7219 setpcmark();
7220
7221 /*
7222 * determine max topline
7223 */
7224 if (curwin->w_p_scb)
7225 {
7226 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007227 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 {
7229 if (wp->w_p_scb && wp->w_buffer)
7230 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007231 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 if (topline > y)
7233 topline = y;
7234 }
7235 }
7236 if (topline < 1)
7237 topline = 1;
7238 }
7239 else
7240 {
7241 topline = 1;
7242 }
7243
7244
7245 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007246 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007248 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {
7250 if (curwin->w_p_scb)
7251 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007252 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 y = topline - curwin->w_topline;
7254 if (y > 0)
7255 scrollup(y, TRUE);
7256 else
7257 scrolldown(-y, TRUE);
7258 curwin->w_scbind_pos = topline;
7259 redraw_later(VALID);
7260 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 }
7263 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007264 curwin = save_curwin;
7265 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 if (curwin->w_p_scb)
7267 {
7268 did_syncbind = TRUE;
7269 checkpcmark();
7270 if (old_linenr != curwin->w_cursor.lnum)
7271 {
7272 char_u ctrl_o[2];
7273
7274 ctrl_o[0] = Ctrl_O;
7275 ctrl_o[1] = 0;
7276 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7277 }
7278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279}
7280
7281
7282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007283ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007285 int i;
7286 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7287 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007289 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 do_bang(1, eap, FALSE, FALSE, TRUE);
7291 else
7292 {
7293 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7294 return;
7295
7296#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007297 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
7299 char_u *browseFile;
7300
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007301 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 NULL, NULL, NULL, curbuf);
7303 if (browseFile != NULL)
7304 {
7305 i = readfile(browseFile, NULL,
7306 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7307 vim_free(browseFile);
7308 }
7309 else
7310 i = OK;
7311 }
7312 else
7313#endif
7314 if (*eap->arg == NUL)
7315 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007316 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 return;
7318 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7319 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7320 }
7321 else
7322 {
7323 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7324 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7325 i = readfile(eap->arg, NULL,
7326 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7327
7328 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007329 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007331#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 if (!aborting())
7333#endif
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007334 semsg(_(e_cant_open_file_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 }
7336 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007337 {
7338 if (empty && exmode_active)
7339 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007340 // Delete the empty line that remains. Historically ex does
7341 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007342 if (eap->line2 == 0)
7343 lnum = curbuf->b_ml.ml_line_count;
7344 else
7345 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007346 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007347 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007348 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007349 if (curwin->w_cursor.lnum > 1
7350 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007351 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007352 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007353 }
7354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 }
7358}
7359
Bram Moolenaarea408852005-06-25 22:49:46 +00007360static char_u *prev_dir = NULL;
7361
7362#if defined(EXITFREE) || defined(PROTO)
7363 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007364free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007365{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007366 VIM_CLEAR(prev_dir);
7367 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007368}
7369#endif
7370
Bram Moolenaarf4258302013-06-02 18:20:17 +02007371/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007372 * Get the previous directory for the given chdir scope.
7373 */
7374 static char_u *
7375get_prevdir(cdscope_T scope)
7376{
7377 if (scope == CDSCOPE_WINDOW)
7378 return curwin->w_prevdir;
7379 else if (scope == CDSCOPE_TABPAGE)
7380 return curtab->tp_prevdir;
7381 return prev_dir;
7382}
7383
7384/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007385 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007386 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7387 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007388 */
7389 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007390post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007391{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007392 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007393 // Clear tab local directory for both :cd and :tcd
7394 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007395 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007396 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007397 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007398 char_u *pdir = get_prevdir(scope);
7399
7400 // If still in the global directory, need to remember current
7401 // directory as the global directory.
7402 if (globaldir == NULL && pdir != NULL)
7403 globaldir = vim_strsave(pdir);
7404
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007405 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007406 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007407 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007408 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007409 curtab->tp_localdir = vim_strsave(NameBuff);
7410 else
7411 curwin->w_localdir = vim_strsave(NameBuff);
7412 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007413 }
7414 else
7415 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007416 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007417 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007418 }
7419
zeertzjq64be6aa2021-11-19 11:59:08 +00007420 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007421 shorten_fnames(TRUE);
7422}
7423
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007424/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007425 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7426 */
7427 void
7428trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7429{
7430#ifdef FEAT_EVAL
7431 dict_T *v_event;
7432 save_v_event_T save_v_event;
7433
7434 v_event = get_v_event(&save_v_event);
7435 (void)dict_add_string(v_event, "directory", new_dir);
7436 dict_set_items_ro(v_event);
7437#endif
7438 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7439#ifdef FEAT_EVAL
7440 restore_v_event(v_event, &save_v_event);
7441#endif
7442}
7443
7444/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007445 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007446 * chdir() function.
7447 * scope == CDSCOPE_WINDOW: changes the window-local directory
7448 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7449 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007450 * Returns TRUE if the directory is successfully changed.
7451 */
7452 int
7453changedir_func(
7454 char_u *new_dir,
7455 int forceit,
7456 cdscope_T scope)
7457{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007458 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007459 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007460 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007461 char_u **pp;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007462
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007463 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007464 return FALSE;
7465
7466 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7467 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007468 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007469 return FALSE;
7470 }
7471
7472 // ":cd -": Change to previous directory
7473 if (STRCMP(new_dir, "-") == 0)
7474 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007475 pdir = get_prevdir(scope);
7476 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007477 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007478 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007479 return FALSE;
7480 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007481 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007482 }
7483
7484 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007485 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007486 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007487 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007488 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007489
Bakudankun29f3a452021-12-11 12:28:08 +00007490 // For UNIX ":cd" means: go to home directory.
7491 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007492#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007493 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007494#else
7495 if (*new_dir == NUL && p_cdh)
7496#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007497 {
7498 // use NameBuff for home directory name
7499# ifdef VMS
7500 char_u *p;
7501
7502 p = mch_getenv((char_u *)"SYS$LOGIN");
7503 if (p == NULL || *p == NUL) // empty is the same as not set
7504 NameBuff[0] = NUL;
7505 else
7506 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7507# else
7508 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7509# endif
7510 new_dir = NameBuff;
7511 }
zeertzjqf38aad82021-12-30 13:45:57 +00007512 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007513 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7514 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007515 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007516 if (scope == CDSCOPE_WINDOW)
7517 acmd_fname = (char_u *)"window";
7518 else if (scope == CDSCOPE_TABPAGE)
7519 acmd_fname = (char_u *)"tabpage";
7520 else
7521 acmd_fname = (char_u *)"global";
7522 trigger_DirChangedPre(acmd_fname, new_dir);
7523
7524 if (vim_chdir(new_dir))
7525 {
7526 emsg(_(e_command_failed));
7527 vim_free(pdir);
7528 return FALSE;
7529 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007530 }
zeertzjq73257142022-02-02 13:16:37 +00007531
7532 if (scope == CDSCOPE_WINDOW)
7533 pp = &curwin->w_prevdir;
7534 else if (scope == CDSCOPE_TABPAGE)
7535 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007536 else
zeertzjq73257142022-02-02 13:16:37 +00007537 pp = &prev_dir;
7538 vim_free(*pp);
7539 *pp = pdir;
7540
7541 post_chdir(scope);
7542
7543 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007544 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
zeertzjq73257142022-02-02 13:16:37 +00007545 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007546}
Bram Moolenaarea408852005-06-25 22:49:46 +00007547
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007549 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007551 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007552ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007554 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555
7556 new_dir = eap->arg;
7557#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007558 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7559 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 ex_pwd(NULL);
7561 else
7562#endif
7563 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007564 cdscope_T scope = CDSCOPE_GLOBAL;
7565
7566 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7567 scope = CDSCOPE_WINDOW;
7568 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7569 scope = CDSCOPE_TABPAGE;
7570
7571 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007572 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007573 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007574 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 ex_pwd(eap);
7576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 }
7578}
7579
7580/*
7581 * ":pwd".
7582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007584ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585{
7586 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7587 {
7588#ifdef BACKSLASH_IN_FILENAME
7589 slash_adjust(NameBuff);
7590#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007591 if (p_verbose > 0)
7592 {
7593 char *context = "global";
7594
Bram Moolenaar05268152021-11-18 18:53:45 +00007595 if (last_chdir_reason != NULL)
7596 context = last_chdir_reason;
7597 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007598 context = "window";
7599 else if (curtab->tp_localdir != NULL)
7600 context = "tabpage";
7601 smsg("[%s] %s", context, (char *)NameBuff);
7602 }
7603 else
7604 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 }
7606 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007607 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608}
7609
7610/*
7611 * ":=".
7612 */
7613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007614ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007616 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007617 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618}
7619
7620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007621ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007623 int n;
7624 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625
7626 if (cursor_valid())
7627 {
7628 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7629 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007630 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007632
7633 len = eap->line2;
7634 switch (*eap->arg)
7635 {
7636 case 'm': break;
7637 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007638 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007639 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007640
7641 // Hide the cursor if invoked with !
7642 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643}
7644
7645/*
7646 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007647 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 */
7649 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007650do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651{
Bram Moolenaar52953192019-08-16 10:27:13 +02007652 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007653 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007654# ifdef ELAPSED_FUNC
7655 elapsed_T start_tv;
7656
7657 // Remember at what time we started, so that we know how much longer we
7658 // should wait after waiting for a bit.
7659 ELAPSED_INIT(start_tv);
7660# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007662 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007663 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007664 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007665 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007666
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007667 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007668 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007670 wait_now = msec - done > 1000L ? 1000L : msec - done;
7671#ifdef FEAT_TIMERS
7672 {
7673 long due_time = check_due_timer();
7674
7675 if (due_time > 0 && due_time < wait_now)
7676 wait_now = due_time;
7677 }
7678#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007679#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007680 if (has_any_channel() && wait_now > 20L)
7681 wait_now = 20L;
7682#endif
7683#ifdef FEAT_SOUND
7684 if (has_any_sound_callback() && wait_now > 20L)
7685 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007686#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007687 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007688
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007689#ifdef FEAT_JOB_CHANNEL
7690 if (has_any_channel())
7691 ui_breakcheck_force(TRUE);
7692 else
7693#endif
7694 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007695#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007696 // Process the netbeans and clientserver messages that may have been
7697 // received in the call to ui_breakcheck() when the GUI is in use. This
7698 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007699 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007700#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007701
7702# ifdef ELAPSED_FUNC
7703 // actual time passed
7704 done = ELAPSED_FUNC(start_tv);
7705# else
7706 // guestimate time passed (will actually be more)
7707 done += wait_now;
7708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007710
7711 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7712 // input buffer, otherwise a following call to input() fails.
7713 if (got_int)
7714 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007715
7716 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007717 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718}
7719
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720/*
7721 * ":winsize" command (obsolete).
7722 */
7723 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007724ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
7726 int w, h;
7727 char_u *arg = eap->arg;
7728 char_u *p;
7729
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007730 if (!isdigit(*arg))
7731 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007732 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007733 return;
7734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 w = getdigits(&arg);
7736 arg = skipwhite(arg);
7737 p = arg;
7738 h = getdigits(&arg);
7739 if (*p != NUL && *arg == NUL)
7740 set_shellsize(w, h, TRUE);
7741 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007742 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743}
7744
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007746ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747{
7748 int xchar = NUL;
7749 char_u *p;
7750
7751 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7752 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007753 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 if (eap->arg[1] == NUL)
7755 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007756 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 return;
7758 }
7759 xchar = eap->arg[1];
7760 p = eap->arg + 2;
7761 }
7762 else
7763 p = eap->arg + 1;
7764
Bram Moolenaar63b91732021-08-05 20:40:03 +02007765 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007767 if (*p != NUL && *p != (
7768#ifdef FEAT_EVAL
7769 in_vim9script() ? '#' :
7770#endif
7771 '"')
7772 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007773 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007774 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007776 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007777 postponed_split_flags = cmdmod.cmod_split;
7778 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7780 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007781 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
7783}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784
Bram Moolenaar843ee412004-06-30 16:16:41 +00007785#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786/*
7787 * ":winpos".
7788 */
7789 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007790ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792 int x, y;
7793 char_u *arg = eap->arg;
7794 char_u *p;
7795
7796 if (*arg == NUL)
7797 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007798# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007799# ifdef VIMDLL
7800 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7801 mch_get_winpos(&x, &y) != FAIL)
7802# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007804# else
7805 if (mch_get_winpos(&x, &y) != FAIL)
7806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 {
7808 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007809 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 }
7811 else
7812# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007813 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 }
7815 else
7816 {
7817 x = getdigits(&arg);
7818 arg = skipwhite(arg);
7819 p = arg;
7820 y = getdigits(&arg);
7821 if (*p == NUL || *arg != NUL)
7822 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007823 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 return;
7825 }
7826# ifdef FEAT_GUI
7827 if (gui.in_use)
7828 gui_mch_set_winpos(x, y);
7829 else if (gui.starting)
7830 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007831 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 gui_win_x = x;
7833 gui_win_y = y;
7834 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007835# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 else
7837# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007838# endif
7839# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007840 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841# endif
7842# ifdef HAVE_TGETENT
7843 if (*T_CWP)
7844 term_set_winpos(x, y);
7845# endif
7846 }
7847}
7848#endif
7849
7850/*
7851 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7852 */
7853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007854ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856 oparg_T oa;
7857
7858 clear_oparg(&oa);
7859 oa.regname = eap->regname;
7860 oa.start.lnum = eap->line1;
7861 oa.end.lnum = eap->line2;
7862 oa.line_count = eap->line2 - eap->line1 + 1;
7863 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007865 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {
7867 setpcmark();
7868 curwin->w_cursor.lnum = eap->line1;
7869 beginline(BL_SOL | BL_FIX);
7870 }
7871
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007872 if (VIsual_active)
7873 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007874
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 switch (eap->cmdidx)
7876 {
7877 case CMD_delete:
7878 oa.op_type = OP_DELETE;
7879 op_delete(&oa);
7880 break;
7881
7882 case CMD_yank:
7883 oa.op_type = OP_YANK;
7884 (void)op_yank(&oa, FALSE, TRUE);
7885 break;
7886
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007887 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007888 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007890 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7891#else
7892 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007894 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 oa.op_type = OP_RSHIFT;
7896 else
7897 oa.op_type = OP_LSHIFT;
7898 op_shift(&oa, FALSE, eap->amount);
7899 break;
7900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007902 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903}
7904
7905/*
7906 * ":put".
7907 */
7908 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007909ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007911 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 if (eap->line2 == 0)
7913 {
7914 eap->line2 = 1;
7915 eap->forceit = TRUE;
7916 }
7917 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00007918 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007919 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007920 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921}
7922
7923/*
7924 * Handle ":copy" and ":move".
7925 */
7926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007927ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928{
7929 long n;
7930
Bram Moolenaar491799b2020-08-01 19:23:43 +02007931#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007932 if (not_in_vim9(eap) == FAIL)
7933 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007934#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007935 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007936 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {
7938 eap->nextcmd = NULL;
7939 return;
7940 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007941 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942
7943 /*
7944 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7945 */
7946 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7947 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007948 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 return;
7950 }
7951
7952 if (eap->cmdidx == CMD_move)
7953 {
7954 if (do_move(eap->line1, eap->line2, n) == FAIL)
7955 return;
7956 }
7957 else
7958 ex_copy(eap->line1, eap->line2, n);
7959 u_clearline();
7960 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007961 ex_may_print(eap);
7962}
7963
7964/*
7965 * Print the current line if flags were given to the Ex command.
7966 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007967 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007968ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007969{
7970 if (eap->flags != 0)
7971 {
7972 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7973 (eap->flags & EXFLAG_LIST));
7974 ex_no_reprint = TRUE;
7975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976}
7977
7978/*
7979 * ":smagic" and ":snomagic".
7980 */
7981 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007982ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007984 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007986 magic_overruled = eap->cmdidx == CMD_smagic
7987 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007988 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007989 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990}
7991
7992/*
7993 * ":join".
7994 */
7995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007996ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997{
7998 curwin->w_cursor.lnum = eap->line1;
7999 if (eap->line1 == eap->line2)
8000 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008001 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 return;
8003 if (eap->line2 == curbuf->b_ml.ml_line_count)
8004 {
8005 beep_flush();
8006 return;
8007 }
8008 ++eap->line2;
8009 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008010 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008012 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013}
8014
8015/*
8016 * ":[addr]@r" or ":[addr]*r": execute register
8017 */
8018 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008019ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020{
8021 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008022 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023
8024 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008025 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026
8027#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008028 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029#endif
8030
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008031 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 c = *eap->arg;
8033 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8034 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008035 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008036 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8037 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 else
8042 {
8043 int save_efr = exec_from_reg;
8044
8045 exec_from_reg = TRUE;
8046
8047 /*
8048 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008049 * Continue until the stuff buffer is empty and all added characters
8050 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008052 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8054
8055 exec_from_reg = save_efr;
8056 }
8057}
8058
8059/*
8060 * ":!".
8061 */
8062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008063ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064{
8065 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8066}
8067
8068/*
8069 * ":undo".
8070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008072ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008074 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008075 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008076 else
8077 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078}
8079
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008080#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008082ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008083{
8084 char_u hash[UNDO_HASH_SIZE];
8085
8086 u_compute_hash(hash);
8087 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8088}
8089
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008091ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008092{
8093 char_u hash[UNDO_HASH_SIZE];
8094
8095 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008096 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008097}
8098#endif
8099
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100/*
8101 * ":redo".
8102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008104ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 u_redo(1);
8107}
8108
8109/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008110 * ":earlier" and ":later".
8111 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008112 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008113ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008114{
8115 long count = 0;
8116 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008117 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008118 char_u *p = eap->arg;
8119
8120 if (*p == NUL)
8121 count = 1;
8122 else if (isdigit(*p))
8123 {
8124 count = getdigits(&p);
8125 switch (*p)
8126 {
8127 case 's': ++p; sec = TRUE; break;
8128 case 'm': ++p; sec = TRUE; count *= 60; break;
8129 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008130 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8131 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008132 }
8133 }
8134
8135 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008136 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008137 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008138 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8139 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008140}
8141
8142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 * ":redir": start/stop redirection.
8144 */
8145 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008146ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147{
8148 char *mode;
8149 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008150 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151
Bram Moolenaarba768492016-07-08 15:32:54 +02008152#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008153 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008154 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008155 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008156 return;
8157 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008158#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008159
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 if (STRICMP(eap->arg, "END") == 0)
8161 close_redir();
8162 else
8163 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008164 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008166 ++arg;
8167 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008169 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 mode = "a";
8171 }
8172 else
8173 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008174 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175
8176 close_redir();
8177
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008178 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008179 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 if (fname == NULL)
8181 return;
8182#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008183 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {
8185 char_u *browseFile;
8186
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008187 browseFile = do_browse(BROWSE_SAVE,
8188 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008189 fname, NULL, NULL,
8190 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008192 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 vim_free(fname);
8194 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008195 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 }
8197#endif
8198
8199 redir_fd = open_exfile(fname, eap->forceit, mode);
8200 vim_free(fname);
8201 }
8202#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008203 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008205 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008207 ++arg;
8208 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008210 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008211 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008213 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008215 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008216 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008217 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008218 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008220 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008221 if (*arg == '>')
8222 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008223 // Make register empty when not using @A-@Z and the
8224 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008225 if (*arg == NUL && !isupper(redir_reg))
8226 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008228 }
8229 if (*arg != NUL)
8230 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008231 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008232 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008233 }
8234 }
8235 else if (*arg == '=' && arg[1] == '>')
8236 {
8237 int append;
8238
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008239 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008240 close_redir();
8241 arg += 2;
8242
8243 if (*arg == '>')
8244 {
8245 ++arg;
8246 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 }
8248 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008249 append = FALSE;
8250
8251 if (var_redir_start(skipwhite(arg), append) == OK)
8252 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 }
8254#endif
8255
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008256 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008259 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008261
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008262 // Make sure redirection is not off. Can happen for cmdline completion
8263 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008264 if (redir_fd != NULL
8265#ifdef FEAT_EVAL
8266 || redir_reg || redir_vname
8267#endif
8268 )
8269 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270}
8271
8272/*
8273 * ":redraw": force redraw
8274 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008275 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008276ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277{
8278 int r = RedrawingDisabled;
8279 int p = p_lz;
8280
8281 RedrawingDisabled = 0;
8282 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008283 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008285 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 if (need_maketitle)
8287 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008288#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8289# ifdef VIMDLL
8290 if (!gui.in_use)
8291# endif
8292 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 RedrawingDisabled = r;
8295 p_lz = p;
8296
Bram Moolenaar5017c662022-04-07 18:06:08 +01008297 // After drawing the statusline screen_attr may still be set.
8298 screen_stop_highlight();
8299
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008300 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 msg_didout = FALSE;
8302 msg_col = 0;
8303
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008304 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008305 need_wait_return = FALSE;
8306
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 out_flush();
8308}
8309
8310/*
8311 * ":redrawstatus": force redraw of status line(s)
8312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008314ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 int r = RedrawingDisabled;
8317 int p = p_lz;
8318
8319 RedrawingDisabled = 0;
8320 p_lz = FALSE;
8321 if (eap->forceit)
8322 status_redraw_all();
8323 else
8324 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008325 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 RedrawingDisabled = r;
8327 p_lz = p;
8328 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329}
8330
Bram Moolenaare12bab32019-01-08 22:02:56 +01008331/*
8332 * ":redrawtabline": force redraw of the tabline
8333 */
8334 static void
8335ex_redrawtabline(exarg_T *eap UNUSED)
8336{
8337 int r = RedrawingDisabled;
8338 int p = p_lz;
8339
8340 RedrawingDisabled = 0;
8341 p_lz = FALSE;
8342
8343 draw_tabline();
8344
8345 RedrawingDisabled = r;
8346 p_lz = p;
8347 out_flush();
8348}
8349
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008351close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
8353 if (redir_fd != NULL)
8354 {
8355 fclose(redir_fd);
8356 redir_fd = NULL;
8357 }
8358#ifdef FEAT_EVAL
8359 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008360 if (redir_vname)
8361 {
8362 var_redir_stop();
8363 redir_vname = 0;
8364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365#endif
8366}
8367
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008368#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008369 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008370vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008371{
8372 if (vim_mkdir(name, prot) != 0)
8373 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008374 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008375 return FAIL;
8376 }
8377 return OK;
8378}
8379#endif
8380
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381/*
8382 * Open a file for writing for an Ex command, with some checks.
8383 * Return file descriptor, or NULL on failure.
8384 */
8385 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008386open_exfile(
8387 char_u *fname,
8388 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008389 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390{
8391 FILE *fd;
8392
8393#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008394 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 if (mch_isdir(fname))
8396 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008397 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 return NULL;
8399 }
8400#endif
8401 if (!forceit && *mode != 'a' && vim_fexists(fname))
8402 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008403 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 return NULL;
8405 }
8406
8407 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008408 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409
8410 return fd;
8411}
8412
8413/*
8414 * ":mark" and ":k".
8415 */
8416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008417ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418{
8419 pos_T pos;
8420
Bram Moolenaar10b94212021-02-19 21:42:57 +01008421#ifdef FEAT_EVAL
8422 if (not_in_vim9(eap) == FAIL)
8423 return;
8424#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008425 if (*eap->arg == NUL) // No argument?
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008426 emsg(_(e_argument_required));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008427 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar74409f62022-01-01 15:58:22 +00008428 semsg(_(e_trailing_characters_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 else
8430 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008431 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 curwin->w_cursor.lnum = eap->line2;
8433 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008434 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaar6d057012021-12-31 18:49:43 +00008435 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008436 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 }
8438}
8439
8440/*
8441 * Update w_topline, w_leftcol and the cursor position.
8442 */
8443 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008444update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008446 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 update_topline();
8448 if (!curwin->w_p_wrap)
8449 validate_cursor();
8450 update_curswant();
8451}
8452
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008454 * Save the current State and go to Normal mode.
8455 * Return TRUE if the typeahead could be saved.
8456 */
8457 int
8458save_current_state(save_state_T *sst)
8459{
8460 sst->save_msg_scroll = msg_scroll;
8461 sst->save_restart_edit = restart_edit;
8462 sst->save_msg_didout = msg_didout;
8463 sst->save_State = State;
8464 sst->save_insertmode = p_im;
8465 sst->save_finish_op = finish_op;
8466 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008467 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008468 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008469
Bram Moolenaar4324d872020-12-01 20:12:24 +01008470 msg_scroll = FALSE; // no msg scrolling in Normal mode
8471 restart_edit = 0; // don't go to Insert mode
8472 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008473
Bram Moolenaara452b802020-12-01 21:47:59 +01008474 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008475 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008476
8477 /*
8478 * Save the current typeahead. This is required to allow using ":normal"
8479 * from an event handler and makes sure we don't hang when the argument
8480 * ends with half a command.
8481 */
8482 save_typeahead(&sst->tabuf);
8483 return sst->tabuf.typebuf_valid;
8484}
8485
8486 void
8487restore_current_state(save_state_T *sst)
8488{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008489 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008490 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008491
8492 msg_scroll = sst->save_msg_scroll;
8493 restart_edit = sst->save_restart_edit;
8494 p_im = sst->save_insertmode;
8495 finish_op = sst->save_finish_op;
8496 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008497 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008498 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008499 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008500 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008501
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008502 // Restore the state (needed when called from a function executed for
8503 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008504 State = sst->save_State;
8505#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008506 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008507#endif
8508}
8509
8510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 * ":normal[!] {commands}": Execute normal mode commands.
8512 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008513 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008514ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008516 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 char_u *arg = NULL;
8518 int l;
8519 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008521 if (ex_normal_lock > 0)
8522 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008523 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008524 return;
8525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 if (ex_normal_busy >= p_mmd)
8527 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008528 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 return;
8530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 /*
8533 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8534 * this for the K_SPECIAL leading byte, otherwise special keys will not
8535 * work.
8536 */
8537 if (has_mbyte)
8538 {
8539 int len = 0;
8540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008541 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 for (p = eap->arg; *p != NUL; ++p)
8543 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008544#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008545 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008547#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008548 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008549 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008550#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 )
8554 len += 2;
8555 }
8556 if (len > 0)
8557 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008558 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 if (arg != NULL)
8560 {
8561 len = 0;
8562 for (p = eap->arg; *p != NUL; ++p)
8563 {
8564 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008565#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 if (*p == CSI)
8567 {
8568 arg[len++] = KS_EXTRA;
8569 arg[len++] = (int)KE_CSI;
8570 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008571#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008572 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 {
8574 arg[len++] = *++p;
8575 if (*p == K_SPECIAL)
8576 {
8577 arg[len++] = KS_SPECIAL;
8578 arg[len++] = KE_FILLER;
8579 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008580#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 else if (*p == CSI)
8582 {
8583 arg[len++] = KS_EXTRA;
8584 arg[len++] = (int)KE_CSI;
8585 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 }
8588 arg[len] = NUL;
8589 }
8590 }
8591 }
8592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008594 ++ex_normal_busy;
8595 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 {
8597 /*
8598 * Repeat the :normal command for each line in the range. When no
8599 * range given, execute it just once, without positioning the cursor
8600 * first.
8601 */
8602 do
8603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 if (eap->addr_count != 0)
8605 {
8606 curwin->w_cursor.lnum = eap->line1++;
8607 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008608 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 }
8610
Bram Moolenaar13505972019-01-24 15:04:48 +01008611 exec_normal_cmd(arg != NULL
8612 ? arg
8613 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 }
8615 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8616 }
8617
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008618 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 update_topline_cursor();
8620
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008621 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008623 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008624#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008625 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008626#endif
8627
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629}
8630
8631/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008632 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 */
8634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008635ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008637 if (eap->forceit)
8638 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008639 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008640 if (!curwin->w_cursor.lnum)
8641 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008642 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008643 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008644#ifdef FEAT_TERMINAL
8645 // Ignore this when running in an active terminal.
8646 if (term_job_running(curbuf->b_term))
8647 return;
8648#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008649
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008650 // Ignore the command when already in Insert mode. Inserting an
8651 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008652 if (State & INSERT)
8653 return;
8654
Bram Moolenaar64969662005-12-14 21:59:55 +00008655 if (eap->cmdidx == CMD_startinsert)
8656 restart_edit = 'a';
8657 else if (eap->cmdidx == CMD_startreplace)
8658 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008660 restart_edit = 'V';
8661
8662 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008664 if (eap->cmdidx == CMD_startinsert)
8665 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008666 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008668
8669 if (VIsual_active)
8670 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671}
8672
8673/*
8674 * ":stopinsert"
8675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008677ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
8679 restart_edit = 0;
8680 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008681 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008684/*
8685 * Execute normal mode command "cmd".
8686 * "remap" can be REMAP_NONE or REMAP_YES.
8687 */
8688 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008689exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008690{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008691 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008692 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008693 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008694}
Bram Moolenaar25281632016-01-21 23:32:32 +01008695
Bram Moolenaar25281632016-01-21 23:32:32 +01008696/*
8697 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008698 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008699 */
8700 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008701exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008702{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008703 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008704 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008705
Bram Moolenaar905dd902019-04-07 14:21:47 +02008706 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8707 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008708 clear_oparg(&oa);
8709 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008710 while ((!stuff_empty()
8711 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008712 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008713 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008714 {
8715 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008716#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008717 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008718 && oa.op_type == OP_NOP && oa.regname == NUL
8719 && !VIsual_active)
8720 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008721 // If terminal_loop() returns OK we got a key that is handled
8722 // in Normal model. With FAIL we first need to position the
8723 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008724 if (terminal_loop(TRUE) == OK)
8725 normal_cmd(&oa, TRUE);
8726 }
8727 else
8728#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008729 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008730 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008731 }
8732}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008733
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734#ifdef FEAT_FIND_ID
8735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008736ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737{
8738 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8739 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8740 (linenr_T)1, (linenr_T)MAXLNUM);
8741}
8742
Bram Moolenaar4033c552017-09-16 20:54:51 +02008743#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744/*
8745 * ":psearch"
8746 */
8747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008748ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749{
8750 g_do_tagpreview = p_pvh;
8751 ex_findpat(eap);
8752 g_do_tagpreview = 0;
8753}
8754#endif
8755
8756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008757ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758{
8759 int whole = TRUE;
8760 long n;
8761 char_u *p;
8762 int action;
8763
8764 switch (cmdnames[eap->cmdidx].cmd_name[2])
8765 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008766 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8768 action = ACTION_GOTO;
8769 else
8770 action = ACTION_SHOW;
8771 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008772 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 action = ACTION_SHOW_ALL;
8774 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008775 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 action = ACTION_GOTO;
8777 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008778 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 action = ACTION_SPLIT;
8780 break;
8781 }
8782
8783 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008784 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 {
8786 n = getdigits(&eap->arg);
8787 eap->arg = skipwhite(eap->arg);
8788 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008789 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 {
8791 whole = FALSE;
8792 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008793 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 if (*p)
8795 {
8796 *p++ = NUL;
8797 p = skipwhite(p);
8798
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008799 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008800 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008801 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008803 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 }
8805 }
8806 if (!eap->skip)
8807 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8808 whole, !eap->forceit,
8809 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8810 n, action, eap->line1, eap->line2);
8811}
8812#endif
8813
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814
Bram Moolenaar4033c552017-09-16 20:54:51 +02008815#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816/*
8817 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8818 */
8819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008820ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008822 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8824}
8825
8826/*
8827 * ":pedit"
8828 */
8829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008830ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831{
8832 win_T *curwin_save = curwin;
8833
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008834 if (ERROR_IF_ANY_POPUP_WINDOW)
8835 return;
8836
Bram Moolenaar026587b2019-08-17 15:08:00 +02008837 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008839 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008840
Bram Moolenaar026587b2019-08-17 15:08:00 +02008841 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008843
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 if (curwin != curwin_save && win_valid(curwin_save))
8845 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008846 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 validate_cursor();
8848 redraw_later(VALID);
8849 win_enter(curwin_save, TRUE);
8850 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008851# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008852 else if (WIN_IS_POPUP(curwin))
8853 {
8854 // can't keep focus in popup window
8855 win_enter(firstwin, TRUE);
8856 }
8857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 g_do_tagpreview = 0;
8859}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861
8862/*
8863 * ":stag", ":stselect" and ":stjump".
8864 */
8865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008866ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867{
8868 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008869 postponed_split_flags = cmdmod.cmod_split;
8870 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8872 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008873 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875
8876/*
8877 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8878 */
8879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008880ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881{
8882 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8883}
8884
8885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008886ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887{
8888 int cmd;
8889
8890 switch (name[1])
8891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008892 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008894 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008896 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008898 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008900 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008902 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008904 case 'f': // ":tfirst"
8905 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008907 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008909 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008911 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008913 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 return;
8915 }
8916#endif
8917 cmd = DT_TAG;
8918 break;
8919 }
8920
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008921 if (name[0] == 'l')
8922 {
8923#ifndef FEAT_QUICKFIX
8924 ex_ni(eap);
8925 return;
8926#else
8927 cmd = DT_LTAG;
8928#endif
8929 }
8930
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8932 eap->forceit, TRUE);
8933}
8934
8935/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008936 * Check "str" for starting with a special cmdline variable.
8937 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8938 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8939 */
8940 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008941find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008942{
8943 int len;
8944 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008945 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008946 "%",
8947#define SPEC_PERC 0
8948 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008949#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008950 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008951#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008952 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008953#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008954 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008955#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008956 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008957#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008958 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008959#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008960 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008961#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008962 "<stack>", // call stack
8963#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01008964 "<script>", // script file name
8965#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008966 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01008967#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008968 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008969#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008970 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008971#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008972 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008973#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008974 "<SID>", // script ID: <SNR>123_
8975#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008976#ifdef FEAT_CLIENTSERVER
8977 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008978# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008979#endif
8980 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008981
K.Takataeeec2542021-06-02 13:28:16 +02008982 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008983 {
8984 len = (int)STRLEN(spec_str[i]);
8985 if (STRNCMP(src, spec_str[i], len) == 0)
8986 {
8987 *usedlen = len;
8988 return i;
8989 }
8990 }
8991 return -1;
8992}
8993
8994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 * Evaluate cmdline variables.
8996 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008997 * change "%" to curbuf->b_ffname
8998 * "#" to curwin->w_alt_fnum
8999 * "%%" to curwin->w_alt_fnum in Vim9 script
9000 * "<cword>" to word under the cursor
9001 * "<cWORD>" to WORD under the cursor
9002 * "<cexpr>" to C-expression under the cursor
9003 * "<cfile>" to path name under the cursor
9004 * "<sfile>" to sourced file name
9005 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009006 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009007 * "<slnum>" to sourced file line number
9008 * "<afile>" to file name for autocommand
9009 * "<abuf>" to buffer number for autocommand
9010 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 *
9012 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9013 * "" for error without a message) and NULL is returned.
9014 * Returns an allocated string if a valid match was found.
9015 * Returns NULL if no match was found. "usedlen" then still contains the
9016 * number of characters to skip.
9017 */
9018 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009019eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009020 char_u *src, // pointer into commandline
9021 char_u *srcstart, // beginning of valid memory for src
9022 int *usedlen, // characters after src that are used
9023 linenr_T *lnump, // line number for :e command, or NULL
9024 char **errormsg, // pointer to error message
9025 int *escaped) // return value has escaped white space (can
9026 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027{
9028 int i;
9029 char_u *s;
9030 char_u *result;
9031 char_u *resultbuf = NULL;
9032 int resultlen;
9033 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009034 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009036 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039
9040 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009041 if (escaped != NULL)
9042 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043
9044 /*
9045 * Check if there is something to do.
9046 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009047 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009048 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 {
9050 *usedlen = 1;
9051 return NULL;
9052 }
9053
9054 /*
9055 * Skip when preceded with a backslash "\%" and "\#".
9056 * Note: In "\\%" the % is also not recognized!
9057 */
9058 if (src > srcstart && src[-1] == '\\')
9059 {
9060 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009061 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 return NULL;
9063 }
9064
9065 /*
9066 * word or WORD under cursor
9067 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009068 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9069 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009071 resultlen = find_ident_under_cursor(&result,
9072 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9073 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9074 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 if (resultlen == 0)
9076 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009077 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 return NULL;
9079 }
9080 }
9081
9082 /*
9083 * '#': Alternate file name
9084 * '%': Current file name
9085 * File name under the cursor
9086 * File name for autocommand
9087 * and following modifiers
9088 */
9089 else
9090 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009091 int off = 0;
9092
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 switch (spec_idx)
9094 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009095 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009096#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009097 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009100 // '%': current file
9101 if (curbuf->b_fname == NULL)
9102 {
9103 result = (char_u *)"";
9104 valid = 0; // Must have ":p:h" to be valid
9105 }
9106 else
9107 {
9108 result = curbuf->b_fname;
9109 tilde_file = STRCMP(result, "~") == 0;
9110 }
9111 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009113#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009114 // "%%" alternate file
9115 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009116#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009117 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009118 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009119 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009121 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 result = arg_all();
9123 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009124 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009125 if (escaped != NULL)
9126 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 break;
9129 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009130 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009131 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009132 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009134 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009135 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009136 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009137 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009139 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009141 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009143 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009144 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009145 return NULL;
9146 }
9147#ifdef FEAT_EVAL
9148 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9149 (long)i);
9150 if (result == NULL)
9151 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009152 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009153 return NULL;
9154 }
9155#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009156 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 }
9160 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009161 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009162 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9163 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009164 buf = buflist_findnr(i);
9165 if (buf == NULL)
9166 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009167 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009168 return NULL;
9169 }
9170 if (lnump != NULL)
9171 *lnump = ECMD_LAST;
9172 if (buf->b_fname == NULL)
9173 {
9174 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009175 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009176 }
9177 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009178 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009179 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009180 tilde_file = STRCMP(result, "~") == 0;
9181 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 break;
9184
9185#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009186 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009187 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 if (result == NULL)
9189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009190 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 return NULL;
9192 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009193 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 break;
9195#endif
9196
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009197 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009199 if (result != NULL && !autocmd_fname_full)
9200 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009201 // Still need to turn the fname into a full path. It is
9202 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009203 autocmd_fname_full = TRUE;
9204 result = FullName_save(autocmd_fname, FALSE);
9205 vim_free(autocmd_fname);
9206 autocmd_fname = result;
9207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 if (result == NULL)
9209 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009210 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 return NULL;
9212 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009213 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 break;
9215
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009216 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 if (autocmd_bufnr <= 0)
9218 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009219 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 return NULL;
9221 }
9222 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9223 result = strbuf;
9224 break;
9225
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009226 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 result = autocmd_match;
9228 if (result == NULL)
9229 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009230 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 return NULL;
9232 }
9233 break;
9234
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009235 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009236 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 if (result == NULL)
9238 {
LemonBoy6013d002022-04-09 21:42:10 +01009239 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9240 return NULL;
9241 }
9242 resultbuf = result; // remember allocated string
9243 break;
9244 case SPEC_STACK: // call stack
9245 result = estack_sfile(ESTACK_STACK);
9246 if (result == NULL)
9247 {
9248 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9249 return NULL;
9250 }
9251 resultbuf = result; // remember allocated string
9252 break;
9253 case SPEC_SCRIPT: // script file name
9254 result = estack_sfile(ESTACK_SCRIPT);
9255 if (result == NULL)
9256 {
9257 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 return NULL;
9259 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009260 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009262
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009263 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009264 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009265 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009266 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009267 return NULL;
9268 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009269 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009270 result = strbuf;
9271 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009272
9273#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009274 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009275 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009276 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009277 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009278 return NULL;
9279 }
9280 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009281 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009282 result = strbuf;
9283 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009284
Bram Moolenaar90944302020-08-01 20:45:11 +02009285 case SPEC_SID:
9286 if (current_sctx.sc_sid <= 0)
9287 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009288 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009289 return NULL;
9290 }
9291 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9292 result = strbuf;
9293 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009294#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009295
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009296#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009297 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009298 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9299 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 result = strbuf;
9301 break;
9302#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009303
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009304 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009305 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 }
9308
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009309 resultlen = (int)STRLEN(result); // length of new string
9310 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 {
9312 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 resultlen = (int)(s - result);
9315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 else if (!skip_mod)
9317 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009318 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 &resultlen);
9320 if (result == NULL)
9321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009322 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 return NULL;
9324 }
9325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 }
9327
9328 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9329 {
9330 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009331 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009333 *errormsg = _(e_evaluates_to_an_empty_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 result = NULL;
9335 }
9336 else
9337 result = vim_strnsave(result, resultlen);
9338 vim_free(resultbuf);
9339 return result;
9340}
9341
9342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 * Expand the <sfile> string in "arg".
9344 *
9345 * Returns an allocated string, or NULL for any error.
9346 */
9347 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009348expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009350 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 int len;
9352 char_u *result;
9353 char_u *newres;
9354 char_u *repl;
9355 int srclen;
9356 char_u *p;
9357
9358 result = vim_strsave(arg);
9359 if (result == NULL)
9360 return NULL;
9361
9362 for (p = result; *p; )
9363 {
9364 if (STRNCMP(p, "<sfile>", 7) != 0)
9365 ++p;
9366 else
9367 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009368 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009369 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 if (errormsg != NULL)
9371 {
9372 if (*errormsg)
9373 emsg(errormsg);
9374 vim_free(result);
9375 return NULL;
9376 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009377 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 {
9379 p += srclen;
9380 continue;
9381 }
9382 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9383 newres = alloc(len);
9384 if (newres == NULL)
9385 {
9386 vim_free(repl);
9387 vim_free(result);
9388 return NULL;
9389 }
9390 mch_memmove(newres, result, (size_t)(p - result));
9391 STRCPY(newres + (p - result), repl);
9392 len = (int)STRLEN(newres);
9393 STRCAT(newres, p + srclen);
9394 vim_free(repl);
9395 vim_free(result);
9396 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009397 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 }
9399 }
9400
9401 return result;
9402}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009405/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009406 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009407 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009410dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 if (fname == NULL)
9413 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009414 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415}
9416#endif
9417
9418/*
9419 * ":behave {mswin,xterm}"
9420 */
9421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009422ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423{
9424 if (STRCMP(eap->arg, "mswin") == 0)
9425 {
9426 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9427 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9428 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9429 set_option_value((char_u *)"keymodel", 0L,
9430 (char_u *)"startsel,stopsel", 0);
9431 }
9432 else if (STRCMP(eap->arg, "xterm") == 0)
9433 {
9434 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9435 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9436 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9437 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9438 }
9439 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009440 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441}
9442
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443static int filetype_detect = FALSE;
9444static int filetype_plugin = FALSE;
9445static int filetype_indent = FALSE;
9446
9447/*
9448 * ":filetype [plugin] [indent] {on,off,detect}"
9449 * on: Load the filetype.vim file to install autocommands for file types.
9450 * off: Load the ftoff.vim file to remove all autocommands for file types.
9451 * plugin on: load filetype.vim and ftplugin.vim
9452 * plugin off: load ftplugof.vim
9453 * indent on: load filetype.vim and indent.vim
9454 * indent off: load indoff.vim
9455 */
9456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009457ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458{
9459 char_u *arg = eap->arg;
9460 int plugin = FALSE;
9461 int indent = FALSE;
9462
9463 if (*eap->arg == NUL)
9464 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009465 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009466 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 filetype_detect ? "ON" : "OFF",
9468 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9469 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9470 return;
9471 }
9472
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009473 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 for (;;)
9475 {
9476 if (STRNCMP(arg, "plugin", 6) == 0)
9477 {
9478 plugin = TRUE;
9479 arg = skipwhite(arg + 6);
9480 continue;
9481 }
9482 if (STRNCMP(arg, "indent", 6) == 0)
9483 {
9484 indent = TRUE;
9485 arg = skipwhite(arg + 6);
9486 continue;
9487 }
9488 break;
9489 }
9490 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9491 {
9492 if (*arg == 'o' || !filetype_detect)
9493 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009494 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 filetype_detect = TRUE;
9496 if (plugin)
9497 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009498 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 filetype_plugin = TRUE;
9500 }
9501 if (indent)
9502 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009503 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 filetype_indent = TRUE;
9505 }
9506 }
9507 if (*arg == 'd')
9508 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009509 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009510 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 }
9512 }
9513 else if (STRCMP(arg, "off") == 0)
9514 {
9515 if (plugin || indent)
9516 {
9517 if (plugin)
9518 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009519 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 filetype_plugin = FALSE;
9521 }
9522 if (indent)
9523 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009524 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 filetype_indent = FALSE;
9526 }
9527 }
9528 else
9529 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009530 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 filetype_detect = FALSE;
9532 }
9533 }
9534 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009535 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536}
9537
9538/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009539 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 */
9541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009542ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543{
9544 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009545 {
9546 char_u *arg = eap->arg;
9547
9548 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9549 arg += 9;
9550
9551 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9552 if (arg != eap->arg)
9553 did_filetype = FALSE;
9554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009558ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559{
9560#ifdef FEAT_DIGRAPHS
9561 if (*eap->arg != NUL)
9562 putdigraph(eap->arg);
9563 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009564 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565#else
mityu61065042021-07-19 20:07:21 +02009566 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567#endif
9568}
9569
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009570#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9571 void
9572set_no_hlsearch(int flag)
9573{
9574 no_hlsearch = flag;
9575# ifdef FEAT_EVAL
9576 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9577# endif
9578}
9579
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580/*
9581 * ":nohlsearch"
9582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009584ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009586 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009587 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589#endif
9590
9591#ifdef FEAT_CRYPT
9592/*
9593 * ":X": Get crypt key
9594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009596ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009598 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009599 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600}
9601#endif
9602
9603#ifdef FEAT_FOLDING
9604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009605ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606{
9607 if (foldManualAllowed(TRUE))
9608 foldCreate(eap->line1, eap->line2);
9609}
9610
9611 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009612ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
9614 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9615 eap->forceit, FALSE);
9616}
9617
9618 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009619ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620{
9621 linenr_T lnum;
9622
Bram Moolenaar4facea32019-10-12 20:17:40 +02009623# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009624 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009625# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009626
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009627 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9629 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9630 ml_setmarked(lnum);
9631
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009632 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009634 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009635# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009636 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638}
9639#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009641#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009642/*
9643 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9644 * instead of a quickfix command.
9645 */
9646 int
9647is_loclist_cmd(int cmdidx)
9648{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009649 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009650 return FALSE;
9651 return cmdnames[cmdidx].cmd_name[0] == 'l';
9652}
9653#endif
9654
Bram Moolenaar4facea32019-10-12 20:17:40 +02009655#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009656 int
9657get_pressedreturn(void)
9658{
9659 return ex_pressedreturn;
9660}
9661
9662 void
9663set_pressedreturn(int val)
9664{
9665 ex_pressedreturn = val;
9666}
9667#endif