blob: 697337cad95750af3cbdb67dc41aa083f2a5947d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020071static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_cquit(exarg_T *eap);
91static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_close(exarg_T *eap);
93static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
94static void ex_only(exarg_T *eap);
95static void ex_resize(exarg_T *eap);
96static void ex_stag(exarg_T *eap);
97static void ex_tabclose(exarg_T *eap);
98static void ex_tabonly(exarg_T *eap);
99static void ex_tabnext(exarg_T *eap);
100static void ex_tabmove(exarg_T *eap);
101static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200102#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100103static void ex_pclose(exarg_T *eap);
104static void ex_ptag(exarg_T *eap);
105static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#else
107# define ex_pclose ex_ni
108# define ex_ptag ex_ni
109# define ex_pedit ex_ni
110#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100111static void ex_hide(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100112static void ex_exit(exarg_T *eap);
113static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100115static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#else
117# define ex_goto ex_ni
118#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100119static void ex_shell(exarg_T *eap);
120static void ex_preserve(exarg_T *eap);
121static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_mode(exarg_T *eap);
123static void ex_wrongmodifier(exarg_T *eap);
124static void ex_find(exarg_T *eap);
125static void ex_open(exarg_T *eap);
126static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifndef FEAT_GUI
128# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100129static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100131#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#else
134# define ex_tearoff ex_ni
135#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100136#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
137 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100138static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#else
140# define ex_popup ex_ni
141#endif
142#ifndef FEAT_GUI_MSWIN
143# define ex_simalt ex_ni
144#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000145#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146# define gui_mch_find_dialog ex_ni
147# define gui_mch_replace_dialog ex_ni
148#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000149#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150# define ex_helpfind ex_ni
151#endif
152#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100153# define ex_cscope ex_ni
154# define ex_scscope ex_ni
155# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
157#ifndef FEAT_SYN_HL
158# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200159# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000160#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200161#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200162# define ex_syntime ex_ni
163#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000164#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000165# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000166# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000167# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000168# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000169# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000170#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200171#ifndef FEAT_PERSISTENT_UNDO
172# define ex_rundo ex_ni
173# define ex_wundo ex_ni
174#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200175#ifndef FEAT_LUA
176# define ex_lua ex_script_ni
177# define ex_luado ex_ni
178# define ex_luafile ex_ni
179#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000180#ifndef FEAT_MZSCHEME
181# define ex_mzscheme ex_script_ni
182# define ex_mzfile ex_ni
183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#ifndef FEAT_PERL
185# define ex_perl ex_script_ni
186# define ex_perldo ex_ni
187#endif
188#ifndef FEAT_PYTHON
189# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200190# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191# define ex_pyfile ex_ni
192#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200193#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200194# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200195# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196# define ex_py3file ex_ni
197#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100198#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
199# define ex_pyx ex_script_ni
200# define ex_pyxdo ex_ni
201# define ex_pyxfile ex_ni
202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#ifndef FEAT_TCL
204# define ex_tcl ex_script_ni
205# define ex_tcldo ex_ni
206# define ex_tclfile ex_ni
207#endif
208#ifndef FEAT_RUBY
209# define ex_ruby ex_script_ni
210# define ex_rubydo ex_ni
211# define ex_rubyfile ex_ni
212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#ifndef FEAT_KEYMAP
214# define ex_loadkeymap ex_ni
215#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100216static void ex_swapname(exarg_T *eap);
217static void ex_syncbind(exarg_T *eap);
218static void ex_read(exarg_T *eap);
219static void ex_pwd(exarg_T *eap);
220static void ex_equal(exarg_T *eap);
221static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000224#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#else
227# define ex_winpos ex_ni
228#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static void ex_operators(exarg_T *eap);
230static void ex_put(exarg_T *eap);
231static void ex_copymove(exarg_T *eap);
232static void ex_submagic(exarg_T *eap);
233static void ex_join(exarg_T *eap);
234static void ex_at(exarg_T *eap);
235static void ex_bang(exarg_T *eap);
236static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200237#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ex_wundo(exarg_T *eap);
239static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200240#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ex_redo(exarg_T *eap);
242static void ex_later(exarg_T *eap);
243static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100245static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_startinsert(exarg_T *eap);
249static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_checkpath(exarg_T *eap);
252static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#else
254# define ex_findpat ex_ni
255# define ex_checkpath ex_ni
256#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200257#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#else
260# define ex_psearch ex_ni
261#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ex_tag(exarg_T *eap);
263static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200265# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200266# define ex_break ex_ni
267# define ex_breakadd ex_ni
268# define ex_breakdel ex_ni
269# define ex_breaklist ex_ni
270# define ex_call ex_ni
271# define ex_catch ex_ni
272# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200273# define ex_continue ex_ni
274# define ex_debug ex_ni
275# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200277# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# define ex_echo ex_ni
280# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200282# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200283# define ex_endfunction ex_ni
284# define ex_endif ex_ni
285# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
Bram Moolenaar340c59e2021-04-24 20:37:03 +0200289# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200290# define ex_finally ex_ni
291# define ex_finish ex_ni
292# define ex_function ex_ni
293# define ex_if ex_ni
294# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200295# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200296# define ex_lockvar ex_ni
297# define ex_oldfiles ex_ni
298# define ex_options ex_ni
299# define ex_packadd ex_ni
300# define ex_packloadall ex_ni
301# define ex_return ex_ni
302# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_throw ex_ni
304# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200306# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307# define ex_import ex_ni
308# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200310#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# define ex_loadview ex_ni
312#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200313#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# define ex_viminfo ex_ni
315#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_filetype(exarg_T *eap);
318static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000320# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321# define ex_diffpatch ex_ni
322# define ex_diffgetput ex_ni
323# define ex_diffsplit ex_ni
324# define ex_diffthis ex_ni
325# define ex_diffupdate ex_ni
326#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#else
331# define ex_nohlsearch ex_ni
332# define ex_match ex_ni
333#endif
334#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#else
337# define ex_X ex_ni
338#endif
339#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100340static void ex_fold(exarg_T *eap);
341static void ex_foldopen(exarg_T *eap);
342static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_fold ex_ni
345# define ex_foldopen ex_ni
346# define ex_folddo ex_ni
347#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100348#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349# define ex_language ex_ni
350#endif
351#ifndef FEAT_SIGNS
352# define ex_sign ex_ni
353#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaar05159a02005-02-26 23:04:13 +0000360#ifndef FEAT_PROFILE
361# define ex_profile ex_ni
362#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200363#ifndef FEAT_TERMINAL
364# define ex_terminal ex_ni
365#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200366#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
367# define ex_xrestore ex_ni
368#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100369#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200370# define ex_popupclear ex_ni
371#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373/*
374 * Declare cmdnames[].
375 */
376#define DO_DECLARE_EXCMD
377#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200378#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380static char_u dollar_command[2] = {'$', 0};
381
382
383#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100384// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385typedef struct
386{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100387 char_u *line; // command line
388 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389} wcmd_T;
390
391/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000392 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000394 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100398 garray_T *lines_gap; // growarray with line info
399 int current_line; // last read line from growarray
400 int repeating; // TRUE when looping a second time
401 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200402 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 void *cookie;
404};
405
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200406static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100407static int store_loop_line(garray_T *gap, char_u *line);
408static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000409
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100410// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000411struct dbg_stuff
412{
413 int trylevel;
414 int force_abort;
415 except_T *caught_stack;
416 char_u *vv_exception;
417 char_u *vv_throwpoint;
418 int did_emsg;
419 int got_int;
420 int did_throw;
421 int need_rethrow;
422 int check_cstack;
423 except_T *current_exception;
424};
425
Bram Moolenaared203462004-06-16 11:19:22 +0000426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100427save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000428{
429 dsp->trylevel = trylevel; trylevel = 0;
430 dsp->force_abort = force_abort; force_abort = FALSE;
431 dsp->caught_stack = caught_stack; caught_stack = NULL;
432 dsp->vv_exception = v_exception(NULL);
433 dsp->vv_throwpoint = v_throwpoint(NULL);
434
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100435 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000436 dsp->did_emsg = did_emsg; did_emsg = FALSE;
437 dsp->got_int = got_int; got_int = FALSE;
438 dsp->did_throw = did_throw; did_throw = FALSE;
439 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
440 dsp->check_cstack = check_cstack; check_cstack = FALSE;
441 dsp->current_exception = current_exception; current_exception = NULL;
442}
443
444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100445restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000446{
447 suppress_errthrow = FALSE;
448 trylevel = dsp->trylevel;
449 force_abort = dsp->force_abort;
450 caught_stack = dsp->caught_stack;
451 (void)v_exception(dsp->vv_exception);
452 (void)v_throwpoint(dsp->vv_throwpoint);
453 did_emsg = dsp->did_emsg;
454 got_int = dsp->got_int;
455 did_throw = dsp->did_throw;
456 need_rethrow = dsp->need_rethrow;
457 check_cstack = dsp->check_cstack;
458 current_exception = dsp->current_exception;
459}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460#endif
461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462/*
463 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
464 * command is given.
465 */
466 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100467do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100468 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469{
470 int save_msg_scroll;
471 int prev_msg_row;
472 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100473 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000474
475 if (improved)
476 exmode_active = EXMODE_VIM;
477 else
478 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100479 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100480 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000481
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100482 // When using ":global /pat/ visual" and then "Q" we return to continue
483 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000484 if (global_busy)
485 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
487 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100488 ++RedrawingDisabled; // don't redisplay the window
489 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 ++hold_gui_events;
493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
Bram Moolenaar32526b32019-01-19 17:43:09 +0100495 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 while (exmode_active)
497 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100498 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000499 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
500 {
501 exmode_active = FALSE;
502 break;
503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 msg_scroll = TRUE;
505 need_wait_return = FALSE;
506 ex_pressedreturn = FALSE;
507 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100508 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 prev_msg_row = msg_row;
510 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (improved)
512 {
513 cmdline_row = msg_row;
514 do_cmdline(NULL, getexline, NULL, 0);
515 }
516 else
517 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
518 lines_left = Rows - 1;
519
Bram Moolenaardf177f62005-02-22 08:39:57 +0000520 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100521 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000524 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if (ex_pressedreturn)
528 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100529 // go up one line, to overwrite the ":<CR>" line, so the
530 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 msg_row = prev_msg_row;
532 if (prev_msg_row == Rows - 1)
533 msg_row--;
534 }
535 msg_col = 0;
536 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
537 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100540 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000541 {
542 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000543 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000545 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
548
549#ifdef FEAT_GUI
550 --hold_gui_events;
551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 --RedrawingDisabled;
553 --no_wait_return;
554 update_screen(CLEAR);
555 need_wait_return = FALSE;
556 msg_scroll = save_msg_scroll;
557}
558
559/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200560 * Print the executed command for when 'verbose' is set.
561 * When "lnum" is 0 only print the command.
562 */
563 static void
564msg_verbose_cmd(linenr_T lnum, char_u *cmd)
565{
566 ++no_wait_return;
567 verbose_enter_scroll();
568
569 if (lnum == 0)
570 smsg(_("Executing: %s"), cmd);
571 else
572 smsg(_("line %ld: %s"), (long)lnum, cmd);
573 if (msg_silent == 0)
574 msg_puts("\n"); // don't overwrite this
575
576 verbose_leave_scroll();
577 --no_wait_return;
578}
579
580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 * Execute a simple command line. Used for translated commands like "*".
582 */
583 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100584do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 return do_cmdline(cmd, NULL, NULL,
587 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
588}
589
590/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100591 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
592 * This allows for using a range without ":" in Vim9 script.
593 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200594 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100595do_cmd_argument(char_u *cmd)
596{
597 return do_cmdline(cmd, NULL, NULL,
598 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
599}
600
601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 * do_cmdline(): execute one Ex command line
603 *
604 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * 2. Split up in parts separated with '|'.
607 *
608 * This function can be called recursively!
609 *
610 * flags:
611 * DOCMD_VERBOSE - The command will be included in the error message.
612 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100613 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 * DOCMD_KEYTYPED - Don't reset KeyTyped.
615 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
616 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
617 *
618 * return FAIL if cmdline could not be executed, OK otherwise
619 */
620 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100621do_cmdline(
622 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200623 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100625 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100627 char_u *next_cmdline; // next cmd to execute
628 char_u *cmdline_copy = NULL; // copy of cmd line
629 int used_getline = FALSE; // used "fgetline" to obtain command
630 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 int count = 0; // line number count
633 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 int retval = OK;
635#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100636 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100637 garray_T lines_ga; // keep lines for ":while"/":for"
638 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200639 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 char_u *fname = NULL; // function or script name
641 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
642 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
643 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200645 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200646 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100648 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200649 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000651 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000653 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100655# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656# define cmd_cookie cookie
657#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100658 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200659#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100660 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
661 // location for storing error messages to be converted to an exception.
662 // This ensures that the do_errthrow() call in do_one_cmd() does not
663 // combine the messages stored by an earlier invocation of do_one_cmd()
664 // with the command name of the later one. This would happen when
665 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 saved_msg_list = msg_list;
667 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668#endif
669
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100670 // It's possible to create an endless loop with ":execute", catch that
671 // here. The value of 200 allows nested function calls, ":source", etc.
672 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100673 if (call_depth >= 200
674#ifdef FEAT_EVAL
675 && call_depth >= p_mfd
676#endif
677 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000679 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100681 // When converting to an exception, we do not include the command name
682 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100683 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 msg_list = saved_msg_list;
685#endif
686 return FAIL;
687 }
688 ++call_depth;
689
690#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100691 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000693 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100695 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100697 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100698 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ++ex_nesting_level;
701
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100702 // Get the function or script name and the address where the next breakpoint
703 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000704 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 fname = func_name(real_cookie);
707 breakpoint = func_breakpoint(real_cookie);
708 dbg_tick = func_dbg_tick(real_cookie);
709 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100710 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100712 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 breakpoint = source_breakpoint(real_cookie);
714 dbg_tick = source_dbg_tick(real_cookie);
715 }
716
717 /*
718 * Initialize "force_abort" and "suppress_errthrow" at the top level.
719 */
720 if (!recursive)
721 {
722 force_abort = FALSE;
723 suppress_errthrow = FALSE;
724 }
725
726 /*
727 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000728 * exception handling (used when debugging). Otherwise clear it to avoid
729 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000731 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000732 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000733 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200734 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
736 initial_trylevel = trylevel;
737
738 /*
739 * "did_throw" will be set to TRUE when an exception is being thrown.
740 */
741 did_throw = FALSE;
742#endif
743 /*
744 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000745 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * If force_abort is set, we cancel everything.
747 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100748#ifdef FEAT_EVAL
749 did_emsg_cumul += did_emsg;
750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 did_emsg = FALSE;
752
753 /*
754 * KeyTyped is only set when calling vgetc(). Reset it here when not
755 * calling vgetc() (sourced command lines).
756 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100757 if (!(flags & DOCMD_KEYTYPED)
758 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 KeyTyped = FALSE;
760
761 /*
762 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000763 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 * - for multiple commands on one line, separated with '|'
765 * - when repeating until there are no more lines (for ":source")
766 */
767 next_cmdline = cmdline;
768 do
769 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100771 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772#endif
773
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100774 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (next_cmdline == NULL
776#ifdef FEAT_EVAL
777 && !force_abort
778 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100782 {
783#ifdef FEAT_EVAL
784 did_emsg_cumul += did_emsg;
785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000790 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100791 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 * 3. If a line is given: Make a copy, so we can mess with it.
793 */
794
795#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100796 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000797 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100799 // Each '|' separated command is stored separately in lines_ga, to
800 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100801 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100803 // Check if a function has returned or, unless it has an unclosed
804 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000808 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809 func_line_end(real_cookie);
810# endif
811 if (func_has_ended(real_cookie))
812 {
813 retval = FAIL;
814 break;
815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000818 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100819 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820 script_line_end();
821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100823 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {
826 retval = FAIL;
827 break;
828 }
829
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100830 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 if (breakpoint != NULL && dbg_tick != NULL
832 && *dbg_tick != debug_tick)
833 {
834 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100835 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100836 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 *dbg_tick = debug_tick;
838 }
839
840 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100841 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100843 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100845 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100847 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100848 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100850 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100851 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 *dbg_tick = debug_tick;
853 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000855 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856 {
857 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100858 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100859 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860 script_line_start();
861 }
862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#endif
865
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100866 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (next_cmdline == NULL)
868 {
869 /*
870 * Need to set msg_didout for the first line after an ":if",
871 * otherwise the ":if" will be overwritten.
872 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100873 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100875 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#ifdef FEAT_EVAL
877 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
878#else
879 0
880#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100881 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
882 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100884 // Don't call wait_return for aborted command line. The NULL
885 // returned for the end of a sourced file or executed function
886 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (KeyTyped && !(flags & DOCMD_REPEAT))
888 need_wait_return = FALSE;
889 retval = FAIL;
890 break;
891 }
892 used_getline = TRUE;
893
894 /*
895 * Keep the first typed line. Clear it when more lines are typed.
896 */
897 if (flags & DOCMD_KEEPLINE)
898 {
899 vim_free(repeat_cmdline);
900 if (count == 0)
901 repeat_cmdline = vim_strsave(next_cmdline);
902 else
903 repeat_cmdline = NULL;
904 }
905 }
906
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100907 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 else if (cmdline_copy == NULL)
909 {
910 next_cmdline = vim_strsave(next_cmdline);
911 if (next_cmdline == NULL)
912 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200913 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 retval = FAIL;
915 break;
916 }
917 }
918 cmdline_copy = next_cmdline;
919
920#ifdef FEAT_EVAL
921 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200922 * Inside a while/for loop, and when the command looks like a ":while"
923 * or ":for", the line is stored, because we may need it later when
924 * looping.
925 *
926 * When there is a '|' and another command, it is stored separately,
927 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000928 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200929 *
930 * Pass a different "fgetline" function to do_one_cmd() below,
931 * that it stores lines in or reads them from "lines_ga". Makes it
932 * possible to define a function inside a while/for loop and handles
933 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200935 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200937 cmd_getline = get_loop_line;
938 cmd_cookie = (void *)&cmd_loop_cookie;
939 cmd_loop_cookie.lines_gap = &lines_ga;
940 cmd_loop_cookie.current_line = current_line;
941 cmd_loop_cookie.getline = fgetline;
942 cmd_loop_cookie.cookie = cookie;
943 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
944
945 // Save the current line when encountering it the first time.
946 if (current_line == lines_ga.ga_len
947 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
949 retval = FAIL;
950 break;
951 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200952 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200954 else
955 {
956 cmd_getline = fgetline;
957 cmd_cookie = cookie;
958 }
959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 did_endif = FALSE;
961#endif
962
963 if (count++ == 0)
964 {
965 /*
966 * All output from the commands is put below each other, without
967 * waiting for a return. Don't do this when executing commands
968 * from a script or when being called recursive (e.g. for ":e
969 * +command file").
970 */
971 if (!(flags & DOCMD_NOWAIT) && !recursive)
972 {
973 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100974 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100976 msg_scroll = TRUE; // put messages below each other
977 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 ++RedrawingDisabled;
979 did_inc = TRUE;
980 }
981 }
982
Bram Moolenaar823654b2020-05-29 23:03:09 +0200983 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100984 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 /*
987 * 2. Execute one '|' separated command.
988 * do_one_cmd() will return NULL if there is no trailing '|'.
989 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
990 */
991 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100992 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#ifdef FEAT_EVAL
994 &cstack,
995#endif
996 cmd_getline, cmd_cookie);
997 --recursive;
998
999#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001000 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001001 // Use "current_line" from "cmd_loop_cookie", it may have been
1002 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001003 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#endif
1005
1006 if (next_cmdline == NULL)
1007 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001008 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 /*
1011 * If the command was typed, remember it for the ':' register.
1012 * Do this AFTER executing the command to make :@: work.
1013 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 && new_last_cmdline != NULL)
1016 {
1017 vim_free(last_cmdline);
1018 last_cmdline = new_last_cmdline;
1019 new_last_cmdline = NULL;
1020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 }
1022 else
1023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001024 // need to copy the command after the '|' to cmdline_copy, for the
1025 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001026 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 next_cmdline = cmdline_copy;
1028 }
1029
1030
1031#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001032 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001034 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001036 {
1037 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
1043 ++current_line;
1044
1045 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 * An ":endwhile", ":endfor" and ":continue" is handled here.
1047 * If we were executing commands, jump back to the ":while" or
1048 * ":for".
1049 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 // Jump back to the matching ":while" or ":for". Be careful
1056 // not to use a cs_line[] from an entry that isn't a ":while"
1057 // or ":for": It would make "current_line" invalid and can
1058 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (!did_emsg && !got_int && !did_throw
1060 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 && (cstack.cs_flags[cstack.cs_idx]
1062 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 && cstack.cs_line[cstack.cs_idx] >= 0
1064 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1065 {
1066 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001067 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001069 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001071 // Check for the next breakpoint at or after the ":while"
1072 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (breakpoint != NULL)
1074 {
1075 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001076 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 fname,
1078 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1079 *dbg_tick = debug_tick;
1080 }
1081 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001084 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001086 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1087 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089 }
1090
1091 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001097 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 }
1100
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001101 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001102 if (breakpoint != NULL && has_watchexpr())
1103 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001104 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001105 *dbg_tick = debug_tick;
1106 }
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 /*
1109 * When not inside any ":while" loop, clear remembered lines.
1110 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
1113 if (lines_ga.ga_len > 0)
1114 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001115 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1117 free_cmdlines(&lines_ga);
1118 }
1119 current_line = 0;
1120 }
1121
1122 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1124 * being restored at the ":endtry". Reset them here and set the
1125 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1126 * This includes the case where a missing ":endif", ":endwhile" or
1127 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 cstack.cs_lflags &= ~CSL_HAD_FINA;
1132 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1133 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 did_throw ? (void *)current_exception : NULL);
1135 did_emsg = got_int = did_throw = FALSE;
1136 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1137 }
1138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 // Update global "trylevel" for recursive calls to do_cmdline() from
1140 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 trylevel = initial_trylevel + cstack.cs_trylevel;
1142
1143 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001144 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 * files) is aborted because of an error, an interrupt, or an uncaught
1146 * exception, cancel everything. If it is left normally, reset
1147 * force_abort to get the non-EH compatible abortion behavior for
1148 * the rest of the script.
1149 */
1150 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1151 force_abort = FALSE;
1152
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001153 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001155#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
1157 }
1158 /*
1159 * Continue executing command lines when:
1160 * - no CTRL-C typed, no aborting error, no exception thrown or try
1161 * conditionals need to be checked for executing finally clauses or
1162 * catching an interrupt exception
1163 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001164 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 * looping for ":source" command or function call.
1166 */
1167 while (!((got_int
1168#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001169 || (did_emsg && (force_abort || in_vim9script()))
1170 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#endif
1172 )
1173#ifdef FEAT_EVAL
1174 && cstack.cs_trylevel == 0
1175#endif
1176 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001177 && !(did_emsg
1178#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001179 // Keep going when inside try/catch, so that the error can be
1180 // deal with, except when it is a syntax error, it may cause
1181 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001182 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1183#endif
1184 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001185 && (getline_equal(fgetline, cookie, getexmodeline)
1186 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 && (next_cmdline != NULL
1188#ifdef FEAT_EVAL
1189 || cstack.cs_idx >= 0
1190#endif
1191 || (flags & DOCMD_REPEAT)));
1192
1193 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001194 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#ifdef FEAT_EVAL
1196 free_cmdlines(&lines_ga);
1197 ga_clear(&lines_ga);
1198
1199 if (cstack.cs_idx >= 0)
1200 {
1201 /*
1202 * If a sourced file or executed function ran to its end, report the
1203 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001204 * In Vim9 script do not give a second error, executing aborts after
1205 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001207 if (!got_int && !did_throw && !aborting()
1208 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001209 && ((getline_equal(fgetline, cookie, getsourceline)
1210 && !source_finished(fgetline, cookie))
1211 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 && !func_has_ended(real_cookie))))
1213 {
1214 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001215 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001217 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001219 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001221 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223
1224 /*
1225 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1226 * ":endtry" in a sourced file or executed function. If the try
1227 * conditional is in its finally clause, ignore anything pending.
1228 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001229 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 */
1231 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001232 {
1233 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1234
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001235 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001236 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001237 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1238 &cstack.cs_looplevel);
1239 }
1240 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 trylevel = initial_trylevel;
1242 }
1243
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001244 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1245 // lack was reported above and the error message is to be converted to an
1246 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001247 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 ? (char_u *)"endfunction" : (char_u *)NULL);
1249
1250 if (trylevel == 0)
1251 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001252 // Just in case did_throw got set but current_exception wasn't.
1253 if (current_exception == NULL)
1254 did_throw = FALSE;
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 /*
1257 * When an exception is being thrown out of the outermost try
1258 * conditional, discard the uncaught exception, disable the conversion
1259 * of interrupts or errors to exceptions, and ensure that no more
1260 * commands are executed.
1261 */
1262 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001263 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265 /*
1266 * On an interrupt or an aborting error not converted to an exception,
1267 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001268 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 * when force_abort is set and did_emsg unset in case of an interrupt
1270 * from a finally clause after an error.
1271 */
1272 else if (got_int || (did_emsg && force_abort))
1273 suppress_errthrow = TRUE;
1274 }
1275
1276 /*
1277 * The current cstack will be freed when do_cmdline() returns. An uncaught
1278 * exception will have to be rethrown in the previous cstack. If a function
1279 * has just returned or a script file was just finished and the previous
1280 * cstack belongs to the same function or, respectively, script file, it
1281 * will have to be checked for finally clauses to be executed due to the
1282 * ":return" or ":finish". This is done in do_one_cmd().
1283 */
1284 if (did_throw)
1285 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001286 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001288 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 && ex_nesting_level > func_level(real_cookie) + 1))
1290 {
1291 if (!did_throw)
1292 check_cstack = TRUE;
1293 }
1294 else
1295 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001296 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001297 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 --ex_nesting_level;
1299 /*
1300 * Go to debug mode when returning from a function in which we are
1301 * single-stepping.
1302 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001303 if ((getline_equal(fgetline, cookie, getsourceline)
1304 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001306 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 ? (char_u *)_("End of sourced file")
1308 : (char_u *)_("End of function"));
1309 }
1310
1311 /*
1312 * Restore the exception environment (done after returning from the
1313 * debugger).
1314 */
1315 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001316 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317
1318 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001319
1320 // Cleanup if "cs_emsg_silent_list" remains.
1321 if (cstack.cs_emsg_silent_list != NULL)
1322 {
1323 eslist_T *elem, *temp;
1324
1325 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1326 {
1327 temp = elem->next;
1328 vim_free(elem);
1329 }
1330 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001331#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
1333 /*
1334 * If there was too much output to fit on the command line, ask the user to
1335 * hit return before redrawing the screen. With the ":global" command we do
1336 * this only once after the command is finished.
1337 */
1338 if (did_inc)
1339 {
1340 --RedrawingDisabled;
1341 --no_wait_return;
1342 msg_scroll = FALSE;
1343
1344 /*
1345 * When just finished an ":if"-":else" which was typed, no need to
1346 * wait for hit-return. Also for an error situation.
1347 */
1348 if (retval == FAIL
1349#ifdef FEAT_EVAL
1350 || (did_endif && KeyTyped && !did_emsg)
1351#endif
1352 )
1353 {
1354 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001355 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
1357 else if (need_wait_return)
1358 {
1359 /*
1360 * The msg_start() above clears msg_didout. The wait_return we do
1361 * here should not overwrite the command that may be shown before
1362 * doing that.
1363 */
1364 msg_didout |= msg_didout_before_start;
1365 wait_return(FALSE);
1366 }
1367 }
1368
Bram Moolenaar2a942252012-11-28 23:03:07 +01001369#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001370 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001371#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 /*
1373 * Reset if_level, in case a sourced script file contains more ":if" than
1374 * ":endif" (could be ":if x | foo | endif").
1375 */
1376 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001377#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 --call_depth;
1380 return retval;
1381}
1382
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001383#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001384/*
1385 * Handle when "did_throw" is set after executing commands.
1386 */
1387 void
1388handle_did_throw()
1389{
1390 char *p = NULL;
1391 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001392 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001393
1394 /*
1395 * If the uncaught exception is a user exception, report it as an
1396 * error. If it is an error exception, display the saved error
1397 * message now. For an interrupt exception, do nothing; the
1398 * interrupt message is given elsewhere.
1399 */
1400 switch (current_exception->type)
1401 {
1402 case ET_USER:
1403 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001404 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001405 current_exception->value);
1406 p = (char *)vim_strsave(IObuff);
1407 break;
1408 case ET_ERROR:
1409 messages = current_exception->messages;
1410 current_exception->messages = NULL;
1411 break;
1412 case ET_INTERRUPT:
1413 break;
1414 }
1415
1416 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1417 current_exception->throw_lnum);
1418 ESTACK_CHECK_SETUP
1419 current_exception->throw_name = NULL;
1420
1421 discard_current_exception(); // uses IObuff if 'verbose'
1422 suppress_errthrow = TRUE;
1423 force_abort = TRUE;
1424
1425 if (messages != NULL)
1426 {
1427 do
1428 {
1429 msglist_T *next = messages->next;
1430 int save_compiling = estack_compiling;
1431
1432 estack_compiling = messages->msg_compiling;
1433 emsg(messages->msg);
1434 vim_free(messages->msg);
1435 vim_free(messages->sfile);
1436 vim_free(messages);
1437 messages = next;
1438 estack_compiling = save_compiling;
1439 }
1440 while (messages != NULL);
1441 }
1442 else if (p != NULL)
1443 {
1444 emsg(p);
1445 vim_free(p);
1446 }
1447 vim_free(SOURCING_NAME);
1448 ESTACK_CHECK_NOW
1449 estack_pop();
1450}
1451
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001453 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 */
1455 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001456get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001458 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 wcmd_T *wp;
1460 char_u *line;
1461
1462 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1463 {
1464 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001465 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001467 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (cp->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001469 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001471 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001472 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 ++cp->current_line;
1474
1475 return line;
1476 }
1477
1478 KeyTyped = FALSE;
1479 ++cp->current_line;
1480 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001481 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 return vim_strsave(wp->line);
1483}
1484
1485/*
1486 * Store a line in "gap" so that a ":while" loop can execute it again.
1487 */
1488 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001489store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 if (ga_grow(gap, 1) == FAIL)
1492 return FAIL;
1493 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001494 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 return OK;
1497}
1498
1499/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001500 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
1502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001503free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 while (gap->ga_len > 0)
1506 {
1507 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1508 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 }
1510}
1511#endif
1512
1513/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001514 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1515 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001518getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001519 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001520 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001521 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001524 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001527 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1528 // function that's originally used to obtain the lines. This may be
1529 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001530 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001531 cp = (struct loop_cookie *)cookie;
1532 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
1534 gp = cp->getline;
1535 cp = cp->cookie;
1536 }
1537 return gp == func;
1538#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001539 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540#endif
1541}
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001544 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 * getline function. Otherwise return "cookie".
1546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001548getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001549 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001550 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
Bram Moolenaar13505972019-01-24 15:04:48 +01001552#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001553 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001554 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001556 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1557 // cookie that's originally used to obtain the lines. This may be nested
1558 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001559 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560 cp = (struct loop_cookie *)cookie;
1561 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 {
1563 gp = cp->getline;
1564 cp = cp->cookie;
1565 }
1566 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001567#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaard5053d02020-06-28 15:51:16 +02001572#if defined(FEAT_EVAL) || defined(PROT)
1573/*
1574 * Get the next line source line without advancing.
1575 */
1576 char_u *
1577getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001578 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001579 void *cookie) // argument for fgetline()
1580{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001581 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001582 struct loop_cookie *cp;
1583 wcmd_T *wp;
1584
1585 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1586 // cookie that's originally used to obtain the lines. This may be nested
1587 // several levels.
1588 gp = fgetline;
1589 cp = (struct loop_cookie *)cookie;
1590 while (gp == get_loop_line)
1591 {
1592 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1593 {
1594 // executing lines a second time, use the stored copy
1595 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1596 return wp->line;
1597 }
1598 gp = cp->getline;
1599 cp = cp->cookie;
1600 }
1601 if (gp == getsourceline)
1602 return source_nextline(cp);
1603 return NULL;
1604}
1605#endif
1606
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607
1608/*
1609 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1610 * ":bwipeout", etc.
1611 * Returns the buffer number.
1612 */
1613 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001614compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615{
1616 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001617 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001618 int count = offset;
1619
1620 buf = firstbuf;
1621 while (buf->b_next != NULL && buf->b_fnum < lnum)
1622 buf = buf->b_next;
1623 while (count != 0)
1624 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001625 count += (offset < 0) ? 1 : -1;
1626 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1627 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001629 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001630 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001631 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001632 while (buf->b_ml.ml_mfp == NULL)
1633 {
1634 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1635 if (nextbuf == NULL)
1636 break;
1637 buf = nextbuf;
1638 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001639 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001640 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001641 if (addr_type == ADDR_LOADED_BUFFERS)
1642 while (buf->b_ml.ml_mfp == NULL)
1643 {
1644 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1645 if (nextbuf == NULL)
1646 break;
1647 buf = nextbuf;
1648 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001649 return buf->b_fnum;
1650}
1651
Bram Moolenaarb7316892019-05-01 18:08:42 +02001652/*
1653 * Return the window number of "win".
1654 * When "win" is NULL return the number of windows.
1655 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001656 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001657current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001658{
1659 win_T *wp;
1660 int nr = 0;
1661
Bram Moolenaar29323592016-07-24 22:04:11 +02001662 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001663 {
1664 ++nr;
1665 if (wp == win)
1666 break;
1667 }
1668 return nr;
1669}
1670
1671 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001672current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001673{
1674 tabpage_T *tp;
1675 int nr = 0;
1676
Bram Moolenaar29323592016-07-24 22:04:11 +02001677 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001678 {
1679 ++nr;
1680 if (tp == tab)
1681 break;
1682 }
1683 return nr;
1684}
1685
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001686 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001687comment_start(char_u *p, int starts_with_colon UNUSED)
1688{
1689#ifdef FEAT_EVAL
1690 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001691 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001692#endif
1693 return *p == '"';
1694}
1695
Bram Moolenaarf240e182014-11-27 18:33:02 +01001696# define CURRENT_WIN_NR current_win_nr(curwin)
1697# define LAST_WIN_NR current_win_nr(NULL)
1698# define CURRENT_TAB_NR current_tab_nr(curtab)
1699# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701/*
1702 * Execute one Ex command.
1703 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001704 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1705 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 *
1707 * 1. skip comment lines and leading space
1708 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001709 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001710 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001711 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001712 * 6. parse arguments
1713 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001715 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 *
1717 * This function may be called recursively!
1718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001720do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001721 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001722 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001724 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001726 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001727 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001729 char_u *p;
1730 linenr_T lnum;
1731 long n;
1732 char *errormsg = NULL; // error message
1733 char_u *after_modifier = NULL;
1734 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001735 cmdmod_T save_cmdmod;
1736 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001737 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001738 int ni; // set when Not Implemented
1739 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001740 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001741#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001742 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001743 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001744 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001745#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001746 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001747 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaara80faa82020-04-12 19:37:17 +02001749 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 ea.line1 = 1;
1751 ea.line2 = 1;
1752#ifdef FEAT_EVAL
1753 ++ex_nesting_level;
1754#endif
1755
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001756 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 if (quitmore
1758#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001759 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001760 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001761#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001762 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001763 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 --quitmore;
1765
1766 /*
1767 * Reset browse, confirm, etc.. They are restored when returning, for
1768 * recursive calls.
1769 */
1770 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001772 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001773 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1774 goto doend;
1775
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001776/*
1777 * 1. Skip comment lines and leading white space and colons.
1778 * 2. Handle command modifiers.
1779 */
1780 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001782 ea.cmdlinep = cmdlinep;
1783 ea.getline = fgetline;
1784 ea.cookie = cookie;
1785#ifdef FEAT_EVAL
1786 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001787 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001789 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001790 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001791 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001792#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001793 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001794#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001795 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
1797#ifdef FEAT_EVAL
1798 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1799 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1800#else
1801 ea.skip = (if_level > 0);
1802#endif
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001805 * 3. Skip over the range to find the command. Let "p" point to after it.
1806 *
1807 * We need the command to know what kind of range it uses.
1808 */
1809 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001810#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001811 // In Vim9 script a colon is required before the range. This may also be
1812 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001813 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001814 {
1815 may_have_range = FALSE;
1816 for (p = ea.cmd; p >= *cmdlinep; --p)
1817 {
1818 if (*p == ':')
1819 may_have_range = TRUE;
1820 if (p < ea.cmd && !VIM_ISWHITE(*p))
1821 break;
1822 }
1823 }
1824 else
1825 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001826 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001827#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001828 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829
1830#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001831 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001832 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001833 if (ea.cmd == cmd + 1 && *cmd == '$')
1834 // should be "$VAR = val"
1835 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001836 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001837 if (ea.cmdidx == CMD_SIZE)
1838 {
1839 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1840
1841 // If a ':' before the range is missing, give a clearer error
1842 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001843 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001844 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001845 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001846 goto doend;
1847 }
1848 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 else
1851#endif
1852 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001853
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001854#ifdef FEAT_EVAL
1855# ifdef FEAT_PROFILE
1856 // Count this line for profiling if skip is TRUE.
1857 if (do_profiling == PROF_YES
1858 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1859 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1860 {
1861 int skip = did_emsg || got_int || did_throw;
1862
1863 if (ea.cmdidx == CMD_catch)
1864 skip = !skip && !(cstack->cs_idx >= 0
1865 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1866 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1867 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1868 skip = skip || !(cstack->cs_idx >= 0
1869 && !(cstack->cs_flags[cstack->cs_idx]
1870 & (CSF_ACTIVE | CSF_TRUE)));
1871 else if (ea.cmdidx == CMD_finally)
1872 skip = FALSE;
1873 else if (ea.cmdidx != CMD_endif
1874 && ea.cmdidx != CMD_endfor
1875 && ea.cmdidx != CMD_endtry
1876 && ea.cmdidx != CMD_endwhile)
1877 skip = ea.skip;
1878
1879 if (!skip)
1880 {
1881 if (getline_equal(fgetline, cookie, get_func_line))
1882 func_line_exec(getline_cookie(fgetline, cookie));
1883 else if (getline_equal(fgetline, cookie, getsourceline))
1884 script_line_exec();
1885 }
1886 }
1887# endif
1888
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001889 // May go to debug mode. If this happens and the ">quit" debug command is
1890 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001891 dbg_check_breakpoint(&ea);
1892 if (!ea.skip && got_int)
1893 {
1894 ea.skip = TRUE;
1895 (void)do_intthrow(cstack);
1896 }
1897#endif
1898
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001899/*
1900 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 *
1902 * where 'addr' is:
1903 *
1904 * % (entire file)
1905 * $ [+-NUM]
1906 * 'x [+-NUM] (where x denotes a currently defined mark)
1907 * . [+-NUM]
1908 * [+-NUM]..
1909 * NUM
1910 *
1911 * The ea.cmd pointer is updated to point to the first character following the
1912 * range spec. If an initial address is found, but no second, the upper bound
1913 * is equal to the lower.
1914 */
1915
Bram Moolenaar25190db2019-05-04 15:05:28 +02001916 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001917 if (!IS_USER_CMDIDX(ea.cmdidx))
1918 {
1919 if (ea.cmdidx != CMD_SIZE)
1920 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1921 else
1922 ea.addr_type = ADDR_LINES;
1923
Bram Moolenaar25190db2019-05-04 15:05:28 +02001924 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001925 if (ea.cmdidx == CMD_wincmd && p != NULL)
1926 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001927#ifdef FEAT_QUICKFIX
1928 // :.cc in quickfix window uses line number
1929 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1930 ea.addr_type = ADDR_OTHER;
1931#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001932 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001933
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001934 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001935#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001936 if (!may_have_range)
1937 ea.line1 = ea.line2 = default_address(&ea);
1938 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001939#endif
1940 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1941 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001944 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 */
1946
1947 /*
1948 * Skip ':' and any white space
1949 */
1950 ea.cmd = skipwhite(ea.cmd);
1951 while (*ea.cmd == ':')
1952 ea.cmd = skipwhite(ea.cmd + 1);
1953
1954 /*
1955 * If we got a line, but no command, then go to the line.
1956 * If we find a '|' or '\n' we set ea.nextcmd.
1957 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001958 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1959 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
1961 /*
1962 * strange vi behaviour:
1963 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001964 * ":3|..." prints line 3 (not in Vim9 script)
1965 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001967 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001969 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 goto doend;
1971 }
1972
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001973 // If this looks like an undefined user command and there are CmdUndefined
1974 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001975 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1976 && ASCII_ISUPPER(*ea.cmd)
1977 && has_cmdundefined())
1978 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001979 int ret;
1980
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001981 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001982 while (ASCII_ISALNUM(*p))
1983 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001984 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001985 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1986 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001987 // If the autocommands did something and didn't cause an error, try
1988 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001989 p = (ret
1990#ifdef FEAT_EVAL
1991 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001992#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001994 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 if (p == NULL)
1997 {
1998 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001999 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 goto doend;
2001 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002002 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002003 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2004 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 {
2006 errormsg = uc_fun_cmd();
2007 goto doend;
2008 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002009
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 if (ea.cmdidx == CMD_SIZE)
2011 {
2012 if (!ea.skip)
2013 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002014 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002016 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002017 // If the modifier was parsed OK the error must be in the
2018 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002019 if (after_modifier != NULL)
2020 append_command(after_modifier);
2021 else
2022 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002023 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002024 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002025 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002026 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
2028 goto doend;
2029 }
2030
Bram Moolenaar958636c2014-10-21 20:01:58 +02002031 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2032 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002033#ifdef HAVE_EX_SCRIPT_NI
2034 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2035#endif
2036 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038#ifndef FEAT_EVAL
2039 /*
2040 * When the expression evaluation is disabled, recognize the ":if" and
2041 * ":endif" commands and ignore everything in between it.
2042 */
2043 if (ea.cmdidx == CMD_if)
2044 ++if_level;
2045 if (if_level)
2046 {
2047 if (ea.cmdidx == CMD_endif)
2048 --if_level;
2049 goto doend;
2050 }
2051
2052#endif
2053
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002054 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002055 if (*p == '!' && ea.cmdidx != CMD_substitute
2056 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
2058 ++p;
2059 ea.forceit = TRUE;
2060 }
2061 else
2062 ea.forceit = FALSE;
2063
2064/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002065 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002067 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002068 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 if (!ea.skip)
2071 {
2072#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002073 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002075 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002076 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 goto doend;
2078 }
2079#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002080 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002081 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002082 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002083 goto doend;
2084 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002085 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002087 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002088 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 goto doend;
2090 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002091
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002092 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002094#ifdef FEAT_CMDWIN
2095 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2096 {
2097 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002098 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002099 goto doend;
2100 }
2101#endif
2102 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2103 {
2104 // Command not allowed when text is locked
2105 errormsg = _(get_text_locked_msg());
2106 goto doend;
2107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002110 // Disallow editing another buffer when "curbuf_lock" is set.
2111 // Do allow ":checktime" (it is postponed).
2112 // Do allow ":edit" (check for an argument later).
2113 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002114 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002115 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002116 && ea.cmdidx != CMD_edit
2117 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002118 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002119 && curbuf_locked())
2120 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002122 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002124 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 goto doend;
2126 }
2127 }
2128
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002129 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002131 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 goto doend;
2133 }
2134
2135 /*
2136 * Don't complain about the range if it is not used
2137 * (could happen if line_count is accidentally set to 0).
2138 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002139 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
2141 /*
2142 * If the range is backwards, ask for confirmation and, if given, swap
2143 * ea.line1 & ea.line2 so it's forwards again.
2144 * When global command is busy, don't ask, will fail below.
2145 */
2146 if (!global_busy && ea.line1 > ea.line2)
2147 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002148 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002150 if (sourcing || exmode_active)
2151 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002152 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002153 goto doend;
2154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 if (ask_yesno((char_u *)
2156 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002157 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159 lnum = ea.line1;
2160 ea.line1 = ea.line2;
2161 ea.line2 = lnum;
2162 }
2163 if ((errormsg = invalid_range(&ea)) != NULL)
2164 goto doend;
2165 }
2166
Bram Moolenaarb7316892019-05-01 18:08:42 +02002167 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2168 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 ea.line2 = 1;
2170
2171 correct_range(&ea);
2172
2173#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002174 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002175 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002177 // Put the first line at the start of a closed fold, put the last line
2178 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 (void)hasFolding(ea.line1, &ea.line1, NULL);
2180 (void)hasFolding(ea.line2, NULL, &ea.line2);
2181 }
2182#endif
2183
2184#ifdef FEAT_QUICKFIX
2185 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002186 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 * option here, so things like % get expanded.
2188 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002189 p = replace_makeprg(&ea, p, cmdlinep);
2190 if (p == NULL)
2191 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192#endif
2193
2194 /*
2195 * Skip to start of argument.
2196 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2197 */
2198 if (ea.cmdidx == CMD_bang)
2199 ea.arg = p;
2200 else
2201 ea.arg = skipwhite(p);
2202
Bram Moolenaar379fb762018-08-30 15:58:28 +02002203 // ":file" cannot be run with an argument when "curbuf_lock" is set
2204 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2205 goto doend;
2206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 /*
2208 * Check for "++opt=val" argument.
2209 * Must be first, allow ":w ++enc=utf8 !cmd"
2210 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002211 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2213 if (getargopt(&ea) == FAIL && !ni)
2214 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002215 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 goto doend;
2217 }
2218
2219 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002221 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002223 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002225 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 goto doend;
2227 }
2228 ea.arg = skipwhite(ea.arg + 1);
2229 ea.append = TRUE;
2230 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002231 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 {
2233 ++ea.arg;
2234 ea.usefilter = TRUE;
2235 }
2236 }
2237
2238 if (ea.cmdidx == CMD_read)
2239 {
2240 if (ea.forceit)
2241 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002242 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 ea.forceit = FALSE;
2244 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002245 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
2247 ++ea.arg;
2248 ea.usefilter = TRUE;
2249 }
2250 }
2251
2252 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2253 {
2254 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002255 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
2257 ++ea.arg;
2258 ++ea.amount;
2259 }
2260 ea.arg = skipwhite(ea.arg);
2261 }
2262
2263 /*
2264 * Check for "+command" argument, before checking for next command.
2265 * Don't do this for ":read !cmd" and ":write !cmd".
2266 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002267 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2269
2270 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002271 * For commands that do not use '|' inside their argument: Check for '|' to
2272 * separate commands and '"' or '#' to start comments.
2273 *
2274 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002275 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002276 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002277 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002279 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2280 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002281 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002282 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002283 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002284 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002285 || ea.cmdidx == CMD_global
2286 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002287 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002288#ifdef FEAT_EVAL
2289 || inside_block(&ea)
2290#endif
2291 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
2293 for (p = ea.arg; *p; ++p)
2294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002295 // Remove one backslash before a newline, so that it's possible to
2296 // pass a newline to the shell and also a newline that is preceded
2297 // with a backslash. This makes it impossible to end a shell
2298 // command in a backslash, but that doesn't appear useful.
2299 // Halving the number of backslashes is incompatible with previous
2300 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002302 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002303 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
2305 ea.nextcmd = p + 1;
2306 *p = NUL;
2307 break;
2308 }
2309 }
2310 }
2311
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002312 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002313 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002315 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002316 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002318 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002319 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002320 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002322#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002323 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002324 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002326 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002327 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002330 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2331 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002332 {
2333 ea.regname = *ea.arg++;
2334#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002335 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002336 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2337 {
kuuote08d7b1c2021-10-04 22:17:36 +01002338 if (!ea.skip)
2339 {
2340 set_expr_line(vim_strsave(ea.arg), &ea);
2341 did_set_expr_line = TRUE;
2342 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002343 ea.arg += STRLEN(ea.arg);
2344 }
2345#endif
2346 ea.arg = skipwhite(ea.arg);
2347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
2349
2350 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002351 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 * count, it's a buffer name.
2353 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002354 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002355 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002356 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002358 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002362 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 goto doend;
2364 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002365 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
2367 ea.line2 = n;
2368 if (ea.addr_count == 0)
2369 ea.addr_count = 1;
2370 }
2371 else
2372 {
2373 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002374 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002375 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002376 else
2377 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 ++ea.addr_count;
2379 /*
2380 * Be vi compatible: no error message for out of range.
2381 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002382 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 ea.line2 = curbuf->b_ml.ml_line_count;
2384 }
2385 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002386
2387 /*
2388 * Check for flags: 'l', 'p' and '#'.
2389 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002390 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002391 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002392 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2393 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002395 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002396 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 goto doend;
2398 }
2399
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002400 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002402 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 goto doend;
2404 }
2405
2406#ifdef FEAT_EVAL
2407 /*
2408 * Skip the command when it's not going to be executed.
2409 * The commands like :if, :endif, etc. always need to be executed.
2410 * Also make an exception for commands that handle a trailing command
2411 * themselves.
2412 */
2413 if (ea.skip)
2414 {
2415 switch (ea.cmdidx)
2416 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002417 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 case CMD_while:
2419 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002420 case CMD_for:
2421 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_if:
2423 case CMD_elseif:
2424 case CMD_else:
2425 case CMD_endif:
2426 case CMD_try:
2427 case CMD_catch:
2428 case CMD_finally:
2429 case CMD_endtry:
2430 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002431 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 break;
2433
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002434 // Commands that handle '|' themselves. Check: A command should
2435 // either have the EX_TRLBAR flag, appear in this list or appear in
2436 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 case CMD_aboveleft:
2438 case CMD_and:
2439 case CMD_belowright:
2440 case CMD_botright:
2441 case CMD_browse:
2442 case CMD_call:
2443 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002444 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 case CMD_delfunction:
2446 case CMD_djump:
2447 case CMD_dlist:
2448 case CMD_dsearch:
2449 case CMD_dsplit:
2450 case CMD_echo:
2451 case CMD_echoerr:
2452 case CMD_echomsg:
2453 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002454 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002456 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002457 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_help:
2459 case CMD_hide:
2460 case CMD_ijump:
2461 case CMD_ilist:
2462 case CMD_isearch:
2463 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002464 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_keepjumps:
2466 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002467 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 case CMD_leftabove:
2469 case CMD_let:
2470 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002471 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002472 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002474 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002475 case CMD_noautocmd:
2476 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 case CMD_perl:
2478 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002479 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002480 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002481 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 case CMD_return:
2483 case CMD_rightbelow:
2484 case CMD_ruby:
2485 case CMD_silent:
2486 case CMD_smagic:
2487 case CMD_snomagic:
2488 case CMD_substitute:
2489 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002490 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 case CMD_tcl:
2492 case CMD_throw:
2493 case CMD_tilde:
2494 case CMD_topleft:
2495 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002496 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002497 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 case CMD_verbose:
2499 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002500 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 break;
2502
2503 default: goto doend;
2504 }
2505 }
2506#endif
2507
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002508 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
2510 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2511 goto doend;
2512 }
2513
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 /*
2515 * Accept buffer name. Cannot be used at the same time with a buffer
2516 * number. Don't do this for a user command.
2517 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002518 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002519 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 /*
2522 * :bdelete, :bwipeout and :bunload take several arguments, separated
2523 * by spaces: find next space (skipping over escaped characters).
2524 * The others take one argument: ignore trailing spaces.
2525 */
2526 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2527 || ea.cmdidx == CMD_bunload)
2528 p = skiptowhite_esc(ea.arg);
2529 else
2530 {
2531 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002532 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 --p;
2534 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002535 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002536 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002537 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 goto doend;
2539 ea.addr_count = 1;
2540 ea.arg = skipwhite(p);
2541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002543 // The :try command saves the emsg_silent flag, reset it here when
2544 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002545 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002546 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002547 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002548 if (emsg_silent < 0)
2549 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002550 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002551 }
2552
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002554 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaar958636c2014-10-21 20:01:58 +02002557 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559 /*
2560 * Execute a user-defined command.
2561 */
2562 do_ucmd(&ea);
2563 }
2564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
2566 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002567 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 */
2569 ea.errmsg = NULL;
2570 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2571 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002572 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574
2575#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002577 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002578 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002579 && current_sctx.sc_sid > 0
2580 && ea.cmdidx != CMD_endif
2581 && (cstack->cs_idx < 0
2582 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002583 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 /*
2586 * If the command just executed called do_cmdline(), any throw or ":return"
2587 * or ":finish" encountered there must also check the cstack of the still
2588 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2589 * exception, or reanimate a returned function or finished script file and
2590 * return or finish it again.
2591 */
2592 if (need_rethrow)
2593 do_throw(cstack);
2594 else if (check_cstack)
2595 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002596 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002598 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 && current_func_returned())
2600 do_return(&ea, TRUE, FALSE, NULL);
2601 }
2602 need_rethrow = check_cstack = FALSE;
2603#endif
2604
2605doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002606 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002607 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002609 curwin->w_cursor.col = 0;
2610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
2612 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2613 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002614 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
2618 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002619 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002621 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
2623 emsg(errormsg);
2624 }
2625#ifdef FEAT_EVAL
2626 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002627 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2628 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002629
2630 if (did_set_expr_line)
2631 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632#endif
2633
Bram Moolenaare1004402020-10-24 20:49:43 +02002634 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002636 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002637 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002639 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 ea.nextcmd = NULL;
2641
2642#ifdef FEAT_EVAL
2643 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002644 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645#endif
2646
2647 return ea.nextcmd;
2648}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002650static char ex_error_buf[MSG_BUF_LEN];
2651
2652/*
2653 * Return an error message with argument included.
2654 * Uses a static buffer, only the last error will be kept.
2655 * "msg" will be translated, caller should use N_().
2656 */
2657 char *
2658ex_errmsg(char *msg, char_u *arg)
2659{
2660 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2661 return ex_error_buf;
2662}
2663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002665 * Handle a range without a command.
2666 * Returns an error message on failure.
2667 */
2668 char *
2669ex_range_without_command(exarg_T *eap)
2670{
2671 char *errormsg = NULL;
2672
2673 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2674#ifdef FEAT_EVAL
2675 && !in_vim9script()
2676#endif
2677 )
2678 {
2679 eap->cmdidx = CMD_print;
2680 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2681 if ((errormsg = invalid_range(eap)) == NULL)
2682 {
2683 correct_range(eap);
2684 ex_print(eap);
2685 }
2686 }
2687 else if (eap->addr_count != 0)
2688 {
2689 if (eap->line2 > curbuf->b_ml.ml_line_count)
2690 {
2691 // With '-' in 'cpoptions' a line number past the file is an
2692 // error, otherwise put it at the end of the file.
2693 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2694 eap->line2 = -1;
2695 else
2696 eap->line2 = curbuf->b_ml.ml_line_count;
2697 }
2698
2699 if (eap->line2 < 0)
2700 errormsg = _(e_invalid_range);
2701 else
2702 {
2703 if (eap->line2 == 0)
2704 curwin->w_cursor.lnum = 1;
2705 else
2706 curwin->w_cursor.lnum = eap->line2;
2707 beginline(BL_SOL | BL_FIX);
2708 }
2709 }
2710 return errormsg;
2711}
2712
2713/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002714 * Check for an Ex command with optional tail.
2715 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002716 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002717 */
2718 static int
2719checkforcmd_opt(
2720 char_u **pp, // start of command
2721 char *cmd, // name of command
2722 int len, // required length
2723 int noparen)
2724{
2725 int i;
2726
2727 for (i = 0; cmd[i] != NUL; ++i)
2728 if (((char_u *)cmd)[i] != (*pp)[i])
2729 break;
Bram Moolenaar68854a82022-01-31 18:59:13 +00002730 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_'
2731 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002732 {
2733 *pp = skipwhite(*pp + i);
2734 return TRUE;
2735 }
2736 return FALSE;
2737}
2738
2739/*
2740 * Check for an Ex command with optional tail.
2741 * If there is a match advance "pp" to the argument and return TRUE.
2742 */
2743 int
2744checkforcmd(
2745 char_u **pp, // start of command
2746 char *cmd, // name of command
2747 int len) // required length
2748{
2749 return checkforcmd_opt(pp, cmd, len, FALSE);
2750}
2751
2752/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002753 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002754 * If there is a match advance "pp" to the argument and return TRUE.
2755 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002756 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002757checkforcmd_noparen(
2758 char_u **pp, // start of command
2759 char *cmd, // name of command
2760 int len) // required length
2761{
2762 return checkforcmd_opt(pp, cmd, len, TRUE);
2763}
2764
2765/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002766 * Parse and skip over command modifiers:
2767 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002768 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002769 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002770 * When "skip_only" is TRUE the global variables are not changed, except for
2771 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002772 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2773 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002774 * Call apply_cmdmod() to get the side effects of the modifiers:
2775 * - Increment "sandbox" for ":sandbox"
2776 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002777 * - set msg_silent for ":silent"
2778 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002779 * Return FAIL when the command is not to be executed.
2780 * May set "errormsg" to an error message.
2781 */
2782 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002783parse_command_modifiers(
2784 exarg_T *eap,
2785 char **errormsg,
2786 cmdmod_T *cmod,
2787 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002788{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002789 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002790 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002791 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002792 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002793 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002794 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002795
Bram Moolenaare1004402020-10-24 20:49:43 +02002796 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002797 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002798
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002799 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2800 {
2801 // The automatically inserted Visual area range is skipped, so that
2802 // typing ":cmdmod cmd" in Visual mode works without having to move the
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002803 // range to after the modififiers. The command will be
2804 // "'<,'>cmdmod cmd", parse "cmdmod cmd" and then put back "'<,'>"
2805 // before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002806 eap->cmd += 5;
2807 cmd_start = eap->cmd;
2808 has_visual_range = TRUE;
2809 }
2810
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002811 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002812 for (;;)
2813 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002814 char_u *p;
2815
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002817 {
2818 if (*eap->cmd == ':')
2819 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002820 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002821 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002822
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002823 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002824 if (*eap->cmd == NUL && exmode_active
2825 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2826 || getline_equal(eap->getline, eap->cookie, getexline))
2827 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2828 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002829 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002830 if (!skip_only)
2831 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002832 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002833 }
2834
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002835 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002836 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002837 {
2838 // a comment ends at a NL
2839 if (eap->nextcmd == NULL)
2840 {
2841 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2842 if (eap->nextcmd != NULL)
2843 ++eap->nextcmd;
2844 }
Bram Moolenaarbc510062022-02-14 21:19:04 +00002845 if (vim9script && has_cmdmod(cmod, FALSE))
2846 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002848 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002849 if (*eap->cmd == NUL)
2850 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002851 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002852 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002853 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002854 if (vim9script && has_cmdmod(cmod, FALSE))
2855 *errormsg = _(e_command_modifier_without_command);
2856 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 return FAIL;
2858 }
2859
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002860 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002861
2862 // In Vim9 script a variable can shadow a command modifier:
2863 // verbose = 123
2864 // verbose += 123
2865 // silent! verbose = func()
2866 // verbose.member = 2
2867 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002868 // But not:
2869 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002870 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002871 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002872 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002873
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002874 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002875 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002876 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002877 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2878 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002879 break;
2880 }
2881
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 switch (*p)
2883 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002884 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002885 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002887 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002888 continue;
2889
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002890 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002891 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002892 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002893 continue;
2894 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002895 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002896 {
2897#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002898 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002899#endif
2900 continue;
2901 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002902 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002903 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002904 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002905 continue;
2906
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002907 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002908 break;
2909#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002910 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002911#endif
2912 continue;
2913
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002914 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002915 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002916 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002917 continue;
2918 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002919 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002920 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002921 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002922 continue;
2923 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002924 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002925 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002926 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002927 continue;
2928 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002929 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002930 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002931 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002932 continue;
2933
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002934 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002935 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002936 char_u *reg_pat;
2937 char_u *nulp = NULL;
2938 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002939
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002940 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002941 || *p == NUL
2942 || (ends_excmd(*p)
2943#ifdef FEAT_EVAL
2944 // in ":filter #pat# cmd" # does not
2945 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002946 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002947#endif
2948 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002949 break;
2950 if (*p == '!')
2951 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002952 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002953 p = skipwhite(p + 1);
2954 if (*p == NUL || ends_excmd(*p))
2955 break;
2956 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002957#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002958 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002959 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002960 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002961#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002962 if (skip_only)
2963 p = skip_vimgrep_pat(p, NULL, NULL);
2964 else
2965 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002966 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2967 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002968 if (p == NULL || *p == NUL)
2969 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002970 if (!skip_only)
2971 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002972 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002973 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002974 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002975 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002976 // restore the character overwritten by NUL
2977 if (nulp != NULL)
2978 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002979 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002980 eap->cmd = p;
2981 continue;
2982 }
2983
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002984 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002985 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002986 || *p == NUL || ends_excmd(*p))
2987 break;
2988 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002989 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002990 continue;
2991
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002992 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002993 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002994 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002995 continue;
2996 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002997 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2998 {
2999 if (ends_excmd2(p, eap->cmd))
3000 {
3001 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003002 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003003 return FAIL;
3004 }
3005 cmod->cmod_flags |= CMOD_LEGACY;
3006 continue;
3007 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003008
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003009 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003010 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003011 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003012 continue;
3013
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003014 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003015 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003016 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003017 continue;
3018 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003019 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003020 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003021 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003022 continue;
3023
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003024 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003025 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003026 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003027 continue;
3028
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003029 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003030 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003031 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003032 continue;
3033 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003034 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003035 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003036 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003037 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3038 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003039 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003040 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003041 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003042 }
3043 continue;
3044
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003045 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003046 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003047 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003048 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003049 long tabnr = get_address(eap, &eap->cmd,
3050 ADDR_TABS, eap->skip,
3051 skip_only, FALSE, 1);
3052 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003053 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003054 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003055 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003056 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3057 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003058 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003059 return FAIL;
3060 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003061 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003062 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003063 }
3064 eap->cmd = p;
3065 continue;
3066 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003067 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003068 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003069 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003070 continue;
3071
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003072 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003073 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003074 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003075 continue;
3076
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003077 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003078 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003079 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003080 continue;
3081 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003082 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003083 {
3084 if (ends_excmd2(p, eap->cmd))
3085 {
3086 *errormsg =
3087 _(e_vim9cmd_must_be_followed_by_command);
3088 return FAIL;
3089 }
3090 cmod->cmod_flags |= CMOD_VIM9CMD;
3091 continue;
3092 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003093 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003094 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003095 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003096 {
zeertzjqcd749632022-06-14 13:30:35 +01003097 // zero means not set, one is verbose == 0, etc.
3098 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003099 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003100 else
zeertzjqcd749632022-06-14 13:30:35 +01003101 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003102 eap->cmd = p;
3103 continue;
3104 }
3105 break;
3106 }
3107
Bram Moolenaar1501b632022-03-27 16:56:21 +01003108 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003109 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003110 if (eap->cmd > cmd_start)
3111 {
3112 // Move the '<,'> range to after the modifiers and insert a colon.
3113 // Since the modifiers have been parsed put the colon on top of the
3114 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3115 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003116 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003117 {
3118 size_t len = STRLEN(cmd_start);
3119
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003120 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003121 // "'<,'>mods" -> "mods *+
3122 // Use "*" instead of "'<,'>" to avoid the command getting
3123 // longer, in case is was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003124 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003125 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003126 }
3127 else
3128 {
3129 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3130 eap->cmd -= 5;
3131 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3132 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003133 }
3134 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003135 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003136 // Special case: change empty command to "+".
3137 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003138 eap->cmd = (char_u *)"'<,'>+";
3139 else
3140 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003141 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003142 else if (use_plus_cmd)
3143 eap->cmd = (char_u *)"+";
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003144
Bram Moolenaareffed932018-08-14 13:38:17 +02003145 return OK;
3146}
3147
3148/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003149 * Return TRUE if "cmod" has anything set.
3150 */
3151 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003152has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003153{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003154 return (cmod->cmod_flags != 0 && (!ignore_silent
3155 || (cmod->cmod_flags
3156 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003157 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003158 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003159 || cmod->cmod_tab != 0
3160 || cmod->cmod_filter_regmatch.regprog != NULL;
3161}
3162
Bram Moolenaar8991be22022-02-14 21:51:46 +00003163#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003164/*
3165 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3166 */
3167 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003168cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003169{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003170 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003171 {
3172 emsg(_(e_misplaced_command_modifier));
3173 return TRUE;
3174 }
3175 return FALSE;
3176}
Dominique Pelle748b3082022-01-08 12:41:16 +00003177#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003178
3179/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003180 * Apply the command modifiers. Saves current state in "cmdmod", call
3181 * undo_cmdmod() later.
3182 */
3183 void
3184apply_cmdmod(cmdmod_T *cmod)
3185{
3186#ifdef HAVE_SANDBOX
3187 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3188 {
3189 ++sandbox;
3190 cmod->cmod_did_sandbox = TRUE;
3191 }
3192#endif
zeertzjqcd749632022-06-14 13:30:35 +01003193 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003194 {
3195 if (cmod->cmod_verbose_save == 0)
3196 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003197 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003198 }
3199
3200 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3201 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003202 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003203 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003204 cmod->cmod_save_msg_scroll = msg_scroll;
3205 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003206 if (cmod->cmod_flags & CMOD_SILENT)
3207 ++msg_silent;
3208 if (cmod->cmod_flags & CMOD_UNSILENT)
3209 msg_silent = 0;
3210
3211 if (cmod->cmod_flags & CMOD_ERRSILENT)
3212 {
3213 ++emsg_silent;
3214 ++cmod->cmod_did_esilent;
3215 }
3216
Bram Moolenaare1004402020-10-24 20:49:43 +02003217 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003218 {
3219 // Set 'eventignore' to "all".
3220 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003221 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003222 set_string_option_direct((char_u *)"ei", -1,
3223 (char_u *)"all", OPT_FREE, SID_NONE);
3224 }
3225}
3226
3227/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003228 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003229 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003230 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003231undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003232{
Bram Moolenaare1004402020-10-24 20:49:43 +02003233 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003234 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003235 p_verbose = cmod->cmod_verbose_save - 1;
3236 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003237 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003238
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003239#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003240 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003241 {
3242 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003243 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003244 }
3245#endif
3246
Bram Moolenaare1004402020-10-24 20:49:43 +02003247 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003248 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003249 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003250 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3251 OPT_FREE, SID_NONE);
3252 free_string_option(cmod->cmod_save_ei);
3253 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003254 }
3255
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003256 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003257
Bram Moolenaare1004402020-10-24 20:49:43 +02003258 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003259 {
3260 // messages could be enabled for a serious error, need to check if the
3261 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003262 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3263 msg_silent = cmod->cmod_save_msg_silent - 1;
3264 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003265 if (emsg_silent < 0)
3266 emsg_silent = 0;
3267 // Restore msg_scroll, it's set by file I/O commands, even when no
3268 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003269 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003270
3271 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3272 // somewhere in the line. Put it back in the first column.
3273 if (redirecting())
3274 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003275
Bram Moolenaare1004402020-10-24 20:49:43 +02003276 cmod->cmod_save_msg_silent = 0;
3277 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003278 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003279}
3280
3281/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003282 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003283 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003284 * Return FAIL and set "errormsg" or return OK.
3285 */
3286 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003287parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003288{
3289 int address_count = 1;
3290 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003291 int need_check_cursor = FALSE;
3292 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003293
3294 // Repeat for all ',' or ';' separated addresses.
3295 for (;;)
3296 {
3297 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003298 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003299 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003300 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003301 eap->addr_count == 0, address_count++);
3302 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003303 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003304 if (lnum == MAXLNUM)
3305 {
3306 if (*eap->cmd == '%') // '%' - all lines
3307 {
3308 ++eap->cmd;
3309 switch (eap->addr_type)
3310 {
3311 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003312 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003313 eap->line1 = 1;
3314 eap->line2 = curbuf->b_ml.ml_line_count;
3315 break;
3316 case ADDR_LOADED_BUFFERS:
3317 {
3318 buf_T *buf = firstbuf;
3319
3320 while (buf->b_next != NULL
3321 && buf->b_ml.ml_mfp == NULL)
3322 buf = buf->b_next;
3323 eap->line1 = buf->b_fnum;
3324 buf = lastbuf;
3325 while (buf->b_prev != NULL
3326 && buf->b_ml.ml_mfp == NULL)
3327 buf = buf->b_prev;
3328 eap->line2 = buf->b_fnum;
3329 break;
3330 }
3331 case ADDR_BUFFERS:
3332 eap->line1 = firstbuf->b_fnum;
3333 eap->line2 = lastbuf->b_fnum;
3334 break;
3335 case ADDR_WINDOWS:
3336 case ADDR_TABS:
3337 if (IS_USER_CMDIDX(eap->cmdidx))
3338 {
3339 eap->line1 = 1;
3340 eap->line2 = eap->addr_type == ADDR_WINDOWS
3341 ? LAST_WIN_NR : LAST_TAB_NR;
3342 }
3343 else
3344 {
3345 // there is no Vim command which uses '%' and
3346 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003347 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003348 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003349 }
3350 break;
3351 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003352 case ADDR_UNSIGNED:
3353 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003354 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003355 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003356 case ADDR_ARGUMENTS:
3357 if (ARGCOUNT == 0)
3358 eap->line1 = eap->line2 = 0;
3359 else
3360 {
3361 eap->line1 = 1;
3362 eap->line2 = ARGCOUNT;
3363 }
3364 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003365 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003366#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003367 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003368 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003369 if (eap->line2 == 0)
3370 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003371#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003372 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003373 case ADDR_NONE:
3374 // Will give an error later if a range is found.
3375 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003376 }
3377 ++eap->addr_count;
3378 }
3379 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3380 {
3381 pos_T *fp;
3382
3383 // '*' - visual area
3384 if (eap->addr_type != ADDR_LINES)
3385 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003386 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003387 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003388 }
3389
3390 ++eap->cmd;
3391 if (!eap->skip)
3392 {
3393 fp = getmark('<', FALSE);
3394 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003395 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003396 eap->line1 = fp->lnum;
3397 fp = getmark('>', FALSE);
3398 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003399 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003400 eap->line2 = fp->lnum;
3401 ++eap->addr_count;
3402 }
3403 }
3404 }
3405 else
3406 eap->line2 = lnum;
3407 eap->addr_count++;
3408
3409 if (*eap->cmd == ';')
3410 {
3411 if (!eap->skip)
3412 {
3413 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003414
Bram Moolenaar0e717042020-04-27 19:29:01 +02003415 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003416 // accept zero as address, so 0;/PATTERN/ works correctly
3417 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003418 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003419 if (eap->line2 > 0)
3420 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003421 else
3422 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003423 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003424 }
3425 }
3426 else if (*eap->cmd != ',')
3427 break;
3428 ++eap->cmd;
3429 }
3430
3431 // One address given: set start and end lines.
3432 if (eap->addr_count == 1)
3433 {
3434 eap->line1 = eap->line2;
3435 // ... but only implicit: really no address given
3436 if (lnum == MAXLNUM)
3437 eap->addr_count = 0;
3438 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003439 ret = OK;
3440
3441theend:
3442 if (need_check_cursor)
3443 check_cursor();
3444 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003445}
3446
3447/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003448 * Append "cmd" to the error message in IObuff.
3449 * Takes care of limiting the length and handling 0xa0, which would be
3450 * invisible otherwise.
3451 */
3452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003453append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003454{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003455 size_t len = STRLEN(IObuff);
3456 char_u *s = cmd;
3457 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003458
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003459 if (len > IOSIZE - 100)
3460 {
3461 // Not enough space, truncate and put in "...".
3462 d = IObuff + IOSIZE - 100;
3463 d -= mb_head_off(IObuff, d);
3464 STRCPY(d, "...");
3465 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003466 STRCAT(IObuff, ": ");
3467 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003468 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003469 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003470 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003471 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003472 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003473 STRCPY(d, "<a0>");
3474 d += 4;
3475 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003476 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3477 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003478 else
3479 MB_COPY_CHAR(s, d);
3480 }
3481 *d = NUL;
3482}
3483
Dominique Pelle748b3082022-01-08 12:41:16 +00003484#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003485/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003486 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3487 * the name. Otherwise just return "start".
3488 */
3489 char_u *
3490skip_option_env_lead(char_u *start)
3491{
3492 char_u *name = start;
3493
3494 if (*start == '&')
3495 {
3496 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3497 name += 3;
3498 else
3499 name += 1;
3500 }
3501 else if (*start == '$')
3502 name += 1;
3503 return name;
3504}
Dominique Pelle748b3082022-01-08 12:41:16 +00003505#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003506
3507/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003508 * Return TRUE and set "*idx" if "p" points to a one letter command.
3509 * If not in Vim9 script:
3510 * - The 'k' command can directly be followed by any character.
3511 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3512 * but :sre[wind] is another command, as are :scr[iptnames],
3513 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3514 */
3515 static int
3516one_letter_cmd(char_u *p, cmdidx_T *idx)
3517{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003518 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003519 return FALSE;
3520 if (*p == 'k')
3521 {
3522 *idx = CMD_k;
3523 return TRUE;
3524 }
3525 if (p[0] == 's'
3526 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3527 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3528 || p[1] == 'g'
3529 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3530 || p[1] == 'I'
3531 || (p[1] == 'r' && p[2] != 'e')))
3532 {
3533 *idx = CMD_substitute;
3534 return TRUE;
3535 }
3536 return FALSE;
3537}
3538
3539/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003540 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3541 * call.
3542 */
3543 int
3544number_method(char_u *cmd)
3545{
3546 char_u *p = skipdigits(cmd);
3547
3548 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3549}
3550
3551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003553 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003554 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003555 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 *
3557 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3558 * assignment without "let". Sets eap->cmdidx to the command while returning
3559 * "eap->cmd".
3560 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 * Returns NULL for an ambiguous user command.
3562 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 char_u *
3564find_ex_command(
3565 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003566 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003567 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003568 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
3570 int len;
3571 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003572 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003573#ifndef FEAT_EVAL
3574 int vim9 = FALSE;
3575#else
3576 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 /*
3579 * Recognize a Vim9 script function/method call and assignment:
3580 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3581 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003582 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003583 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003585 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003587 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003588 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3589 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003591 int oplen;
3592 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003593 char_u *swp;
3594
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003595 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003596 || (eap->cmd[0] == '$'
3597 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003598 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003599 && (valid_yank_reg(eap->cmd[1], FALSE)
3600 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003601 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003602 if (*eap->cmd == '&')
3603 {
3604 p = eap->cmd + 1;
3605 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3606 p += 2;
3607 p = to_name_end(p, FALSE);
3608 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003609 else if (*eap->cmd == '$')
3610 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003611 else
3612 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003613 if (ends_excmd(*skipwhite(p)))
3614 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003615 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3616 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003617 eap->cmdidx = CMD_eval;
3618 return eap->cmd;
3619 }
3620 // "&option" can be followed by "->" or "=", check below
3621 }
3622
3623 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003625 if (
3626 // "(..." is an expression.
3627 // "funcname(" is always a function call.
3628 *p == '('
3629 || (p == eap->cmd
3630 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003631 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003632 *eap->cmd == '{'
3633 // "'string'->func()" is an expression.
3634 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003635 // '"string"->func()' is an expression.
3636 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003637 // '$"string"->func()' is an expression.
3638 // "$'string'->func()" is an expression.
3639 || (eap->cmd[0] == '$'
3640 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3641 // '0z1234->func()' is an expression.
3642 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003643 // "g:varname" is an expression.
3644 || eap->cmd[1] == ':'
3645 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003646 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003647 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003648 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003649 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3650 {
3651 // "{" by itself is the start of a block.
3652 eap->cmdidx = CMD_block;
3653 return eap->cmd + 1;
3654 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003655 eap->cmdidx = CMD_eval;
3656 return eap->cmd;
3657 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003658
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003659 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003660 // "varname[]" is an expression.
3661 *p == '['
3662 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003663 || (*p == '.'
3664 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3665 // g:[key] is an expression
3666 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003667 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003668 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003669
3670 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003671 // Skip over the whole thing, it can be:
3672 // name.member = val
3673 // name[a : b] = val
3674 // name[idx] = val
3675 // name[idx].member = val
3676 // etc.
3677 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003678 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003679 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003680 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003681 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003682 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003683 || (after[0] == '.' && after[1] == '.'
3684 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003685 eap->cmdidx = CMD_var;
3686 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003687 --emsg_silent;
3688 return eap->cmd;
3689 }
3690
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003691 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3692 // an assignment.
3693 // If there is no line break inside the "[...]" then "p" is
3694 // advanced to after the "]" by to_name_const_end(): check if a "="
3695 // follows.
3696 // If "[...]" has a line break "p" still points at the "[" and it
3697 // can't be an assignment.
3698 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003699 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003700 char_u *eq;
3701
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003702 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003703 if (p == eap->cmd && *p == '[')
3704 {
3705 int count = 0;
3706 int semicolon = FALSE;
3707
3708 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3709 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003710 eq = p;
3711 if (eq != NULL)
3712 {
3713 eq = skipwhite(eq);
3714 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3715 ++eq;
3716 }
3717 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003718 {
3719 eap->cmdidx = CMD_eval;
3720 return eap->cmd;
3721 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003722 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003723 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003724 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003725 return eap->cmd;
3726 }
3727 }
3728
3729 // Recognize an assignment if we recognize the variable name:
3730 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003731 // "@r = expr"
3732 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003733 // "var = expr" where "var" is a variable name or we are skipping
3734 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003735 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003736 oplen = assignment_len(skipwhite(p), &heredoc);
3737 if (oplen > 0)
3738 {
3739 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3740 || *eap->cmd == '&'
3741 || *eap->cmd == '$'
3742 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003743 || (eap->skip && IS_WHITE_OR_NUL(
3744 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003745 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003746 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003747 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003748 return eap->cmd;
3749 }
3750 }
3751
3752 // Recognize using a type for a w:, b:, t: or g: variable:
3753 // "w:varname: number = 123".
3754 if (eap->cmd[1] == ':' && *p == ':')
3755 {
3756 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003757 return eap->cmd;
3758 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003759 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003760
Bram Moolenaar31d99482022-05-26 22:24:43 +01003761 // 1234->func() is a method call
3762 if (number_method(eap->cmd))
3763 {
3764 eap->cmdidx = CMD_eval;
3765 return eap->cmd;
3766 }
3767
Bram Moolenaar7b829262021-10-13 15:04:34 +01003768 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3769 // an expression. A global/substitute/list command needs to use a
3770 // longer name.
3771 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3772 {
3773 eap->cmdidx = CMD_eval;
3774 return eap->cmd;
3775 }
3776
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003777 // If it is an ID it might be a variable with an operator on the next
3778 // line, if the variable exists it can't be an Ex command.
3779 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003780 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003781 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3782 {
3783 eap->cmdidx = CMD_eval;
3784 return eap->cmd;
3785 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003786
3787 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003788 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3789 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003790 {
3791 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3792 return eap->cmd + 2;
3793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 }
3795#endif
3796
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 /*
3798 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 */
3800 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003801 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 ++p;
3804 }
3805 else
3806 {
3807 while (ASCII_ISALPHA(*p))
3808 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003809 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003810 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003812 while (ASCII_ISALNUM(*p))
3813 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 }
3815 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3816 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003817 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 ++p;
3819 while (ASCII_ISALPHA(*p))
3820 ++p;
3821 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003822
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003823 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003824 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 ++p;
3826 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003827 // The "d" command can directly be followed by 'l' or 'p' flag, when
3828 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003829 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003830 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003831 // Check for ":dl", ":dell", etc. to ":deletel": that's
3832 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003833 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003834 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003835 break;
3836 if (i == len - 1)
3837 {
3838 --len;
3839 if (p[-1] == 'l')
3840 eap->flags |= EXFLAG_LIST;
3841 else
3842 eap->flags |= EXFLAG_PRINT;
3843 }
3844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003846 if (ASCII_ISLOWER(eap->cmd[0]))
3847 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003848 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003849 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003850
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003851 if (command_count != (int)CMD_SIZE)
3852 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003853 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003854 getout(1);
3855 }
3856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003857 // Use a precomputed index for fast look-up in cmdnames[]
3858 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003859 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3860 if (ASCII_ISLOWER(c2))
3861 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3862 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003863 else if (ASCII_ISUPPER(eap->cmd[0]))
3864 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003866 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
3868 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3869 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3870 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3871 (size_t)len) == 0)
3872 {
3873#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003874 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 *full = TRUE;
3876#endif
3877 break;
3878 }
3879
Bram Moolenaar204852a2022-03-05 12:56:44 +00003880 // :Print and :mode are not supported in Vim9 script.
3881 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003882 if (vim9 && eap->cmdidx != CMD_SIZE)
3883 {
3884 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3885 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003886 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003887 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003888 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003889 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003890 eap->cmdidx = CMD_SIZE;
3891 }
3892 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003893
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003894 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003895 // 'cpoptions'.
3896 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3897 p = eap->cmd;
3898
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003899 // Look for a user defined command as a last resort. Let ":Print" be
3900 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003901 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3902 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003904 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 while (ASCII_ISALNUM(*p))
3906 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003907 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003909 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 eap->cmdidx = CMD_SIZE;
3911 }
3912
Bram Moolenaar204852a2022-03-05 12:56:44 +00003913 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3914 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003915 eap->cmdidx = CMD_finally;
3916
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003917#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003918 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003919 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003920 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003921 && (eap->cmdidx < 0 ||
3922 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003923 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003924 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3925
3926 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003927 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003928 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003929 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003930#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003931
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 return p;
3933}
3934
3935#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003936static struct cmdmod
3937{
3938 char *name;
3939 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003940 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003941} cmdmods[] = {
3942 {"aboveleft", 3, FALSE},
3943 {"belowright", 3, FALSE},
3944 {"botright", 2, FALSE},
3945 {"browse", 3, FALSE},
3946 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003947 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003948 {"hide", 3, FALSE},
3949 {"keepalt", 5, FALSE},
3950 {"keepjumps", 5, FALSE},
3951 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003952 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003953 {"leftabove", 5, FALSE},
3954 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003955 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003956 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003957 {"rightbelow", 6, FALSE},
3958 {"sandbox", 3, FALSE},
3959 {"silent", 3, FALSE},
3960 {"tab", 3, TRUE},
3961 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003962 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003963 {"verbose", 4, TRUE},
3964 {"vertical", 4, FALSE},
3965};
3966
3967/*
3968 * Return length of a command modifier (including optional count).
3969 * Return zero when it's not a modifier.
3970 */
3971 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003972modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003973{
3974 int i, j;
3975 char_u *p = cmd;
3976
3977 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003978 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003979 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003980 {
3981 for (j = 0; p[j] != NUL; ++j)
3982 if (p[j] != cmdmods[i].name[j])
3983 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003984 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003985 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003986 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003987 }
3988 return 0;
3989}
3990
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991/*
3992 * Return > 0 if an Ex command "name" exists.
3993 * Return 2 if there is an exact match.
3994 * Return 3 if there is an ambiguous match.
3995 */
3996 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003997cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
3999 exarg_T ea;
4000 int full = FALSE;
4001 int i;
4002 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004003 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004005 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02004006 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
4008 for (j = 0; name[j] != NUL; ++j)
4009 if (name[j] != cmdmods[i].name[j])
4010 break;
4011 if (name[j] == NUL && j >= cmdmods[i].minlen)
4012 return (cmdmods[i].name[j] == NUL ? 2 : 1);
4013 }
4014
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004015 // Check built-in commands and user defined commands.
4016 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004017 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004019 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004021 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004023 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4024 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004025 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004026 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4028}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004029
4030/*
4031 * "fullcommand" function
4032 */
4033 void
4034f_fullcommand(typval_T *argvars, typval_T *rettv)
4035{
4036 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004037 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004038 char_u *p;
4039
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004040 rettv->v_type = VAR_STRING;
4041 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004042
4043 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
4044 return;
4045
4046 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004047 if (name == NULL)
4048 return;
4049
zeertzjqc024ed92022-01-04 16:22:52 +00004050 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004051 name++;
4052 name = skip_range(name, TRUE, NULL);
4053
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004054 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4055 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004056 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004057 p = find_ex_command(&ea, NULL, NULL, NULL);
4058 if (p == NULL || ea.cmdidx == CMD_SIZE)
4059 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004060 if (in_vim9script())
4061 {
4062 int res;
4063
4064 ++emsg_silent;
4065 res = not_in_vim9(&ea);
4066 --emsg_silent;
4067
4068 if (res == FAIL)
4069 return;
4070 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004071
4072 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004073 ? get_user_command_name(ea.useridx, ea.cmdidx)
4074 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004075}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076#endif
4077
Bram Moolenaard0190392019-08-23 21:17:35 +02004078 cmdidx_T
4079excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080{
Bram Moolenaard0190392019-08-23 21:17:35 +02004081 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004083 if (!one_letter_cmd(cmd, &idx))
4084 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4085 idx = (cmdidx_T)((int)idx + 1))
4086 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4087 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
Bram Moolenaard0190392019-08-23 21:17:35 +02004089 return idx;
4090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
Bram Moolenaard0190392019-08-23 21:17:35 +02004092 long
4093excmd_get_argt(cmdidx_T idx)
4094{
4095 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096}
4097
4098/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004099 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 *
4101 * Backslashed delimiters after / or ? will be skipped, and commands will
4102 * not be expanded between /'s and ?'s or after "'".
4103 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004104 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 * Returns the "cmd" pointer advanced to beyond the range.
4106 */
4107 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004108skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004109 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004110 int skip_star, // skip "*" used for Visual range
4111 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004113 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004114 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004116 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004118 if (*cmd == '\\')
4119 {
4120 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4121 ++cmd;
4122 else
4123 break;
4124 }
4125 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004127 char_u *p = cmd;
4128
4129 // a quote is only valid at the start or after a separator
4130 while (p > cmd_start)
4131 {
4132 --p;
4133 if (!VIM_ISWHITE(*p))
4134 break;
4135 }
4136 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4137 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (*++cmd == NUL && ctx != NULL)
4139 *ctx = EXPAND_NOTHING;
4140 }
4141 else if (*cmd == '/' || *cmd == '?')
4142 {
4143 delim = *cmd++;
4144 while (*cmd != NUL && *cmd != delim)
4145 if (*cmd++ == '\\' && *cmd != NUL)
4146 ++cmd;
4147 if (*cmd == NUL && ctx != NULL)
4148 *ctx = EXPAND_NOTHING;
4149 }
4150 if (*cmd != NUL)
4151 ++cmd;
4152 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004153
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004154 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004155 while (*cmd == ':')
4156 cmd = skipwhite(cmd + 1);
4157
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004158 // Skip "*" used for Visual range.
4159 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4160 cmd = skipwhite(cmd + 1);
4161
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 return cmd;
4163}
4164
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004165 static void
4166addr_error(cmd_addr_T addr_type)
4167{
4168 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004169 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004170 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004171 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004172}
4173
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004175 * Return the default address for an address type.
4176 */
4177 static linenr_T
4178default_address(exarg_T *eap)
4179{
4180 linenr_T lnum = 0;
4181
4182 switch (eap->addr_type)
4183 {
4184 case ADDR_LINES:
4185 case ADDR_OTHER:
4186 // Default is the cursor line number. Avoid using an invalid
4187 // line number though.
4188 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4189 lnum = curbuf->b_ml.ml_line_count;
4190 else
4191 lnum = curwin->w_cursor.lnum;
4192 break;
4193 case ADDR_WINDOWS:
4194 lnum = CURRENT_WIN_NR;
4195 break;
4196 case ADDR_ARGUMENTS:
4197 lnum = curwin->w_arg_idx + 1;
4198 if (lnum > ARGCOUNT)
4199 lnum = ARGCOUNT;
4200 break;
4201 case ADDR_LOADED_BUFFERS:
4202 case ADDR_BUFFERS:
4203 lnum = curbuf->b_fnum;
4204 break;
4205 case ADDR_TABS:
4206 lnum = CURRENT_TAB_NR;
4207 break;
4208 case ADDR_TABS_RELATIVE:
4209 case ADDR_UNSIGNED:
4210 lnum = 1;
4211 break;
4212 case ADDR_QUICKFIX:
4213#ifdef FEAT_QUICKFIX
4214 lnum = qf_get_cur_idx(eap);
4215#endif
4216 break;
4217 case ADDR_QUICKFIX_VALID:
4218#ifdef FEAT_QUICKFIX
4219 lnum = qf_get_cur_valid_idx(eap);
4220#endif
4221 break;
4222 case ADDR_NONE:
4223 // Will give an error later if a range is found.
4224 break;
4225 }
4226 return lnum;
4227}
4228
4229/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004230 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 *
4232 * Set ptr to the next character after the part that was interpreted.
4233 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004234 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 *
4236 * Return MAXLNUM when no Ex address was found.
4237 */
4238 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004239get_address(
4240 exarg_T *eap UNUSED,
4241 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004242 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004243 int skip, // only skip the address, don't use it
4244 int silent, // no errors or side effects
4245 int to_other_file, // flag: may jump to other file
4246 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
4248 int c;
4249 int i;
4250 long n;
4251 char_u *cmd;
4252 pos_T pos;
4253 pos_T *fp;
4254 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004255 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 cmd = skipwhite(*ptr);
4258 lnum = MAXLNUM;
4259 do
4260 {
4261 switch (*cmd)
4262 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004263 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004264 ++cmd;
4265 switch (addr_type)
4266 {
4267 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004268 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 lnum = curwin->w_cursor.lnum;
4270 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004272 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004273 break;
4274 case ADDR_ARGUMENTS:
4275 lnum = curwin->w_arg_idx + 1;
4276 break;
4277 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004278 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004279 lnum = curbuf->b_fnum;
4280 break;
4281 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004282 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004283 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004284 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004285 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004286 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004287 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004288 cmd = NULL;
4289 goto error;
4290 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004291 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004292#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004293 lnum = qf_get_cur_idx(eap);
4294#endif
4295 break;
4296 case ADDR_QUICKFIX_VALID:
4297#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004298 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004299#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004300 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004301 }
4302 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004304 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004305 ++cmd;
4306 switch (addr_type)
4307 {
4308 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004309 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 lnum = curbuf->b_ml.ml_line_count;
4311 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004312 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004313 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004314 break;
4315 case ADDR_ARGUMENTS:
4316 lnum = ARGCOUNT;
4317 break;
4318 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004319 buf = lastbuf;
4320 while (buf->b_ml.ml_mfp == NULL)
4321 {
4322 if (buf->b_prev == NULL)
4323 break;
4324 buf = buf->b_prev;
4325 }
4326 lnum = buf->b_fnum;
4327 break;
4328 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004329 lnum = lastbuf->b_fnum;
4330 break;
4331 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004332 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004333 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004334 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004335 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004336 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004337 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004338 cmd = NULL;
4339 goto error;
4340 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004341 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004342#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004343 lnum = qf_get_size(eap);
4344 if (lnum == 0)
4345 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004346#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004347 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004348 case ADDR_QUICKFIX_VALID:
4349#ifdef FEAT_QUICKFIX
4350 lnum = qf_get_valid_size(eap);
4351 if (lnum == 0)
4352 lnum = 1;
4353#endif
4354 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004355 }
4356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004358 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004359 if (*++cmd == NUL)
4360 {
4361 cmd = NULL;
4362 goto error;
4363 }
4364 if (addr_type != ADDR_LINES)
4365 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004366 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004367 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004368 goto error;
4369 }
4370 if (skip)
4371 ++cmd;
4372 else
4373 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004374 // Only accept a mark in another file when it is
4375 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004376 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4377 ++cmd;
4378 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004379 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004380 lnum = curwin->w_cursor.lnum;
4381 else
4382 {
4383 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 {
4385 cmd = NULL;
4386 goto error;
4387 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004388 lnum = fp->lnum;
4389 }
4390 }
4391 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004394 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 c = *cmd++;
4396 if (addr_type != ADDR_LINES)
4397 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004398 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004399 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 goto error;
4401 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004402 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004403 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004404 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004405 if (*cmd == c)
4406 ++cmd;
4407 }
4408 else
4409 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004410 int flags;
4411
4412 pos = curwin->w_cursor; // save curwin->w_cursor
4413
4414 // When '/' or '?' follows another address, start from
4415 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004416 if (lnum > 0 && lnum != MAXLNUM)
4417 curwin->w_cursor.lnum =
4418 lnum > curbuf->b_ml.ml_line_count
4419 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004420
4421 // Start a forward search at the end of the line (unless
4422 // before the first line).
4423 // Start a backward search at the start of the line.
4424 // This makes sure we never match in the current
4425 // line, and can match anywhere in the
4426 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004427 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004428 curwin->w_cursor.col = MAXCOL;
4429 else
4430 curwin->w_cursor.col = 0;
4431 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004432 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004433 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004434 {
4435 curwin->w_cursor = pos;
4436 cmd = NULL;
4437 goto error;
4438 }
4439 lnum = curwin->w_cursor.lnum;
4440 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004441 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 cmd += searchcmdlen;
4443 }
4444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004446 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004447 ++cmd;
4448 if (addr_type != ADDR_LINES)
4449 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004450 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004451 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004452 goto error;
4453 }
4454 if (*cmd == '&')
4455 i = RE_SUBST;
4456 else if (*cmd == '?' || *cmd == '/')
4457 i = RE_SEARCH;
4458 else
4459 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004460 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004461 cmd = NULL;
4462 goto error;
4463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004465 if (!skip)
4466 {
4467 /*
4468 * When search follows another address, start from
4469 * there.
4470 */
4471 if (lnum != MAXLNUM)
4472 pos.lnum = lnum;
4473 else
4474 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 /*
4477 * Start the search just like for the above
4478 * do_search().
4479 */
4480 if (*cmd != '?')
4481 pos.col = MAXCOL;
4482 else
4483 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004484 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004485 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004487 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004488 lnum = pos.lnum;
4489 else
4490 {
4491 cmd = NULL;
4492 goto error;
4493 }
4494 }
4495 ++cmd;
4496 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004499 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502
4503 for (;;)
4504 {
4505 cmd = skipwhite(cmd);
4506 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4507 break;
4508
4509 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 {
4511 switch (addr_type)
4512 {
4513 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004514 case ADDR_OTHER:
4515 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004516 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004517 break;
4518 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004519 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004520 break;
4521 case ADDR_ARGUMENTS:
4522 lnum = curwin->w_arg_idx + 1;
4523 break;
4524 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004525 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004526 lnum = curbuf->b_fnum;
4527 break;
4528 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004529 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004530 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004531 case ADDR_TABS_RELATIVE:
4532 lnum = 1;
4533 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004534 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004535#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004536 lnum = qf_get_cur_idx(eap);
4537#endif
4538 break;
4539 case ADDR_QUICKFIX_VALID:
4540#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004541 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004542#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004543 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004544 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004545 case ADDR_UNSIGNED:
4546 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004547 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004548 }
4549 }
4550
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004552 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 else
4554 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004555 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 n = 1;
4557 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004558 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004560 if (n == MAXLNUM)
4561 {
4562 emsg(_(e_line_number_out_of_range));
4563 goto error;
4564 }
4565 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004566
4567 if (addr_type == ADDR_TABS_RELATIVE)
4568 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004569 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004570 cmd = NULL;
4571 goto error;
4572 }
4573 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004574 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004575 lnum = compute_buffer_local_count(
4576 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004578 {
4579#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004580 // Relative line addressing, need to adjust for folded lines
4581 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004582 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004583 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004584 (void)hasFolding(lnum, NULL, &lnum);
4585#endif
4586 if (i == '-')
4587 lnum -= n;
4588 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004589 {
4590 if (n >= LONG_MAX - lnum)
4591 {
4592 emsg(_(e_line_number_out_of_range));
4593 goto error;
4594 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004595 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004596 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
4599 } while (*cmd == '/' || *cmd == '?');
4600
4601error:
4602 *ptr = cmd;
4603 return lnum;
4604}
4605
4606/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004607 * Set eap->line1 and eap->line2 to the whole range.
4608 * Used for commands with the EX_DFLALL flag and no range given.
4609 */
4610 static void
4611address_default_all(exarg_T *eap)
4612{
4613 eap->line1 = 1;
4614 switch (eap->addr_type)
4615 {
4616 case ADDR_LINES:
4617 case ADDR_OTHER:
4618 eap->line2 = curbuf->b_ml.ml_line_count;
4619 break;
4620 case ADDR_LOADED_BUFFERS:
4621 {
4622 buf_T *buf = firstbuf;
4623
4624 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4625 buf = buf->b_next;
4626 eap->line1 = buf->b_fnum;
4627 buf = lastbuf;
4628 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4629 buf = buf->b_prev;
4630 eap->line2 = buf->b_fnum;
4631 }
4632 break;
4633 case ADDR_BUFFERS:
4634 eap->line1 = firstbuf->b_fnum;
4635 eap->line2 = lastbuf->b_fnum;
4636 break;
4637 case ADDR_WINDOWS:
4638 eap->line2 = LAST_WIN_NR;
4639 break;
4640 case ADDR_TABS:
4641 eap->line2 = LAST_TAB_NR;
4642 break;
4643 case ADDR_TABS_RELATIVE:
4644 eap->line2 = 1;
4645 break;
4646 case ADDR_ARGUMENTS:
4647 if (ARGCOUNT == 0)
4648 eap->line1 = eap->line2 = 0;
4649 else
4650 eap->line2 = ARGCOUNT;
4651 break;
4652 case ADDR_QUICKFIX_VALID:
4653#ifdef FEAT_QUICKFIX
4654 eap->line2 = qf_get_valid_size(eap);
4655 if (eap->line2 == 0)
4656 eap->line2 = 1;
4657#endif
4658 break;
4659 case ADDR_NONE:
4660 case ADDR_UNSIGNED:
4661 case ADDR_QUICKFIX:
4662 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4663 break;
4664 }
4665}
4666
4667
4668/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004669 * Get flags from an Ex command argument.
4670 */
4671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004672get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004673{
4674 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4675 {
4676 if (*eap->arg == 'l')
4677 eap->flags |= EXFLAG_LIST;
4678 else if (*eap->arg == 'p')
4679 eap->flags |= EXFLAG_PRINT;
4680 else
4681 eap->flags |= EXFLAG_NR;
4682 eap->arg = skipwhite(eap->arg + 1);
4683 }
4684}
4685
4686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 * Function called for command which is Not Implemented. NI!
4688 */
4689 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004690ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
4692 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004693 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004694 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695}
4696
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004697#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698/*
4699 * Function called for script command which is Not Implemented. NI!
4700 * Skips over ":perl <<EOF" constructs.
4701 */
4702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004703ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
4705 if (!eap->skip)
4706 ex_ni(eap);
4707 else
4708 vim_free(script_get(eap, eap->arg));
4709}
4710#endif
4711
4712/*
4713 * Check range in Ex command for validity.
4714 * Return NULL when valid, error message when invalid.
4715 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004716 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004717invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004719 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004720
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 if ( eap->line1 < 0
4722 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004723 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004724 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004725
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004726 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004727 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004728 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004729 {
4730 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004731 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004732#ifdef FEAT_DIFF
4733 + (eap->cmdidx == CMD_diffget)
4734#endif
4735 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004736 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004737 break;
4738 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004739 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004740 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004741 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004742 break;
4743 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004744 // Only a boundary check, not whether the buffers actually
4745 // exist.
4746 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004747 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004748 break;
4749 case ADDR_LOADED_BUFFERS:
4750 buf = firstbuf;
4751 while (buf->b_ml.ml_mfp == NULL)
4752 {
4753 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004754 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004755 buf = buf->b_next;
4756 }
4757 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004758 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004759 buf = lastbuf;
4760 while (buf->b_ml.ml_mfp == NULL)
4761 {
4762 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004763 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004764 buf = buf->b_prev;
4765 }
4766 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004767 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004768 break;
4769 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004770 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004771 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004772 break;
4773 case ADDR_TABS:
4774 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004775 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004776 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004777 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004778 case ADDR_OTHER:
4779 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004780 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004781 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004782#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004783 // No error for value that is too big, will use the last entry.
4784 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004785 {
4786 if (eap->addr_count == 0)
4787 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004788 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004789 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004790#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004791 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004792 case ADDR_QUICKFIX_VALID:
4793#ifdef FEAT_QUICKFIX
4794 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4795 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004796 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004797#endif
4798 break;
4799 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004800 case ADDR_NONE:
4801 // Will give an error elsewhere.
4802 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004803 }
4804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 return NULL;
4806}
4807
4808/*
4809 * Correct the range for zero line number, if required.
4810 */
4811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004812correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004814 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
4816 if (eap->line1 == 0)
4817 eap->line1 = 1;
4818 if (eap->line2 == 0)
4819 eap->line2 = 1;
4820 }
4821}
4822
Bram Moolenaar748bf032005-02-02 23:04:36 +00004823#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004824/*
4825 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4826 * pattern. Otherwise return eap->arg.
4827 */
4828 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004829skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004830{
4831 char_u *p = eap->arg;
4832
Bram Moolenaara37420f2006-02-04 22:37:47 +00004833 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4834 || eap->cmdidx == CMD_vimgrepadd
4835 || eap->cmdidx == CMD_lvimgrepadd
4836 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004837 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004838 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004839 if (p == NULL)
4840 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004841 }
4842 return p;
4843}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004844
4845/*
4846 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4847 * in the command line, so that things like % get expanded.
4848 */
4849 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004850replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004851{
4852 char_u *new_cmdline;
4853 char_u *program;
4854 char_u *pos;
4855 char_u *ptr;
4856 int len;
4857 int i;
4858
4859 /*
4860 * Don't do it when ":vimgrep" is used for ":grep".
4861 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004862 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4863 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4864 || eap->cmdidx == CMD_grepadd
4865 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004866 && !grep_internal(eap->cmdidx))
4867 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004868 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4869 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004870 {
4871 if (*curbuf->b_p_gp == NUL)
4872 program = p_gp;
4873 else
4874 program = curbuf->b_p_gp;
4875 }
4876 else
4877 {
4878 if (*curbuf->b_p_mp == NUL)
4879 program = p_mp;
4880 else
4881 program = curbuf->b_p_mp;
4882 }
4883
4884 p = skipwhite(p);
4885
4886 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4887 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004888 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004889 i = 1;
4890 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4891 ++i;
4892 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004893 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004894 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004895 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004896 ptr = new_cmdline;
4897 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4898 {
4899 i = (int)(pos - program);
4900 STRNCPY(ptr, program, i);
4901 STRCPY(ptr += i, p);
4902 ptr += len;
4903 program = pos + 2;
4904 }
4905 STRCPY(ptr, program);
4906 }
4907 else
4908 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004909 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004910 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004911 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004912 STRCPY(new_cmdline, program);
4913 STRCAT(new_cmdline, " ");
4914 STRCAT(new_cmdline, p);
4915 }
4916 msg_make(p);
4917
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004918 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004919 vim_free(*cmdlinep);
4920 *cmdlinep = new_cmdline;
4921 p = new_cmdline;
4922 }
4923 return p;
4924}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004925#endif
4926
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927/*
4928 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004929 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 * Return FAIL for failure, OK otherwise.
4931 */
4932 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004933expand_filename(
4934 exarg_T *eap,
4935 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004936 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004938 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 char_u *repl;
4940 int srclen;
4941 char_u *p;
4942 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004943 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
Bram Moolenaar748bf032005-02-02 23:04:36 +00004945#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004946 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004947 p = skip_grep_pat(eap);
4948#else
4949 p = eap->arg;
4950#endif
4951
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 /*
4953 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4954 * the file name contains a wildcard it should not cause expanding.
4955 * (it will be expanded anyway if there is a wildcard before replacing).
4956 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004957 has_wildcards = mch_has_wildcard(p);
4958 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004960#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004961 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004962 if (p[0] == '`' && p[1] == '=')
4963 {
4964 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004965 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004966 if (*p == '`')
4967 ++p;
4968 continue;
4969 }
4970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 /*
4972 * Quick check if this cannot be the start of a special string.
4973 * Also removes backslash before '%', '#' and '<'.
4974 */
4975 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4976 {
4977 ++p;
4978 continue;
4979 }
4980
4981 /*
4982 * Try to find a match at this position.
4983 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004984 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01004985 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004986 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004988 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
4990 p += srclen;
4991 continue;
4992 }
4993
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004994 // Wildcards won't be expanded below, the replacement is taken
4995 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004996 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4997 {
4998 char_u *l = repl;
4999
5000 repl = expand_env_save(repl);
5001 vim_free(l);
5002 }
5003
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005004 // Need to escape white space et al. with a backslash.
5005 // Don't do this for:
5006 // - replacement that already has been escaped: "##"
5007 // - shell commands (may have to use quotes instead).
5008 // - non-unix systems when there is a single argument (spaces don't
5009 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005011 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 && eap->cmdidx != CMD_grep
5014 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005015 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005016 && eap->cmdidx != CMD_lgrep
5017 && eap->cmdidx != CMD_lgrepadd
5018 && eap->cmdidx != CMD_lmake
5019 && eap->cmdidx != CMD_make
5020 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005022 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023#endif
5024 )
5025 {
5026 char_u *l;
5027#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005028 // Don't escape a backslash here, because rem_backslash() doesn't
5029 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 static char_u *nobslash = (char_u *)" \t\"|";
5031# define ESCAPE_CHARS nobslash
5032#else
5033# define ESCAPE_CHARS escape_chars
5034#endif
5035
5036 for (l = repl; *l; ++l)
5037 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5038 {
5039 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5040 if (l != NULL)
5041 {
5042 vim_free(repl);
5043 repl = l;
5044 }
5045 break;
5046 }
5047 }
5048
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005049 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005050 if ((eap->usefilter || eap->cmdidx == CMD_bang
5051 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005052 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
5054 char_u *l;
5055
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005056 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (l != NULL)
5058 {
5059 vim_free(repl);
5060 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062 }
5063
5064 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5065 vim_free(repl);
5066 if (p == NULL)
5067 return FAIL;
5068 }
5069
5070 /*
5071 * One file argument: Expand wildcards.
5072 * Don't do this with ":r !command" or ":w !command".
5073 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005074 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
5076 /*
5077 * May do this twice:
5078 * 1. Replace environment variables.
5079 * 2. Replace any other wildcards, remove backslashes.
5080 */
5081 for (n = 1; n <= 2; ++n)
5082 {
5083 if (n == 2)
5084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 /*
5086 * Halve the number of backslashes (this is Vi compatible).
5087 * For Unix and OS/2, when wildcards are expanded, this is
5088 * done by ExpandOne() below.
5089 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005090#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 if (!has_wildcards)
5092#endif
5093 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095
5096 if (has_wildcards)
5097 {
5098 if (n == 1)
5099 {
5100 /*
5101 * First loop: May expand environment variables. This
5102 * can be done much faster with expand_env() than with
5103 * something else (e.g., calling a shell).
5104 * After expanding environment variables, check again
5105 * if there are still wildcards present.
5106 */
5107 if (vim_strchr(eap->arg, '$') != NULL
5108 || vim_strchr(eap->arg, '~') != NULL)
5109 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005110 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005111 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 has_wildcards = mch_has_wildcard(NameBuff);
5113 p = NameBuff;
5114 }
5115 else
5116 p = NULL;
5117 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005118 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
5120 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005121 int options = WILD_LIST_NOTFOUND
5122 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
5124 ExpandInit(&xpc);
5125 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005126 if (p_wic)
5127 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005129 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (p == NULL)
5131 return FAIL;
5132 }
5133 if (p != NULL)
5134 {
5135 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5136 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005137 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 vim_free(p);
5139 }
5140 }
5141 }
5142 }
5143 return OK;
5144}
5145
5146/*
5147 * Replace part of the command line, keeping eap->cmd, eap->arg and
5148 * eap->nextcmd correct.
5149 * "src" points to the part that is to be replaced, of length "srclen".
5150 * "repl" is the replacement string.
5151 * Returns a pointer to the character after the replaced string.
5152 * Returns NULL for failure.
5153 */
5154 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005155repl_cmdline(
5156 exarg_T *eap,
5157 char_u *src,
5158 int srclen,
5159 char_u *repl,
5160 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161{
5162 int len;
5163 int i;
5164 char_u *new_cmdline;
5165
5166 /*
5167 * The new command line is build in new_cmdline[].
5168 * First allocate it.
5169 * Careful: a "+cmd" argument may have been NUL terminated.
5170 */
5171 len = (int)STRLEN(repl);
5172 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005173 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005174 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005175 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005176 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177
5178 /*
5179 * Copy the stuff before the expanded part.
5180 * Copy the expanded stuff.
5181 * Copy what came after the expanded part.
5182 * Copy the next commands, if there are any.
5183 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005184 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005186
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005188 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005190 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005192 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
5194 i = (int)STRLEN(new_cmdline) + 1;
5195 STRCPY(new_cmdline + i, eap->nextcmd);
5196 eap->nextcmd = new_cmdline + i;
5197 }
5198 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5199 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5200 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5201 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5202 vim_free(*cmdlinep);
5203 *cmdlinep = new_cmdline;
5204
5205 return src;
5206}
5207
5208/*
5209 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005210 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 */
5212 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005213separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214{
5215 char_u *p;
5216
Bram Moolenaar86b68352004-12-27 21:59:20 +00005217#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005218 p = skip_grep_pat(eap);
5219#else
5220 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005221#endif
5222
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005223 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {
5225 if (*p == Ctrl_V)
5226 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005227 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005228 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005230 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005231 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005232 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 break;
5234 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005235
5236#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005237 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005238 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005239 {
5240 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005241 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005242 if (*p == NUL) // stop at NUL after CTRL-V
5243 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005244 }
5245#endif
5246
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005247 // Check for '"': start of comment or '|': next command
5248 // :@" and :*" do not start a comment!
5249 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005250 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005251#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005252 && !in_vim9script()
5253#endif
5254 && !(eap->argt & EX_NOTRLCOM)
5255 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5256 || p != eap->arg)
5257 && (eap->cmdidx != CMD_redir
5258 || p != eap->arg + 1 || p[-1] != '@'))
5259#ifdef FEAT_EVAL
5260 || (*p == '#'
5261 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005262 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005263 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 || *p == '|' || *p == '\n')
5266 {
5267 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005268 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 * AND 'b' is present in 'cpoptions'.
5270 */
5271 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005272 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005274 if (!keep_backslash)
5275 {
5276 STRMOVE(p - 1, p); // remove the '\'
5277 --p;
5278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280 else
5281 {
5282 eap->nextcmd = check_nextcmd(p);
5283 *p = NUL;
5284 break;
5285 }
5286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005288
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005289 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 del_trailing_spaces(eap->arg);
5291}
5292
5293/*
5294 * get + command from ex argument
5295 */
5296 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005297getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298{
5299 char_u *arg = *argp;
5300 char_u *command = NULL;
5301
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005302 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 {
5304 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005305 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 command = dollar_command;
5307 else
5308 {
5309 command = arg;
5310 arg = skip_cmd_arg(command, TRUE);
5311 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005312 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 }
5314
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005315 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 *argp = arg;
5317 }
5318 return command;
5319}
5320
5321/*
5322 * Find end of "+command" argument. Skip over "\ " and "\\".
5323 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005324 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005325skip_cmd_arg(
5326 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005327 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328{
5329 while (*p && !vim_isspace(*p))
5330 {
5331 if (*p == '\\' && p[1] != NUL)
5332 {
5333 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005334 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 else
5336 ++p;
5337 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005338 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
5340 return p;
5341}
5342
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005343 int
5344get_bad_opt(char_u *p, exarg_T *eap)
5345{
5346 if (STRICMP(p, "keep") == 0)
5347 eap->bad_char = BAD_KEEP;
5348 else if (STRICMP(p, "drop") == 0)
5349 eap->bad_char = BAD_DROP;
5350 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5351 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005352 else
5353 return FAIL;
5354 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005355}
5356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357/*
5358 * Get "++opt=arg" argument.
5359 * Return FAIL or OK.
5360 */
5361 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005362getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363{
5364 char_u *arg = eap->arg + 2;
5365 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005366 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005369 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5371 {
5372 if (*arg == 'n')
5373 {
5374 arg += 2;
5375 eap->force_bin = FORCE_NOBIN;
5376 }
5377 else
5378 eap->force_bin = FORCE_BIN;
5379 if (!checkforcmd(&arg, "binary", 3))
5380 return FAIL;
5381 eap->arg = skipwhite(arg);
5382 return OK;
5383 }
5384
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005385 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005386 if (STRNCMP(arg, "edit", 4) == 0)
5387 {
5388 eap->read_edit = TRUE;
5389 eap->arg = skipwhite(arg + 4);
5390 return OK;
5391 }
5392
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 if (STRNCMP(arg, "ff", 2) == 0)
5394 {
5395 arg += 2;
5396 pp = &eap->force_ff;
5397 }
5398 else if (STRNCMP(arg, "fileformat", 10) == 0)
5399 {
5400 arg += 10;
5401 pp = &eap->force_ff;
5402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 else if (STRNCMP(arg, "enc", 3) == 0)
5404 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005405 if (STRNCMP(arg, "encoding", 8) == 0)
5406 arg += 8;
5407 else
5408 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 pp = &eap->force_enc;
5410 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005411 else if (STRNCMP(arg, "bad", 3) == 0)
5412 {
5413 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005414 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
5417 if (pp == NULL || *arg != '=')
5418 return FAIL;
5419
5420 ++arg;
5421 *pp = (int)(arg - eap->cmd);
5422 arg = skip_cmd_arg(arg, FALSE);
5423 eap->arg = skipwhite(arg);
5424 *arg = NUL;
5425
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 if (pp == &eap->force_ff)
5427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5429 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005430 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005432 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005434 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5436 *p = TOLOWER_ASC(*p);
5437 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005438 else
5439 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005440 // Check ++bad= argument. Must be a single-byte character, "keep" or
5441 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005442 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005443 return FAIL;
5444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
5446 return OK;
5447}
5448
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005450ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
5452 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005453 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 * directory for security reasons.
5455 */
5456 if (secure)
5457 {
5458 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005459 eap->errmsg =
5460 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 }
5462 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005463 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 else
5465 do_augroup(eap->arg, eap->forceit);
5466}
5467
5468/*
5469 * ":doautocmd": Apply the automatic commands to the current buffer.
5470 */
5471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005472ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005474 char_u *arg = eap->arg;
5475 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005476 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005477
Bram Moolenaar1610d052016-06-09 22:53:01 +02005478 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005479 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005480 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005481 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484/*
5485 * :[N]bunload[!] [N] [bufname] unload buffer
5486 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5487 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5488 */
5489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005490ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005492 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005493 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 eap->errmsg = do_bufdel(
5495 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5496 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5497 : DOBUF_UNLOAD, eap->arg,
5498 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5499}
5500
5501/*
5502 * :[N]buffer [N] to buffer N
5503 * :[N]sbuffer [N] to buffer N
5504 */
5505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005506ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005508 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005509 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005511 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 else
5513 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005514 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5516 else
5517 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
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 }
5521}
5522
5523/*
5524 * :[N]bmodified [N] to next mod. buffer
5525 * :[N]sbmodified [N] to next mod. buffer
5526 */
5527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005528ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005531 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005532 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533}
5534
5535/*
5536 * :[N]bnext [N] to next buffer
5537 * :[N]sbnext [N] split and to next buffer
5538 */
5539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005540ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005542 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005543 return;
5544
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005546 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005547 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548}
5549
5550/*
5551 * :[N]bNext [N] to previous buffer
5552 * :[N]bprevious [N] to previous buffer
5553 * :[N]sbNext [N] split and to previous buffer
5554 * :[N]sbprevious [N] split and to previous buffer
5555 */
5556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005557ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005559 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005560 return;
5561
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005563 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005564 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565}
5566
5567/*
5568 * :brewind to first buffer
5569 * :bfirst to first buffer
5570 * :sbrewind split and to first buffer
5571 * :sbfirst split and to first buffer
5572 */
5573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005574ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005576 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005577 return;
5578
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005580 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005581 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582}
5583
5584/*
5585 * :blast to last buffer
5586 * :sblast split and to last buffer
5587 */
5588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005589ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005591 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005592 return;
5593
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005595 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005596 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaar7a092242020-04-16 22:10:49 +02005599/*
5600 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005601 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005604ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005606 int comment_char = '"';
5607
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005608#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005609 if (in_vim9script())
5610 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005611#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005612 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005613}
5614
5615/*
5616 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5617 * to "cmd_start" or has a white space character before it.
5618 */
5619 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005620ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005621{
5622 int c = *cmd;
5623
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005624 if (c == NUL || c == '|' || c == '\n')
5625 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005626#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005627 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005628 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5629 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005630 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005631#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005632 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633}
5634
5635#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5636 || defined(PROTO)
5637/*
5638 * Return the next command, after the first '|' or '\n'.
5639 * Return NULL if not found.
5640 */
5641 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005642find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643{
5644 while (*p != '|' && *p != '\n')
5645 {
5646 if (*p == NUL)
5647 return NULL;
5648 ++p;
5649 }
5650 return (p + 1);
5651}
5652#endif
5653
5654/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005655 * Check if *p is a separator between Ex commands, skipping over white space.
5656 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 */
5658 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005661 char_u *s = skipwhite(p);
5662
5663 if (*s == '|' || *s == '\n')
5664 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 else
5666 return NULL;
5667}
5668
5669/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005670 * If "eap->nextcmd" is not set, check for a next command at "p".
5671 */
5672 void
5673set_nextcmd(exarg_T *eap, char_u *arg)
5674{
5675 char_u *p = check_nextcmd(arg);
5676
5677 if (eap->nextcmd == NULL)
5678 eap->nextcmd = p;
5679 else if (p != NULL)
5680 // cannot use "| command" inside a {} block
5681 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5682}
5683
5684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 * - if there are more files to edit
5686 * - and this is the last window
5687 * - and forceit not used
5688 * - and not repeated twice on a row
5689 * return FAIL and give error message if 'message' TRUE
5690 * return OK otherwise
5691 */
5692 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005693check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005694 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005695 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696{
5697 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5698
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005699 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005700 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
5702 if (message)
5703 {
5704#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005705 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5706 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005708 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005710 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5711 NGETTEXT("%d more file to edit. Quit anyway?",
5712 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5714 return OK;
5715 return FAIL;
5716 }
5717#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005718 semsg(NGETTEXT(e_nr_more_file_to_edit,
5719 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005720 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 return FAIL;
5723 }
5724 return OK;
5725}
5726
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727/*
5728 * Function given to ExpandGeneric() to obtain the list of command names.
5729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005731get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732{
5733 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005734 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 return cmdnames[idx].cmd_name;
5736}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005739ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
Bram Moolenaare6850792010-05-14 15:28:44 +02005741 if (*eap->arg == NUL)
5742 {
5743#ifdef FEAT_EVAL
5744 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5745 char_u *p = NULL;
5746
5747 if (expr != NULL)
5748 {
5749 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005750 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005751 --emsg_off;
5752 vim_free(expr);
5753 }
5754 if (p != NULL)
5755 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005756 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005757 vim_free(p);
5758 }
5759 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005760 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005761#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005762 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005763#endif
5764 }
5765 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005766 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005767
5768#ifdef FEAT_VTP
5769 else if (has_vtp_working())
5770 {
5771 // background color change requires clear + redraw
5772 update_screen(CLEAR);
5773 redrawcmd();
5774 }
5775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776}
5777
5778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005779ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005782 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 do_highlight(eap->arg, eap->forceit, FALSE);
5784}
5785
5786
5787/*
5788 * Call this function if we thought we were going to exit, but we won't
5789 * (because of an error). May need to restore the terminal mode.
5790 */
5791 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005792not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793{
5794 exiting = FALSE;
5795 settmode(TMODE_RAW);
5796}
5797
Bram Moolenaar3552e742021-05-29 12:21:58 +02005798 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005799before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5800{
5801 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5802
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005803 // Bail out when autocommands closed the window.
5804 // Refuse to quit when the buffer in the last window is being closed (can
5805 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005806 if (!win_valid(wp)
5807 || curbuf_locked()
5808 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5809 return TRUE;
5810
5811 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5812 {
5813 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005814 // Refuse to quit when locked or when the window was closed or the
5815 // buffer in the last window is being closed (can only happen in
5816 // autocommands).
5817 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005818 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5819 return TRUE;
5820 }
5821
5822 return FALSE;
5823}
5824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005826 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005827 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005828 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005830 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005831ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005833 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835#ifdef FEAT_CMDWIN
5836 if (cmdwin_type != 0)
5837 {
5838 cmdwin_result = Ctrl_C;
5839 return;
5840 }
5841#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005842 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005843 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005844 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005845 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005846 return;
5847 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005848 if (eap->addr_count > 0)
5849 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005850 int wnr = eap->line2;
5851
5852 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5853 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005854 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005855 }
5856 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005857 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005858
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005859 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005860 if (curbuf_locked())
5861 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005862
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005863 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005864 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005865 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
5867#ifdef FEAT_NETBEANS_INTG
5868 netbeansForcedQuit = eap->forceit;
5869#endif
5870
5871 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005872 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 */
5874 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5875 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005876 if ((!buf_hide(wp->w_buffer)
5877 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005878 | (eap->forceit ? CCGD_FORCEIT : 0)
5879 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005881 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
5883 not_exiting();
5884 }
5885 else
5886 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005887 // quit last window
5888 // Note: only_one_window() returns true, even so a help window is
5889 // still open. In that case only quit, if no address has been
5890 // specified. Example:
5891 // :h|wincmd w|1q - don't quit
5892 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005893 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005895 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005896#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005898#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005899 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005900 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
5902}
5903
5904/*
5905 * ":cquit".
5906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005908ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005910 // this does not always pass on the exit code to the Manx compiler. why?
5911 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912}
5913
5914/*
5915 * ":qall": try to quit all windows
5916 */
5917 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005918ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919{
5920# ifdef FEAT_CMDWIN
5921 if (cmdwin_type != 0)
5922 {
5923 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005924 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 else
5926 cmdwin_result = K_XF2;
5927 return;
5928 }
5929# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005930
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005931 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005932 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005933 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005934 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005935 return;
5936 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005937
5938 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005939 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005940
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005942 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 getout(0);
5944 not_exiting();
5945}
5946
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947/*
5948 * ":close": close current window, unless it is the last one
5949 */
5950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005951ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005953 win_T *win;
5954 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005955#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005957 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005959#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005960 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005961 {
5962 if (eap->addr_count == 0)
5963 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005964 else
5965 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005966 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005967 {
5968 winnr++;
5969 if (winnr == eap->line2)
5970 break;
5971 }
5972 if (win == NULL)
5973 win = lastwin;
5974 ex_win_close(eap->forceit, win, NULL);
5975 }
5976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977}
5978
Bram Moolenaar4033c552017-09-16 20:54:51 +02005979#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005980/*
5981 * ":pclose": Close any preview window.
5982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005984ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005985{
5986 win_T *win;
5987
Bram Moolenaar79648732019-07-18 21:43:07 +02005988 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005989 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005990 if (win->w_p_pvw)
5991 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005992 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005993 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005994 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005995# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005996 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005997 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005998# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005999}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006000#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006001
Bram Moolenaarf740b292006-02-16 22:11:02 +00006002/*
6003 * Close window "win" and take care of handling closing the last window for a
6004 * modified buffer.
6005 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006007ex_win_close(
6008 int forceit,
6009 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006010 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011{
6012 int need_hide;
6013 buf_T *buf = win->w_buffer;
6014
Bram Moolenaarcf844172020-06-26 19:44:06 +02006015 // Never close the autocommand window.
6016 if (win == aucmd_win)
6017 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006018 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006019 return;
6020 }
6021
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006023 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006025#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006026 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006028 bufref_T bufref;
6029
6030 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006032 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 return;
6034 need_hide = FALSE;
6035 }
6036 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006039 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 return;
6041 }
6042 }
6043
Bram Moolenaar4033c552017-09-16 20:54:51 +02006044#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006046#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006047
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006048 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006049 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006050 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006051 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006052 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053}
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006056 * Handle the argument for a tabpage related ex command.
6057 * Returns a tabpage number.
6058 * When an error is encountered then eap->errmsg is set.
6059 */
6060 static int
6061get_tabpage_arg(exarg_T *eap)
6062{
6063 int tab_number;
6064 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6065
6066 if (eap->arg && *eap->arg != NUL)
6067 {
6068 char_u *p = eap->arg;
6069 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006070 int relative = 0; // argument +N/-N means: go to N places to the
6071 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006072
6073 if (*p == '-')
6074 {
6075 relative = -1;
6076 p++;
6077 }
6078 else if (*p == '+')
6079 {
6080 relative = 1;
6081 p++;
6082 }
6083
6084 p_save = p;
6085 tab_number = getdigits(&p);
6086
6087 if (relative == 0)
6088 {
6089 if (STRCMP(p, "$") == 0)
6090 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006091 else if (STRCMP(p, "#") == 0)
6092 if (valid_tabpage(lastused_tabpage))
6093 tab_number = tabpage_index(lastused_tabpage);
6094 else
6095 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006096 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006097 tab_number = 0;
6098 goto theend;
6099 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006100 else if (p == p_save || *p_save == '-' || *p != NUL
6101 || tab_number > LAST_TAB_NR)
6102 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006104 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006105 goto theend;
6106 }
6107 }
6108 else
6109 {
6110 if (*p_save == NUL)
6111 tab_number = 1;
6112 else if (p == p_save || *p_save == '-' || *p != NUL
6113 || tab_number == 0)
6114 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006115 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006116 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006117 goto theend;
6118 }
6119 tab_number = tab_number * relative + tabpage_index(curtab);
6120 if (!unaccept_arg0 && relative == -1)
6121 --tab_number;
6122 }
6123 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006124 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006125 }
6126 else if (eap->addr_count > 0)
6127 {
6128 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006129 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006130 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006131 tab_number = 0;
6132 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006133 else
6134 {
6135 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006136 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006137 {
6138 --tab_number;
6139 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006140 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006141 }
6142 }
6143 }
6144 else
6145 {
6146 switch (eap->cmdidx)
6147 {
6148 case CMD_tabnext:
6149 tab_number = tabpage_index(curtab) + 1;
6150 if (tab_number > LAST_TAB_NR)
6151 tab_number = 1;
6152 break;
6153 case CMD_tabmove:
6154 tab_number = LAST_TAB_NR;
6155 break;
6156 default:
6157 tab_number = tabpage_index(curtab);
6158 }
6159 }
6160
6161theend:
6162 return tab_number;
6163}
6164
6165/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006166 * ":tabclose": close current tab page, unless it is the last one.
6167 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 */
6169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006170ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006172 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006173 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006174
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006175# ifdef FEAT_CMDWIN
6176 if (cmdwin_type != 0)
6177 cmdwin_result = K_IGNORE;
6178 else
6179# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006180 if (first_tabpage->tp_next == NULL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00006181 emsg(_(e_cannot_close_last_tab_page));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006182 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006184 tab_number = get_tabpage_arg(eap);
6185 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006186 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006187 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006188 if (tp == NULL)
6189 {
6190 beep_flush();
6191 return;
6192 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006193 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006194 {
6195 tabpage_close_other(tp, eap->forceit);
6196 return;
6197 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006198 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006199 tabpage_close(eap->forceit);
6200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006202}
6203
6204/*
6205 * ":tabonly": close all tab pages except the current one
6206 */
6207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006208ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006209{
6210 tabpage_T *tp;
6211 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006212 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006213
6214# ifdef FEAT_CMDWIN
6215 if (cmdwin_type != 0)
6216 cmdwin_result = K_IGNORE;
6217 else
6218# endif
6219 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006220 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006221 else
6222 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006223 tab_number = get_tabpage_arg(eap);
6224 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006225 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006226 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006227 // Repeat this up to a 1000 times, because autocommands may
6228 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006229 for (done = 0; done < 1000; ++done)
6230 {
6231 FOR_ALL_TABPAGES(tp)
6232 if (tp->tp_topframe != topframe)
6233 {
6234 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006235 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006236 if (valid_tabpage(tp))
6237 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006238 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006239 break;
6240 }
6241 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006242 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006243 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006244 }
6245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247
6248/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006249 * Close the current tab page.
6250 */
6251 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006252tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006253{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006254 // First close all the windows but the current one. If that worked then
6255 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006256 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006257 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006258 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006259 ex_win_close(forceit, curwin, NULL);
6260# ifdef FEAT_GUI
6261 need_mouse_correct = TRUE;
6262# endif
6263}
6264
6265/*
6266 * Close tab page "tp", which is not the current tab page.
6267 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006268 * Also takes care of the tab pages line disappearing when closing the
6269 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006270 */
6271 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006272tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006273{
6274 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006275 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006276 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006277
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006278 // Limit to 1000 windows, autocommands may add a window while we close
6279 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006280 while (++done < 1000)
6281 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006282 wp = tp->tp_firstwin;
6283 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006284
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006285 // Autocommands may delete the tab page under our fingers and we may
6286 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006287 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006288 break;
6289 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006290
Bram Moolenaar29323592016-07-24 22:04:11 +02006291 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006292
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006293 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006294 if (h != tabline_height())
6295 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006296}
6297
6298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 * ":only".
6300 */
6301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006302ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006304 win_T *wp;
6305 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306# ifdef FEAT_GUI
6307 need_mouse_correct = TRUE;
6308# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006309 if (eap->addr_count > 0)
6310 {
6311 wnr = eap->line2;
6312 for (wp = firstwin; --wnr > 0; )
6313 {
6314 if (wp->w_next == NULL)
6315 break;
6316 else
6317 wp = wp->w_next;
6318 }
6319 win_goto(wp);
6320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 close_others(TRUE, eap->forceit);
6322}
6323
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006325ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006327 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006328 if (!eap->skip)
6329 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006330#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006331 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006332#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006333 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006334 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006335 else
6336 {
6337 int winnr = 0;
6338 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006339
Bram Moolenaar2256c992016-11-15 21:17:07 +01006340 FOR_ALL_WINDOWS(win)
6341 {
6342 winnr++;
6343 if (winnr == eap->line2)
6344 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006345 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006346 if (win == NULL)
6347 win = lastwin;
6348 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 }
6351}
6352
6353/*
6354 * ":stop" and ":suspend": Suspend Vim.
6355 */
dbivolaruab16ad32021-12-29 19:41:47 +00006356 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006357ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 /*
6360 * Disallow suspending for "rvim".
6361 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006362 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 {
6364 if (!eap->forceit)
6365 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006366 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 windgoto((int)Rows - 1, 0);
6368 out_char('\n');
6369 out_flush();
6370 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006371 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006372 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006373 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006375 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006377 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006379 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006380 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 }
6382}
6383
6384/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006385 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 */
6387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006388ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006390#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006391 if (not_in_vim9(eap) == FAIL)
6392 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394#ifdef FEAT_CMDWIN
6395 if (cmdwin_type != 0)
6396 {
6397 cmdwin_result = Ctrl_C;
6398 return;
6399 }
6400#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006401 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006402 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006403 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006404 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006405 return;
6406 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006407
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006409 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 */
6411 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6412 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006413
6414 // Write the buffer for ":wq" or when it was changed.
6415 // Trigger QuitPre and ExitPre.
6416 // Check if we can exit now, after autocommands have changed things.
6417 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6418 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006420 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 {
6422 not_exiting();
6423 }
6424 else
6425 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006426 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006428 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429# ifdef FEAT_GUI
6430 need_mouse_correct = TRUE;
6431# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006432 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006433 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 }
6435}
6436
6437/*
6438 * ":print", ":list", ":number".
6439 */
6440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006441ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006443 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006444 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006447 for ( ;!got_int; ui_breakcheck())
6448 {
6449 print_line(eap->line1,
6450 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6451 || (eap->flags & EXFLAG_NR)),
6452 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6453 if (++eap->line1 > eap->line2)
6454 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006455 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006456 }
6457 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006458 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006459 curwin->w_cursor.lnum = eap->line2;
6460 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 }
6462
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464}
6465
6466#ifdef FEAT_BYTEOFF
6467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006468ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 goto_byte(eap->line2);
6471}
6472#endif
6473
6474/*
6475 * ":shell".
6476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006478ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
6480 do_shell(NULL, 0);
6481}
6482
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006483#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006485static int drop_busy = FALSE;
6486static int drop_filec;
6487static char_u **drop_filev = NULL;
6488static int drop_split;
6489static void (*drop_callback)(void *);
6490static void *drop_cookie;
6491
6492 static void
6493handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
6495 exarg_T ea;
6496 int save_msg_scroll = msg_scroll;
6497
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006498 // Setting the argument list may cause screen updates and being called
6499 // recursively. Avoid that by setting drop_busy.
6500 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006502 // Check whether the current buffer is changed. If so, we will need
6503 // to split the current window or data could be lost.
6504 // We don't need to check if the 'hidden' option is set, as in this
6505 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006506 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
6508 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006509 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 --emsg_off;
6511 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006512 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 if (win_split(0, 0) == FAIL)
6515 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006516 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006518 // When splitting the window, create a new alist. Otherwise the
6519 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 alist_unlink(curwin->w_alist);
6521 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 }
6523
6524 /*
6525 * Set up the new argument list.
6526 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006527 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528
6529 /*
6530 * Move to the first file.
6531 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006532 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006533 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 ea.cmd = (char_u *)"next";
6535 do_argfile(&ea, 0);
6536
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006537 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6538 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 need_start_insertmode = FALSE;
6540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006541 // Restore msg_scroll, otherwise a following command may cause scrolling
6542 // unexpectedly. The screen will be redrawn by the caller, thus
6543 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006545
6546 if (drop_callback != NULL)
6547 drop_callback(drop_cookie);
6548
6549 drop_filev = NULL;
6550 drop_busy = FALSE;
6551}
6552
6553/*
6554 * Handle a file drop. The code is here because a drop is *nearly* like an
6555 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006556 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006557 * code is very similar to :args and hence needs access to a lot of the static
6558 * functions in this file.
6559 *
6560 * The "filev" list must have been allocated using alloc(), as should each item
6561 * in the list. This function takes over responsibility for freeing the "filev"
6562 * list.
6563 */
6564 void
6565handle_drop(
6566 int filec, // the number of files dropped
6567 char_u **filev, // the list of files dropped
6568 int split, // force splitting the window
6569 void (*callback)(void *), // to be called after setting the argument
6570 // list
6571 void *cookie) // argument for "callback" (allocated)
6572{
6573 // Cannot handle recursive drops, finish the pending one.
6574 if (drop_busy)
6575 {
6576 FreeWild(filec, filev);
6577 vim_free(cookie);
6578 return;
6579 }
6580
6581 // When calling handle_drop() more than once in a row we only use the last
6582 // one.
6583 if (drop_filev != NULL)
6584 {
6585 FreeWild(drop_filec, drop_filev);
6586 vim_free(drop_cookie);
6587 }
6588
6589 drop_filec = filec;
6590 drop_filev = filev;
6591 drop_split = split;
6592 drop_callback = callback;
6593 drop_cookie = cookie;
6594
6595 // Postpone this when:
6596 // - editing the command line
6597 // - not possible to change the current buffer
6598 // - updating the screen
6599 // As it may change buffers and window structures that are in use and cause
6600 // freed memory to be used.
6601 if (text_locked() || curbuf_locked() || updating_screen)
6602 return;
6603
6604 handle_drop_internal();
6605}
6606
6607/*
6608 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6609 * reset: Handle a postponed drop.
6610 */
6611 void
6612handle_any_postponed_drop(void)
6613{
6614 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006615 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006616 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617}
6618#endif
6619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 * ":preserve".
6622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006624ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006626 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 ml_preserve(curbuf, TRUE);
6628}
6629
6630/*
6631 * ":recover".
6632 */
6633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006634ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006636 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006638 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6639 | CCGD_MULTWIN
6640 | (eap->forceit ? CCGD_FORCEIT : 0)
6641 | CCGD_EXCMD)
6642
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 && (*eap->arg == NUL
6644 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006645 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 recoverymode = FALSE;
6647}
6648
6649/*
6650 * Command modifier used in a wrong way.
6651 */
6652 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006653ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006655 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656}
6657
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658/*
6659 * :sview [+command] file split window with new file, read-only
6660 * :split [[+command] file] split window with current or new file
6661 * :vsplit [[+command] file] split window vertically with current or new file
6662 * :new [[+command] file] split window with no or new file
6663 * :vnew [[+command] file] split vertically window with no or new file
6664 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006665 *
6666 * :tabedit open new Tab page with empty window
6667 * :tabedit [+command] file open new Tab page and edit "file"
6668 * :tabnew [[+command] file] just like :tabedit
6669 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 */
6671 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006672ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006674 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006675#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006677#endif
6678#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006679 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006680 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006681#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006682 int use_tab = eap->cmdidx == CMD_tabedit
6683 || eap->cmdidx == CMD_tabfind
6684 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006686 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006687 return;
6688
Bram Moolenaar4033c552017-09-16 20:54:51 +02006689#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
Bram Moolenaar4033c552017-09-16 20:54:51 +02006693#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006694 // A ":split" in the quickfix window works like ":new". Don't want two
6695 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006696 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 {
6698 if (eap->cmdidx == CMD_split)
6699 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (eap->cmdidx == CMD_vsplit)
6701 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
Bram Moolenaar4033c552017-09-16 20:54:51 +02006705#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006706 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {
6708 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6709 FNAME_MESS, TRUE, curbuf->b_ffname);
6710 if (fname == NULL)
6711 goto theend;
6712 eap->arg = fname;
6713 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006714# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006715 else
6716# endif
6717#endif
6718#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006719 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 && eap->cmdidx != CMD_new)
6722 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006723 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006724# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006725 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006726# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006727 au_has_group((char_u *)"FileExplorer"))
6728 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006729 // No browsing supported but we do have the file explorer:
6730 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006731 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006732 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006733 }
6734 else
6735 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006736 fname = do_browse(0, (char_u *)(use_tab
6737 ? _("Edit File in new tab page")
6738 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006740 if (fname == NULL)
6741 goto theend;
6742 eap->arg = fname;
6743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006745 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006748 /*
6749 * Either open new tab page or split the window.
6750 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006751 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006752 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006753 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006754 : eap->addr_count == 0 ? 0
6755 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006756 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006757 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006758
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006759 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006760 if (curwin != old_curwin
6761 && win_valid(old_curwin)
6762 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006763 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006764 old_curwin->w_alt_fnum = curbuf->b_fnum;
6765 }
6766 }
6767 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6769 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006770 // Reset 'scrollbind' when editing another file, but keep it when
6771 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006772 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006773 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 else
6775 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 do_exedit(eap, old_curwin);
6777 }
6778
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006779# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006780 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006783# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784theend:
6785 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006788
6789/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006790 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006791 */
6792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006793tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006794{
6795 exarg_T ea;
6796
Bram Moolenaara80faa82020-04-12 19:37:17 +02006797 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006798 ea.cmdidx = CMD_tabnew;
6799 ea.cmd = (char_u *)"tabn";
6800 ea.arg = (char_u *)"";
6801 ex_splitview(&ea);
6802}
6803
6804/*
6805 * :tabnext command
6806 */
6807 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006808ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006809{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006810 int tab_number;
6811
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006812 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006813 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006814 switch (eap->cmdidx)
6815 {
6816 case CMD_tabfirst:
6817 case CMD_tabrewind:
6818 goto_tabpage(1);
6819 break;
6820 case CMD_tablast:
6821 goto_tabpage(9999);
6822 break;
6823 case CMD_tabprevious:
6824 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006825 if (eap->arg && *eap->arg != NUL)
6826 {
6827 char_u *p = eap->arg;
6828 char_u *p_save = p;
6829
6830 tab_number = getdigits(&p);
6831 if (p == p_save || *p_save == '-' || *p != NUL
6832 || tab_number == 0)
6833 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006834 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006835 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006836 return;
6837 }
6838 }
6839 else
6840 {
6841 if (eap->addr_count == 0)
6842 tab_number = 1;
6843 else
6844 {
6845 tab_number = eap->line2;
6846 if (tab_number < 1)
6847 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006848 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006849 return;
6850 }
6851 }
6852 }
6853 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006854 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006855 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006856 tab_number = get_tabpage_arg(eap);
6857 if (eap->errmsg == NULL)
6858 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006859 break;
6860 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006861}
6862
6863/*
6864 * :tabmove command
6865 */
6866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006867ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006868{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006869 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006870
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006871 tab_number = get_tabpage_arg(eap);
6872 if (eap->errmsg == NULL)
6873 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006874}
6875
6876/*
6877 * :tabs command: List tabs and their contents.
6878 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006880ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006881{
6882 tabpage_T *tp;
6883 win_T *wp;
6884 int tabcount = 1;
6885
6886 msg_start();
6887 msg_scroll = TRUE;
6888 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6889 {
6890 msg_putchar('\n');
6891 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006892 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006893 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006894 ui_breakcheck();
6895
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006896 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006897 wp = firstwin;
6898 else
6899 wp = tp->tp_firstwin;
6900 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6901 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006902 msg_putchar('\n');
6903 msg_putchar(wp == curwin ? '>' : ' ');
6904 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006905 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6906 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006907 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006908 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006909 else
6910 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6911 IObuff, IOSIZE, TRUE);
6912 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006913 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006914 ui_breakcheck();
6915 }
6916 }
6917}
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919/*
6920 * ":mode": Set screen mode.
6921 * If no argument given, just get the screen size and redraw.
6922 */
6923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006924ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 if (*eap->arg == NUL)
6927 shell_resized();
6928 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006929 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930}
6931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932/*
6933 * ":resize".
6934 * set, increment or decrement current window height
6935 */
6936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006937ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938{
6939 int n;
6940 win_T *wp = curwin;
6941
6942 if (eap->addr_count > 0)
6943 {
6944 n = eap->line2;
6945 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6946 ;
6947 }
6948
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006949# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006953 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 {
6955 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006956 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006957 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006959 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 }
6961 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {
6963 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006964 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006965 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006967 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 }
6969}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
6971/*
6972 * ":find [+command] <file>" command.
6973 */
6974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006975ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
6977#ifdef FEAT_SEARCHPATH
6978 char_u *fname;
6979 int count;
6980
6981 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6982 TRUE, curbuf->b_ffname);
6983 if (eap->addr_count > 0)
6984 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006985 // Repeat finding the file "count" times. This matters when it
6986 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 count = eap->line2;
6988 while (fname != NULL && --count > 0)
6989 {
6990 vim_free(fname);
6991 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6992 FALSE, curbuf->b_ffname);
6993 }
6994 }
6995
6996 if (fname != NULL)
6997 {
6998 eap->arg = fname;
6999#endif
7000 do_exedit(eap, NULL);
7001#ifdef FEAT_SEARCHPATH
7002 vim_free(fname);
7003 }
7004#endif
7005}
7006
7007/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007008 * ":open" simulation: for now just work like ":visual".
7009 */
7010 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007011ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007012{
7013 regmatch_T regmatch;
7014 char_u *p;
7015
Bram Moolenaar65088802021-03-13 21:07:21 +01007016#ifdef FEAT_EVAL
7017 if (not_in_vim9(eap) == FAIL)
7018 return;
7019#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007020 curwin->w_cursor.lnum = eap->line2;
7021 beginline(BL_SOL | BL_FIX);
7022 if (*eap->arg == '/')
7023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007024 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007025 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007026 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007027 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007028 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007029 if (regmatch.regprog != NULL)
7030 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007031 // make a copy of the line, when searching for a mark it might be
7032 // flushed
7033 char_u *line = vim_strsave(ml_get_curline());
7034
Bram Moolenaardf177f62005-02-22 08:39:57 +00007035 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007036 if (vim_regexec(&regmatch, line, (colnr_T)0))
7037 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007038 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007039 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007040 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007041 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007042 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007043 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007044 eap->arg += STRLEN(eap->arg);
7045 }
7046 check_cursor();
7047
7048 eap->cmdidx = CMD_visual;
7049 do_exedit(eap, NULL);
7050}
7051
7052/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007053 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 */
7055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007056ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057{
7058 do_exedit(eap, NULL);
7059}
7060
7061/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007062 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007065do_exedit(
7066 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007067 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
7069 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007071 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007073 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7074 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007075 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 /*
7077 * ":vi" command ends Ex mode.
7078 */
7079 if (exmode_active && (eap->cmdidx == CMD_visual
7080 || eap->cmdidx == CMD_view))
7081 {
7082 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007083 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007086 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007087 if (global_busy)
7088 {
7089 int rd = RedrawingDisabled;
7090 int nwr = no_wait_return;
7091 int ms = msg_scroll;
7092#ifdef FEAT_GUI
7093 int he = hold_gui_events;
7094#endif
7095
7096 if (eap->nextcmd != NULL)
7097 {
7098 stuffReadbuff(eap->nextcmd);
7099 eap->nextcmd = NULL;
7100 }
7101
7102 if (exmode_was != EXMODE_VIM)
7103 settmode(TMODE_RAW);
7104 RedrawingDisabled = 0;
7105 no_wait_return = 0;
7106 need_wait_return = FALSE;
7107 msg_scroll = 0;
7108#ifdef FEAT_GUI
7109 hold_gui_events = 0;
7110#endif
7111 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007112 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007113
7114 main_loop(FALSE, TRUE);
7115
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007116 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007117 RedrawingDisabled = rd;
7118 no_wait_return = nwr;
7119 msg_scroll = ms;
7120#ifdef FEAT_GUI
7121 hold_gui_events = he;
7122#endif
7123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 }
7127
7128 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007129 || eap->cmdidx == CMD_tabnew
7130 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007131 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007133 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 setpcmark();
7135 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007136 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7137 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007139 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 || *eap->arg != NUL
7141#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007142 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143#endif
7144 )
7145 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007146 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7147 // Only ":edit" or ":script" can bring us here, others are stopped
7148 // earlier.
7149 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007150 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007151
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 n = readonlymode;
7153 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7154 readonlymode = TRUE;
7155 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007156 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7157 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007158 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7159 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7161 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007162 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7164 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7165 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007166 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007168 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007169 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007170 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7171 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007172 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007174 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 if (old_curwin != NULL)
7176 {
7177 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007178 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007180#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007181 cleanup_T cs;
7182
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007183 // Reset the error/interrupt/exception state here so that
7184 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007185 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007186#endif
7187#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007189#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007190 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007191
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007192#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007193 // Restore the error/interrupt/exception state if not
7194 // discarded by a new aborting error, interrupt, or
7195 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007196 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 }
7199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 }
7201 else if (readonlymode && curbuf->b_nwindows == 1)
7202 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007203 // When editing an already visited buffer, 'readonly' won't be set
7204 // but the previous value is kept. With ":view" and ":sview" we
7205 // want the file to be readonly, except when another window is
7206 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 curbuf->b_p_ro = TRUE;
7208 }
7209 readonlymode = n;
7210 }
7211 else
7212 {
7213 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007214 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 if (n != curwin->w_arg_idx_invalid)
7218 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 }
7220
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 /*
7222 * if ":split file" worked, set alternate file name in old window to new
7223 * file
7224 */
7225 if (old_curwin != NULL
7226 && *eap->arg != NUL
7227 && curwin != old_curwin
7228 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007229 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007230 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
7233 ex_no_reprint = TRUE;
7234}
7235
7236#ifndef FEAT_GUI
7237/*
7238 * ":gui" and ":gvim" when there is no GUI.
7239 */
7240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007241ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007243 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244}
7245#endif
7246
Bram Moolenaar4f974752019-02-17 17:44:42 +01007247#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007249ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250{
7251 gui_make_tearoff(eap->arg);
7252}
7253#endif
7254
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007255#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7256 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007258ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007260# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7261 if (gui.in_use)
7262 gui_make_popup(eap->arg, eap->forceit);
7263# ifdef FEAT_TERM_POPUP_MENU
7264 else
7265# endif
7266# endif
7267# ifdef FEAT_TERM_POPUP_MENU
7268 pum_make_popup(eap->arg, eap->forceit);
7269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270}
7271#endif
7272
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007274ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
7276 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007277 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007279 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280}
7281
7282/*
7283 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7284 * offset.
7285 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007288ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007291 win_T *save_curwin = curwin;
7292 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 long topline;
7294 long y;
7295 linenr_T old_linenr = curwin->w_cursor.lnum;
7296
7297 setpcmark();
7298
7299 /*
7300 * determine max topline
7301 */
7302 if (curwin->w_p_scb)
7303 {
7304 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007305 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 {
7307 if (wp->w_p_scb && wp->w_buffer)
7308 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007309 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 if (topline > y)
7311 topline = y;
7312 }
7313 }
7314 if (topline < 1)
7315 topline = 1;
7316 }
7317 else
7318 {
7319 topline = 1;
7320 }
7321
7322
7323 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007324 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007326 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 {
7328 if (curwin->w_p_scb)
7329 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007330 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 y = topline - curwin->w_topline;
7332 if (y > 0)
7333 scrollup(y, TRUE);
7334 else
7335 scrolldown(-y, TRUE);
7336 curwin->w_scbind_pos = topline;
7337 redraw_later(VALID);
7338 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 }
7341 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007342 curwin = save_curwin;
7343 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 if (curwin->w_p_scb)
7345 {
7346 did_syncbind = TRUE;
7347 checkpcmark();
7348 if (old_linenr != curwin->w_cursor.lnum)
7349 {
7350 char_u ctrl_o[2];
7351
7352 ctrl_o[0] = Ctrl_O;
7353 ctrl_o[1] = 0;
7354 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7355 }
7356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357}
7358
7359
7360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007361ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007363 int i;
7364 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7365 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007367 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 do_bang(1, eap, FALSE, FALSE, TRUE);
7369 else
7370 {
7371 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7372 return;
7373
7374#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007375 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 {
7377 char_u *browseFile;
7378
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007379 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 NULL, NULL, NULL, curbuf);
7381 if (browseFile != NULL)
7382 {
7383 i = readfile(browseFile, NULL,
7384 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7385 vim_free(browseFile);
7386 }
7387 else
7388 i = OK;
7389 }
7390 else
7391#endif
7392 if (*eap->arg == NUL)
7393 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007394 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 return;
7396 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7397 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7398 }
7399 else
7400 {
7401 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7402 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7403 i = readfile(eap->arg, NULL,
7404 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7405
7406 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007407 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007409#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 if (!aborting())
7411#endif
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007412 semsg(_(e_cant_open_file_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 }
7414 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007415 {
7416 if (empty && exmode_active)
7417 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007418 // Delete the empty line that remains. Historically ex does
7419 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007420 if (eap->line2 == 0)
7421 lnum = curbuf->b_ml.ml_line_count;
7422 else
7423 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007424 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007425 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007426 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007427 if (curwin->w_cursor.lnum > 1
7428 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007429 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007430 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007431 }
7432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 }
7436}
7437
Bram Moolenaarea408852005-06-25 22:49:46 +00007438static char_u *prev_dir = NULL;
7439
7440#if defined(EXITFREE) || defined(PROTO)
7441 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007442free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007443{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007444 VIM_CLEAR(prev_dir);
7445 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007446}
7447#endif
7448
Bram Moolenaarf4258302013-06-02 18:20:17 +02007449/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007450 * Get the previous directory for the given chdir scope.
7451 */
7452 static char_u *
7453get_prevdir(cdscope_T scope)
7454{
7455 if (scope == CDSCOPE_WINDOW)
7456 return curwin->w_prevdir;
7457 else if (scope == CDSCOPE_TABPAGE)
7458 return curtab->tp_prevdir;
7459 return prev_dir;
7460}
7461
7462/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007463 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007464 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7465 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007466 */
7467 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007468post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007469{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007470 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007471 // Clear tab local directory for both :cd and :tcd
7472 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007473 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007474 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007475 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007476 char_u *pdir = get_prevdir(scope);
7477
7478 // If still in the global directory, need to remember current
7479 // directory as the global directory.
7480 if (globaldir == NULL && pdir != NULL)
7481 globaldir = vim_strsave(pdir);
7482
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007483 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007484 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007485 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007486 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007487 curtab->tp_localdir = vim_strsave(NameBuff);
7488 else
7489 curwin->w_localdir = vim_strsave(NameBuff);
7490 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007491 }
7492 else
7493 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007494 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007495 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007496 }
7497
zeertzjq64be6aa2021-11-19 11:59:08 +00007498 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007499 shorten_fnames(TRUE);
7500}
7501
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007502/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007503 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7504 */
7505 void
7506trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7507{
7508#ifdef FEAT_EVAL
7509 dict_T *v_event;
7510 save_v_event_T save_v_event;
7511
7512 v_event = get_v_event(&save_v_event);
7513 (void)dict_add_string(v_event, "directory", new_dir);
7514 dict_set_items_ro(v_event);
7515#endif
7516 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7517#ifdef FEAT_EVAL
7518 restore_v_event(v_event, &save_v_event);
7519#endif
7520}
7521
7522/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007523 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007524 * chdir() function.
7525 * scope == CDSCOPE_WINDOW: changes the window-local directory
7526 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7527 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007528 * Returns TRUE if the directory is successfully changed.
7529 */
7530 int
7531changedir_func(
7532 char_u *new_dir,
7533 int forceit,
7534 cdscope_T scope)
7535{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007536 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007537 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007538 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007539 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007540 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007541
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007542 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007543 return FALSE;
7544
7545 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7546 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007547 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007548 return FALSE;
7549 }
7550
7551 // ":cd -": Change to previous directory
7552 if (STRCMP(new_dir, "-") == 0)
7553 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007554 pdir = get_prevdir(scope);
7555 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007556 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007557 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007558 return FALSE;
7559 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007560 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007561 }
7562
7563 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007564 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007565 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007566 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007567 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007568
Bakudankun29f3a452021-12-11 12:28:08 +00007569 // For UNIX ":cd" means: go to home directory.
7570 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007571#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007572 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007573#else
7574 if (*new_dir == NUL && p_cdh)
7575#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007576 {
7577 // use NameBuff for home directory name
7578# ifdef VMS
7579 char_u *p;
7580
7581 p = mch_getenv((char_u *)"SYS$LOGIN");
7582 if (p == NULL || *p == NUL) // empty is the same as not set
7583 NameBuff[0] = NUL;
7584 else
7585 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7586# else
7587 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7588# endif
7589 new_dir = NameBuff;
7590 }
zeertzjqf38aad82021-12-30 13:45:57 +00007591 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007592 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7593 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007594 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007595 if (scope == CDSCOPE_WINDOW)
7596 acmd_fname = (char_u *)"window";
7597 else if (scope == CDSCOPE_TABPAGE)
7598 acmd_fname = (char_u *)"tabpage";
7599 else
7600 acmd_fname = (char_u *)"global";
7601 trigger_DirChangedPre(acmd_fname, new_dir);
7602
7603 if (vim_chdir(new_dir))
7604 {
7605 emsg(_(e_command_failed));
7606 vim_free(pdir);
7607 return FALSE;
7608 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007609 }
zeertzjq73257142022-02-02 13:16:37 +00007610
7611 if (scope == CDSCOPE_WINDOW)
7612 pp = &curwin->w_prevdir;
7613 else if (scope == CDSCOPE_TABPAGE)
7614 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007615 else
zeertzjq73257142022-02-02 13:16:37 +00007616 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007617 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007618 *pp = pdir;
7619
7620 post_chdir(scope);
7621
7622 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007623 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007624 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007625 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007626}
Bram Moolenaarea408852005-06-25 22:49:46 +00007627
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007629 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007631 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007632ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007634 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635
7636 new_dir = eap->arg;
7637#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007638 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7639 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 ex_pwd(NULL);
7641 else
7642#endif
7643 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007644 cdscope_T scope = CDSCOPE_GLOBAL;
7645
7646 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7647 scope = CDSCOPE_WINDOW;
7648 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7649 scope = CDSCOPE_TABPAGE;
7650
7651 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007652 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007653 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007654 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 ex_pwd(eap);
7656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 }
7658}
7659
7660/*
7661 * ":pwd".
7662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007664ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665{
7666 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7667 {
7668#ifdef BACKSLASH_IN_FILENAME
7669 slash_adjust(NameBuff);
7670#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007671 if (p_verbose > 0)
7672 {
7673 char *context = "global";
7674
Bram Moolenaar05268152021-11-18 18:53:45 +00007675 if (last_chdir_reason != NULL)
7676 context = last_chdir_reason;
7677 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007678 context = "window";
7679 else if (curtab->tp_localdir != NULL)
7680 context = "tabpage";
7681 smsg("[%s] %s", context, (char *)NameBuff);
7682 }
7683 else
7684 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 }
7686 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007687 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688}
7689
7690/*
7691 * ":=".
7692 */
7693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007694ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007696 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007697 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698}
7699
7700 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007701ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007703 int n;
7704 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705
7706 if (cursor_valid())
7707 {
7708 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7709 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007710 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007712
7713 len = eap->line2;
7714 switch (*eap->arg)
7715 {
7716 case 'm': break;
7717 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007718 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007719 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007720
7721 // Hide the cursor if invoked with !
7722 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723}
7724
7725/*
7726 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007727 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 */
7729 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007730do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731{
Bram Moolenaar52953192019-08-16 10:27:13 +02007732 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007733 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007734# ifdef ELAPSED_FUNC
7735 elapsed_T start_tv;
7736
7737 // Remember at what time we started, so that we know how much longer we
7738 // should wait after waiting for a bit.
7739 ELAPSED_INIT(start_tv);
7740# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007742 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007743 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007744 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007745 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007746
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007747 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007748 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007750 wait_now = msec - done > 1000L ? 1000L : msec - done;
7751#ifdef FEAT_TIMERS
7752 {
7753 long due_time = check_due_timer();
7754
7755 if (due_time > 0 && due_time < wait_now)
7756 wait_now = due_time;
7757 }
7758#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007759#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007760 if (has_any_channel() && wait_now > 20L)
7761 wait_now = 20L;
7762#endif
7763#ifdef FEAT_SOUND
7764 if (has_any_sound_callback() && wait_now > 20L)
7765 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007766#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007767 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007768
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007769#ifdef FEAT_JOB_CHANNEL
7770 if (has_any_channel())
7771 ui_breakcheck_force(TRUE);
7772 else
7773#endif
7774 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007775#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007776 // Process the netbeans and clientserver messages that may have been
7777 // received in the call to ui_breakcheck() when the GUI is in use. This
7778 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007779 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007780#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007781
7782# ifdef ELAPSED_FUNC
7783 // actual time passed
7784 done = ELAPSED_FUNC(start_tv);
7785# else
7786 // guestimate time passed (will actually be more)
7787 done += wait_now;
7788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007790
7791 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7792 // input buffer, otherwise a following call to input() fails.
7793 if (got_int)
7794 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007795
7796 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007797 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798}
7799
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800/*
7801 * ":winsize" command (obsolete).
7802 */
7803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007804ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
7806 int w, h;
7807 char_u *arg = eap->arg;
7808 char_u *p;
7809
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007810 if (!isdigit(*arg))
7811 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007812 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007813 return;
7814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 w = getdigits(&arg);
7816 arg = skipwhite(arg);
7817 p = arg;
7818 h = getdigits(&arg);
7819 if (*p != NUL && *arg == NUL)
7820 set_shellsize(w, h, TRUE);
7821 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007822 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823}
7824
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007826ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
7828 int xchar = NUL;
7829 char_u *p;
7830
7831 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7832 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007833 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 if (eap->arg[1] == NUL)
7835 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007836 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 return;
7838 }
7839 xchar = eap->arg[1];
7840 p = eap->arg + 2;
7841 }
7842 else
7843 p = eap->arg + 1;
7844
Bram Moolenaar63b91732021-08-05 20:40:03 +02007845 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007847 if (*p != NUL && *p != (
7848#ifdef FEAT_EVAL
7849 in_vim9script() ? '#' :
7850#endif
7851 '"')
7852 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007853 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007854 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007856 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007857 postponed_split_flags = cmdmod.cmod_split;
7858 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7860 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007861 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 }
7863}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864
Bram Moolenaar843ee412004-06-30 16:16:41 +00007865#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866/*
7867 * ":winpos".
7868 */
7869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007870ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871{
7872 int x, y;
7873 char_u *arg = eap->arg;
7874 char_u *p;
7875
7876 if (*arg == NUL)
7877 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007878# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007879# ifdef VIMDLL
7880 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7881 mch_get_winpos(&x, &y) != FAIL)
7882# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007884# else
7885 if (mch_get_winpos(&x, &y) != FAIL)
7886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {
7888 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007889 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 }
7891 else
7892# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007893 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 }
7895 else
7896 {
7897 x = getdigits(&arg);
7898 arg = skipwhite(arg);
7899 p = arg;
7900 y = getdigits(&arg);
7901 if (*p == NUL || *arg != NUL)
7902 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007903 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 return;
7905 }
7906# ifdef FEAT_GUI
7907 if (gui.in_use)
7908 gui_mch_set_winpos(x, y);
7909 else if (gui.starting)
7910 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007911 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 gui_win_x = x;
7913 gui_win_y = y;
7914 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007915# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 else
7917# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007918# endif
7919# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007920 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921# endif
7922# ifdef HAVE_TGETENT
7923 if (*T_CWP)
7924 term_set_winpos(x, y);
7925# endif
7926 }
7927}
7928#endif
7929
7930/*
7931 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7932 */
7933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007934ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
7936 oparg_T oa;
7937
7938 clear_oparg(&oa);
7939 oa.regname = eap->regname;
7940 oa.start.lnum = eap->line1;
7941 oa.end.lnum = eap->line2;
7942 oa.line_count = eap->line2 - eap->line1 + 1;
7943 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007945 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {
7947 setpcmark();
7948 curwin->w_cursor.lnum = eap->line1;
7949 beginline(BL_SOL | BL_FIX);
7950 }
7951
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007952 if (VIsual_active)
7953 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007954
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 switch (eap->cmdidx)
7956 {
7957 case CMD_delete:
7958 oa.op_type = OP_DELETE;
7959 op_delete(&oa);
7960 break;
7961
7962 case CMD_yank:
7963 oa.op_type = OP_YANK;
7964 (void)op_yank(&oa, FALSE, TRUE);
7965 break;
7966
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007967 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007968 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007970 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7971#else
7972 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007974 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 oa.op_type = OP_RSHIFT;
7976 else
7977 oa.op_type = OP_LSHIFT;
7978 op_shift(&oa, FALSE, eap->amount);
7979 break;
7980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007982 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983}
7984
7985/*
7986 * ":put".
7987 */
7988 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007989ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007991 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 if (eap->line2 == 0)
7993 {
7994 eap->line2 = 1;
7995 eap->forceit = TRUE;
7996 }
7997 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00007998 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007999 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008000 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001}
8002
8003/*
8004 * Handle ":copy" and ":move".
8005 */
8006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008007ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
8009 long n;
8010
Bram Moolenaar491799b2020-08-01 19:23:43 +02008011#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008012 if (not_in_vim9(eap) == FAIL)
8013 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008014#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008015 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008016 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 {
8018 eap->nextcmd = NULL;
8019 return;
8020 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008021 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022
8023 /*
8024 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8025 */
8026 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8027 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008028 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 return;
8030 }
8031
8032 if (eap->cmdidx == CMD_move)
8033 {
8034 if (do_move(eap->line1, eap->line2, n) == FAIL)
8035 return;
8036 }
8037 else
8038 ex_copy(eap->line1, eap->line2, n);
8039 u_clearline();
8040 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008041 ex_may_print(eap);
8042}
8043
8044/*
8045 * Print the current line if flags were given to the Ex command.
8046 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008047 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008048ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008049{
8050 if (eap->flags != 0)
8051 {
8052 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8053 (eap->flags & EXFLAG_LIST));
8054 ex_no_reprint = TRUE;
8055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056}
8057
8058/*
8059 * ":smagic" and ":snomagic".
8060 */
8061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008062ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008064 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008066 magic_overruled = eap->cmdidx == CMD_smagic
8067 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008068 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008069 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070}
8071
8072/*
8073 * ":join".
8074 */
8075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008076ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
8078 curwin->w_cursor.lnum = eap->line1;
8079 if (eap->line1 == eap->line2)
8080 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008081 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 return;
8083 if (eap->line2 == curbuf->b_ml.ml_line_count)
8084 {
8085 beep_flush();
8086 return;
8087 }
8088 ++eap->line2;
8089 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008090 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008092 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093}
8094
8095/*
8096 * ":[addr]@r" or ":[addr]*r": execute register
8097 */
8098 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008099ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100{
8101 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008102 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103
8104 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008105 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106
8107#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008108 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109#endif
8110
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008111 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 c = *eap->arg;
8113 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8114 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008115 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008116 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8117 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008118 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 else
8122 {
8123 int save_efr = exec_from_reg;
8124
8125 exec_from_reg = TRUE;
8126
8127 /*
8128 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008129 * Continue until the stuff buffer is empty and all added characters
8130 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008132 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8134
8135 exec_from_reg = save_efr;
8136 }
8137}
8138
8139/*
8140 * ":!".
8141 */
8142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008143ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144{
8145 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8146}
8147
8148/*
8149 * ":undo".
8150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008152ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008154 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008155 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008156 else
8157 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158}
8159
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008160#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008162ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008163{
8164 char_u hash[UNDO_HASH_SIZE];
8165
8166 u_compute_hash(hash);
8167 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8168}
8169
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008171ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008172{
8173 char_u hash[UNDO_HASH_SIZE];
8174
8175 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008176 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008177}
8178#endif
8179
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180/*
8181 * ":redo".
8182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008184ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185{
8186 u_redo(1);
8187}
8188
8189/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008190 * ":earlier" and ":later".
8191 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008193ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008194{
8195 long count = 0;
8196 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008197 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008198 char_u *p = eap->arg;
8199
8200 if (*p == NUL)
8201 count = 1;
8202 else if (isdigit(*p))
8203 {
8204 count = getdigits(&p);
8205 switch (*p)
8206 {
8207 case 's': ++p; sec = TRUE; break;
8208 case 'm': ++p; sec = TRUE; count *= 60; break;
8209 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008210 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8211 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008212 }
8213 }
8214
8215 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008216 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008217 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008218 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8219 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008220}
8221
8222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 * ":redir": start/stop redirection.
8224 */
8225 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008226ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227{
8228 char *mode;
8229 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008230 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
Bram Moolenaarba768492016-07-08 15:32:54 +02008232#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008233 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008234 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008235 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008236 return;
8237 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008238#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008239
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 if (STRICMP(eap->arg, "END") == 0)
8241 close_redir();
8242 else
8243 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008244 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008246 ++arg;
8247 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008249 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 mode = "a";
8251 }
8252 else
8253 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008254 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255
8256 close_redir();
8257
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008258 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008259 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 if (fname == NULL)
8261 return;
8262#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008263 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {
8265 char_u *browseFile;
8266
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008267 browseFile = do_browse(BROWSE_SAVE,
8268 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008269 fname, NULL, NULL,
8270 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008272 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 vim_free(fname);
8274 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008275 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 }
8277#endif
8278
8279 redir_fd = open_exfile(fname, eap->forceit, mode);
8280 vim_free(fname);
8281 }
8282#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008283 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008285 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008287 ++arg;
8288 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008290 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008291 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008293 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008295 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008296 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008297 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008300 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008301 if (*arg == '>')
8302 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008303 // Make register empty when not using @A-@Z and the
8304 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008305 if (*arg == NUL && !isupper(redir_reg))
8306 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008308 }
8309 if (*arg != NUL)
8310 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008311 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008312 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008313 }
8314 }
8315 else if (*arg == '=' && arg[1] == '>')
8316 {
8317 int append;
8318
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008319 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008320 close_redir();
8321 arg += 2;
8322
8323 if (*arg == '>')
8324 {
8325 ++arg;
8326 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008329 append = FALSE;
8330
8331 if (var_redir_start(skipwhite(arg), append) == OK)
8332 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 }
8334#endif
8335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008336 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008339 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008341
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008342 // Make sure redirection is not off. Can happen for cmdline completion
8343 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008344 if (redir_fd != NULL
8345#ifdef FEAT_EVAL
8346 || redir_reg || redir_vname
8347#endif
8348 )
8349 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350}
8351
8352/*
8353 * ":redraw": force redraw
8354 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008355 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008356ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357{
8358 int r = RedrawingDisabled;
8359 int p = p_lz;
8360
8361 RedrawingDisabled = 0;
8362 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008363 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008365 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 if (need_maketitle)
8367 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008368#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8369# ifdef VIMDLL
8370 if (!gui.in_use)
8371# endif
8372 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 RedrawingDisabled = r;
8375 p_lz = p;
8376
Bram Moolenaar5017c662022-04-07 18:06:08 +01008377 // After drawing the statusline screen_attr may still be set.
8378 screen_stop_highlight();
8379
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008380 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 msg_didout = FALSE;
8382 msg_col = 0;
8383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008384 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008385 need_wait_return = FALSE;
8386
Bram Moolenaara653e532022-04-19 11:38:24 +01008387 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008388 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008389 redrawcmdline();
8390
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 out_flush();
8392}
8393
8394/*
8395 * ":redrawstatus": force redraw of status line(s)
8396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008398ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 int r = RedrawingDisabled;
8401 int p = p_lz;
8402
8403 RedrawingDisabled = 0;
8404 p_lz = FALSE;
8405 if (eap->forceit)
8406 status_redraw_all();
8407 else
8408 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008409 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 RedrawingDisabled = r;
8411 p_lz = p;
8412 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413}
8414
Bram Moolenaare12bab32019-01-08 22:02:56 +01008415/*
8416 * ":redrawtabline": force redraw of the tabline
8417 */
8418 static void
8419ex_redrawtabline(exarg_T *eap UNUSED)
8420{
8421 int r = RedrawingDisabled;
8422 int p = p_lz;
8423
8424 RedrawingDisabled = 0;
8425 p_lz = FALSE;
8426
8427 draw_tabline();
8428
8429 RedrawingDisabled = r;
8430 p_lz = p;
8431 out_flush();
8432}
8433
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008435close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436{
8437 if (redir_fd != NULL)
8438 {
8439 fclose(redir_fd);
8440 redir_fd = NULL;
8441 }
8442#ifdef FEAT_EVAL
8443 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008444 if (redir_vname)
8445 {
8446 var_redir_stop();
8447 redir_vname = 0;
8448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449#endif
8450}
8451
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008452#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008453 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008454vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008455{
8456 if (vim_mkdir(name, prot) != 0)
8457 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008458 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008459 return FAIL;
8460 }
8461 return OK;
8462}
8463#endif
8464
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465/*
8466 * Open a file for writing for an Ex command, with some checks.
8467 * Return file descriptor, or NULL on failure.
8468 */
8469 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008470open_exfile(
8471 char_u *fname,
8472 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008473 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474{
8475 FILE *fd;
8476
8477#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008478 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 if (mch_isdir(fname))
8480 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008481 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 return NULL;
8483 }
8484#endif
8485 if (!forceit && *mode != 'a' && vim_fexists(fname))
8486 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008487 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 return NULL;
8489 }
8490
8491 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008492 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493
8494 return fd;
8495}
8496
8497/*
8498 * ":mark" and ":k".
8499 */
8500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008501ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
8503 pos_T pos;
8504
Bram Moolenaar10b94212021-02-19 21:42:57 +01008505#ifdef FEAT_EVAL
8506 if (not_in_vim9(eap) == FAIL)
8507 return;
8508#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008509 if (*eap->arg == NUL) // No argument?
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008510 emsg(_(e_argument_required));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008511 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar74409f62022-01-01 15:58:22 +00008512 semsg(_(e_trailing_characters_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 else
8514 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008515 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 curwin->w_cursor.lnum = eap->line2;
8517 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008518 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaar6d057012021-12-31 18:49:43 +00008519 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008520 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 }
8522}
8523
8524/*
8525 * Update w_topline, w_leftcol and the cursor position.
8526 */
8527 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008528update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008530 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 update_topline();
8532 if (!curwin->w_p_wrap)
8533 validate_cursor();
8534 update_curswant();
8535}
8536
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008538 * Save the current State and go to Normal mode.
8539 * Return TRUE if the typeahead could be saved.
8540 */
8541 int
8542save_current_state(save_state_T *sst)
8543{
8544 sst->save_msg_scroll = msg_scroll;
8545 sst->save_restart_edit = restart_edit;
8546 sst->save_msg_didout = msg_didout;
8547 sst->save_State = State;
8548 sst->save_insertmode = p_im;
8549 sst->save_finish_op = finish_op;
8550 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008551 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008552 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008553
Bram Moolenaar4324d872020-12-01 20:12:24 +01008554 msg_scroll = FALSE; // no msg scrolling in Normal mode
8555 restart_edit = 0; // don't go to Insert mode
8556 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008557
Bram Moolenaara452b802020-12-01 21:47:59 +01008558 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008559 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008560
8561 /*
8562 * Save the current typeahead. This is required to allow using ":normal"
8563 * from an event handler and makes sure we don't hang when the argument
8564 * ends with half a command.
8565 */
8566 save_typeahead(&sst->tabuf);
8567 return sst->tabuf.typebuf_valid;
8568}
8569
8570 void
8571restore_current_state(save_state_T *sst)
8572{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008573 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008574 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008575
8576 msg_scroll = sst->save_msg_scroll;
8577 restart_edit = sst->save_restart_edit;
8578 p_im = sst->save_insertmode;
8579 finish_op = sst->save_finish_op;
8580 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008581 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008582 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008583 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008584 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008585
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008586 // Restore the state (needed when called from a function executed for
8587 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008588 State = sst->save_State;
8589#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008590 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008591#endif
8592}
8593
8594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 * ":normal[!] {commands}": Execute normal mode commands.
8596 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008597 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008598ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008600 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 char_u *arg = NULL;
8602 int l;
8603 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008605 if (ex_normal_lock > 0)
8606 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008607 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008608 return;
8609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 if (ex_normal_busy >= p_mmd)
8611 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008612 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 return;
8614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 /*
8617 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8618 * this for the K_SPECIAL leading byte, otherwise special keys will not
8619 * work.
8620 */
8621 if (has_mbyte)
8622 {
8623 int len = 0;
8624
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008625 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 for (p = eap->arg; *p != NUL; ++p)
8627 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008628#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008629 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008631#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008632 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008633 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008634#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 )
8638 len += 2;
8639 }
8640 if (len > 0)
8641 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008642 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 if (arg != NULL)
8644 {
8645 len = 0;
8646 for (p = eap->arg; *p != NUL; ++p)
8647 {
8648 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008649#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 if (*p == CSI)
8651 {
8652 arg[len++] = KS_EXTRA;
8653 arg[len++] = (int)KE_CSI;
8654 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008655#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008656 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 {
8658 arg[len++] = *++p;
8659 if (*p == K_SPECIAL)
8660 {
8661 arg[len++] = KS_SPECIAL;
8662 arg[len++] = KE_FILLER;
8663 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008664#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 else if (*p == CSI)
8666 {
8667 arg[len++] = KS_EXTRA;
8668 arg[len++] = (int)KE_CSI;
8669 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 }
8672 arg[len] = NUL;
8673 }
8674 }
8675 }
8676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008678 ++ex_normal_busy;
8679 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 {
8681 /*
8682 * Repeat the :normal command for each line in the range. When no
8683 * range given, execute it just once, without positioning the cursor
8684 * first.
8685 */
8686 do
8687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 if (eap->addr_count != 0)
8689 {
8690 curwin->w_cursor.lnum = eap->line1++;
8691 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008692 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 }
8694
Bram Moolenaar13505972019-01-24 15:04:48 +01008695 exec_normal_cmd(arg != NULL
8696 ? arg
8697 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 }
8699 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8700 }
8701
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008702 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 update_topline_cursor();
8704
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008705 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008707 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008708#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008709 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008710#endif
8711
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713}
8714
8715/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008716 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 */
8718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008719ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008721 if (eap->forceit)
8722 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008723 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008724 if (!curwin->w_cursor.lnum)
8725 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008726 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008727 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008728#ifdef FEAT_TERMINAL
8729 // Ignore this when running in an active terminal.
8730 if (term_job_running(curbuf->b_term))
8731 return;
8732#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008733
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008734 // Ignore the command when already in Insert mode. Inserting an
8735 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01008736 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00008737 return;
8738
Bram Moolenaar64969662005-12-14 21:59:55 +00008739 if (eap->cmdidx == CMD_startinsert)
8740 restart_edit = 'a';
8741 else if (eap->cmdidx == CMD_startreplace)
8742 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008744 restart_edit = 'V';
8745
8746 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008748 if (eap->cmdidx == CMD_startinsert)
8749 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008750 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008752
8753 if (VIsual_active)
8754 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755}
8756
8757/*
8758 * ":stopinsert"
8759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008761ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762{
8763 restart_edit = 0;
8764 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008765 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008768/*
8769 * Execute normal mode command "cmd".
8770 * "remap" can be REMAP_NONE or REMAP_YES.
8771 */
8772 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008773exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008774{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008775 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008776 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008777 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008778}
Bram Moolenaar25281632016-01-21 23:32:32 +01008779
Bram Moolenaar25281632016-01-21 23:32:32 +01008780/*
8781 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008782 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008783 */
8784 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008785exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008786{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008787 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008788 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008789
Bram Moolenaar905dd902019-04-07 14:21:47 +02008790 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8791 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008792 clear_oparg(&oa);
8793 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008794 while ((!stuff_empty()
8795 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008796 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008797 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008798 {
8799 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008800#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008801 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008802 && oa.op_type == OP_NOP && oa.regname == NUL
8803 && !VIsual_active)
8804 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008805 // If terminal_loop() returns OK we got a key that is handled
8806 // in Normal model. With FAIL we first need to position the
8807 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008808 if (terminal_loop(TRUE) == OK)
8809 normal_cmd(&oa, TRUE);
8810 }
8811 else
8812#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008813 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008814 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008815 }
8816}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008817
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818#ifdef FEAT_FIND_ID
8819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008820ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
8822 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8823 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8824 (linenr_T)1, (linenr_T)MAXLNUM);
8825}
8826
Bram Moolenaar4033c552017-09-16 20:54:51 +02008827#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828/*
8829 * ":psearch"
8830 */
8831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008832ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833{
8834 g_do_tagpreview = p_pvh;
8835 ex_findpat(eap);
8836 g_do_tagpreview = 0;
8837}
8838#endif
8839
8840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008841ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842{
8843 int whole = TRUE;
8844 long n;
8845 char_u *p;
8846 int action;
8847
8848 switch (cmdnames[eap->cmdidx].cmd_name[2])
8849 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008850 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8852 action = ACTION_GOTO;
8853 else
8854 action = ACTION_SHOW;
8855 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008856 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 action = ACTION_SHOW_ALL;
8858 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008859 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 action = ACTION_GOTO;
8861 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008862 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 action = ACTION_SPLIT;
8864 break;
8865 }
8866
8867 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008868 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 {
8870 n = getdigits(&eap->arg);
8871 eap->arg = skipwhite(eap->arg);
8872 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008873 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 {
8875 whole = FALSE;
8876 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008877 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 if (*p)
8879 {
8880 *p++ = NUL;
8881 p = skipwhite(p);
8882
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008883 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008884 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008885 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008887 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 }
8889 }
8890 if (!eap->skip)
8891 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8892 whole, !eap->forceit,
8893 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8894 n, action, eap->line1, eap->line2);
8895}
8896#endif
8897
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
Bram Moolenaar4033c552017-09-16 20:54:51 +02008899#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900/*
8901 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8902 */
8903 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008904ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008906 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8908}
8909
8910/*
8911 * ":pedit"
8912 */
8913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008914ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915{
8916 win_T *curwin_save = curwin;
8917
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008918 if (ERROR_IF_ANY_POPUP_WINDOW)
8919 return;
8920
Bram Moolenaar026587b2019-08-17 15:08:00 +02008921 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008923 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008924
Bram Moolenaar026587b2019-08-17 15:08:00 +02008925 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008927
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 if (curwin != curwin_save && win_valid(curwin_save))
8929 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008930 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 validate_cursor();
8932 redraw_later(VALID);
8933 win_enter(curwin_save, TRUE);
8934 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008935# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008936 else if (WIN_IS_POPUP(curwin))
8937 {
8938 // can't keep focus in popup window
8939 win_enter(firstwin, TRUE);
8940 }
8941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 g_do_tagpreview = 0;
8943}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945
8946/*
8947 * ":stag", ":stselect" and ":stjump".
8948 */
8949 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008950ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951{
8952 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008953 postponed_split_flags = cmdmod.cmod_split;
8954 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8956 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008957 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
8960/*
8961 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8962 */
8963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008964ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965{
8966 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8967}
8968
8969 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008970ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971{
8972 int cmd;
8973
8974 switch (name[1])
8975 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008976 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008978 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008980 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008982 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008984 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008986 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008988 case 'f': // ":tfirst"
8989 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008991 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008993 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008995 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008997 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 return;
8999 }
9000#endif
9001 cmd = DT_TAG;
9002 break;
9003 }
9004
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009005 if (name[0] == 'l')
9006 {
9007#ifndef FEAT_QUICKFIX
9008 ex_ni(eap);
9009 return;
9010#else
9011 cmd = DT_LTAG;
9012#endif
9013 }
9014
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9016 eap->forceit, TRUE);
9017}
9018
9019/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009020 * Check "str" for starting with a special cmdline variable.
9021 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9022 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9023 */
9024 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009025find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009026{
9027 int len;
9028 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009029 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009030 "%",
9031#define SPEC_PERC 0
9032 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009033#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009034 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02009035#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009036 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02009037#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009038 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02009039#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009040 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009041#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009042 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009043#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009044 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02009045#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02009046 "<stack>", // call stack
9047#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01009048 "<script>", // script file name
9049#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009050 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01009051#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009052 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009053#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009054 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009055#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009056 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009057#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02009058 "<SID>", // script ID: <SNR>123_
9059#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009060#ifdef FEAT_CLIENTSERVER
9061 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02009062# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009063#endif
9064 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009065
K.Takataeeec2542021-06-02 13:28:16 +02009066 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009067 {
9068 len = (int)STRLEN(spec_str[i]);
9069 if (STRNCMP(src, spec_str[i], len) == 0)
9070 {
9071 *usedlen = len;
9072 return i;
9073 }
9074 }
9075 return -1;
9076}
9077
9078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 * Evaluate cmdline variables.
9080 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009081 * change "%" to curbuf->b_ffname
9082 * "#" to curwin->w_alt_fnum
9083 * "%%" to curwin->w_alt_fnum in Vim9 script
9084 * "<cword>" to word under the cursor
9085 * "<cWORD>" to WORD under the cursor
9086 * "<cexpr>" to C-expression under the cursor
9087 * "<cfile>" to path name under the cursor
9088 * "<sfile>" to sourced file name
9089 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009090 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009091 * "<slnum>" to sourced file line number
9092 * "<afile>" to file name for autocommand
9093 * "<abuf>" to buffer number for autocommand
9094 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 *
9096 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9097 * "" for error without a message) and NULL is returned.
9098 * Returns an allocated string if a valid match was found.
9099 * Returns NULL if no match was found. "usedlen" then still contains the
9100 * number of characters to skip.
9101 */
9102 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009103eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009104 char_u *src, // pointer into commandline
9105 char_u *srcstart, // beginning of valid memory for src
9106 int *usedlen, // characters after src that are used
9107 linenr_T *lnump, // line number for :e command, or NULL
9108 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009109 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009110 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009111 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112{
9113 int i;
9114 char_u *s;
9115 char_u *result;
9116 char_u *resultbuf = NULL;
9117 int resultlen;
9118 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009119 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009121 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124
9125 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009126 if (escaped != NULL)
9127 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128
9129 /*
9130 * Check if there is something to do.
9131 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009132 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009133 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 {
9135 *usedlen = 1;
9136 return NULL;
9137 }
9138
9139 /*
9140 * Skip when preceded with a backslash "\%" and "\#".
9141 * Note: In "\\%" the % is also not recognized!
9142 */
9143 if (src > srcstart && src[-1] == '\\')
9144 {
9145 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009146 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 return NULL;
9148 }
9149
9150 /*
9151 * word or WORD under cursor
9152 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009153 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9154 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009156 resultlen = find_ident_under_cursor(&result,
9157 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9158 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9159 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 if (resultlen == 0)
9161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009162 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 return NULL;
9164 }
9165 }
9166
9167 /*
9168 * '#': Alternate file name
9169 * '%': Current file name
9170 * File name under the cursor
9171 * File name for autocommand
9172 * and following modifiers
9173 */
9174 else
9175 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009176 int off = 0;
9177
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 switch (spec_idx)
9179 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009180 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009181#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009182 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009185 // '%': current file
9186 if (curbuf->b_fname == NULL)
9187 {
9188 result = (char_u *)"";
9189 valid = 0; // Must have ":p:h" to be valid
9190 }
9191 else
9192 {
9193 result = curbuf->b_fname;
9194 tilde_file = STRCMP(result, "~") == 0;
9195 }
9196 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009198#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009199 // "%%" alternate file
9200 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009201#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009202 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009203 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009204 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009206 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 result = arg_all();
9208 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009209 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009210 if (escaped != NULL)
9211 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 break;
9214 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009215 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009216 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009217 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009219 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009220 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009221 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009222 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009224 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009226 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009228 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009229 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009230 return NULL;
9231 }
9232#ifdef FEAT_EVAL
9233 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9234 (long)i);
9235 if (result == NULL)
9236 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009237 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009238 return NULL;
9239 }
9240#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009241 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 }
9245 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009246 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009247 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9248 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009249 buf = buflist_findnr(i);
9250 if (buf == NULL)
9251 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009252 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009253 return NULL;
9254 }
9255 if (lnump != NULL)
9256 *lnump = ECMD_LAST;
9257 if (buf->b_fname == NULL)
9258 {
9259 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009260 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009261 }
9262 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009263 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009264 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009265 tilde_file = STRCMP(result, "~") == 0;
9266 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 break;
9269
9270#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009271 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009272 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 if (result == NULL)
9274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009275 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 return NULL;
9277 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009278 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 break;
9280#endif
9281
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009282 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009284 if (result != NULL && !autocmd_fname_full)
9285 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009286 // Still need to turn the fname into a full path. It is
9287 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009288 autocmd_fname_full = TRUE;
9289 result = FullName_save(autocmd_fname, FALSE);
9290 vim_free(autocmd_fname);
9291 autocmd_fname = result;
9292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 if (result == NULL)
9294 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009295 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 return NULL;
9297 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009298 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 break;
9300
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009301 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 if (autocmd_bufnr <= 0)
9303 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009304 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 return NULL;
9306 }
9307 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9308 result = strbuf;
9309 break;
9310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009311 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 result = autocmd_match;
9313 if (result == NULL)
9314 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009315 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 return NULL;
9317 }
9318 break;
9319
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009320 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009321 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 if (result == NULL)
9323 {
LemonBoy6013d002022-04-09 21:42:10 +01009324 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9325 return NULL;
9326 }
9327 resultbuf = result; // remember allocated string
9328 break;
9329 case SPEC_STACK: // call stack
9330 result = estack_sfile(ESTACK_STACK);
9331 if (result == NULL)
9332 {
9333 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9334 return NULL;
9335 }
9336 resultbuf = result; // remember allocated string
9337 break;
9338 case SPEC_SCRIPT: // script file name
9339 result = estack_sfile(ESTACK_SCRIPT);
9340 if (result == NULL)
9341 {
9342 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 return NULL;
9344 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009345 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009347
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009348 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009349 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009350 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009351 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009352 return NULL;
9353 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009354 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009355 result = strbuf;
9356 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009357
9358#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009359 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009360 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009361 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009362 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009363 return NULL;
9364 }
9365 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009366 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009367 result = strbuf;
9368 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009369
Bram Moolenaar90944302020-08-01 20:45:11 +02009370 case SPEC_SID:
9371 if (current_sctx.sc_sid <= 0)
9372 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009373 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009374 return NULL;
9375 }
9376 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9377 result = strbuf;
9378 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009379#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009380
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009381#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009382 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009383 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9384 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 result = strbuf;
9386 break;
9387#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009388
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009389 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009390 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009391 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 }
9393
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009394 resultlen = (int)STRLEN(result); // length of new string
9395 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 {
9397 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 resultlen = (int)(s - result);
9400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 else if (!skip_mod)
9402 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009403 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 &resultlen);
9405 if (result == NULL)
9406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009407 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 return NULL;
9409 }
9410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 }
9412
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009413 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009415 if (empty_is_error)
9416 {
9417 if (valid != VALID_HEAD + VALID_PATH)
9418 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9419 else
9420 *errormsg = _(e_evaluates_to_an_empty_string);
9421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 result = NULL;
9423 }
9424 else
9425 result = vim_strnsave(result, resultlen);
9426 vim_free(resultbuf);
9427 return result;
9428}
9429
9430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 * Expand the <sfile> string in "arg".
9432 *
9433 * Returns an allocated string, or NULL for any error.
9434 */
9435 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009436expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009438 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 int len;
9440 char_u *result;
9441 char_u *newres;
9442 char_u *repl;
9443 int srclen;
9444 char_u *p;
9445
9446 result = vim_strsave(arg);
9447 if (result == NULL)
9448 return NULL;
9449
9450 for (p = result; *p; )
9451 {
9452 if (STRNCMP(p, "<sfile>", 7) != 0)
9453 ++p;
9454 else
9455 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009456 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009457 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 if (errormsg != NULL)
9459 {
9460 if (*errormsg)
9461 emsg(errormsg);
9462 vim_free(result);
9463 return NULL;
9464 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009465 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 {
9467 p += srclen;
9468 continue;
9469 }
9470 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9471 newres = alloc(len);
9472 if (newres == NULL)
9473 {
9474 vim_free(repl);
9475 vim_free(result);
9476 return NULL;
9477 }
9478 mch_memmove(newres, result, (size_t)(p - result));
9479 STRCPY(newres + (p - result), repl);
9480 len = (int)STRLEN(newres);
9481 STRCAT(newres, p + srclen);
9482 vim_free(repl);
9483 vim_free(result);
9484 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009485 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 }
9487 }
9488
9489 return result;
9490}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009493/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009494 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009495 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009498dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 if (fname == NULL)
9501 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009502 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503}
9504#endif
9505
9506/*
9507 * ":behave {mswin,xterm}"
9508 */
9509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009510ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511{
9512 if (STRCMP(eap->arg, "mswin") == 0)
9513 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009514 set_option_value_give_err((char_u *)"selection",
9515 0L, (char_u *)"exclusive", 0);
9516 set_option_value_give_err((char_u *)"selectmode",
9517 0L, (char_u *)"mouse,key", 0);
9518 set_option_value_give_err((char_u *)"mousemodel",
9519 0L, (char_u *)"popup", 0);
9520 set_option_value_give_err((char_u *)"keymodel",
9521 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 }
9523 else if (STRCMP(eap->arg, "xterm") == 0)
9524 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009525 set_option_value_give_err((char_u *)"selection",
9526 0L, (char_u *)"inclusive", 0);
9527 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9528 set_option_value_give_err((char_u *)"mousemodel",
9529 0L, (char_u *)"extend", 0);
9530 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 }
9532 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009533 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534}
9535
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536static int filetype_detect = FALSE;
9537static int filetype_plugin = FALSE;
9538static int filetype_indent = FALSE;
9539
9540/*
9541 * ":filetype [plugin] [indent] {on,off,detect}"
9542 * on: Load the filetype.vim file to install autocommands for file types.
9543 * off: Load the ftoff.vim file to remove all autocommands for file types.
9544 * plugin on: load filetype.vim and ftplugin.vim
9545 * plugin off: load ftplugof.vim
9546 * indent on: load filetype.vim and indent.vim
9547 * indent off: load indoff.vim
9548 */
9549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009550ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551{
9552 char_u *arg = eap->arg;
9553 int plugin = FALSE;
9554 int indent = FALSE;
9555
9556 if (*eap->arg == NUL)
9557 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009558 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009559 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 filetype_detect ? "ON" : "OFF",
9561 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9562 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9563 return;
9564 }
9565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009566 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 for (;;)
9568 {
9569 if (STRNCMP(arg, "plugin", 6) == 0)
9570 {
9571 plugin = TRUE;
9572 arg = skipwhite(arg + 6);
9573 continue;
9574 }
9575 if (STRNCMP(arg, "indent", 6) == 0)
9576 {
9577 indent = TRUE;
9578 arg = skipwhite(arg + 6);
9579 continue;
9580 }
9581 break;
9582 }
9583 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9584 {
9585 if (*arg == 'o' || !filetype_detect)
9586 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009587 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 filetype_detect = TRUE;
9589 if (plugin)
9590 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009591 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 filetype_plugin = TRUE;
9593 }
9594 if (indent)
9595 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009596 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 filetype_indent = TRUE;
9598 }
9599 }
9600 if (*arg == 'd')
9601 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009602 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009603 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 }
9605 }
9606 else if (STRCMP(arg, "off") == 0)
9607 {
9608 if (plugin || indent)
9609 {
9610 if (plugin)
9611 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009612 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 filetype_plugin = FALSE;
9614 }
9615 if (indent)
9616 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009617 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 filetype_indent = FALSE;
9619 }
9620 }
9621 else
9622 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009623 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 filetype_detect = FALSE;
9625 }
9626 }
9627 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009628 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629}
9630
9631/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009632 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 */
9634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009635ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636{
9637 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009638 {
9639 char_u *arg = eap->arg;
9640
9641 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9642 arg += 9;
9643
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009644 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
Bram Moolenaar3e545692017-06-04 19:00:32 +02009645 if (arg != eap->arg)
9646 did_filetype = FALSE;
9647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009651ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
9653#ifdef FEAT_DIGRAPHS
9654 if (*eap->arg != NUL)
9655 putdigraph(eap->arg);
9656 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009657 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658#else
mityu61065042021-07-19 20:07:21 +02009659 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660#endif
9661}
9662
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009663#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9664 void
9665set_no_hlsearch(int flag)
9666{
9667 no_hlsearch = flag;
9668# ifdef FEAT_EVAL
9669 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9670# endif
9671}
9672
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673/*
9674 * ":nohlsearch"
9675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009677ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009679 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009680 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682#endif
9683
9684#ifdef FEAT_CRYPT
9685/*
9686 * ":X": Get crypt key
9687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009689ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009691 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009692 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693}
9694#endif
9695
9696#ifdef FEAT_FOLDING
9697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009698ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
9700 if (foldManualAllowed(TRUE))
9701 foldCreate(eap->line1, eap->line2);
9702}
9703
9704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009705ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9708 eap->forceit, FALSE);
9709}
9710
9711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009712ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713{
9714 linenr_T lnum;
9715
Bram Moolenaar4facea32019-10-12 20:17:40 +02009716# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009717 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009718# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009719
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009720 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9722 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9723 ml_setmarked(lnum);
9724
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009725 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009727 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009728# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009729 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009734#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009735/*
9736 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9737 * instead of a quickfix command.
9738 */
9739 int
9740is_loclist_cmd(int cmdidx)
9741{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009742 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009743 return FALSE;
9744 return cmdnames[cmdidx].cmd_name[0] == 'l';
9745}
9746#endif
9747
Bram Moolenaar4facea32019-10-12 20:17:40 +02009748#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009749 int
9750get_pressedreturn(void)
9751{
9752 return ex_pressedreturn;
9753}
9754
9755 void
9756set_pressedreturn(int val)
9757{
9758 ex_pressedreturn = val;
9759}
9760#endif