blob: 048c224362c796e378acb0d7b4f399998b965d4f [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 Moolenaar227c58a2021-04-28 20:40:44 +02001207 if (!got_int && !did_throw && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001208 && ((getline_equal(fgetline, cookie, getsourceline)
1209 && !source_finished(fgetline, cookie))
1210 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 && !func_has_ended(real_cookie))))
1212 {
1213 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001214 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001216 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001218 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001220 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
1222
1223 /*
1224 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1225 * ":endtry" in a sourced file or executed function. If the try
1226 * conditional is in its finally clause, ignore anything pending.
1227 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001228 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 */
1230 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001231 {
1232 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1233
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001234 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001235 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001236 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1237 &cstack.cs_looplevel);
1238 }
1239 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 trylevel = initial_trylevel;
1241 }
1242
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001243 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1244 // lack was reported above and the error message is to be converted to an
1245 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001246 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 ? (char_u *)"endfunction" : (char_u *)NULL);
1248
1249 if (trylevel == 0)
1250 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001251 // Just in case did_throw got set but current_exception wasn't.
1252 if (current_exception == NULL)
1253 did_throw = FALSE;
1254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 /*
1256 * When an exception is being thrown out of the outermost try
1257 * conditional, discard the uncaught exception, disable the conversion
1258 * of interrupts or errors to exceptions, and ensure that no more
1259 * commands are executed.
1260 */
1261 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001262 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264 /*
1265 * On an interrupt or an aborting error not converted to an exception,
1266 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001267 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 * when force_abort is set and did_emsg unset in case of an interrupt
1269 * from a finally clause after an error.
1270 */
1271 else if (got_int || (did_emsg && force_abort))
1272 suppress_errthrow = TRUE;
1273 }
1274
1275 /*
1276 * The current cstack will be freed when do_cmdline() returns. An uncaught
1277 * exception will have to be rethrown in the previous cstack. If a function
1278 * has just returned or a script file was just finished and the previous
1279 * cstack belongs to the same function or, respectively, script file, it
1280 * will have to be checked for finally clauses to be executed due to the
1281 * ":return" or ":finish". This is done in do_one_cmd().
1282 */
1283 if (did_throw)
1284 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001285 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001287 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 && ex_nesting_level > func_level(real_cookie) + 1))
1289 {
1290 if (!did_throw)
1291 check_cstack = TRUE;
1292 }
1293 else
1294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001295 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001296 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 --ex_nesting_level;
1298 /*
1299 * Go to debug mode when returning from a function in which we are
1300 * single-stepping.
1301 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001302 if ((getline_equal(fgetline, cookie, getsourceline)
1303 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001305 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 ? (char_u *)_("End of sourced file")
1307 : (char_u *)_("End of function"));
1308 }
1309
1310 /*
1311 * Restore the exception environment (done after returning from the
1312 * debugger).
1313 */
1314 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001315 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
1317 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001318
1319 // Cleanup if "cs_emsg_silent_list" remains.
1320 if (cstack.cs_emsg_silent_list != NULL)
1321 {
1322 eslist_T *elem, *temp;
1323
1324 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1325 {
1326 temp = elem->next;
1327 vim_free(elem);
1328 }
1329 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001330#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
1332 /*
1333 * If there was too much output to fit on the command line, ask the user to
1334 * hit return before redrawing the screen. With the ":global" command we do
1335 * this only once after the command is finished.
1336 */
1337 if (did_inc)
1338 {
1339 --RedrawingDisabled;
1340 --no_wait_return;
1341 msg_scroll = FALSE;
1342
1343 /*
1344 * When just finished an ":if"-":else" which was typed, no need to
1345 * wait for hit-return. Also for an error situation.
1346 */
1347 if (retval == FAIL
1348#ifdef FEAT_EVAL
1349 || (did_endif && KeyTyped && !did_emsg)
1350#endif
1351 )
1352 {
1353 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001354 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 else if (need_wait_return)
1357 {
1358 /*
1359 * The msg_start() above clears msg_didout. The wait_return we do
1360 * here should not overwrite the command that may be shown before
1361 * doing that.
1362 */
1363 msg_didout |= msg_didout_before_start;
1364 wait_return(FALSE);
1365 }
1366 }
1367
Bram Moolenaar2a942252012-11-28 23:03:07 +01001368#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001369 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001370#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 /*
1372 * Reset if_level, in case a sourced script file contains more ":if" than
1373 * ":endif" (could be ":if x | foo | endif").
1374 */
1375 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001376#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 --call_depth;
1379 return retval;
1380}
1381
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001382#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001383/*
1384 * Handle when "did_throw" is set after executing commands.
1385 */
1386 void
1387handle_did_throw()
1388{
1389 char *p = NULL;
1390 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001391 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001392
1393 /*
1394 * If the uncaught exception is a user exception, report it as an
1395 * error. If it is an error exception, display the saved error
1396 * message now. For an interrupt exception, do nothing; the
1397 * interrupt message is given elsewhere.
1398 */
1399 switch (current_exception->type)
1400 {
1401 case ET_USER:
1402 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001403 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001404 current_exception->value);
1405 p = (char *)vim_strsave(IObuff);
1406 break;
1407 case ET_ERROR:
1408 messages = current_exception->messages;
1409 current_exception->messages = NULL;
1410 break;
1411 case ET_INTERRUPT:
1412 break;
1413 }
1414
1415 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1416 current_exception->throw_lnum);
1417 ESTACK_CHECK_SETUP
1418 current_exception->throw_name = NULL;
1419
1420 discard_current_exception(); // uses IObuff if 'verbose'
1421 suppress_errthrow = TRUE;
1422 force_abort = TRUE;
1423
1424 if (messages != NULL)
1425 {
1426 do
1427 {
1428 msglist_T *next = messages->next;
1429 int save_compiling = estack_compiling;
1430
1431 estack_compiling = messages->msg_compiling;
1432 emsg(messages->msg);
1433 vim_free(messages->msg);
1434 vim_free(messages->sfile);
1435 vim_free(messages);
1436 messages = next;
1437 estack_compiling = save_compiling;
1438 }
1439 while (messages != NULL);
1440 }
1441 else if (p != NULL)
1442 {
1443 emsg(p);
1444 vim_free(p);
1445 }
1446 vim_free(SOURCING_NAME);
1447 ESTACK_CHECK_NOW
1448 estack_pop();
1449}
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001452 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 */
1454 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001455get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001457 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 wcmd_T *wp;
1459 char_u *line;
1460
1461 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1462 {
1463 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001464 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001466 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 if (cp->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001468 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001470 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001471 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 ++cp->current_line;
1473
1474 return line;
1475 }
1476
1477 KeyTyped = FALSE;
1478 ++cp->current_line;
1479 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001480 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 return vim_strsave(wp->line);
1482}
1483
1484/*
1485 * Store a line in "gap" so that a ":while" loop can execute it again.
1486 */
1487 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001488store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 if (ga_grow(gap, 1) == FAIL)
1491 return FAIL;
1492 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001493 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 return OK;
1496}
1497
1498/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 */
1501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001502free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 while (gap->ga_len > 0)
1505 {
1506 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1507 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
1509}
1510#endif
1511
1512/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001513 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1514 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001517getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001518 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001519 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001520 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
1522#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001523 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001526 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1527 // function that's originally used to obtain the lines. This may be
1528 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001529 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001530 cp = (struct loop_cookie *)cookie;
1531 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
1533 gp = cp->getline;
1534 cp = cp->cookie;
1535 }
1536 return gp == func;
1537#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001538 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539#endif
1540}
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001543 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 * getline function. Otherwise return "cookie".
1545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001547getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001548 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001549 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550{
Bram Moolenaar13505972019-01-24 15:04:48 +01001551#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001552 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001553 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001555 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1556 // cookie that's originally used to obtain the lines. This may be nested
1557 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001558 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001559 cp = (struct loop_cookie *)cookie;
1560 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
1562 gp = cp->getline;
1563 cp = cp->cookie;
1564 }
1565 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001566#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
Bram Moolenaard5053d02020-06-28 15:51:16 +02001571#if defined(FEAT_EVAL) || defined(PROT)
1572/*
1573 * Get the next line source line without advancing.
1574 */
1575 char_u *
1576getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001577 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001578 void *cookie) // argument for fgetline()
1579{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001580 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001581 struct loop_cookie *cp;
1582 wcmd_T *wp;
1583
1584 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1585 // cookie that's originally used to obtain the lines. This may be nested
1586 // several levels.
1587 gp = fgetline;
1588 cp = (struct loop_cookie *)cookie;
1589 while (gp == get_loop_line)
1590 {
1591 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1592 {
1593 // executing lines a second time, use the stored copy
1594 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1595 return wp->line;
1596 }
1597 gp = cp->getline;
1598 cp = cp->cookie;
1599 }
1600 if (gp == getsourceline)
1601 return source_nextline(cp);
1602 return NULL;
1603}
1604#endif
1605
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001606
1607/*
1608 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1609 * ":bwipeout", etc.
1610 * Returns the buffer number.
1611 */
1612 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001613compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614{
1615 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001616 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617 int count = offset;
1618
1619 buf = firstbuf;
1620 while (buf->b_next != NULL && buf->b_fnum < lnum)
1621 buf = buf->b_next;
1622 while (count != 0)
1623 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001624 count += (offset < 0) ? 1 : -1;
1625 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1626 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001627 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001628 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001629 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001630 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001631 while (buf->b_ml.ml_mfp == NULL)
1632 {
1633 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1634 if (nextbuf == NULL)
1635 break;
1636 buf = nextbuf;
1637 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001638 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001639 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001640 if (addr_type == ADDR_LOADED_BUFFERS)
1641 while (buf->b_ml.ml_mfp == NULL)
1642 {
1643 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1644 if (nextbuf == NULL)
1645 break;
1646 buf = nextbuf;
1647 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001648 return buf->b_fnum;
1649}
1650
Bram Moolenaarb7316892019-05-01 18:08:42 +02001651/*
1652 * Return the window number of "win".
1653 * When "win" is NULL return the number of windows.
1654 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001655 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001656current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001657{
1658 win_T *wp;
1659 int nr = 0;
1660
Bram Moolenaar29323592016-07-24 22:04:11 +02001661 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001662 {
1663 ++nr;
1664 if (wp == win)
1665 break;
1666 }
1667 return nr;
1668}
1669
1670 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001671current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001672{
1673 tabpage_T *tp;
1674 int nr = 0;
1675
Bram Moolenaar29323592016-07-24 22:04:11 +02001676 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001677 {
1678 ++nr;
1679 if (tp == tab)
1680 break;
1681 }
1682 return nr;
1683}
1684
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001685 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001686comment_start(char_u *p, int starts_with_colon UNUSED)
1687{
1688#ifdef FEAT_EVAL
1689 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001690 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001691#endif
1692 return *p == '"';
1693}
1694
Bram Moolenaarf240e182014-11-27 18:33:02 +01001695# define CURRENT_WIN_NR current_win_nr(curwin)
1696# define LAST_WIN_NR current_win_nr(NULL)
1697# define CURRENT_TAB_NR current_tab_nr(curtab)
1698# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700/*
1701 * Execute one Ex command.
1702 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001703 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1704 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 *
1706 * 1. skip comment lines and leading space
1707 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001708 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001709 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001710 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001711 * 6. parse arguments
1712 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001714 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 *
1716 * This function may be called recursively!
1717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001719do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001720 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001721 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001723 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001725 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001726 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001728 char_u *p;
1729 linenr_T lnum;
1730 long n;
1731 char *errormsg = NULL; // error message
1732 char_u *after_modifier = NULL;
1733 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001734 cmdmod_T save_cmdmod;
1735 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001736 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001737 int ni; // set when Not Implemented
1738 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001739 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001740#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001741 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001742 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001743 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001744#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001745 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaara80faa82020-04-12 19:37:17 +02001747 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 ea.line1 = 1;
1749 ea.line2 = 1;
1750#ifdef FEAT_EVAL
1751 ++ex_nesting_level;
1752#endif
1753
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001754 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (quitmore
1756#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001757 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001759#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001760 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001761 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 --quitmore;
1763
1764 /*
1765 * Reset browse, confirm, etc.. They are restored when returning, for
1766 * recursive calls.
1767 */
1768 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001770 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001771 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1772 goto doend;
1773
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001774/*
1775 * 1. Skip comment lines and leading white space and colons.
1776 * 2. Handle command modifiers.
1777 */
1778 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001780 ea.cmdlinep = cmdlinep;
1781 ea.getline = fgetline;
1782 ea.cookie = cookie;
1783#ifdef FEAT_EVAL
1784 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001785 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001787 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001788 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001789 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001790#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001791 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001792#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001793 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795#ifdef FEAT_EVAL
1796 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1797 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1798#else
1799 ea.skip = (if_level > 0);
1800#endif
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001803 * 3. Skip over the range to find the command. Let "p" point to after it.
1804 *
1805 * We need the command to know what kind of range it uses.
1806 */
1807 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001808#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001809 // In Vim9 script a colon is required before the range. This may also be
1810 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001811 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001812 {
1813 may_have_range = FALSE;
1814 for (p = ea.cmd; p >= *cmdlinep; --p)
1815 {
1816 if (*p == ':')
1817 may_have_range = TRUE;
1818 if (p < ea.cmd && !VIM_ISWHITE(*p))
1819 break;
1820 }
1821 }
1822 else
1823 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001824 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001825#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001826 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827
1828#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001829 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001830 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001831 if (ea.cmd == cmd + 1 && *cmd == '$')
1832 // should be "$VAR = val"
1833 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001834 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001835 if (ea.cmdidx == CMD_SIZE)
1836 {
1837 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1838
1839 // If a ':' before the range is missing, give a clearer error
1840 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001841 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001842 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001843 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001844 goto doend;
1845 }
1846 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 else
1849#endif
1850 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001851
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001852#ifdef FEAT_EVAL
1853# ifdef FEAT_PROFILE
1854 // Count this line for profiling if skip is TRUE.
1855 if (do_profiling == PROF_YES
1856 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1857 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1858 {
1859 int skip = did_emsg || got_int || did_throw;
1860
1861 if (ea.cmdidx == CMD_catch)
1862 skip = !skip && !(cstack->cs_idx >= 0
1863 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1864 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1865 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1866 skip = skip || !(cstack->cs_idx >= 0
1867 && !(cstack->cs_flags[cstack->cs_idx]
1868 & (CSF_ACTIVE | CSF_TRUE)));
1869 else if (ea.cmdidx == CMD_finally)
1870 skip = FALSE;
1871 else if (ea.cmdidx != CMD_endif
1872 && ea.cmdidx != CMD_endfor
1873 && ea.cmdidx != CMD_endtry
1874 && ea.cmdidx != CMD_endwhile)
1875 skip = ea.skip;
1876
1877 if (!skip)
1878 {
1879 if (getline_equal(fgetline, cookie, get_func_line))
1880 func_line_exec(getline_cookie(fgetline, cookie));
1881 else if (getline_equal(fgetline, cookie, getsourceline))
1882 script_line_exec();
1883 }
1884 }
1885# endif
1886
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001887 // May go to debug mode. If this happens and the ">quit" debug command is
1888 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001889 dbg_check_breakpoint(&ea);
1890 if (!ea.skip && got_int)
1891 {
1892 ea.skip = TRUE;
1893 (void)do_intthrow(cstack);
1894 }
1895#endif
1896
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001897/*
1898 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 *
1900 * where 'addr' is:
1901 *
1902 * % (entire file)
1903 * $ [+-NUM]
1904 * 'x [+-NUM] (where x denotes a currently defined mark)
1905 * . [+-NUM]
1906 * [+-NUM]..
1907 * NUM
1908 *
1909 * The ea.cmd pointer is updated to point to the first character following the
1910 * range spec. If an initial address is found, but no second, the upper bound
1911 * is equal to the lower.
1912 */
1913
Bram Moolenaar25190db2019-05-04 15:05:28 +02001914 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001915 if (!IS_USER_CMDIDX(ea.cmdidx))
1916 {
1917 if (ea.cmdidx != CMD_SIZE)
1918 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1919 else
1920 ea.addr_type = ADDR_LINES;
1921
Bram Moolenaar25190db2019-05-04 15:05:28 +02001922 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001923 if (ea.cmdidx == CMD_wincmd && p != NULL)
1924 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001925#ifdef FEAT_QUICKFIX
1926 // :.cc in quickfix window uses line number
1927 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1928 ea.addr_type = ADDR_OTHER;
1929#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001930 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001931
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001932 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001933#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001934 if (!may_have_range)
1935 ea.line1 = ea.line2 = default_address(&ea);
1936 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001937#endif
1938 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1939 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001942 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 */
1944
1945 /*
1946 * Skip ':' and any white space
1947 */
1948 ea.cmd = skipwhite(ea.cmd);
1949 while (*ea.cmd == ':')
1950 ea.cmd = skipwhite(ea.cmd + 1);
1951
1952 /*
1953 * If we got a line, but no command, then go to the line.
1954 * If we find a '|' or '\n' we set ea.nextcmd.
1955 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001956 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1957 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 /*
1960 * strange vi behaviour:
1961 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001962 * ":3|..." prints line 3 (not in Vim9 script)
1963 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001965 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001967 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 goto doend;
1969 }
1970
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001971 // If this looks like an undefined user command and there are CmdUndefined
1972 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001973 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1974 && ASCII_ISUPPER(*ea.cmd)
1975 && has_cmdundefined())
1976 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001977 int ret;
1978
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001979 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001980 while (ASCII_ISALNUM(*p))
1981 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001982 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001983 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1984 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001985 // If the autocommands did something and didn't cause an error, try
1986 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001987 p = (ret
1988#ifdef FEAT_EVAL
1989 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001990#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001992 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (p == NULL)
1995 {
1996 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001997 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 goto doend;
1999 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002000 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002001 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2002 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 {
2004 errormsg = uc_fun_cmd();
2005 goto doend;
2006 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002007
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (ea.cmdidx == CMD_SIZE)
2009 {
2010 if (!ea.skip)
2011 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002012 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002014 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002015 // If the modifier was parsed OK the error must be in the
2016 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002017 if (after_modifier != NULL)
2018 append_command(after_modifier);
2019 else
2020 append_command(*cmdlinep);
2021 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002022 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002023 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025 goto doend;
2026 }
2027
Bram Moolenaar958636c2014-10-21 20:01:58 +02002028 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2029 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002030#ifdef HAVE_EX_SCRIPT_NI
2031 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2032#endif
2033 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
2035#ifndef FEAT_EVAL
2036 /*
2037 * When the expression evaluation is disabled, recognize the ":if" and
2038 * ":endif" commands and ignore everything in between it.
2039 */
2040 if (ea.cmdidx == CMD_if)
2041 ++if_level;
2042 if (if_level)
2043 {
2044 if (ea.cmdidx == CMD_endif)
2045 --if_level;
2046 goto doend;
2047 }
2048
2049#endif
2050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002051 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002052 if (*p == '!' && ea.cmdidx != CMD_substitute
2053 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
2055 ++p;
2056 ea.forceit = TRUE;
2057 }
2058 else
2059 ea.forceit = FALSE;
2060
2061/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002062 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002064 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002065 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
2067 if (!ea.skip)
2068 {
2069#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002070 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002072 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002073 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 goto doend;
2075 }
2076#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002077 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002078 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002079 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002080 goto doend;
2081 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002082 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002084 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002085 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 goto doend;
2087 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002088
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002089 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002091#ifdef FEAT_CMDWIN
2092 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2093 {
2094 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002095 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002096 goto doend;
2097 }
2098#endif
2099 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2100 {
2101 // Command not allowed when text is locked
2102 errormsg = _(get_text_locked_msg());
2103 goto doend;
2104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002106
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002107 // Disallow editing another buffer when "curbuf_lock" is set.
2108 // Do allow ":checktime" (it is postponed).
2109 // Do allow ":edit" (check for an argument later).
2110 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002111 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002112 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002113 && ea.cmdidx != CMD_edit
2114 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002115 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002116 && curbuf_locked())
2117 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002119 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002121 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 goto doend;
2123 }
2124 }
2125
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002126 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002128 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 goto doend;
2130 }
2131
2132 /*
2133 * Don't complain about the range if it is not used
2134 * (could happen if line_count is accidentally set to 0).
2135 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002136 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
2138 /*
2139 * If the range is backwards, ask for confirmation and, if given, swap
2140 * ea.line1 & ea.line2 so it's forwards again.
2141 * When global command is busy, don't ask, will fail below.
2142 */
2143 if (!global_busy && ea.line1 > ea.line2)
2144 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002145 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002147 if (sourcing || exmode_active)
2148 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002149 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002150 goto doend;
2151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (ask_yesno((char_u *)
2153 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002154 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 lnum = ea.line1;
2157 ea.line1 = ea.line2;
2158 ea.line2 = lnum;
2159 }
2160 if ((errormsg = invalid_range(&ea)) != NULL)
2161 goto doend;
2162 }
2163
Bram Moolenaarb7316892019-05-01 18:08:42 +02002164 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2165 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 ea.line2 = 1;
2167
2168 correct_range(&ea);
2169
2170#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002171 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002172 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002174 // Put the first line at the start of a closed fold, put the last line
2175 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 (void)hasFolding(ea.line1, &ea.line1, NULL);
2177 (void)hasFolding(ea.line2, NULL, &ea.line2);
2178 }
2179#endif
2180
2181#ifdef FEAT_QUICKFIX
2182 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002183 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 * option here, so things like % get expanded.
2185 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002186 p = replace_makeprg(&ea, p, cmdlinep);
2187 if (p == NULL)
2188 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189#endif
2190
2191 /*
2192 * Skip to start of argument.
2193 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2194 */
2195 if (ea.cmdidx == CMD_bang)
2196 ea.arg = p;
2197 else
2198 ea.arg = skipwhite(p);
2199
Bram Moolenaar379fb762018-08-30 15:58:28 +02002200 // ":file" cannot be run with an argument when "curbuf_lock" is set
2201 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2202 goto doend;
2203
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 /*
2205 * Check for "++opt=val" argument.
2206 * Must be first, allow ":w ++enc=utf8 !cmd"
2207 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002208 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2210 if (getargopt(&ea) == FAIL && !ni)
2211 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002212 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 goto doend;
2214 }
2215
2216 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2217 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002218 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002220 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002222 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 goto doend;
2224 }
2225 ea.arg = skipwhite(ea.arg + 1);
2226 ea.append = TRUE;
2227 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002228 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
2230 ++ea.arg;
2231 ea.usefilter = TRUE;
2232 }
2233 }
2234
2235 if (ea.cmdidx == CMD_read)
2236 {
2237 if (ea.forceit)
2238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002239 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 ea.forceit = FALSE;
2241 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002242 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 ++ea.arg;
2245 ea.usefilter = TRUE;
2246 }
2247 }
2248
2249 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2250 {
2251 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002252 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
2254 ++ea.arg;
2255 ++ea.amount;
2256 }
2257 ea.arg = skipwhite(ea.arg);
2258 }
2259
2260 /*
2261 * Check for "+command" argument, before checking for next command.
2262 * Don't do this for ":read !cmd" and ":write !cmd".
2263 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002264 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2266
2267 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002268 * For commands that do not use '|' inside their argument: Check for '|' to
2269 * separate commands and '"' or '#' to start comments.
2270 *
2271 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002272 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002273 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002274 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002276 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2277 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002278 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002279 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002280 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002281 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002282 || ea.cmdidx == CMD_global
2283 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002284 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002285#ifdef FEAT_EVAL
2286 || inside_block(&ea)
2287#endif
2288 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 for (p = ea.arg; *p; ++p)
2291 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 // Remove one backslash before a newline, so that it's possible to
2293 // pass a newline to the shell and also a newline that is preceded
2294 // with a backslash. This makes it impossible to end a shell
2295 // command in a backslash, but that doesn't appear useful.
2296 // Halving the number of backslashes is incompatible with previous
2297 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002299 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002300 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 ea.nextcmd = p + 1;
2303 *p = NUL;
2304 break;
2305 }
2306 }
2307 }
2308
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002309 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002310 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002312 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002313 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002315 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002316 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002317 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002319#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002320 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002321 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002323 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002324 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
2326#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002327 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2328 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002329 {
2330 ea.regname = *ea.arg++;
2331#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002332 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002333 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2334 {
kuuote08d7b1c2021-10-04 22:17:36 +01002335 if (!ea.skip)
2336 {
2337 set_expr_line(vim_strsave(ea.arg), &ea);
2338 did_set_expr_line = TRUE;
2339 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002340 ea.arg += STRLEN(ea.arg);
2341 }
2342#endif
2343 ea.arg = skipwhite(ea.arg);
2344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346
2347 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002348 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 * count, it's a buffer name.
2350 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002351 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002352 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002353 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002355 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002357 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002359 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 goto doend;
2361 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002362 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
2364 ea.line2 = n;
2365 if (ea.addr_count == 0)
2366 ea.addr_count = 1;
2367 }
2368 else
2369 {
2370 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002371 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002372 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002373 else
2374 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 ++ea.addr_count;
2376 /*
2377 * Be vi compatible: no error message for out of range.
2378 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002379 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 ea.line2 = curbuf->b_ml.ml_line_count;
2381 }
2382 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002383
2384 /*
2385 * Check for flags: 'l', 'p' and '#'.
2386 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002387 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002388 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002389 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2390 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002392 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002393 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 goto doend;
2395 }
2396
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002397 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002399 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 goto doend;
2401 }
2402
2403#ifdef FEAT_EVAL
2404 /*
2405 * Skip the command when it's not going to be executed.
2406 * The commands like :if, :endif, etc. always need to be executed.
2407 * Also make an exception for commands that handle a trailing command
2408 * themselves.
2409 */
2410 if (ea.skip)
2411 {
2412 switch (ea.cmdidx)
2413 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002414 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 case CMD_while:
2416 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002417 case CMD_for:
2418 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_if:
2420 case CMD_elseif:
2421 case CMD_else:
2422 case CMD_endif:
2423 case CMD_try:
2424 case CMD_catch:
2425 case CMD_finally:
2426 case CMD_endtry:
2427 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002428 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 break;
2430
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002431 // Commands that handle '|' themselves. Check: A command should
2432 // either have the EX_TRLBAR flag, appear in this list or appear in
2433 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 case CMD_aboveleft:
2435 case CMD_and:
2436 case CMD_belowright:
2437 case CMD_botright:
2438 case CMD_browse:
2439 case CMD_call:
2440 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002441 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 case CMD_delfunction:
2443 case CMD_djump:
2444 case CMD_dlist:
2445 case CMD_dsearch:
2446 case CMD_dsplit:
2447 case CMD_echo:
2448 case CMD_echoerr:
2449 case CMD_echomsg:
2450 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002451 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002453 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002454 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 case CMD_help:
2456 case CMD_hide:
2457 case CMD_ijump:
2458 case CMD_ilist:
2459 case CMD_isearch:
2460 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002461 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_keepjumps:
2463 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002464 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_leftabove:
2466 case CMD_let:
2467 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002468 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002469 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002471 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002472 case CMD_noautocmd:
2473 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 case CMD_perl:
2475 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002476 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002477 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002478 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_return:
2480 case CMD_rightbelow:
2481 case CMD_ruby:
2482 case CMD_silent:
2483 case CMD_smagic:
2484 case CMD_snomagic:
2485 case CMD_substitute:
2486 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002487 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 case CMD_tcl:
2489 case CMD_throw:
2490 case CMD_tilde:
2491 case CMD_topleft:
2492 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002493 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002494 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 case CMD_verbose:
2496 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002497 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 break;
2499
2500 default: goto doend;
2501 }
2502 }
2503#endif
2504
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002505 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2508 goto doend;
2509 }
2510
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 /*
2512 * Accept buffer name. Cannot be used at the same time with a buffer
2513 * number. Don't do this for a user command.
2514 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002515 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002516 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
2518 /*
2519 * :bdelete, :bwipeout and :bunload take several arguments, separated
2520 * by spaces: find next space (skipping over escaped characters).
2521 * The others take one argument: ignore trailing spaces.
2522 */
2523 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2524 || ea.cmdidx == CMD_bunload)
2525 p = skiptowhite_esc(ea.arg);
2526 else
2527 {
2528 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002529 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 --p;
2531 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002532 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002533 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002534 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 goto doend;
2536 ea.addr_count = 1;
2537 ea.arg = skipwhite(p);
2538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002540 // The :try command saves the emsg_silent flag, reset it here when
2541 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002542 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002543 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002544 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002545 if (emsg_silent < 0)
2546 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002547 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002548 }
2549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002551 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaar958636c2014-10-21 20:01:58 +02002554 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
2556 /*
2557 * Execute a user-defined command.
2558 */
2559 do_ucmd(&ea);
2560 }
2561 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
2563 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002564 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 */
2566 ea.errmsg = NULL;
2567 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2568 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002569 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571
2572#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002574 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002575 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002576 && current_sctx.sc_sid > 0
2577 && ea.cmdidx != CMD_endif
2578 && (cstack->cs_idx < 0
2579 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002580 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 /*
2583 * If the command just executed called do_cmdline(), any throw or ":return"
2584 * or ":finish" encountered there must also check the cstack of the still
2585 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2586 * exception, or reanimate a returned function or finished script file and
2587 * return or finish it again.
2588 */
2589 if (need_rethrow)
2590 do_throw(cstack);
2591 else if (check_cstack)
2592 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002593 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002595 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 && current_func_returned())
2597 do_return(&ea, TRUE, FALSE, NULL);
2598 }
2599 need_rethrow = check_cstack = FALSE;
2600#endif
2601
2602doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002603 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002606 curwin->w_cursor.col = 0;
2607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
2609 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2610 {
Bram Moolenaarbed34f02022-01-19 20:48:37 +00002611 if (sourcing || !KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002613 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002618 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620 emsg(errormsg);
2621 }
2622#ifdef FEAT_EVAL
2623 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002624 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2625 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002626
2627 if (did_set_expr_line)
2628 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629#endif
2630
Bram Moolenaare1004402020-10-24 20:49:43 +02002631 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002633 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002634 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002636 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 ea.nextcmd = NULL;
2638
2639#ifdef FEAT_EVAL
2640 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002641 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642#endif
2643
2644 return ea.nextcmd;
2645}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002647static char ex_error_buf[MSG_BUF_LEN];
2648
2649/*
2650 * Return an error message with argument included.
2651 * Uses a static buffer, only the last error will be kept.
2652 * "msg" will be translated, caller should use N_().
2653 */
2654 char *
2655ex_errmsg(char *msg, char_u *arg)
2656{
2657 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2658 return ex_error_buf;
2659}
2660
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002662 * Handle a range without a command.
2663 * Returns an error message on failure.
2664 */
2665 char *
2666ex_range_without_command(exarg_T *eap)
2667{
2668 char *errormsg = NULL;
2669
2670 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2671#ifdef FEAT_EVAL
2672 && !in_vim9script()
2673#endif
2674 )
2675 {
2676 eap->cmdidx = CMD_print;
2677 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2678 if ((errormsg = invalid_range(eap)) == NULL)
2679 {
2680 correct_range(eap);
2681 ex_print(eap);
2682 }
2683 }
2684 else if (eap->addr_count != 0)
2685 {
2686 if (eap->line2 > curbuf->b_ml.ml_line_count)
2687 {
2688 // With '-' in 'cpoptions' a line number past the file is an
2689 // error, otherwise put it at the end of the file.
2690 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2691 eap->line2 = -1;
2692 else
2693 eap->line2 = curbuf->b_ml.ml_line_count;
2694 }
2695
2696 if (eap->line2 < 0)
2697 errormsg = _(e_invalid_range);
2698 else
2699 {
2700 if (eap->line2 == 0)
2701 curwin->w_cursor.lnum = 1;
2702 else
2703 curwin->w_cursor.lnum = eap->line2;
2704 beginline(BL_SOL | BL_FIX);
2705 }
2706 }
2707 return errormsg;
2708}
2709
2710/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002711 * Check for an Ex command with optional tail.
2712 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002713 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002714 */
2715 static int
2716checkforcmd_opt(
2717 char_u **pp, // start of command
2718 char *cmd, // name of command
2719 int len, // required length
2720 int noparen)
2721{
2722 int i;
2723
2724 for (i = 0; cmd[i] != NUL; ++i)
2725 if (((char_u *)cmd)[i] != (*pp)[i])
2726 break;
Bram Moolenaar68854a82022-01-31 18:59:13 +00002727 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_'
2728 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002729 {
2730 *pp = skipwhite(*pp + i);
2731 return TRUE;
2732 }
2733 return FALSE;
2734}
2735
2736/*
2737 * Check for an Ex command with optional tail.
2738 * If there is a match advance "pp" to the argument and return TRUE.
2739 */
2740 int
2741checkforcmd(
2742 char_u **pp, // start of command
2743 char *cmd, // name of command
2744 int len) // required length
2745{
2746 return checkforcmd_opt(pp, cmd, len, FALSE);
2747}
2748
2749/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002750 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002751 * If there is a match advance "pp" to the argument and return TRUE.
2752 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002753 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002754checkforcmd_noparen(
2755 char_u **pp, // start of command
2756 char *cmd, // name of command
2757 int len) // required length
2758{
2759 return checkforcmd_opt(pp, cmd, len, TRUE);
2760}
2761
2762/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 * Parse and skip over command modifiers:
2764 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002765 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002766 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002767 * When "skip_only" is TRUE the global variables are not changed, except for
2768 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002769 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2770 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002771 * Call apply_cmdmod() to get the side effects of the modifiers:
2772 * - Increment "sandbox" for ":sandbox"
2773 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002774 * - set msg_silent for ":silent"
2775 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002776 * Return FAIL when the command is not to be executed.
2777 * May set "errormsg" to an error message.
2778 */
2779 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002780parse_command_modifiers(
2781 exarg_T *eap,
2782 char **errormsg,
2783 cmdmod_T *cmod,
2784 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002785{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002786 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002787 char_u *cmd_start = NULL;
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002788 int did_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002789 char_u *p;
2790 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002791 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002792 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002793
Bram Moolenaare1004402020-10-24 20:49:43 +02002794 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002795 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002796
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002797 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2798 {
2799 // The automatically inserted Visual area range is skipped, so that
2800 // typing ":cmdmod cmd" in Visual mode works without having to move the
2801 // range to after the modififiers.
2802 eap->cmd += 5;
2803 cmd_start = eap->cmd;
2804 has_visual_range = TRUE;
2805 }
2806
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002807 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002808 for (;;)
2809 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002811 {
2812 if (*eap->cmd == ':')
2813 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002814 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002815 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002816
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002817 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002818 if (*eap->cmd == NUL && exmode_active
2819 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2820 || getline_equal(eap->getline, eap->cookie, getexline))
2821 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2822 {
2823 eap->cmd = (char_u *)"+";
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002824 did_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002825 if (!skip_only)
2826 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002827 }
2828
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002829 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002830 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002831 {
2832 // a comment ends at a NL
2833 if (eap->nextcmd == NULL)
2834 {
2835 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2836 if (eap->nextcmd != NULL)
2837 ++eap->nextcmd;
2838 }
Bram Moolenaarbc510062022-02-14 21:19:04 +00002839 if (vim9script && has_cmdmod(cmod, FALSE))
2840 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002842 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002843 if (*eap->cmd == NUL)
2844 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002845 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002846 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002847 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002848 if (vim9script && has_cmdmod(cmod, FALSE))
2849 *errormsg = _(e_command_modifier_without_command);
2850 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002851 return FAIL;
2852 }
2853
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002854 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002855
2856 // In Vim9 script a variable can shadow a command modifier:
2857 // verbose = 123
2858 // verbose += 123
2859 // silent! verbose = func()
2860 // verbose.member = 2
2861 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002862 // But not:
2863 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002864 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002865 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002866 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002867
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002868 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002869 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002870 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002871 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2872 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002873 break;
2874 }
2875
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 switch (*p)
2877 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002878 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002879 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002881 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 continue;
2883
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002884 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002886 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002887 continue;
2888 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002889 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002890 {
2891#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002892 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002893#endif
2894 continue;
2895 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002896 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002898 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002899 continue;
2900
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002901 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 break;
2903#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002904 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002905#endif
2906 continue;
2907
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002908 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002910 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002911 continue;
2912 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002913 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002915 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002916 continue;
2917 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002918 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002919 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002920 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 continue;
2922 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002923 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002924 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002925 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002926 continue;
2927
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002928 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002929 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002930 char_u *reg_pat;
2931 char_u *nulp = NULL;
2932 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002933
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002934 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002935 || *p == NUL
2936 || (ends_excmd(*p)
2937#ifdef FEAT_EVAL
2938 // in ":filter #pat# cmd" # does not
2939 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002940 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002941#endif
2942 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002943 break;
2944 if (*p == '!')
2945 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002946 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002947 p = skipwhite(p + 1);
2948 if (*p == NUL || ends_excmd(*p))
2949 break;
2950 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002951#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002952 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002953 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002954 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002955#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002956 if (skip_only)
2957 p = skip_vimgrep_pat(p, NULL, NULL);
2958 else
2959 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002960 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2961 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002962 if (p == NULL || *p == NUL)
2963 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002964 if (!skip_only)
2965 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002966 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002967 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002968 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002969 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002970 // restore the character overwritten by NUL
2971 if (nulp != NULL)
2972 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002973 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002974 eap->cmd = p;
2975 continue;
2976 }
2977
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002978 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002979 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002980 || *p == NUL || ends_excmd(*p))
2981 break;
2982 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002983 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002984 continue;
2985
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002986 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 continue;
2990 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002991 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2992 {
2993 if (ends_excmd2(p, eap->cmd))
2994 {
2995 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02002996 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002997 return FAIL;
2998 }
2999 cmod->cmod_flags |= CMOD_LEGACY;
3000 continue;
3001 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003002
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003003 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003005 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003006 continue;
3007
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003008 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003010 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003011 continue;
3012 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003013 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003014 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003015 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003016 continue;
3017
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003018 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003019 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003020 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 continue;
3022
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003023 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003024 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003025 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003026 continue;
3027 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003028 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003029 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003030 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003031 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003033 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003034 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003035 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003036 }
3037 continue;
3038
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003039 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003040 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003041 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003042 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003043 long tabnr = get_address(eap, &eap->cmd,
3044 ADDR_TABS, eap->skip,
3045 skip_only, FALSE, 1);
3046 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003047 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003048 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003049 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003050 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3051 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003052 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003053 return FAIL;
3054 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003055 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003056 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003057 }
3058 eap->cmd = p;
3059 continue;
3060 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003061 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003062 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003063 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003064 continue;
3065
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003066 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003067 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003068 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003069 continue;
3070
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003071 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003072 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003073 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003074 continue;
3075 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003076 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003077 {
3078 if (ends_excmd2(p, eap->cmd))
3079 {
3080 *errormsg =
3081 _(e_vim9cmd_must_be_followed_by_command);
3082 return FAIL;
3083 }
3084 cmod->cmod_flags |= CMOD_VIM9CMD;
3085 continue;
3086 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003087 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003088 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003089 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003090 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003091 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar60895f32022-04-12 14:23:19 +01003092 if (cmod->cmod_verbose == 0)
3093 cmod->cmod_verbose = -1;
3094 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003095 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003096 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003097 eap->cmd = p;
3098 continue;
3099 }
3100 break;
3101 }
3102
Bram Moolenaar1501b632022-03-27 16:56:21 +01003103 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003104 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003105 if (eap->cmd > cmd_start)
3106 {
3107 // Move the '<,'> range to after the modifiers and insert a colon.
3108 // Since the modifiers have been parsed put the colon on top of the
3109 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3110 // Put eap->cmd after the colon.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003111 if (did_plus_cmd)
3112 {
3113 size_t len = STRLEN(cmd_start);
3114
3115 // Special case: empty command may have been changed to "+":
3116 // "'<,'>mod" -> "mod'<,'>+
3117 mch_memmove(orig_cmd, cmd_start, len);
3118 STRCPY(orig_cmd + len, "'<,'>+");
3119 }
3120 else
3121 {
3122 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3123 eap->cmd -= 5;
3124 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3125 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003126 }
3127 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003128 // No modifiers, move the pointer back.
3129 // Special case: empty command may have been changed to "+".
3130 if (did_plus_cmd)
3131 eap->cmd = (char_u *)"'<,'>+";
3132 else
3133 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003134 }
3135
Bram Moolenaareffed932018-08-14 13:38:17 +02003136 return OK;
3137}
3138
3139/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003140 * Return TRUE if "cmod" has anything set.
3141 */
3142 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003143has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003144{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003145 return (cmod->cmod_flags != 0 && (!ignore_silent
3146 || (cmod->cmod_flags
3147 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003148 || cmod->cmod_split != 0
3149 || cmod->cmod_verbose != 0
3150 || cmod->cmod_tab != 0
3151 || cmod->cmod_filter_regmatch.regprog != NULL;
3152}
3153
Bram Moolenaar8991be22022-02-14 21:51:46 +00003154#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003155/*
3156 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3157 */
3158 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003159cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003160{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003161 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003162 {
3163 emsg(_(e_misplaced_command_modifier));
3164 return TRUE;
3165 }
3166 return FALSE;
3167}
Dominique Pelle748b3082022-01-08 12:41:16 +00003168#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003169
3170/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003171 * Apply the command modifiers. Saves current state in "cmdmod", call
3172 * undo_cmdmod() later.
3173 */
3174 void
3175apply_cmdmod(cmdmod_T *cmod)
3176{
3177#ifdef HAVE_SANDBOX
3178 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3179 {
3180 ++sandbox;
3181 cmod->cmod_did_sandbox = TRUE;
3182 }
3183#endif
Bram Moolenaar60895f32022-04-12 14:23:19 +01003184 if (cmod->cmod_verbose != 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003185 {
3186 if (cmod->cmod_verbose_save == 0)
3187 cmod->cmod_verbose_save = p_verbose + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003188 p_verbose = cmod->cmod_verbose < 0 ? 0 : cmod->cmod_verbose;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003189 }
3190
3191 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3192 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003193 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003194 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003195 cmod->cmod_save_msg_scroll = msg_scroll;
3196 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003197 if (cmod->cmod_flags & CMOD_SILENT)
3198 ++msg_silent;
3199 if (cmod->cmod_flags & CMOD_UNSILENT)
3200 msg_silent = 0;
3201
3202 if (cmod->cmod_flags & CMOD_ERRSILENT)
3203 {
3204 ++emsg_silent;
3205 ++cmod->cmod_did_esilent;
3206 }
3207
Bram Moolenaare1004402020-10-24 20:49:43 +02003208 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003209 {
3210 // Set 'eventignore' to "all".
3211 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003212 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003213 set_string_option_direct((char_u *)"ei", -1,
3214 (char_u *)"all", OPT_FREE, SID_NONE);
3215 }
3216}
3217
3218/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003219 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003220 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003221 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003222undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003223{
Bram Moolenaare1004402020-10-24 20:49:43 +02003224 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003225 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003226 p_verbose = cmod->cmod_verbose_save - 1;
3227 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003228 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003229
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003230#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003231 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003232 {
3233 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003234 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003235 }
3236#endif
3237
Bram Moolenaare1004402020-10-24 20:49:43 +02003238 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003240 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003241 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3242 OPT_FREE, SID_NONE);
3243 free_string_option(cmod->cmod_save_ei);
3244 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003245 }
3246
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003247 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003248
Bram Moolenaare1004402020-10-24 20:49:43 +02003249 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003250 {
3251 // messages could be enabled for a serious error, need to check if the
3252 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003253 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3254 msg_silent = cmod->cmod_save_msg_silent - 1;
3255 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003256 if (emsg_silent < 0)
3257 emsg_silent = 0;
3258 // Restore msg_scroll, it's set by file I/O commands, even when no
3259 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003260 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003261
3262 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3263 // somewhere in the line. Put it back in the first column.
3264 if (redirecting())
3265 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003266
Bram Moolenaare1004402020-10-24 20:49:43 +02003267 cmod->cmod_save_msg_silent = 0;
3268 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003269 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003270}
3271
3272/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003273 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003274 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003275 * Return FAIL and set "errormsg" or return OK.
3276 */
3277 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003278parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003279{
3280 int address_count = 1;
3281 linenr_T lnum;
3282
3283 // Repeat for all ',' or ';' separated addresses.
3284 for (;;)
3285 {
3286 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003287 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003288 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003289 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003290 eap->addr_count == 0, address_count++);
3291 if (eap->cmd == NULL) // error detected
3292 return FAIL;
3293 if (lnum == MAXLNUM)
3294 {
3295 if (*eap->cmd == '%') // '%' - all lines
3296 {
3297 ++eap->cmd;
3298 switch (eap->addr_type)
3299 {
3300 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003301 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003302 eap->line1 = 1;
3303 eap->line2 = curbuf->b_ml.ml_line_count;
3304 break;
3305 case ADDR_LOADED_BUFFERS:
3306 {
3307 buf_T *buf = firstbuf;
3308
3309 while (buf->b_next != NULL
3310 && buf->b_ml.ml_mfp == NULL)
3311 buf = buf->b_next;
3312 eap->line1 = buf->b_fnum;
3313 buf = lastbuf;
3314 while (buf->b_prev != NULL
3315 && buf->b_ml.ml_mfp == NULL)
3316 buf = buf->b_prev;
3317 eap->line2 = buf->b_fnum;
3318 break;
3319 }
3320 case ADDR_BUFFERS:
3321 eap->line1 = firstbuf->b_fnum;
3322 eap->line2 = lastbuf->b_fnum;
3323 break;
3324 case ADDR_WINDOWS:
3325 case ADDR_TABS:
3326 if (IS_USER_CMDIDX(eap->cmdidx))
3327 {
3328 eap->line1 = 1;
3329 eap->line2 = eap->addr_type == ADDR_WINDOWS
3330 ? LAST_WIN_NR : LAST_TAB_NR;
3331 }
3332 else
3333 {
3334 // there is no Vim command which uses '%' and
3335 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003336 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003337 return FAIL;
3338 }
3339 break;
3340 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003341 case ADDR_UNSIGNED:
3342 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003343 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003344 return FAIL;
3345 case ADDR_ARGUMENTS:
3346 if (ARGCOUNT == 0)
3347 eap->line1 = eap->line2 = 0;
3348 else
3349 {
3350 eap->line1 = 1;
3351 eap->line2 = ARGCOUNT;
3352 }
3353 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003354 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003355#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003356 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003357 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003358 if (eap->line2 == 0)
3359 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003360#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003361 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003362 case ADDR_NONE:
3363 // Will give an error later if a range is found.
3364 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003365 }
3366 ++eap->addr_count;
3367 }
3368 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3369 {
3370 pos_T *fp;
3371
3372 // '*' - visual area
3373 if (eap->addr_type != ADDR_LINES)
3374 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003375 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003376 return FAIL;
3377 }
3378
3379 ++eap->cmd;
3380 if (!eap->skip)
3381 {
3382 fp = getmark('<', FALSE);
3383 if (check_mark(fp) == FAIL)
3384 return FAIL;
3385 eap->line1 = fp->lnum;
3386 fp = getmark('>', FALSE);
3387 if (check_mark(fp) == FAIL)
3388 return FAIL;
3389 eap->line2 = fp->lnum;
3390 ++eap->addr_count;
3391 }
3392 }
3393 }
3394 else
3395 eap->line2 = lnum;
3396 eap->addr_count++;
3397
3398 if (*eap->cmd == ';')
3399 {
3400 if (!eap->skip)
3401 {
3402 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003403 // Don't leave the cursor on an illegal line or column, but do
3404 // accept zero as address, so 0;/PATTERN/ works correctly.
3405 if (eap->line2 > 0)
3406 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003407 }
3408 }
3409 else if (*eap->cmd != ',')
3410 break;
3411 ++eap->cmd;
3412 }
3413
3414 // One address given: set start and end lines.
3415 if (eap->addr_count == 1)
3416 {
3417 eap->line1 = eap->line2;
3418 // ... but only implicit: really no address given
3419 if (lnum == MAXLNUM)
3420 eap->addr_count = 0;
3421 }
3422 return OK;
3423}
3424
3425/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003426 * Append "cmd" to the error message in IObuff.
3427 * Takes care of limiting the length and handling 0xa0, which would be
3428 * invisible otherwise.
3429 */
3430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003431append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003432{
3433 char_u *s = cmd;
3434 char_u *d;
3435
3436 STRCAT(IObuff, ": ");
3437 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003438 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003439 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003440 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003441 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003442 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003443 STRCPY(d, "<a0>");
3444 d += 4;
3445 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003446 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3447 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003448 else
3449 MB_COPY_CHAR(s, d);
3450 }
3451 *d = NUL;
3452}
3453
Dominique Pelle748b3082022-01-08 12:41:16 +00003454#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003455/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003456 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3457 * the name. Otherwise just return "start".
3458 */
3459 char_u *
3460skip_option_env_lead(char_u *start)
3461{
3462 char_u *name = start;
3463
3464 if (*start == '&')
3465 {
3466 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3467 name += 3;
3468 else
3469 name += 1;
3470 }
3471 else if (*start == '$')
3472 name += 1;
3473 return name;
3474}
Dominique Pelle748b3082022-01-08 12:41:16 +00003475#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003476
3477/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003478 * Return TRUE and set "*idx" if "p" points to a one letter command.
3479 * If not in Vim9 script:
3480 * - The 'k' command can directly be followed by any character.
3481 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3482 * but :sre[wind] is another command, as are :scr[iptnames],
3483 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3484 */
3485 static int
3486one_letter_cmd(char_u *p, cmdidx_T *idx)
3487{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003488 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003489 return FALSE;
3490 if (*p == 'k')
3491 {
3492 *idx = CMD_k;
3493 return TRUE;
3494 }
3495 if (p[0] == 's'
3496 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3497 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3498 || p[1] == 'g'
3499 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3500 || p[1] == 'I'
3501 || (p[1] == 'r' && p[2] != 'e')))
3502 {
3503 *idx = CMD_substitute;
3504 return TRUE;
3505 }
3506 return FALSE;
3507}
3508
3509/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003510 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3511 * call.
3512 */
3513 int
3514number_method(char_u *cmd)
3515{
3516 char_u *p = skipdigits(cmd);
3517
3518 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3519}
3520
3521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003523 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003524 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003525 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 *
3527 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3528 * assignment without "let". Sets eap->cmdidx to the command while returning
3529 * "eap->cmd".
3530 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 * Returns NULL for an ambiguous user command.
3532 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 char_u *
3534find_ex_command(
3535 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003536 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003537 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003538 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539{
3540 int len;
3541 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003542 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003543#ifndef FEAT_EVAL
3544 int vim9 = FALSE;
3545#else
3546 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 /*
3549 * Recognize a Vim9 script function/method call and assignment:
3550 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3551 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003552 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003553 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003555 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003557 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003558 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3559 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003561 int oplen;
3562 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003563 char_u *swp;
3564
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003565 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003566 || (eap->cmd[0] == '$'
3567 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003568 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003569 && (valid_yank_reg(eap->cmd[1], FALSE)
3570 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003571 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003572 if (*eap->cmd == '&')
3573 {
3574 p = eap->cmd + 1;
3575 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3576 p += 2;
3577 p = to_name_end(p, FALSE);
3578 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003579 else if (*eap->cmd == '$')
3580 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003581 else
3582 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003583 if (ends_excmd(*skipwhite(p)))
3584 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003585 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3586 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003587 eap->cmdidx = CMD_eval;
3588 return eap->cmd;
3589 }
3590 // "&option" can be followed by "->" or "=", check below
3591 }
3592
3593 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003595 if (
3596 // "(..." is an expression.
3597 // "funcname(" is always a function call.
3598 *p == '('
3599 || (p == eap->cmd
3600 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003601 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003602 *eap->cmd == '{'
3603 // "'string'->func()" is an expression.
3604 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003605 // '"string"->func()' is an expression.
3606 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003607 // '$"string"->func()' is an expression.
3608 // "$'string'->func()" is an expression.
3609 || (eap->cmd[0] == '$'
3610 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3611 // '0z1234->func()' is an expression.
3612 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003613 // "g:varname" is an expression.
3614 || eap->cmd[1] == ':'
3615 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003616 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003617 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003618 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003619 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3620 {
3621 // "{" by itself is the start of a block.
3622 eap->cmdidx = CMD_block;
3623 return eap->cmd + 1;
3624 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003625 eap->cmdidx = CMD_eval;
3626 return eap->cmd;
3627 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003628
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003629 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003630 // "varname[]" is an expression.
3631 *p == '['
3632 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003633 || (*p == '.'
3634 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3635 // g:[key] is an expression
3636 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003637 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003638 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003639
3640 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003641 // Skip over the whole thing, it can be:
3642 // name.member = val
3643 // name[a : b] = val
3644 // name[idx] = val
3645 // name[idx].member = val
3646 // etc.
3647 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003648 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003649 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003650 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003651 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003652 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003653 || (after[0] == '.' && after[1] == '.'
3654 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003655 eap->cmdidx = CMD_var;
3656 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003657 --emsg_silent;
3658 return eap->cmd;
3659 }
3660
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003661 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3662 // an assignment.
3663 // If there is no line break inside the "[...]" then "p" is
3664 // advanced to after the "]" by to_name_const_end(): check if a "="
3665 // follows.
3666 // If "[...]" has a line break "p" still points at the "[" and it
3667 // can't be an assignment.
3668 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003669 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003670 char_u *eq;
3671
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003672 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003673 if (p == eap->cmd && *p == '[')
3674 {
3675 int count = 0;
3676 int semicolon = FALSE;
3677
3678 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3679 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003680 eq = p;
3681 if (eq != NULL)
3682 {
3683 eq = skipwhite(eq);
3684 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3685 ++eq;
3686 }
3687 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003688 {
3689 eap->cmdidx = CMD_eval;
3690 return eap->cmd;
3691 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003692 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003693 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003694 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003695 return eap->cmd;
3696 }
3697 }
3698
3699 // Recognize an assignment if we recognize the variable name:
3700 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003701 // "@r = expr"
3702 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003703 // "var = expr" where "var" is a variable name or we are skipping
3704 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003705 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003706 oplen = assignment_len(skipwhite(p), &heredoc);
3707 if (oplen > 0)
3708 {
3709 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3710 || *eap->cmd == '&'
3711 || *eap->cmd == '$'
3712 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003713 || (eap->skip && IS_WHITE_OR_NUL(
3714 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003715 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003716 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003717 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003718 return eap->cmd;
3719 }
3720 }
3721
3722 // Recognize using a type for a w:, b:, t: or g: variable:
3723 // "w:varname: number = 123".
3724 if (eap->cmd[1] == ':' && *p == ':')
3725 {
3726 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003727 return eap->cmd;
3728 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003729 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003730
Bram Moolenaar31d99482022-05-26 22:24:43 +01003731 // 1234->func() is a method call
3732 if (number_method(eap->cmd))
3733 {
3734 eap->cmdidx = CMD_eval;
3735 return eap->cmd;
3736 }
3737
Bram Moolenaar7b829262021-10-13 15:04:34 +01003738 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3739 // an expression. A global/substitute/list command needs to use a
3740 // longer name.
3741 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3742 {
3743 eap->cmdidx = CMD_eval;
3744 return eap->cmd;
3745 }
3746
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003747 // If it is an ID it might be a variable with an operator on the next
3748 // line, if the variable exists it can't be an Ex command.
3749 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003750 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003751 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3752 {
3753 eap->cmdidx = CMD_eval;
3754 return eap->cmd;
3755 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003756
3757 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003758 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3759 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003760 {
3761 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3762 return eap->cmd + 2;
3763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 }
3765#endif
3766
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 /*
3768 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 */
3770 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003771 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 ++p;
3774 }
3775 else
3776 {
3777 while (ASCII_ISALPHA(*p))
3778 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003779 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003780 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003782 while (ASCII_ISALNUM(*p))
3783 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 }
3785 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3786 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003787 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 ++p;
3789 while (ASCII_ISALPHA(*p))
3790 ++p;
3791 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003792
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003793 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003794 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 ++p;
3796 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003797 // The "d" command can directly be followed by 'l' or 'p' flag, when
3798 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003799 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003800 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003801 // Check for ":dl", ":dell", etc. to ":deletel": that's
3802 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003803 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003804 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003805 break;
3806 if (i == len - 1)
3807 {
3808 --len;
3809 if (p[-1] == 'l')
3810 eap->flags |= EXFLAG_LIST;
3811 else
3812 eap->flags |= EXFLAG_PRINT;
3813 }
3814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003816 if (ASCII_ISLOWER(eap->cmd[0]))
3817 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003818 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003819 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003820
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003821 if (command_count != (int)CMD_SIZE)
3822 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003823 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003824 getout(1);
3825 }
3826
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003827 // Use a precomputed index for fast look-up in cmdnames[]
3828 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003829 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3830 if (ASCII_ISLOWER(c2))
3831 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3832 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003833 else if (ASCII_ISUPPER(eap->cmd[0]))
3834 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003836 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
3838 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3839 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3840 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3841 (size_t)len) == 0)
3842 {
3843#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003844 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 *full = TRUE;
3846#endif
3847 break;
3848 }
3849
Bram Moolenaar204852a2022-03-05 12:56:44 +00003850 // :Print and :mode are not supported in Vim9 script.
3851 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003852 if (vim9 && eap->cmdidx != CMD_SIZE)
3853 {
3854 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3855 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003856 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003857 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003858 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003859 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003860 eap->cmdidx = CMD_SIZE;
3861 }
3862 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003863
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003864 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003865 // 'cpoptions'.
3866 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3867 p = eap->cmd;
3868
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003869 // Look for a user defined command as a last resort. Let ":Print" be
3870 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003871 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3872 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003874 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 while (ASCII_ISALNUM(*p))
3876 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003877 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003879 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 eap->cmdidx = CMD_SIZE;
3881 }
3882
Bram Moolenaar204852a2022-03-05 12:56:44 +00003883 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3884 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003885 eap->cmdidx = CMD_finally;
3886
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003887#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003888 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003889 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003890 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003891 && (eap->cmdidx < 0 ||
3892 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003893 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003894 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3895
3896 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003897 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003898 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003899 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003900#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 return p;
3903}
3904
3905#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003906static struct cmdmod
3907{
3908 char *name;
3909 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003910 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003911} cmdmods[] = {
3912 {"aboveleft", 3, FALSE},
3913 {"belowright", 3, FALSE},
3914 {"botright", 2, FALSE},
3915 {"browse", 3, FALSE},
3916 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003917 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003918 {"hide", 3, FALSE},
3919 {"keepalt", 5, FALSE},
3920 {"keepjumps", 5, FALSE},
3921 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003922 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003923 {"leftabove", 5, FALSE},
3924 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003925 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003926 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003927 {"rightbelow", 6, FALSE},
3928 {"sandbox", 3, FALSE},
3929 {"silent", 3, FALSE},
3930 {"tab", 3, TRUE},
3931 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003932 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003933 {"verbose", 4, TRUE},
3934 {"vertical", 4, FALSE},
3935};
3936
3937/*
3938 * Return length of a command modifier (including optional count).
3939 * Return zero when it's not a modifier.
3940 */
3941 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003942modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003943{
3944 int i, j;
3945 char_u *p = cmd;
3946
3947 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003948 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003949 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003950 {
3951 for (j = 0; p[j] != NUL; ++j)
3952 if (p[j] != cmdmods[i].name[j])
3953 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003954 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003955 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003956 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003957 }
3958 return 0;
3959}
3960
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961/*
3962 * Return > 0 if an Ex command "name" exists.
3963 * Return 2 if there is an exact match.
3964 * Return 3 if there is an ambiguous match.
3965 */
3966 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003967cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968{
3969 exarg_T ea;
3970 int full = FALSE;
3971 int i;
3972 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003973 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003975 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003976 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
3978 for (j = 0; name[j] != NUL; ++j)
3979 if (name[j] != cmdmods[i].name[j])
3980 break;
3981 if (name[j] == NUL && j >= cmdmods[i].minlen)
3982 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3983 }
3984
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003985 // Check built-in commands and user defined commands.
3986 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003987 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00003989 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003991 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003993 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3994 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003995 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003996 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3998}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003999
4000/*
4001 * "fullcommand" function
4002 */
4003 void
4004f_fullcommand(typval_T *argvars, typval_T *rettv)
4005{
4006 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004007 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004008 char_u *p;
4009
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004010 rettv->v_type = VAR_STRING;
4011 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004012
4013 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
4014 return;
4015
4016 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004017 if (name == NULL)
4018 return;
4019
zeertzjqc024ed92022-01-04 16:22:52 +00004020 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004021 name++;
4022 name = skip_range(name, TRUE, NULL);
4023
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004024 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4025 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004026 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004027 p = find_ex_command(&ea, NULL, NULL, NULL);
4028 if (p == NULL || ea.cmdidx == CMD_SIZE)
4029 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004030 if (in_vim9script())
4031 {
4032 int res;
4033
4034 ++emsg_silent;
4035 res = not_in_vim9(&ea);
4036 --emsg_silent;
4037
4038 if (res == FAIL)
4039 return;
4040 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004041
4042 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004043 ? get_user_command_name(ea.useridx, ea.cmdidx)
4044 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046#endif
4047
Bram Moolenaard0190392019-08-23 21:17:35 +02004048 cmdidx_T
4049excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050{
Bram Moolenaard0190392019-08-23 21:17:35 +02004051 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004053 if (!one_letter_cmd(cmd, &idx))
4054 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4055 idx = (cmdidx_T)((int)idx + 1))
4056 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4057 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
Bram Moolenaard0190392019-08-23 21:17:35 +02004059 return idx;
4060}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
Bram Moolenaard0190392019-08-23 21:17:35 +02004062 long
4063excmd_get_argt(cmdidx_T idx)
4064{
4065 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066}
4067
4068/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004069 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 *
4071 * Backslashed delimiters after / or ? will be skipped, and commands will
4072 * not be expanded between /'s and ?'s or after "'".
4073 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004074 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 * Returns the "cmd" pointer advanced to beyond the range.
4076 */
4077 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004078skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004079 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004080 int skip_star, // skip "*" used for Visual range
4081 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004083 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004084 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004086 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004088 if (*cmd == '\\')
4089 {
4090 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4091 ++cmd;
4092 else
4093 break;
4094 }
4095 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004097 char_u *p = cmd;
4098
4099 // a quote is only valid at the start or after a separator
4100 while (p > cmd_start)
4101 {
4102 --p;
4103 if (!VIM_ISWHITE(*p))
4104 break;
4105 }
4106 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4107 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if (*++cmd == NUL && ctx != NULL)
4109 *ctx = EXPAND_NOTHING;
4110 }
4111 else if (*cmd == '/' || *cmd == '?')
4112 {
4113 delim = *cmd++;
4114 while (*cmd != NUL && *cmd != delim)
4115 if (*cmd++ == '\\' && *cmd != NUL)
4116 ++cmd;
4117 if (*cmd == NUL && ctx != NULL)
4118 *ctx = EXPAND_NOTHING;
4119 }
4120 if (*cmd != NUL)
4121 ++cmd;
4122 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004123
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004124 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004125 while (*cmd == ':')
4126 cmd = skipwhite(cmd + 1);
4127
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004128 // Skip "*" used for Visual range.
4129 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4130 cmd = skipwhite(cmd + 1);
4131
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 return cmd;
4133}
4134
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004135 static void
4136addr_error(cmd_addr_T addr_type)
4137{
4138 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004139 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004140 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004141 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004142}
4143
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004145 * Return the default address for an address type.
4146 */
4147 static linenr_T
4148default_address(exarg_T *eap)
4149{
4150 linenr_T lnum = 0;
4151
4152 switch (eap->addr_type)
4153 {
4154 case ADDR_LINES:
4155 case ADDR_OTHER:
4156 // Default is the cursor line number. Avoid using an invalid
4157 // line number though.
4158 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4159 lnum = curbuf->b_ml.ml_line_count;
4160 else
4161 lnum = curwin->w_cursor.lnum;
4162 break;
4163 case ADDR_WINDOWS:
4164 lnum = CURRENT_WIN_NR;
4165 break;
4166 case ADDR_ARGUMENTS:
4167 lnum = curwin->w_arg_idx + 1;
4168 if (lnum > ARGCOUNT)
4169 lnum = ARGCOUNT;
4170 break;
4171 case ADDR_LOADED_BUFFERS:
4172 case ADDR_BUFFERS:
4173 lnum = curbuf->b_fnum;
4174 break;
4175 case ADDR_TABS:
4176 lnum = CURRENT_TAB_NR;
4177 break;
4178 case ADDR_TABS_RELATIVE:
4179 case ADDR_UNSIGNED:
4180 lnum = 1;
4181 break;
4182 case ADDR_QUICKFIX:
4183#ifdef FEAT_QUICKFIX
4184 lnum = qf_get_cur_idx(eap);
4185#endif
4186 break;
4187 case ADDR_QUICKFIX_VALID:
4188#ifdef FEAT_QUICKFIX
4189 lnum = qf_get_cur_valid_idx(eap);
4190#endif
4191 break;
4192 case ADDR_NONE:
4193 // Will give an error later if a range is found.
4194 break;
4195 }
4196 return lnum;
4197}
4198
4199/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004200 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 *
4202 * Set ptr to the next character after the part that was interpreted.
4203 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004204 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 *
4206 * Return MAXLNUM when no Ex address was found.
4207 */
4208 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004209get_address(
4210 exarg_T *eap UNUSED,
4211 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004212 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004213 int skip, // only skip the address, don't use it
4214 int silent, // no errors or side effects
4215 int to_other_file, // flag: may jump to other file
4216 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217{
4218 int c;
4219 int i;
4220 long n;
4221 char_u *cmd;
4222 pos_T pos;
4223 pos_T *fp;
4224 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004225 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
4227 cmd = skipwhite(*ptr);
4228 lnum = MAXLNUM;
4229 do
4230 {
4231 switch (*cmd)
4232 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004233 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004234 ++cmd;
4235 switch (addr_type)
4236 {
4237 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004238 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 lnum = curwin->w_cursor.lnum;
4240 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004241 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004242 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004243 break;
4244 case ADDR_ARGUMENTS:
4245 lnum = curwin->w_arg_idx + 1;
4246 break;
4247 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004248 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004249 lnum = curbuf->b_fnum;
4250 break;
4251 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004252 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004253 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004254 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004255 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004256 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004257 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004258 cmd = NULL;
4259 goto error;
4260 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004261 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004262#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004263 lnum = qf_get_cur_idx(eap);
4264#endif
4265 break;
4266 case ADDR_QUICKFIX_VALID:
4267#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004268 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004269#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004270 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 }
4272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004274 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004275 ++cmd;
4276 switch (addr_type)
4277 {
4278 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004279 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 lnum = curbuf->b_ml.ml_line_count;
4281 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004282 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004283 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004284 break;
4285 case ADDR_ARGUMENTS:
4286 lnum = ARGCOUNT;
4287 break;
4288 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004289 buf = lastbuf;
4290 while (buf->b_ml.ml_mfp == NULL)
4291 {
4292 if (buf->b_prev == NULL)
4293 break;
4294 buf = buf->b_prev;
4295 }
4296 lnum = buf->b_fnum;
4297 break;
4298 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004299 lnum = lastbuf->b_fnum;
4300 break;
4301 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004302 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004303 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004304 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004305 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004306 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004307 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004308 cmd = NULL;
4309 goto error;
4310 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004311 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004312#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004313 lnum = qf_get_size(eap);
4314 if (lnum == 0)
4315 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004316#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004317 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004318 case ADDR_QUICKFIX_VALID:
4319#ifdef FEAT_QUICKFIX
4320 lnum = qf_get_valid_size(eap);
4321 if (lnum == 0)
4322 lnum = 1;
4323#endif
4324 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004325 }
4326 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004328 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004329 if (*++cmd == NUL)
4330 {
4331 cmd = NULL;
4332 goto error;
4333 }
4334 if (addr_type != ADDR_LINES)
4335 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004336 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004337 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004338 goto error;
4339 }
4340 if (skip)
4341 ++cmd;
4342 else
4343 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004344 // Only accept a mark in another file when it is
4345 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004346 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4347 ++cmd;
4348 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004349 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004350 lnum = curwin->w_cursor.lnum;
4351 else
4352 {
4353 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
4355 cmd = NULL;
4356 goto error;
4357 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 lnum = fp->lnum;
4359 }
4360 }
4361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004364 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004365 c = *cmd++;
4366 if (addr_type != ADDR_LINES)
4367 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004368 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004369 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004370 goto error;
4371 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004372 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004373 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004374 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004375 if (*cmd == c)
4376 ++cmd;
4377 }
4378 else
4379 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004380 int flags;
4381
4382 pos = curwin->w_cursor; // save curwin->w_cursor
4383
4384 // When '/' or '?' follows another address, start from
4385 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004386 if (lnum > 0 && lnum != MAXLNUM)
4387 curwin->w_cursor.lnum =
4388 lnum > curbuf->b_ml.ml_line_count
4389 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004390
4391 // Start a forward search at the end of the line (unless
4392 // before the first line).
4393 // Start a backward search at the start of the line.
4394 // This makes sure we never match in the current
4395 // line, and can match anywhere in the
4396 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004397 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004398 curwin->w_cursor.col = MAXCOL;
4399 else
4400 curwin->w_cursor.col = 0;
4401 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004402 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004403 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004404 {
4405 curwin->w_cursor = pos;
4406 cmd = NULL;
4407 goto error;
4408 }
4409 lnum = curwin->w_cursor.lnum;
4410 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004411 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004412 cmd += searchcmdlen;
4413 }
4414 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004416 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 ++cmd;
4418 if (addr_type != ADDR_LINES)
4419 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004420 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004421 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004422 goto error;
4423 }
4424 if (*cmd == '&')
4425 i = RE_SUBST;
4426 else if (*cmd == '?' || *cmd == '/')
4427 i = RE_SEARCH;
4428 else
4429 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004430 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004431 cmd = NULL;
4432 goto error;
4433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 if (!skip)
4436 {
4437 /*
4438 * When search follows another address, start from
4439 * there.
4440 */
4441 if (lnum != MAXLNUM)
4442 pos.lnum = lnum;
4443 else
4444 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 /*
4447 * Start the search just like for the above
4448 * do_search().
4449 */
4450 if (*cmd != '?')
4451 pos.col = MAXCOL;
4452 else
4453 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004454 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004455 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004456 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004457 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004458 lnum = pos.lnum;
4459 else
4460 {
4461 cmd = NULL;
4462 goto error;
4463 }
4464 }
4465 ++cmd;
4466 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
4468 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004469 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004470 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472
4473 for (;;)
4474 {
4475 cmd = skipwhite(cmd);
4476 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4477 break;
4478
4479 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004480 {
4481 switch (addr_type)
4482 {
4483 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004484 case ADDR_OTHER:
4485 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004486 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 break;
4488 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004489 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 break;
4491 case ADDR_ARGUMENTS:
4492 lnum = curwin->w_arg_idx + 1;
4493 break;
4494 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004495 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004496 lnum = curbuf->b_fnum;
4497 break;
4498 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004499 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004501 case ADDR_TABS_RELATIVE:
4502 lnum = 1;
4503 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004504 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004505#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004506 lnum = qf_get_cur_idx(eap);
4507#endif
4508 break;
4509 case ADDR_QUICKFIX_VALID:
4510#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004511 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004512#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004513 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004514 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004515 case ADDR_UNSIGNED:
4516 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004517 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 }
4519 }
4520
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004522 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 else
4524 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004525 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 n = 1;
4527 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004530 if (n == MAXLNUM)
4531 {
4532 emsg(_(e_line_number_out_of_range));
4533 goto error;
4534 }
4535 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004536
4537 if (addr_type == ADDR_TABS_RELATIVE)
4538 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004539 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004540 cmd = NULL;
4541 goto error;
4542 }
4543 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004544 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004545 lnum = compute_buffer_local_count(
4546 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004548 {
4549#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004550 // Relative line addressing, need to adjust for folded lines
4551 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004552 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004553 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004554 (void)hasFolding(lnum, NULL, &lnum);
4555#endif
4556 if (i == '-')
4557 lnum -= n;
4558 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004559 {
4560 if (n >= LONG_MAX - lnum)
4561 {
4562 emsg(_(e_line_number_out_of_range));
4563 goto error;
4564 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004565 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004566 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 }
4569 } while (*cmd == '/' || *cmd == '?');
4570
4571error:
4572 *ptr = cmd;
4573 return lnum;
4574}
4575
4576/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004577 * Set eap->line1 and eap->line2 to the whole range.
4578 * Used for commands with the EX_DFLALL flag and no range given.
4579 */
4580 static void
4581address_default_all(exarg_T *eap)
4582{
4583 eap->line1 = 1;
4584 switch (eap->addr_type)
4585 {
4586 case ADDR_LINES:
4587 case ADDR_OTHER:
4588 eap->line2 = curbuf->b_ml.ml_line_count;
4589 break;
4590 case ADDR_LOADED_BUFFERS:
4591 {
4592 buf_T *buf = firstbuf;
4593
4594 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4595 buf = buf->b_next;
4596 eap->line1 = buf->b_fnum;
4597 buf = lastbuf;
4598 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4599 buf = buf->b_prev;
4600 eap->line2 = buf->b_fnum;
4601 }
4602 break;
4603 case ADDR_BUFFERS:
4604 eap->line1 = firstbuf->b_fnum;
4605 eap->line2 = lastbuf->b_fnum;
4606 break;
4607 case ADDR_WINDOWS:
4608 eap->line2 = LAST_WIN_NR;
4609 break;
4610 case ADDR_TABS:
4611 eap->line2 = LAST_TAB_NR;
4612 break;
4613 case ADDR_TABS_RELATIVE:
4614 eap->line2 = 1;
4615 break;
4616 case ADDR_ARGUMENTS:
4617 if (ARGCOUNT == 0)
4618 eap->line1 = eap->line2 = 0;
4619 else
4620 eap->line2 = ARGCOUNT;
4621 break;
4622 case ADDR_QUICKFIX_VALID:
4623#ifdef FEAT_QUICKFIX
4624 eap->line2 = qf_get_valid_size(eap);
4625 if (eap->line2 == 0)
4626 eap->line2 = 1;
4627#endif
4628 break;
4629 case ADDR_NONE:
4630 case ADDR_UNSIGNED:
4631 case ADDR_QUICKFIX:
4632 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4633 break;
4634 }
4635}
4636
4637
4638/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004639 * Get flags from an Ex command argument.
4640 */
4641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004642get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004643{
4644 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4645 {
4646 if (*eap->arg == 'l')
4647 eap->flags |= EXFLAG_LIST;
4648 else if (*eap->arg == 'p')
4649 eap->flags |= EXFLAG_PRINT;
4650 else
4651 eap->flags |= EXFLAG_NR;
4652 eap->arg = skipwhite(eap->arg + 1);
4653 }
4654}
4655
4656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 * Function called for command which is Not Implemented. NI!
4658 */
4659 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004660ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661{
4662 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004663 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004664 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665}
4666
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004667#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668/*
4669 * Function called for script command which is Not Implemented. NI!
4670 * Skips over ":perl <<EOF" constructs.
4671 */
4672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004673ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
4675 if (!eap->skip)
4676 ex_ni(eap);
4677 else
4678 vim_free(script_get(eap, eap->arg));
4679}
4680#endif
4681
4682/*
4683 * Check range in Ex command for validity.
4684 * Return NULL when valid, error message when invalid.
4685 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004686 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004687invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004689 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004690
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if ( eap->line1 < 0
4692 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004693 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004694 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004695
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004696 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004697 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004698 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004699 {
4700 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004701 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004702#ifdef FEAT_DIFF
4703 + (eap->cmdidx == CMD_diffget)
4704#endif
4705 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004706 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004707 break;
4708 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004709 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004710 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004711 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004712 break;
4713 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004714 // Only a boundary check, not whether the buffers actually
4715 // exist.
4716 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004717 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004718 break;
4719 case ADDR_LOADED_BUFFERS:
4720 buf = firstbuf;
4721 while (buf->b_ml.ml_mfp == NULL)
4722 {
4723 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004724 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004725 buf = buf->b_next;
4726 }
4727 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004728 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004729 buf = lastbuf;
4730 while (buf->b_ml.ml_mfp == NULL)
4731 {
4732 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004733 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004734 buf = buf->b_prev;
4735 }
4736 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004737 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004738 break;
4739 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004740 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004741 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004742 break;
4743 case ADDR_TABS:
4744 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004745 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004746 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004747 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004748 case ADDR_OTHER:
4749 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004750 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004751 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004752#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004753 // No error for value that is too big, will use the last entry.
4754 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004755 {
4756 if (eap->addr_count == 0)
4757 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004758 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004759 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004760#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004761 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004762 case ADDR_QUICKFIX_VALID:
4763#ifdef FEAT_QUICKFIX
4764 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4765 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004766 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004767#endif
4768 break;
4769 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004770 case ADDR_NONE:
4771 // Will give an error elsewhere.
4772 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004773 }
4774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 return NULL;
4776}
4777
4778/*
4779 * Correct the range for zero line number, if required.
4780 */
4781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004782correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004784 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 if (eap->line1 == 0)
4787 eap->line1 = 1;
4788 if (eap->line2 == 0)
4789 eap->line2 = 1;
4790 }
4791}
4792
Bram Moolenaar748bf032005-02-02 23:04:36 +00004793#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004794/*
4795 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4796 * pattern. Otherwise return eap->arg.
4797 */
4798 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004799skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004800{
4801 char_u *p = eap->arg;
4802
Bram Moolenaara37420f2006-02-04 22:37:47 +00004803 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4804 || eap->cmdidx == CMD_vimgrepadd
4805 || eap->cmdidx == CMD_lvimgrepadd
4806 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004807 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004808 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004809 if (p == NULL)
4810 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004811 }
4812 return p;
4813}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004814
4815/*
4816 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4817 * in the command line, so that things like % get expanded.
4818 */
4819 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004820replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004821{
4822 char_u *new_cmdline;
4823 char_u *program;
4824 char_u *pos;
4825 char_u *ptr;
4826 int len;
4827 int i;
4828
4829 /*
4830 * Don't do it when ":vimgrep" is used for ":grep".
4831 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004832 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4833 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4834 || eap->cmdidx == CMD_grepadd
4835 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004836 && !grep_internal(eap->cmdidx))
4837 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004838 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4839 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004840 {
4841 if (*curbuf->b_p_gp == NUL)
4842 program = p_gp;
4843 else
4844 program = curbuf->b_p_gp;
4845 }
4846 else
4847 {
4848 if (*curbuf->b_p_mp == NUL)
4849 program = p_mp;
4850 else
4851 program = curbuf->b_p_mp;
4852 }
4853
4854 p = skipwhite(p);
4855
4856 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4857 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004858 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004859 i = 1;
4860 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4861 ++i;
4862 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004863 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004864 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004865 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004866 ptr = new_cmdline;
4867 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4868 {
4869 i = (int)(pos - program);
4870 STRNCPY(ptr, program, i);
4871 STRCPY(ptr += i, p);
4872 ptr += len;
4873 program = pos + 2;
4874 }
4875 STRCPY(ptr, program);
4876 }
4877 else
4878 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004879 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004880 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004881 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004882 STRCPY(new_cmdline, program);
4883 STRCAT(new_cmdline, " ");
4884 STRCAT(new_cmdline, p);
4885 }
4886 msg_make(p);
4887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004888 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004889 vim_free(*cmdlinep);
4890 *cmdlinep = new_cmdline;
4891 p = new_cmdline;
4892 }
4893 return p;
4894}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004895#endif
4896
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897/*
4898 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004899 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 * Return FAIL for failure, OK otherwise.
4901 */
4902 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004903expand_filename(
4904 exarg_T *eap,
4905 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004906 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004908 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 char_u *repl;
4910 int srclen;
4911 char_u *p;
4912 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004913 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914
Bram Moolenaar748bf032005-02-02 23:04:36 +00004915#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004916 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004917 p = skip_grep_pat(eap);
4918#else
4919 p = eap->arg;
4920#endif
4921
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 /*
4923 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4924 * the file name contains a wildcard it should not cause expanding.
4925 * (it will be expanded anyway if there is a wildcard before replacing).
4926 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004927 has_wildcards = mch_has_wildcard(p);
4928 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004930#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004931 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004932 if (p[0] == '`' && p[1] == '=')
4933 {
4934 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004935 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004936 if (*p == '`')
4937 ++p;
4938 continue;
4939 }
4940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 /*
4942 * Quick check if this cannot be the start of a special string.
4943 * Also removes backslash before '%', '#' and '<'.
4944 */
4945 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4946 {
4947 ++p;
4948 continue;
4949 }
4950
4951 /*
4952 * Try to find a match at this position.
4953 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004954 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01004955 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004956 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004958 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
4960 p += srclen;
4961 continue;
4962 }
4963
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004964 // Wildcards won't be expanded below, the replacement is taken
4965 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004966 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4967 {
4968 char_u *l = repl;
4969
4970 repl = expand_env_save(repl);
4971 vim_free(l);
4972 }
4973
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004974 // Need to escape white space et al. with a backslash.
4975 // Don't do this for:
4976 // - replacement that already has been escaped: "##"
4977 // - shell commands (may have to use quotes instead).
4978 // - non-unix systems when there is a single argument (spaces don't
4979 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004981 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 && eap->cmdidx != CMD_grep
4984 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004985 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004986 && eap->cmdidx != CMD_lgrep
4987 && eap->cmdidx != CMD_lgrepadd
4988 && eap->cmdidx != CMD_lmake
4989 && eap->cmdidx != CMD_make
4990 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004992 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993#endif
4994 )
4995 {
4996 char_u *l;
4997#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004998 // Don't escape a backslash here, because rem_backslash() doesn't
4999 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 static char_u *nobslash = (char_u *)" \t\"|";
5001# define ESCAPE_CHARS nobslash
5002#else
5003# define ESCAPE_CHARS escape_chars
5004#endif
5005
5006 for (l = repl; *l; ++l)
5007 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5008 {
5009 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5010 if (l != NULL)
5011 {
5012 vim_free(repl);
5013 repl = l;
5014 }
5015 break;
5016 }
5017 }
5018
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005019 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005020 if ((eap->usefilter || eap->cmdidx == CMD_bang
5021 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005022 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
5024 char_u *l;
5025
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005026 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 if (l != NULL)
5028 {
5029 vim_free(repl);
5030 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
5032 }
5033
5034 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5035 vim_free(repl);
5036 if (p == NULL)
5037 return FAIL;
5038 }
5039
5040 /*
5041 * One file argument: Expand wildcards.
5042 * Don't do this with ":r !command" or ":w !command".
5043 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005044 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {
5046 /*
5047 * May do this twice:
5048 * 1. Replace environment variables.
5049 * 2. Replace any other wildcards, remove backslashes.
5050 */
5051 for (n = 1; n <= 2; ++n)
5052 {
5053 if (n == 2)
5054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 /*
5056 * Halve the number of backslashes (this is Vi compatible).
5057 * For Unix and OS/2, when wildcards are expanded, this is
5058 * done by ExpandOne() below.
5059 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005060#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 if (!has_wildcards)
5062#endif
5063 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065
5066 if (has_wildcards)
5067 {
5068 if (n == 1)
5069 {
5070 /*
5071 * First loop: May expand environment variables. This
5072 * can be done much faster with expand_env() than with
5073 * something else (e.g., calling a shell).
5074 * After expanding environment variables, check again
5075 * if there are still wildcards present.
5076 */
5077 if (vim_strchr(eap->arg, '$') != NULL
5078 || vim_strchr(eap->arg, '~') != NULL)
5079 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005080 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005081 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 has_wildcards = mch_has_wildcard(NameBuff);
5083 p = NameBuff;
5084 }
5085 else
5086 p = NULL;
5087 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005088 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
5090 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005091 int options = WILD_LIST_NOTFOUND
5092 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093
5094 ExpandInit(&xpc);
5095 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005096 if (p_wic)
5097 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005099 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 if (p == NULL)
5101 return FAIL;
5102 }
5103 if (p != NULL)
5104 {
5105 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5106 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005107 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 vim_free(p);
5109 }
5110 }
5111 }
5112 }
5113 return OK;
5114}
5115
5116/*
5117 * Replace part of the command line, keeping eap->cmd, eap->arg and
5118 * eap->nextcmd correct.
5119 * "src" points to the part that is to be replaced, of length "srclen".
5120 * "repl" is the replacement string.
5121 * Returns a pointer to the character after the replaced string.
5122 * Returns NULL for failure.
5123 */
5124 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005125repl_cmdline(
5126 exarg_T *eap,
5127 char_u *src,
5128 int srclen,
5129 char_u *repl,
5130 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131{
5132 int len;
5133 int i;
5134 char_u *new_cmdline;
5135
5136 /*
5137 * The new command line is build in new_cmdline[].
5138 * First allocate it.
5139 * Careful: a "+cmd" argument may have been NUL terminated.
5140 */
5141 len = (int)STRLEN(repl);
5142 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005143 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005144 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005145 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005146 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 /*
5149 * Copy the stuff before the expanded part.
5150 * Copy the expanded stuff.
5151 * Copy what came after the expanded part.
5152 * Copy the next commands, if there are any.
5153 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005154 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005156
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005158 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005160 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005162 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
5164 i = (int)STRLEN(new_cmdline) + 1;
5165 STRCPY(new_cmdline + i, eap->nextcmd);
5166 eap->nextcmd = new_cmdline + i;
5167 }
5168 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5169 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5170 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5171 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5172 vim_free(*cmdlinep);
5173 *cmdlinep = new_cmdline;
5174
5175 return src;
5176}
5177
5178/*
5179 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005180 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 */
5182 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005183separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184{
5185 char_u *p;
5186
Bram Moolenaar86b68352004-12-27 21:59:20 +00005187#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005188 p = skip_grep_pat(eap);
5189#else
5190 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005191#endif
5192
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005193 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
5195 if (*p == Ctrl_V)
5196 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005197 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005198 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005200 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005201 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005202 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 break;
5204 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005205
5206#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005207 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005208 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005209 {
5210 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005211 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005212 if (*p == NUL) // stop at NUL after CTRL-V
5213 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005214 }
5215#endif
5216
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005217 // Check for '"': start of comment or '|': next command
5218 // :@" and :*" do not start a comment!
5219 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005220 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005221#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005222 && !in_vim9script()
5223#endif
5224 && !(eap->argt & EX_NOTRLCOM)
5225 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5226 || p != eap->arg)
5227 && (eap->cmdidx != CMD_redir
5228 || p != eap->arg + 1 || p[-1] != '@'))
5229#ifdef FEAT_EVAL
5230 || (*p == '#'
5231 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005232 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005233 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 || *p == '|' || *p == '\n')
5236 {
5237 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005238 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 * AND 'b' is present in 'cpoptions'.
5240 */
5241 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005242 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005244 if (!keep_backslash)
5245 {
5246 STRMOVE(p - 1, p); // remove the '\'
5247 --p;
5248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250 else
5251 {
5252 eap->nextcmd = check_nextcmd(p);
5253 *p = NUL;
5254 break;
5255 }
5256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005258
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005259 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 del_trailing_spaces(eap->arg);
5261}
5262
5263/*
5264 * get + command from ex argument
5265 */
5266 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005267getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
5269 char_u *arg = *argp;
5270 char_u *command = NULL;
5271
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005272 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
5274 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005275 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 command = dollar_command;
5277 else
5278 {
5279 command = arg;
5280 arg = skip_cmd_arg(command, TRUE);
5281 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005282 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
5284
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005285 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 *argp = arg;
5287 }
5288 return command;
5289}
5290
5291/*
5292 * Find end of "+command" argument. Skip over "\ " and "\\".
5293 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005294 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005295skip_cmd_arg(
5296 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005297 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298{
5299 while (*p && !vim_isspace(*p))
5300 {
5301 if (*p == '\\' && p[1] != NUL)
5302 {
5303 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005304 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 else
5306 ++p;
5307 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005308 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
5310 return p;
5311}
5312
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005313 int
5314get_bad_opt(char_u *p, exarg_T *eap)
5315{
5316 if (STRICMP(p, "keep") == 0)
5317 eap->bad_char = BAD_KEEP;
5318 else if (STRICMP(p, "drop") == 0)
5319 eap->bad_char = BAD_DROP;
5320 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5321 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005322 else
5323 return FAIL;
5324 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005325}
5326
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327/*
5328 * Get "++opt=arg" argument.
5329 * Return FAIL or OK.
5330 */
5331 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005332getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333{
5334 char_u *arg = eap->arg + 2;
5335 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005336 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005339 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5341 {
5342 if (*arg == 'n')
5343 {
5344 arg += 2;
5345 eap->force_bin = FORCE_NOBIN;
5346 }
5347 else
5348 eap->force_bin = FORCE_BIN;
5349 if (!checkforcmd(&arg, "binary", 3))
5350 return FAIL;
5351 eap->arg = skipwhite(arg);
5352 return OK;
5353 }
5354
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005355 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005356 if (STRNCMP(arg, "edit", 4) == 0)
5357 {
5358 eap->read_edit = TRUE;
5359 eap->arg = skipwhite(arg + 4);
5360 return OK;
5361 }
5362
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 if (STRNCMP(arg, "ff", 2) == 0)
5364 {
5365 arg += 2;
5366 pp = &eap->force_ff;
5367 }
5368 else if (STRNCMP(arg, "fileformat", 10) == 0)
5369 {
5370 arg += 10;
5371 pp = &eap->force_ff;
5372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 else if (STRNCMP(arg, "enc", 3) == 0)
5374 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005375 if (STRNCMP(arg, "encoding", 8) == 0)
5376 arg += 8;
5377 else
5378 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 pp = &eap->force_enc;
5380 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005381 else if (STRNCMP(arg, "bad", 3) == 0)
5382 {
5383 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005384 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
5387 if (pp == NULL || *arg != '=')
5388 return FAIL;
5389
5390 ++arg;
5391 *pp = (int)(arg - eap->cmd);
5392 arg = skip_cmd_arg(arg, FALSE);
5393 eap->arg = skipwhite(arg);
5394 *arg = NUL;
5395
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 if (pp == &eap->force_ff)
5397 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5399 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005400 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005402 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005404 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5406 *p = TOLOWER_ASC(*p);
5407 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005408 else
5409 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005410 // Check ++bad= argument. Must be a single-byte character, "keep" or
5411 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005412 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005413 return FAIL;
5414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
5416 return OK;
5417}
5418
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005420ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421{
5422 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005423 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 * directory for security reasons.
5425 */
5426 if (secure)
5427 {
5428 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005429 eap->errmsg =
5430 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
5432 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005433 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 else
5435 do_augroup(eap->arg, eap->forceit);
5436}
5437
5438/*
5439 * ":doautocmd": Apply the automatic commands to the current buffer.
5440 */
5441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005442ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005444 char_u *arg = eap->arg;
5445 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005446 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005447
Bram Moolenaar1610d052016-06-09 22:53:01 +02005448 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005449 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005450 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005451 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454/*
5455 * :[N]bunload[!] [N] [bufname] unload buffer
5456 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5457 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5458 */
5459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005460ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005462 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005463 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 eap->errmsg = do_bufdel(
5465 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5466 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5467 : DOBUF_UNLOAD, eap->arg,
5468 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5469}
5470
5471/*
5472 * :[N]buffer [N] to buffer N
5473 * :[N]sbuffer [N] to buffer N
5474 */
5475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005476ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005478 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005479 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005481 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 else
5483 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005484 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5486 else
5487 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005488 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005489 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
5491}
5492
5493/*
5494 * :[N]bmodified [N] to next mod. buffer
5495 * :[N]sbmodified [N] to next mod. buffer
5496 */
5497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005498ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005501 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005502 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503}
5504
5505/*
5506 * :[N]bnext [N] to next buffer
5507 * :[N]sbnext [N] split and to next buffer
5508 */
5509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005510ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005512 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005513 return;
5514
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005516 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005517 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518}
5519
5520/*
5521 * :[N]bNext [N] to previous buffer
5522 * :[N]bprevious [N] to previous buffer
5523 * :[N]sbNext [N] split and to previous buffer
5524 * :[N]sbprevious [N] split and to previous buffer
5525 */
5526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005527ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005529 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005530 return;
5531
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005533 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005534 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535}
5536
5537/*
5538 * :brewind to first buffer
5539 * :bfirst to first buffer
5540 * :sbrewind split and to first buffer
5541 * :sbfirst split and to first buffer
5542 */
5543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005544ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005546 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005547 return;
5548
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005550 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005551 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552}
5553
5554/*
5555 * :blast to last buffer
5556 * :sblast split and to last buffer
5557 */
5558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005559ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005561 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005562 return;
5563
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005565 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005566 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
Bram Moolenaar7a092242020-04-16 22:10:49 +02005569/*
5570 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005571 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005574ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005576 int comment_char = '"';
5577
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005578#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005579 if (in_vim9script())
5580 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005581#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005582 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005583}
5584
5585/*
5586 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5587 * to "cmd_start" or has a white space character before it.
5588 */
5589 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005590ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005591{
5592 int c = *cmd;
5593
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005594 if (c == NUL || c == '|' || c == '\n')
5595 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005596#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005597 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005598 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5599 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005600 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005601#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005602 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603}
5604
5605#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5606 || defined(PROTO)
5607/*
5608 * Return the next command, after the first '|' or '\n'.
5609 * Return NULL if not found.
5610 */
5611 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005612find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614 while (*p != '|' && *p != '\n')
5615 {
5616 if (*p == NUL)
5617 return NULL;
5618 ++p;
5619 }
5620 return (p + 1);
5621}
5622#endif
5623
5624/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005625 * Check if *p is a separator between Ex commands, skipping over white space.
5626 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 */
5628 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005629check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005631 char_u *s = skipwhite(p);
5632
5633 if (*s == '|' || *s == '\n')
5634 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 else
5636 return NULL;
5637}
5638
5639/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005640 * If "eap->nextcmd" is not set, check for a next command at "p".
5641 */
5642 void
5643set_nextcmd(exarg_T *eap, char_u *arg)
5644{
5645 char_u *p = check_nextcmd(arg);
5646
5647 if (eap->nextcmd == NULL)
5648 eap->nextcmd = p;
5649 else if (p != NULL)
5650 // cannot use "| command" inside a {} block
5651 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5652}
5653
5654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 * - if there are more files to edit
5656 * - and this is the last window
5657 * - and forceit not used
5658 * - and not repeated twice on a row
5659 * return FAIL and give error message if 'message' TRUE
5660 * return OK otherwise
5661 */
5662 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005663check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005664 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005665 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666{
5667 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5668
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005669 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005670 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 {
5672 if (message)
5673 {
5674#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005675 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5676 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005678 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005680 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5681 NGETTEXT("%d more file to edit. Quit anyway?",
5682 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5684 return OK;
5685 return FAIL;
5686 }
5687#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005688 semsg(NGETTEXT(e_nr_more_file_to_edit,
5689 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005690 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 return FAIL;
5693 }
5694 return OK;
5695}
5696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697/*
5698 * Function given to ExpandGeneric() to obtain the list of command names.
5699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005701get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
5703 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005704 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 return cmdnames[idx].cmd_name;
5706}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005709ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
Bram Moolenaare6850792010-05-14 15:28:44 +02005711 if (*eap->arg == NUL)
5712 {
5713#ifdef FEAT_EVAL
5714 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5715 char_u *p = NULL;
5716
5717 if (expr != NULL)
5718 {
5719 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005720 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005721 --emsg_off;
5722 vim_free(expr);
5723 }
5724 if (p != NULL)
5725 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005726 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005727 vim_free(p);
5728 }
5729 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005730 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005731#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005732 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005733#endif
5734 }
5735 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005736 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005737
5738#ifdef FEAT_VTP
5739 else if (has_vtp_working())
5740 {
5741 // background color change requires clear + redraw
5742 update_screen(CLEAR);
5743 redrawcmd();
5744 }
5745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746}
5747
5748 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005749ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
5751 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005752 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 do_highlight(eap->arg, eap->forceit, FALSE);
5754}
5755
5756
5757/*
5758 * Call this function if we thought we were going to exit, but we won't
5759 * (because of an error). May need to restore the terminal mode.
5760 */
5761 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005762not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763{
5764 exiting = FALSE;
5765 settmode(TMODE_RAW);
5766}
5767
Bram Moolenaar3552e742021-05-29 12:21:58 +02005768 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005769before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5770{
5771 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5772
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005773 // Bail out when autocommands closed the window.
5774 // Refuse to quit when the buffer in the last window is being closed (can
5775 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005776 if (!win_valid(wp)
5777 || curbuf_locked()
5778 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5779 return TRUE;
5780
5781 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5782 {
5783 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005784 // Refuse to quit when locked or when the window was closed or the
5785 // buffer in the last window is being closed (can only happen in
5786 // autocommands).
5787 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005788 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5789 return TRUE;
5790 }
5791
5792 return FALSE;
5793}
5794
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005796 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005797 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005798 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005800 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005801ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005803 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005804
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805#ifdef FEAT_CMDWIN
5806 if (cmdwin_type != 0)
5807 {
5808 cmdwin_result = Ctrl_C;
5809 return;
5810 }
5811#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005812 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005813 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005814 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005815 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005816 return;
5817 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005818 if (eap->addr_count > 0)
5819 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005820 int wnr = eap->line2;
5821
5822 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5823 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005824 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005825 }
5826 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005827 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005828
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005829 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005830 if (curbuf_locked())
5831 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005832
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005833 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005834 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005835 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
5837#ifdef FEAT_NETBEANS_INTG
5838 netbeansForcedQuit = eap->forceit;
5839#endif
5840
5841 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005842 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 */
5844 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5845 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005846 if ((!buf_hide(wp->w_buffer)
5847 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005848 | (eap->forceit ? CCGD_FORCEIT : 0)
5849 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005851 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {
5853 not_exiting();
5854 }
5855 else
5856 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005857 // quit last window
5858 // Note: only_one_window() returns true, even so a help window is
5859 // still open. In that case only quit, if no address has been
5860 // specified. Example:
5861 // :h|wincmd w|1q - don't quit
5862 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005863 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005865 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005866#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005868#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005869 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005870 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 }
5872}
5873
5874/*
5875 * ":cquit".
5876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005878ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005880 // this does not always pass on the exit code to the Manx compiler. why?
5881 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882}
5883
5884/*
5885 * ":qall": try to quit all windows
5886 */
5887 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005888ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
5890# ifdef FEAT_CMDWIN
5891 if (cmdwin_type != 0)
5892 {
5893 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005894 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 else
5896 cmdwin_result = K_XF2;
5897 return;
5898 }
5899# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005900
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005901 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005902 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005903 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005904 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005905 return;
5906 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005907
5908 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005909 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005910
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005912 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 getout(0);
5914 not_exiting();
5915}
5916
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917/*
5918 * ":close": close current window, unless it is the last one
5919 */
5920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005921ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005923 win_T *win;
5924 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005925#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005927 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005929#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005930 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005931 {
5932 if (eap->addr_count == 0)
5933 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005934 else
5935 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005936 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005937 {
5938 winnr++;
5939 if (winnr == eap->line2)
5940 break;
5941 }
5942 if (win == NULL)
5943 win = lastwin;
5944 ex_win_close(eap->forceit, win, NULL);
5945 }
5946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947}
5948
Bram Moolenaar4033c552017-09-16 20:54:51 +02005949#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005950/*
5951 * ":pclose": Close any preview window.
5952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005954ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005955{
5956 win_T *win;
5957
Bram Moolenaar79648732019-07-18 21:43:07 +02005958 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005959 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005960 if (win->w_p_pvw)
5961 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005962 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005963 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005964 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005965# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005966 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005967 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005968# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005969}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005970#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005971
Bram Moolenaarf740b292006-02-16 22:11:02 +00005972/*
5973 * Close window "win" and take care of handling closing the last window for a
5974 * modified buffer.
5975 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005977ex_win_close(
5978 int forceit,
5979 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005980 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981{
5982 int need_hide;
5983 buf_T *buf = win->w_buffer;
5984
Bram Moolenaarcf844172020-06-26 19:44:06 +02005985 // Never close the autocommand window.
5986 if (win == aucmd_win)
5987 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00005988 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02005989 return;
5990 }
5991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005993 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005995#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005996 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005998 bufref_T bufref;
5999
6000 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006002 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 return;
6004 need_hide = FALSE;
6005 }
6006 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006009 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 return;
6011 }
6012 }
6013
Bram Moolenaar4033c552017-09-16 20:54:51 +02006014#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006016#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006017
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006018 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006019 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006020 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006021 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006022 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023}
6024
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006026 * Handle the argument for a tabpage related ex command.
6027 * Returns a tabpage number.
6028 * When an error is encountered then eap->errmsg is set.
6029 */
6030 static int
6031get_tabpage_arg(exarg_T *eap)
6032{
6033 int tab_number;
6034 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6035
6036 if (eap->arg && *eap->arg != NUL)
6037 {
6038 char_u *p = eap->arg;
6039 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006040 int relative = 0; // argument +N/-N means: go to N places to the
6041 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006042
6043 if (*p == '-')
6044 {
6045 relative = -1;
6046 p++;
6047 }
6048 else if (*p == '+')
6049 {
6050 relative = 1;
6051 p++;
6052 }
6053
6054 p_save = p;
6055 tab_number = getdigits(&p);
6056
6057 if (relative == 0)
6058 {
6059 if (STRCMP(p, "$") == 0)
6060 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006061 else if (STRCMP(p, "#") == 0)
6062 if (valid_tabpage(lastused_tabpage))
6063 tab_number = tabpage_index(lastused_tabpage);
6064 else
6065 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006066 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006067 tab_number = 0;
6068 goto theend;
6069 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006070 else if (p == p_save || *p_save == '-' || *p != NUL
6071 || tab_number > LAST_TAB_NR)
6072 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006073 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006074 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006075 goto theend;
6076 }
6077 }
6078 else
6079 {
6080 if (*p_save == NUL)
6081 tab_number = 1;
6082 else if (p == p_save || *p_save == '-' || *p != NUL
6083 || tab_number == 0)
6084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006085 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006086 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006087 goto theend;
6088 }
6089 tab_number = tab_number * relative + tabpage_index(curtab);
6090 if (!unaccept_arg0 && relative == -1)
6091 --tab_number;
6092 }
6093 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006094 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006095 }
6096 else if (eap->addr_count > 0)
6097 {
6098 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006099 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006100 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006101 tab_number = 0;
6102 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006103 else
6104 {
6105 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006106 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006107 {
6108 --tab_number;
6109 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006110 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006111 }
6112 }
6113 }
6114 else
6115 {
6116 switch (eap->cmdidx)
6117 {
6118 case CMD_tabnext:
6119 tab_number = tabpage_index(curtab) + 1;
6120 if (tab_number > LAST_TAB_NR)
6121 tab_number = 1;
6122 break;
6123 case CMD_tabmove:
6124 tab_number = LAST_TAB_NR;
6125 break;
6126 default:
6127 tab_number = tabpage_index(curtab);
6128 }
6129 }
6130
6131theend:
6132 return tab_number;
6133}
6134
6135/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006136 * ":tabclose": close current tab page, unless it is the last one.
6137 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 */
6139 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006140ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006142 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006143 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006144
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006145# ifdef FEAT_CMDWIN
6146 if (cmdwin_type != 0)
6147 cmdwin_result = K_IGNORE;
6148 else
6149# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006150 if (first_tabpage->tp_next == NULL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00006151 emsg(_(e_cannot_close_last_tab_page));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006152 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006154 tab_number = get_tabpage_arg(eap);
6155 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006156 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006157 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006158 if (tp == NULL)
6159 {
6160 beep_flush();
6161 return;
6162 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006163 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006164 {
6165 tabpage_close_other(tp, eap->forceit);
6166 return;
6167 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006168 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006169 tabpage_close(eap->forceit);
6170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006172}
6173
6174/*
6175 * ":tabonly": close all tab pages except the current one
6176 */
6177 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006178ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006179{
6180 tabpage_T *tp;
6181 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006182 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006183
6184# ifdef FEAT_CMDWIN
6185 if (cmdwin_type != 0)
6186 cmdwin_result = K_IGNORE;
6187 else
6188# endif
6189 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006190 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006191 else
6192 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006193 tab_number = get_tabpage_arg(eap);
6194 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006195 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006196 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006197 // Repeat this up to a 1000 times, because autocommands may
6198 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006199 for (done = 0; done < 1000; ++done)
6200 {
6201 FOR_ALL_TABPAGES(tp)
6202 if (tp->tp_topframe != topframe)
6203 {
6204 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006205 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006206 if (valid_tabpage(tp))
6207 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006208 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006209 break;
6210 }
6211 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006212 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006213 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006214 }
6215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
6218/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006219 * Close the current tab page.
6220 */
6221 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006222tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006223{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006224 // First close all the windows but the current one. If that worked then
6225 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006226 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006227 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006228 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006229 ex_win_close(forceit, curwin, NULL);
6230# ifdef FEAT_GUI
6231 need_mouse_correct = TRUE;
6232# endif
6233}
6234
6235/*
6236 * Close tab page "tp", which is not the current tab page.
6237 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006238 * Also takes care of the tab pages line disappearing when closing the
6239 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006240 */
6241 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006242tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006243{
6244 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006245 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006246 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006247
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006248 // Limit to 1000 windows, autocommands may add a window while we close
6249 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006250 while (++done < 1000)
6251 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006252 wp = tp->tp_firstwin;
6253 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006254
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006255 // Autocommands may delete the tab page under our fingers and we may
6256 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006257 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006258 break;
6259 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006260
Bram Moolenaar29323592016-07-24 22:04:11 +02006261 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006262
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006263 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006264 if (h != tabline_height())
6265 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006266}
6267
6268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 * ":only".
6270 */
6271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006272ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006274 win_T *wp;
6275 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276# ifdef FEAT_GUI
6277 need_mouse_correct = TRUE;
6278# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006279 if (eap->addr_count > 0)
6280 {
6281 wnr = eap->line2;
6282 for (wp = firstwin; --wnr > 0; )
6283 {
6284 if (wp->w_next == NULL)
6285 break;
6286 else
6287 wp = wp->w_next;
6288 }
6289 win_goto(wp);
6290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 close_others(TRUE, eap->forceit);
6292}
6293
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006295ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006297 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006298 if (!eap->skip)
6299 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006300#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006301 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006302#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006303 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006304 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006305 else
6306 {
6307 int winnr = 0;
6308 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006309
Bram Moolenaar2256c992016-11-15 21:17:07 +01006310 FOR_ALL_WINDOWS(win)
6311 {
6312 winnr++;
6313 if (winnr == eap->line2)
6314 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006315 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006316 if (win == NULL)
6317 win = lastwin;
6318 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 }
6321}
6322
6323/*
6324 * ":stop" and ":suspend": Suspend Vim.
6325 */
dbivolaruab16ad32021-12-29 19:41:47 +00006326 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006327ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328{
6329 /*
6330 * Disallow suspending for "rvim".
6331 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006332 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 {
6334 if (!eap->forceit)
6335 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006336 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 windgoto((int)Rows - 1, 0);
6338 out_char('\n');
6339 out_flush();
6340 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006341 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006342 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006343 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006345 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006347 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006349 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006350 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 }
6352}
6353
6354/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006355 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 */
6357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006358ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006360#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006361 if (not_in_vim9(eap) == FAIL)
6362 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364#ifdef FEAT_CMDWIN
6365 if (cmdwin_type != 0)
6366 {
6367 cmdwin_result = Ctrl_C;
6368 return;
6369 }
6370#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006371 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006372 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006373 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006374 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006375 return;
6376 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006377
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006379 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 */
6381 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6382 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006383
6384 // Write the buffer for ":wq" or when it was changed.
6385 // Trigger QuitPre and ExitPre.
6386 // Check if we can exit now, after autocommands have changed things.
6387 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6388 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006390 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 {
6392 not_exiting();
6393 }
6394 else
6395 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006396 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006398 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399# ifdef FEAT_GUI
6400 need_mouse_correct = TRUE;
6401# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006402 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006403 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 }
6405}
6406
6407/*
6408 * ":print", ":list", ":number".
6409 */
6410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006411ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006413 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006414 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006417 for ( ;!got_int; ui_breakcheck())
6418 {
6419 print_line(eap->line1,
6420 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6421 || (eap->flags & EXFLAG_NR)),
6422 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6423 if (++eap->line1 > eap->line2)
6424 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006425 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006426 }
6427 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006428 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006429 curwin->w_cursor.lnum = eap->line2;
6430 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 }
6432
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434}
6435
6436#ifdef FEAT_BYTEOFF
6437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006438ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
6440 goto_byte(eap->line2);
6441}
6442#endif
6443
6444/*
6445 * ":shell".
6446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006448ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
6450 do_shell(NULL, 0);
6451}
6452
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006453#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006455static int drop_busy = FALSE;
6456static int drop_filec;
6457static char_u **drop_filev = NULL;
6458static int drop_split;
6459static void (*drop_callback)(void *);
6460static void *drop_cookie;
6461
6462 static void
6463handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
6465 exarg_T ea;
6466 int save_msg_scroll = msg_scroll;
6467
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006468 // Setting the argument list may cause screen updates and being called
6469 // recursively. Avoid that by setting drop_busy.
6470 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006472 // Check whether the current buffer is changed. If so, we will need
6473 // to split the current window or data could be lost.
6474 // We don't need to check if the 'hidden' option is set, as in this
6475 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006476 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 {
6478 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006479 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 --emsg_off;
6481 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006482 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (win_split(0, 0) == FAIL)
6485 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006486 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006488 // When splitting the window, create a new alist. Otherwise the
6489 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 alist_unlink(curwin->w_alist);
6491 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 }
6493
6494 /*
6495 * Set up the new argument list.
6496 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006497 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498
6499 /*
6500 * Move to the first file.
6501 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006502 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006503 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 ea.cmd = (char_u *)"next";
6505 do_argfile(&ea, 0);
6506
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006507 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6508 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 need_start_insertmode = FALSE;
6510
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006511 // Restore msg_scroll, otherwise a following command may cause scrolling
6512 // unexpectedly. The screen will be redrawn by the caller, thus
6513 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006515
6516 if (drop_callback != NULL)
6517 drop_callback(drop_cookie);
6518
6519 drop_filev = NULL;
6520 drop_busy = FALSE;
6521}
6522
6523/*
6524 * Handle a file drop. The code is here because a drop is *nearly* like an
6525 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006526 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006527 * code is very similar to :args and hence needs access to a lot of the static
6528 * functions in this file.
6529 *
6530 * The "filev" list must have been allocated using alloc(), as should each item
6531 * in the list. This function takes over responsibility for freeing the "filev"
6532 * list.
6533 */
6534 void
6535handle_drop(
6536 int filec, // the number of files dropped
6537 char_u **filev, // the list of files dropped
6538 int split, // force splitting the window
6539 void (*callback)(void *), // to be called after setting the argument
6540 // list
6541 void *cookie) // argument for "callback" (allocated)
6542{
6543 // Cannot handle recursive drops, finish the pending one.
6544 if (drop_busy)
6545 {
6546 FreeWild(filec, filev);
6547 vim_free(cookie);
6548 return;
6549 }
6550
6551 // When calling handle_drop() more than once in a row we only use the last
6552 // one.
6553 if (drop_filev != NULL)
6554 {
6555 FreeWild(drop_filec, drop_filev);
6556 vim_free(drop_cookie);
6557 }
6558
6559 drop_filec = filec;
6560 drop_filev = filev;
6561 drop_split = split;
6562 drop_callback = callback;
6563 drop_cookie = cookie;
6564
6565 // Postpone this when:
6566 // - editing the command line
6567 // - not possible to change the current buffer
6568 // - updating the screen
6569 // As it may change buffers and window structures that are in use and cause
6570 // freed memory to be used.
6571 if (text_locked() || curbuf_locked() || updating_screen)
6572 return;
6573
6574 handle_drop_internal();
6575}
6576
6577/*
6578 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6579 * reset: Handle a postponed drop.
6580 */
6581 void
6582handle_any_postponed_drop(void)
6583{
6584 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006585 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006586 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587}
6588#endif
6589
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 * ":preserve".
6592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006594ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006596 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 ml_preserve(curbuf, TRUE);
6598}
6599
6600/*
6601 * ":recover".
6602 */
6603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006604ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006606 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006608 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6609 | CCGD_MULTWIN
6610 | (eap->forceit ? CCGD_FORCEIT : 0)
6611 | CCGD_EXCMD)
6612
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 && (*eap->arg == NUL
6614 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006615 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 recoverymode = FALSE;
6617}
6618
6619/*
6620 * Command modifier used in a wrong way.
6621 */
6622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006623ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006625 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626}
6627
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628/*
6629 * :sview [+command] file split window with new file, read-only
6630 * :split [[+command] file] split window with current or new file
6631 * :vsplit [[+command] file] split window vertically with current or new file
6632 * :new [[+command] file] split window with no or new file
6633 * :vnew [[+command] file] split vertically window with no or new file
6634 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006635 *
6636 * :tabedit open new Tab page with empty window
6637 * :tabedit [+command] file open new Tab page and edit "file"
6638 * :tabnew [[+command] file] just like :tabedit
6639 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 */
6641 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006642ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006644 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006645#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006647#endif
6648#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006649 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006650 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006651#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006652 int use_tab = eap->cmdidx == CMD_tabedit
6653 || eap->cmdidx == CMD_tabfind
6654 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006656 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006657 return;
6658
Bram Moolenaar4033c552017-09-16 20:54:51 +02006659#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
Bram Moolenaar4033c552017-09-16 20:54:51 +02006663#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006664 // A ":split" in the quickfix window works like ":new". Don't want two
6665 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006666 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {
6668 if (eap->cmdidx == CMD_split)
6669 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 if (eap->cmdidx == CMD_vsplit)
6671 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
Bram Moolenaar4033c552017-09-16 20:54:51 +02006675#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006676 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 {
6678 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6679 FNAME_MESS, TRUE, curbuf->b_ffname);
6680 if (fname == NULL)
6681 goto theend;
6682 eap->arg = fname;
6683 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006684# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006685 else
6686# endif
6687#endif
6688#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006689 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 && eap->cmdidx != CMD_new)
6692 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006693 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006694# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006695 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006696# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006697 au_has_group((char_u *)"FileExplorer"))
6698 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006699 // No browsing supported but we do have the file explorer:
6700 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006701 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006702 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006703 }
6704 else
6705 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006706 fname = do_browse(0, (char_u *)(use_tab
6707 ? _("Edit File in new tab page")
6708 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006710 if (fname == NULL)
6711 goto theend;
6712 eap->arg = fname;
6713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006715 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006718 /*
6719 * Either open new tab page or split the window.
6720 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006721 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006722 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006723 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006724 : eap->addr_count == 0 ? 0
6725 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006726 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006727 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006728
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006729 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006730 if (curwin != old_curwin
6731 && win_valid(old_curwin)
6732 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006733 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006734 old_curwin->w_alt_fnum = curbuf->b_fnum;
6735 }
6736 }
6737 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6739 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006740 // Reset 'scrollbind' when editing another file, but keep it when
6741 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006742 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006743 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 else
6745 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 do_exedit(eap, old_curwin);
6747 }
6748
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006749# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006750 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006753# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754theend:
6755 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006758
6759/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006760 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006761 */
6762 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006763tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006764{
6765 exarg_T ea;
6766
Bram Moolenaara80faa82020-04-12 19:37:17 +02006767 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006768 ea.cmdidx = CMD_tabnew;
6769 ea.cmd = (char_u *)"tabn";
6770 ea.arg = (char_u *)"";
6771 ex_splitview(&ea);
6772}
6773
6774/*
6775 * :tabnext command
6776 */
6777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006778ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006779{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006780 int tab_number;
6781
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006782 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006783 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006784 switch (eap->cmdidx)
6785 {
6786 case CMD_tabfirst:
6787 case CMD_tabrewind:
6788 goto_tabpage(1);
6789 break;
6790 case CMD_tablast:
6791 goto_tabpage(9999);
6792 break;
6793 case CMD_tabprevious:
6794 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006795 if (eap->arg && *eap->arg != NUL)
6796 {
6797 char_u *p = eap->arg;
6798 char_u *p_save = p;
6799
6800 tab_number = getdigits(&p);
6801 if (p == p_save || *p_save == '-' || *p != NUL
6802 || tab_number == 0)
6803 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006804 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006805 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006806 return;
6807 }
6808 }
6809 else
6810 {
6811 if (eap->addr_count == 0)
6812 tab_number = 1;
6813 else
6814 {
6815 tab_number = eap->line2;
6816 if (tab_number < 1)
6817 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006818 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006819 return;
6820 }
6821 }
6822 }
6823 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006824 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006825 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006826 tab_number = get_tabpage_arg(eap);
6827 if (eap->errmsg == NULL)
6828 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006829 break;
6830 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006831}
6832
6833/*
6834 * :tabmove command
6835 */
6836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006837ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006838{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006839 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006840
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006841 tab_number = get_tabpage_arg(eap);
6842 if (eap->errmsg == NULL)
6843 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006844}
6845
6846/*
6847 * :tabs command: List tabs and their contents.
6848 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006849 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006850ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006851{
6852 tabpage_T *tp;
6853 win_T *wp;
6854 int tabcount = 1;
6855
6856 msg_start();
6857 msg_scroll = TRUE;
6858 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6859 {
6860 msg_putchar('\n');
6861 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006862 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006863 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006864 ui_breakcheck();
6865
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006866 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006867 wp = firstwin;
6868 else
6869 wp = tp->tp_firstwin;
6870 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6871 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006872 msg_putchar('\n');
6873 msg_putchar(wp == curwin ? '>' : ' ');
6874 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006875 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6876 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006877 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006878 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006879 else
6880 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6881 IObuff, IOSIZE, TRUE);
6882 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006883 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006884 ui_breakcheck();
6885 }
6886 }
6887}
6888
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889/*
6890 * ":mode": Set screen mode.
6891 * If no argument given, just get the screen size and redraw.
6892 */
6893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006894ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
6896 if (*eap->arg == NUL)
6897 shell_resized();
6898 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006899 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900}
6901
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902/*
6903 * ":resize".
6904 * set, increment or decrement current window height
6905 */
6906 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006907ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908{
6909 int n;
6910 win_T *wp = curwin;
6911
6912 if (eap->addr_count > 0)
6913 {
6914 n = eap->line2;
6915 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6916 ;
6917 }
6918
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006919# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006923 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {
6925 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006926 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006927 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006929 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 }
6931 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {
6933 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006934 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006935 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006937 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
6939}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
6941/*
6942 * ":find [+command] <file>" command.
6943 */
6944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006945ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946{
6947#ifdef FEAT_SEARCHPATH
6948 char_u *fname;
6949 int count;
6950
6951 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6952 TRUE, curbuf->b_ffname);
6953 if (eap->addr_count > 0)
6954 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006955 // Repeat finding the file "count" times. This matters when it
6956 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 count = eap->line2;
6958 while (fname != NULL && --count > 0)
6959 {
6960 vim_free(fname);
6961 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6962 FALSE, curbuf->b_ffname);
6963 }
6964 }
6965
6966 if (fname != NULL)
6967 {
6968 eap->arg = fname;
6969#endif
6970 do_exedit(eap, NULL);
6971#ifdef FEAT_SEARCHPATH
6972 vim_free(fname);
6973 }
6974#endif
6975}
6976
6977/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006978 * ":open" simulation: for now just work like ":visual".
6979 */
6980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006981ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006982{
6983 regmatch_T regmatch;
6984 char_u *p;
6985
Bram Moolenaar65088802021-03-13 21:07:21 +01006986#ifdef FEAT_EVAL
6987 if (not_in_vim9(eap) == FAIL)
6988 return;
6989#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006990 curwin->w_cursor.lnum = eap->line2;
6991 beginline(BL_SOL | BL_FIX);
6992 if (*eap->arg == '/')
6993 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006994 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006995 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006996 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006997 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006998 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006999 if (regmatch.regprog != NULL)
7000 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007001 // make a copy of the line, when searching for a mark it might be
7002 // flushed
7003 char_u *line = vim_strsave(ml_get_curline());
7004
Bram Moolenaardf177f62005-02-22 08:39:57 +00007005 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007006 if (vim_regexec(&regmatch, line, (colnr_T)0))
7007 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007008 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007009 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007010 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007011 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007012 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007013 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007014 eap->arg += STRLEN(eap->arg);
7015 }
7016 check_cursor();
7017
7018 eap->cmdidx = CMD_visual;
7019 do_exedit(eap, NULL);
7020}
7021
7022/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007023 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 */
7025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007026ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 do_exedit(eap, NULL);
7029}
7030
7031/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007032 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007035do_exedit(
7036 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007037 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
7039 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007041 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007043 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7044 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007045 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 /*
7047 * ":vi" command ends Ex mode.
7048 */
7049 if (exmode_active && (eap->cmdidx == CMD_visual
7050 || eap->cmdidx == CMD_view))
7051 {
7052 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007053 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007055 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007056 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007057 if (global_busy)
7058 {
7059 int rd = RedrawingDisabled;
7060 int nwr = no_wait_return;
7061 int ms = msg_scroll;
7062#ifdef FEAT_GUI
7063 int he = hold_gui_events;
7064#endif
7065
7066 if (eap->nextcmd != NULL)
7067 {
7068 stuffReadbuff(eap->nextcmd);
7069 eap->nextcmd = NULL;
7070 }
7071
7072 if (exmode_was != EXMODE_VIM)
7073 settmode(TMODE_RAW);
7074 RedrawingDisabled = 0;
7075 no_wait_return = 0;
7076 need_wait_return = FALSE;
7077 msg_scroll = 0;
7078#ifdef FEAT_GUI
7079 hold_gui_events = 0;
7080#endif
7081 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007082 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007083
7084 main_loop(FALSE, TRUE);
7085
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007086 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007087 RedrawingDisabled = rd;
7088 no_wait_return = nwr;
7089 msg_scroll = ms;
7090#ifdef FEAT_GUI
7091 hold_gui_events = he;
7092#endif
7093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 }
7097
7098 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007099 || eap->cmdidx == CMD_tabnew
7100 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007101 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007103 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 setpcmark();
7105 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007106 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7107 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007109 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 || *eap->arg != NUL
7111#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007112 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113#endif
7114 )
7115 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007116 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
7117 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007118 if (*eap->arg != NUL && curbuf_locked())
7119 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007120
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 n = readonlymode;
7122 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7123 readonlymode = TRUE;
7124 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007125 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7126 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007127 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7128 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7130 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007131 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7133 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7134 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007135 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007137 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007138 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007139 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7140 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007141 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007143 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 if (old_curwin != NULL)
7145 {
7146 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007147 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007149#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007150 cleanup_T cs;
7151
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007152 // Reset the error/interrupt/exception state here so that
7153 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007154 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007155#endif
7156#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007158#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007159 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007160
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007161#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007162 // Restore the error/interrupt/exception state if not
7163 // discarded by a new aborting error, interrupt, or
7164 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007165 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
7168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 }
7170 else if (readonlymode && curbuf->b_nwindows == 1)
7171 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007172 // When editing an already visited buffer, 'readonly' won't be set
7173 // but the previous value is kept. With ":view" and ":sview" we
7174 // want the file to be readonly, except when another window is
7175 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 curbuf->b_p_ro = TRUE;
7177 }
7178 readonlymode = n;
7179 }
7180 else
7181 {
7182 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007183 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 if (n != curwin->w_arg_idx_invalid)
7187 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
7189
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 /*
7191 * if ":split file" worked, set alternate file name in old window to new
7192 * file
7193 */
7194 if (old_curwin != NULL
7195 && *eap->arg != NUL
7196 && curwin != old_curwin
7197 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007198 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007199 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
7202 ex_no_reprint = TRUE;
7203}
7204
7205#ifndef FEAT_GUI
7206/*
7207 * ":gui" and ":gvim" when there is no GUI.
7208 */
7209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007210ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007212 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213}
7214#endif
7215
Bram Moolenaar4f974752019-02-17 17:44:42 +01007216#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007218ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219{
7220 gui_make_tearoff(eap->arg);
7221}
7222#endif
7223
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007224#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7225 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007227ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007229# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7230 if (gui.in_use)
7231 gui_make_popup(eap->arg, eap->forceit);
7232# ifdef FEAT_TERM_POPUP_MENU
7233 else
7234# endif
7235# endif
7236# ifdef FEAT_TERM_POPUP_MENU
7237 pum_make_popup(eap->arg, eap->forceit);
7238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239}
7240#endif
7241
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007243ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
7245 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007246 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007248 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249}
7250
7251/*
7252 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7253 * offset.
7254 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007257ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007260 win_T *save_curwin = curwin;
7261 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 long topline;
7263 long y;
7264 linenr_T old_linenr = curwin->w_cursor.lnum;
7265
7266 setpcmark();
7267
7268 /*
7269 * determine max topline
7270 */
7271 if (curwin->w_p_scb)
7272 {
7273 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007274 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {
7276 if (wp->w_p_scb && wp->w_buffer)
7277 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007278 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 if (topline > y)
7280 topline = y;
7281 }
7282 }
7283 if (topline < 1)
7284 topline = 1;
7285 }
7286 else
7287 {
7288 topline = 1;
7289 }
7290
7291
7292 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007293 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007295 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 {
7297 if (curwin->w_p_scb)
7298 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007299 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 y = topline - curwin->w_topline;
7301 if (y > 0)
7302 scrollup(y, TRUE);
7303 else
7304 scrolldown(-y, TRUE);
7305 curwin->w_scbind_pos = topline;
7306 redraw_later(VALID);
7307 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 }
7310 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007311 curwin = save_curwin;
7312 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 if (curwin->w_p_scb)
7314 {
7315 did_syncbind = TRUE;
7316 checkpcmark();
7317 if (old_linenr != curwin->w_cursor.lnum)
7318 {
7319 char_u ctrl_o[2];
7320
7321 ctrl_o[0] = Ctrl_O;
7322 ctrl_o[1] = 0;
7323 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7324 }
7325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326}
7327
7328
7329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007330ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007332 int i;
7333 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7334 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007336 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 do_bang(1, eap, FALSE, FALSE, TRUE);
7338 else
7339 {
7340 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7341 return;
7342
7343#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007344 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 {
7346 char_u *browseFile;
7347
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007348 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 NULL, NULL, NULL, curbuf);
7350 if (browseFile != NULL)
7351 {
7352 i = readfile(browseFile, NULL,
7353 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7354 vim_free(browseFile);
7355 }
7356 else
7357 i = OK;
7358 }
7359 else
7360#endif
7361 if (*eap->arg == NUL)
7362 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007363 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 return;
7365 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7366 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7367 }
7368 else
7369 {
7370 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7371 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7372 i = readfile(eap->arg, NULL,
7373 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7374
7375 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007376 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007378#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 if (!aborting())
7380#endif
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007381 semsg(_(e_cant_open_file_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 }
7383 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007384 {
7385 if (empty && exmode_active)
7386 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007387 // Delete the empty line that remains. Historically ex does
7388 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007389 if (eap->line2 == 0)
7390 lnum = curbuf->b_ml.ml_line_count;
7391 else
7392 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007393 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007394 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007395 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007396 if (curwin->w_cursor.lnum > 1
7397 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007398 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007399 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007400 }
7401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 }
7405}
7406
Bram Moolenaarea408852005-06-25 22:49:46 +00007407static char_u *prev_dir = NULL;
7408
7409#if defined(EXITFREE) || defined(PROTO)
7410 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007411free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007412{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007413 VIM_CLEAR(prev_dir);
7414 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007415}
7416#endif
7417
Bram Moolenaarf4258302013-06-02 18:20:17 +02007418/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007419 * Get the previous directory for the given chdir scope.
7420 */
7421 static char_u *
7422get_prevdir(cdscope_T scope)
7423{
7424 if (scope == CDSCOPE_WINDOW)
7425 return curwin->w_prevdir;
7426 else if (scope == CDSCOPE_TABPAGE)
7427 return curtab->tp_prevdir;
7428 return prev_dir;
7429}
7430
7431/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007432 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007433 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7434 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007435 */
7436 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007437post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007438{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007439 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007440 // Clear tab local directory for both :cd and :tcd
7441 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007442 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007443 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007444 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007445 char_u *pdir = get_prevdir(scope);
7446
7447 // If still in the global directory, need to remember current
7448 // directory as the global directory.
7449 if (globaldir == NULL && pdir != NULL)
7450 globaldir = vim_strsave(pdir);
7451
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007452 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007453 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007454 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007455 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007456 curtab->tp_localdir = vim_strsave(NameBuff);
7457 else
7458 curwin->w_localdir = vim_strsave(NameBuff);
7459 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007460 }
7461 else
7462 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007463 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007464 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007465 }
7466
zeertzjq64be6aa2021-11-19 11:59:08 +00007467 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007468 shorten_fnames(TRUE);
7469}
7470
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007471/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007472 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7473 */
7474 void
7475trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7476{
7477#ifdef FEAT_EVAL
7478 dict_T *v_event;
7479 save_v_event_T save_v_event;
7480
7481 v_event = get_v_event(&save_v_event);
7482 (void)dict_add_string(v_event, "directory", new_dir);
7483 dict_set_items_ro(v_event);
7484#endif
7485 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7486#ifdef FEAT_EVAL
7487 restore_v_event(v_event, &save_v_event);
7488#endif
7489}
7490
7491/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007492 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007493 * chdir() function.
7494 * scope == CDSCOPE_WINDOW: changes the window-local directory
7495 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7496 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007497 * Returns TRUE if the directory is successfully changed.
7498 */
7499 int
7500changedir_func(
7501 char_u *new_dir,
7502 int forceit,
7503 cdscope_T scope)
7504{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007505 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007506 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007507 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007508 char_u **pp;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007509
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007510 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007511 return FALSE;
7512
7513 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7514 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007515 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007516 return FALSE;
7517 }
7518
7519 // ":cd -": Change to previous directory
7520 if (STRCMP(new_dir, "-") == 0)
7521 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007522 pdir = get_prevdir(scope);
7523 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007524 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007525 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007526 return FALSE;
7527 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007528 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007529 }
7530
7531 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007532 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007533 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007534 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007535 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007536
Bakudankun29f3a452021-12-11 12:28:08 +00007537 // For UNIX ":cd" means: go to home directory.
7538 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007539#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007540 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007541#else
7542 if (*new_dir == NUL && p_cdh)
7543#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007544 {
7545 // use NameBuff for home directory name
7546# ifdef VMS
7547 char_u *p;
7548
7549 p = mch_getenv((char_u *)"SYS$LOGIN");
7550 if (p == NULL || *p == NUL) // empty is the same as not set
7551 NameBuff[0] = NUL;
7552 else
7553 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7554# else
7555 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7556# endif
7557 new_dir = NameBuff;
7558 }
zeertzjqf38aad82021-12-30 13:45:57 +00007559 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007560 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7561 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007562 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007563 if (scope == CDSCOPE_WINDOW)
7564 acmd_fname = (char_u *)"window";
7565 else if (scope == CDSCOPE_TABPAGE)
7566 acmd_fname = (char_u *)"tabpage";
7567 else
7568 acmd_fname = (char_u *)"global";
7569 trigger_DirChangedPre(acmd_fname, new_dir);
7570
7571 if (vim_chdir(new_dir))
7572 {
7573 emsg(_(e_command_failed));
7574 vim_free(pdir);
7575 return FALSE;
7576 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007577 }
zeertzjq73257142022-02-02 13:16:37 +00007578
7579 if (scope == CDSCOPE_WINDOW)
7580 pp = &curwin->w_prevdir;
7581 else if (scope == CDSCOPE_TABPAGE)
7582 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007583 else
zeertzjq73257142022-02-02 13:16:37 +00007584 pp = &prev_dir;
7585 vim_free(*pp);
7586 *pp = pdir;
7587
7588 post_chdir(scope);
7589
7590 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007591 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
zeertzjq73257142022-02-02 13:16:37 +00007592 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007593}
Bram Moolenaarea408852005-06-25 22:49:46 +00007594
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007596 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007598 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007599ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007601 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602
7603 new_dir = eap->arg;
7604#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007605 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7606 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 ex_pwd(NULL);
7608 else
7609#endif
7610 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007611 cdscope_T scope = CDSCOPE_GLOBAL;
7612
7613 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7614 scope = CDSCOPE_WINDOW;
7615 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7616 scope = CDSCOPE_TABPAGE;
7617
7618 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007619 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007620 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007621 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 ex_pwd(eap);
7623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 }
7625}
7626
7627/*
7628 * ":pwd".
7629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007631ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632{
7633 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7634 {
7635#ifdef BACKSLASH_IN_FILENAME
7636 slash_adjust(NameBuff);
7637#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007638 if (p_verbose > 0)
7639 {
7640 char *context = "global";
7641
Bram Moolenaar05268152021-11-18 18:53:45 +00007642 if (last_chdir_reason != NULL)
7643 context = last_chdir_reason;
7644 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007645 context = "window";
7646 else if (curtab->tp_localdir != NULL)
7647 context = "tabpage";
7648 smsg("[%s] %s", context, (char *)NameBuff);
7649 }
7650 else
7651 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 }
7653 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007654 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655}
7656
7657/*
7658 * ":=".
7659 */
7660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007661ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007663 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007664 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665}
7666
7667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007668ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007670 int n;
7671 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
7673 if (cursor_valid())
7674 {
7675 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7676 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007677 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007679
7680 len = eap->line2;
7681 switch (*eap->arg)
7682 {
7683 case 'm': break;
7684 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007685 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007686 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007687
7688 // Hide the cursor if invoked with !
7689 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690}
7691
7692/*
7693 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007694 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 */
7696 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007697do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
Bram Moolenaar52953192019-08-16 10:27:13 +02007699 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007700 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007701# ifdef ELAPSED_FUNC
7702 elapsed_T start_tv;
7703
7704 // Remember at what time we started, so that we know how much longer we
7705 // should wait after waiting for a bit.
7706 ELAPSED_INIT(start_tv);
7707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007709 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007710 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007711 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007712 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007713
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007714 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007715 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007717 wait_now = msec - done > 1000L ? 1000L : msec - done;
7718#ifdef FEAT_TIMERS
7719 {
7720 long due_time = check_due_timer();
7721
7722 if (due_time > 0 && due_time < wait_now)
7723 wait_now = due_time;
7724 }
7725#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007726#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007727 if (has_any_channel() && wait_now > 20L)
7728 wait_now = 20L;
7729#endif
7730#ifdef FEAT_SOUND
7731 if (has_any_sound_callback() && wait_now > 20L)
7732 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007733#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007734 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007735
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007736#ifdef FEAT_JOB_CHANNEL
7737 if (has_any_channel())
7738 ui_breakcheck_force(TRUE);
7739 else
7740#endif
7741 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007742#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007743 // Process the netbeans and clientserver messages that may have been
7744 // received in the call to ui_breakcheck() when the GUI is in use. This
7745 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007746 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007747#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007748
7749# ifdef ELAPSED_FUNC
7750 // actual time passed
7751 done = ELAPSED_FUNC(start_tv);
7752# else
7753 // guestimate time passed (will actually be more)
7754 done += wait_now;
7755# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007757
7758 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7759 // input buffer, otherwise a following call to input() fails.
7760 if (got_int)
7761 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007762
7763 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007764 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765}
7766
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767/*
7768 * ":winsize" command (obsolete).
7769 */
7770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007771ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773 int w, h;
7774 char_u *arg = eap->arg;
7775 char_u *p;
7776
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007777 if (!isdigit(*arg))
7778 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007779 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007780 return;
7781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 w = getdigits(&arg);
7783 arg = skipwhite(arg);
7784 p = arg;
7785 h = getdigits(&arg);
7786 if (*p != NUL && *arg == NUL)
7787 set_shellsize(w, h, TRUE);
7788 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007789 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790}
7791
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007793ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
7795 int xchar = NUL;
7796 char_u *p;
7797
7798 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7799 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007800 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 if (eap->arg[1] == NUL)
7802 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007803 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 return;
7805 }
7806 xchar = eap->arg[1];
7807 p = eap->arg + 2;
7808 }
7809 else
7810 p = eap->arg + 1;
7811
Bram Moolenaar63b91732021-08-05 20:40:03 +02007812 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007814 if (*p != NUL && *p != (
7815#ifdef FEAT_EVAL
7816 in_vim9script() ? '#' :
7817#endif
7818 '"')
7819 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007820 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007821 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007823 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007824 postponed_split_flags = cmdmod.cmod_split;
7825 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7827 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007828 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831
Bram Moolenaar843ee412004-06-30 16:16:41 +00007832#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833/*
7834 * ":winpos".
7835 */
7836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007837ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
7839 int x, y;
7840 char_u *arg = eap->arg;
7841 char_u *p;
7842
7843 if (*arg == NUL)
7844 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007845# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007846# ifdef VIMDLL
7847 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7848 mch_get_winpos(&x, &y) != FAIL)
7849# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007851# else
7852 if (mch_get_winpos(&x, &y) != FAIL)
7853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {
7855 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007856 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 }
7858 else
7859# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007860 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 }
7862 else
7863 {
7864 x = getdigits(&arg);
7865 arg = skipwhite(arg);
7866 p = arg;
7867 y = getdigits(&arg);
7868 if (*p == NUL || *arg != NUL)
7869 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007870 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 return;
7872 }
7873# ifdef FEAT_GUI
7874 if (gui.in_use)
7875 gui_mch_set_winpos(x, y);
7876 else if (gui.starting)
7877 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007878 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 gui_win_x = x;
7880 gui_win_y = y;
7881 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007882# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 else
7884# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007885# endif
7886# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007887 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888# endif
7889# ifdef HAVE_TGETENT
7890 if (*T_CWP)
7891 term_set_winpos(x, y);
7892# endif
7893 }
7894}
7895#endif
7896
7897/*
7898 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7899 */
7900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007901ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
7903 oparg_T oa;
7904
7905 clear_oparg(&oa);
7906 oa.regname = eap->regname;
7907 oa.start.lnum = eap->line1;
7908 oa.end.lnum = eap->line2;
7909 oa.line_count = eap->line2 - eap->line1 + 1;
7910 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007912 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
7914 setpcmark();
7915 curwin->w_cursor.lnum = eap->line1;
7916 beginline(BL_SOL | BL_FIX);
7917 }
7918
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007919 if (VIsual_active)
7920 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007921
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 switch (eap->cmdidx)
7923 {
7924 case CMD_delete:
7925 oa.op_type = OP_DELETE;
7926 op_delete(&oa);
7927 break;
7928
7929 case CMD_yank:
7930 oa.op_type = OP_YANK;
7931 (void)op_yank(&oa, FALSE, TRUE);
7932 break;
7933
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007934 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007935 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007937 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7938#else
7939 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007941 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 oa.op_type = OP_RSHIFT;
7943 else
7944 oa.op_type = OP_LSHIFT;
7945 op_shift(&oa, FALSE, eap->amount);
7946 break;
7947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007949 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950}
7951
7952/*
7953 * ":put".
7954 */
7955 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007956ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007958 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 if (eap->line2 == 0)
7960 {
7961 eap->line2 = 1;
7962 eap->forceit = TRUE;
7963 }
7964 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00007965 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007966 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007967 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968}
7969
7970/*
7971 * Handle ":copy" and ":move".
7972 */
7973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007974ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975{
7976 long n;
7977
Bram Moolenaar491799b2020-08-01 19:23:43 +02007978#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007979 if (not_in_vim9(eap) == FAIL)
7980 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007981#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007982 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007983 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {
7985 eap->nextcmd = NULL;
7986 return;
7987 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007988 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990 /*
7991 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7992 */
7993 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7994 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007995 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 return;
7997 }
7998
7999 if (eap->cmdidx == CMD_move)
8000 {
8001 if (do_move(eap->line1, eap->line2, n) == FAIL)
8002 return;
8003 }
8004 else
8005 ex_copy(eap->line1, eap->line2, n);
8006 u_clearline();
8007 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008008 ex_may_print(eap);
8009}
8010
8011/*
8012 * Print the current line if flags were given to the Ex command.
8013 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008014 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008015ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008016{
8017 if (eap->flags != 0)
8018 {
8019 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8020 (eap->flags & EXFLAG_LIST));
8021 ex_no_reprint = TRUE;
8022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023}
8024
8025/*
8026 * ":smagic" and ":snomagic".
8027 */
8028 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008029ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008031 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008033 magic_overruled = eap->cmdidx == CMD_smagic
8034 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008035 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008036 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037}
8038
8039/*
8040 * ":join".
8041 */
8042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008043ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
8045 curwin->w_cursor.lnum = eap->line1;
8046 if (eap->line1 == eap->line2)
8047 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008048 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 return;
8050 if (eap->line2 == curbuf->b_ml.ml_line_count)
8051 {
8052 beep_flush();
8053 return;
8054 }
8055 ++eap->line2;
8056 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008057 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008059 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060}
8061
8062/*
8063 * ":[addr]@r" or ":[addr]*r": execute register
8064 */
8065 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008066ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067{
8068 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008069 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070
8071 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008072 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073
8074#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008075 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076#endif
8077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008078 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 c = *eap->arg;
8080 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8081 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008082 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008083 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8084 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 else
8089 {
8090 int save_efr = exec_from_reg;
8091
8092 exec_from_reg = TRUE;
8093
8094 /*
8095 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008096 * Continue until the stuff buffer is empty and all added characters
8097 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008099 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8101
8102 exec_from_reg = save_efr;
8103 }
8104}
8105
8106/*
8107 * ":!".
8108 */
8109 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008110ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111{
8112 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8113}
8114
8115/*
8116 * ":undo".
8117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008119ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008121 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008122 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008123 else
8124 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125}
8126
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008127#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008129ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008130{
8131 char_u hash[UNDO_HASH_SIZE];
8132
8133 u_compute_hash(hash);
8134 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8135}
8136
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008138ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008139{
8140 char_u hash[UNDO_HASH_SIZE];
8141
8142 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008143 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008144}
8145#endif
8146
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147/*
8148 * ":redo".
8149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008151ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152{
8153 u_redo(1);
8154}
8155
8156/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008157 * ":earlier" and ":later".
8158 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008160ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008161{
8162 long count = 0;
8163 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008164 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008165 char_u *p = eap->arg;
8166
8167 if (*p == NUL)
8168 count = 1;
8169 else if (isdigit(*p))
8170 {
8171 count = getdigits(&p);
8172 switch (*p)
8173 {
8174 case 's': ++p; sec = TRUE; break;
8175 case 'm': ++p; sec = TRUE; count *= 60; break;
8176 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008177 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8178 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008179 }
8180 }
8181
8182 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008183 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008184 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008185 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8186 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008187}
8188
8189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 * ":redir": start/stop redirection.
8191 */
8192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008193ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194{
8195 char *mode;
8196 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008197 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198
Bram Moolenaarba768492016-07-08 15:32:54 +02008199#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008200 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008201 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008202 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008203 return;
8204 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008205#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008206
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 if (STRICMP(eap->arg, "END") == 0)
8208 close_redir();
8209 else
8210 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008211 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008213 ++arg;
8214 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008216 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 mode = "a";
8218 }
8219 else
8220 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008221 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222
8223 close_redir();
8224
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008225 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008226 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 if (fname == NULL)
8228 return;
8229#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008230 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {
8232 char_u *browseFile;
8233
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008234 browseFile = do_browse(BROWSE_SAVE,
8235 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008236 fname, NULL, NULL,
8237 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008239 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 vim_free(fname);
8241 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008242 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 }
8244#endif
8245
8246 redir_fd = open_exfile(fname, eap->forceit, mode);
8247 vim_free(fname);
8248 }
8249#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008250 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008252 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008254 ++arg;
8255 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008257 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008258 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008260 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008262 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008263 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008264 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008265 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008267 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008268 if (*arg == '>')
8269 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008270 // Make register empty when not using @A-@Z and the
8271 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008272 if (*arg == NUL && !isupper(redir_reg))
8273 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008275 }
8276 if (*arg != NUL)
8277 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008278 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008279 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008280 }
8281 }
8282 else if (*arg == '=' && arg[1] == '>')
8283 {
8284 int append;
8285
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008286 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008287 close_redir();
8288 arg += 2;
8289
8290 if (*arg == '>')
8291 {
8292 ++arg;
8293 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 }
8295 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008296 append = FALSE;
8297
8298 if (var_redir_start(skipwhite(arg), append) == OK)
8299 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 }
8301#endif
8302
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008303 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008306 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008308
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008309 // Make sure redirection is not off. Can happen for cmdline completion
8310 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008311 if (redir_fd != NULL
8312#ifdef FEAT_EVAL
8313 || redir_reg || redir_vname
8314#endif
8315 )
8316 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317}
8318
8319/*
8320 * ":redraw": force redraw
8321 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008322 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008323ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324{
8325 int r = RedrawingDisabled;
8326 int p = p_lz;
8327
8328 RedrawingDisabled = 0;
8329 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008330 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008332 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 if (need_maketitle)
8334 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008335#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8336# ifdef VIMDLL
8337 if (!gui.in_use)
8338# endif
8339 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 RedrawingDisabled = r;
8342 p_lz = p;
8343
Bram Moolenaar5017c662022-04-07 18:06:08 +01008344 // After drawing the statusline screen_attr may still be set.
8345 screen_stop_highlight();
8346
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008347 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 msg_didout = FALSE;
8349 msg_col = 0;
8350
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008351 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008352 need_wait_return = FALSE;
8353
Bram Moolenaara653e532022-04-19 11:38:24 +01008354 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008355 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008356 redrawcmdline();
8357
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 out_flush();
8359}
8360
8361/*
8362 * ":redrawstatus": force redraw of status line(s)
8363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008365ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 int r = RedrawingDisabled;
8368 int p = p_lz;
8369
8370 RedrawingDisabled = 0;
8371 p_lz = FALSE;
8372 if (eap->forceit)
8373 status_redraw_all();
8374 else
8375 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008376 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 RedrawingDisabled = r;
8378 p_lz = p;
8379 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380}
8381
Bram Moolenaare12bab32019-01-08 22:02:56 +01008382/*
8383 * ":redrawtabline": force redraw of the tabline
8384 */
8385 static void
8386ex_redrawtabline(exarg_T *eap UNUSED)
8387{
8388 int r = RedrawingDisabled;
8389 int p = p_lz;
8390
8391 RedrawingDisabled = 0;
8392 p_lz = FALSE;
8393
8394 draw_tabline();
8395
8396 RedrawingDisabled = r;
8397 p_lz = p;
8398 out_flush();
8399}
8400
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008402close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 if (redir_fd != NULL)
8405 {
8406 fclose(redir_fd);
8407 redir_fd = NULL;
8408 }
8409#ifdef FEAT_EVAL
8410 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008411 if (redir_vname)
8412 {
8413 var_redir_stop();
8414 redir_vname = 0;
8415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416#endif
8417}
8418
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008419#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008420 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008421vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008422{
8423 if (vim_mkdir(name, prot) != 0)
8424 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008425 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008426 return FAIL;
8427 }
8428 return OK;
8429}
8430#endif
8431
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432/*
8433 * Open a file for writing for an Ex command, with some checks.
8434 * Return file descriptor, or NULL on failure.
8435 */
8436 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008437open_exfile(
8438 char_u *fname,
8439 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008440 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441{
8442 FILE *fd;
8443
8444#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008445 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 if (mch_isdir(fname))
8447 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008448 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 return NULL;
8450 }
8451#endif
8452 if (!forceit && *mode != 'a' && vim_fexists(fname))
8453 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008454 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 return NULL;
8456 }
8457
8458 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008459 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460
8461 return fd;
8462}
8463
8464/*
8465 * ":mark" and ":k".
8466 */
8467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008468ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469{
8470 pos_T pos;
8471
Bram Moolenaar10b94212021-02-19 21:42:57 +01008472#ifdef FEAT_EVAL
8473 if (not_in_vim9(eap) == FAIL)
8474 return;
8475#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008476 if (*eap->arg == NUL) // No argument?
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008477 emsg(_(e_argument_required));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008478 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar74409f62022-01-01 15:58:22 +00008479 semsg(_(e_trailing_characters_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 else
8481 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008482 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 curwin->w_cursor.lnum = eap->line2;
8484 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008485 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaar6d057012021-12-31 18:49:43 +00008486 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008487 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 }
8489}
8490
8491/*
8492 * Update w_topline, w_leftcol and the cursor position.
8493 */
8494 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008495update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008497 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 update_topline();
8499 if (!curwin->w_p_wrap)
8500 validate_cursor();
8501 update_curswant();
8502}
8503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008505 * Save the current State and go to Normal mode.
8506 * Return TRUE if the typeahead could be saved.
8507 */
8508 int
8509save_current_state(save_state_T *sst)
8510{
8511 sst->save_msg_scroll = msg_scroll;
8512 sst->save_restart_edit = restart_edit;
8513 sst->save_msg_didout = msg_didout;
8514 sst->save_State = State;
8515 sst->save_insertmode = p_im;
8516 sst->save_finish_op = finish_op;
8517 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008518 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008519 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008520
Bram Moolenaar4324d872020-12-01 20:12:24 +01008521 msg_scroll = FALSE; // no msg scrolling in Normal mode
8522 restart_edit = 0; // don't go to Insert mode
8523 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008524
Bram Moolenaara452b802020-12-01 21:47:59 +01008525 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008526 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008527
8528 /*
8529 * Save the current typeahead. This is required to allow using ":normal"
8530 * from an event handler and makes sure we don't hang when the argument
8531 * ends with half a command.
8532 */
8533 save_typeahead(&sst->tabuf);
8534 return sst->tabuf.typebuf_valid;
8535}
8536
8537 void
8538restore_current_state(save_state_T *sst)
8539{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008540 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008541 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008542
8543 msg_scroll = sst->save_msg_scroll;
8544 restart_edit = sst->save_restart_edit;
8545 p_im = sst->save_insertmode;
8546 finish_op = sst->save_finish_op;
8547 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008548 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008549 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008550 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008551 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008553 // Restore the state (needed when called from a function executed for
8554 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008555 State = sst->save_State;
8556#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008557 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008558#endif
8559}
8560
8561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 * ":normal[!] {commands}": Execute normal mode commands.
8563 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008564 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008565ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008567 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 char_u *arg = NULL;
8569 int l;
8570 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008572 if (ex_normal_lock > 0)
8573 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008574 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008575 return;
8576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 if (ex_normal_busy >= p_mmd)
8578 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008579 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 return;
8581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 /*
8584 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8585 * this for the K_SPECIAL leading byte, otherwise special keys will not
8586 * work.
8587 */
8588 if (has_mbyte)
8589 {
8590 int len = 0;
8591
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008592 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 for (p = eap->arg; *p != NUL; ++p)
8594 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008595#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008596 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008598#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008599 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008600 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008601#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 )
8605 len += 2;
8606 }
8607 if (len > 0)
8608 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008609 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 if (arg != NULL)
8611 {
8612 len = 0;
8613 for (p = eap->arg; *p != NUL; ++p)
8614 {
8615 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008616#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 if (*p == CSI)
8618 {
8619 arg[len++] = KS_EXTRA;
8620 arg[len++] = (int)KE_CSI;
8621 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008622#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008623 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 {
8625 arg[len++] = *++p;
8626 if (*p == K_SPECIAL)
8627 {
8628 arg[len++] = KS_SPECIAL;
8629 arg[len++] = KE_FILLER;
8630 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008631#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 else if (*p == CSI)
8633 {
8634 arg[len++] = KS_EXTRA;
8635 arg[len++] = (int)KE_CSI;
8636 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 }
8639 arg[len] = NUL;
8640 }
8641 }
8642 }
8643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008645 ++ex_normal_busy;
8646 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 {
8648 /*
8649 * Repeat the :normal command for each line in the range. When no
8650 * range given, execute it just once, without positioning the cursor
8651 * first.
8652 */
8653 do
8654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 if (eap->addr_count != 0)
8656 {
8657 curwin->w_cursor.lnum = eap->line1++;
8658 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008659 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 }
8661
Bram Moolenaar13505972019-01-24 15:04:48 +01008662 exec_normal_cmd(arg != NULL
8663 ? arg
8664 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 }
8666 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8667 }
8668
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008669 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 update_topline_cursor();
8671
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008672 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008674 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008675#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008676 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008677#endif
8678
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680}
8681
8682/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008683 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 */
8685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008686ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008688 if (eap->forceit)
8689 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008690 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008691 if (!curwin->w_cursor.lnum)
8692 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008693 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008694 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008695#ifdef FEAT_TERMINAL
8696 // Ignore this when running in an active terminal.
8697 if (term_job_running(curbuf->b_term))
8698 return;
8699#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008700
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008701 // Ignore the command when already in Insert mode. Inserting an
8702 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01008703 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00008704 return;
8705
Bram Moolenaar64969662005-12-14 21:59:55 +00008706 if (eap->cmdidx == CMD_startinsert)
8707 restart_edit = 'a';
8708 else if (eap->cmdidx == CMD_startreplace)
8709 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008711 restart_edit = 'V';
8712
8713 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008715 if (eap->cmdidx == CMD_startinsert)
8716 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008717 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008719
8720 if (VIsual_active)
8721 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722}
8723
8724/*
8725 * ":stopinsert"
8726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008728ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729{
8730 restart_edit = 0;
8731 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008732 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008735/*
8736 * Execute normal mode command "cmd".
8737 * "remap" can be REMAP_NONE or REMAP_YES.
8738 */
8739 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008740exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008741{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008742 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008743 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008744 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008745}
Bram Moolenaar25281632016-01-21 23:32:32 +01008746
Bram Moolenaar25281632016-01-21 23:32:32 +01008747/*
8748 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008749 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008750 */
8751 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008752exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008753{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008754 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008755 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008756
Bram Moolenaar905dd902019-04-07 14:21:47 +02008757 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8758 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008759 clear_oparg(&oa);
8760 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008761 while ((!stuff_empty()
8762 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008763 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008764 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008765 {
8766 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008767#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008768 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008769 && oa.op_type == OP_NOP && oa.regname == NUL
8770 && !VIsual_active)
8771 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008772 // If terminal_loop() returns OK we got a key that is handled
8773 // in Normal model. With FAIL we first need to position the
8774 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008775 if (terminal_loop(TRUE) == OK)
8776 normal_cmd(&oa, TRUE);
8777 }
8778 else
8779#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008780 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008781 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008782 }
8783}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008784
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785#ifdef FEAT_FIND_ID
8786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008787ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
8789 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8790 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8791 (linenr_T)1, (linenr_T)MAXLNUM);
8792}
8793
Bram Moolenaar4033c552017-09-16 20:54:51 +02008794#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795/*
8796 * ":psearch"
8797 */
8798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008799ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800{
8801 g_do_tagpreview = p_pvh;
8802 ex_findpat(eap);
8803 g_do_tagpreview = 0;
8804}
8805#endif
8806
8807 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008808ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809{
8810 int whole = TRUE;
8811 long n;
8812 char_u *p;
8813 int action;
8814
8815 switch (cmdnames[eap->cmdidx].cmd_name[2])
8816 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008817 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8819 action = ACTION_GOTO;
8820 else
8821 action = ACTION_SHOW;
8822 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008823 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 action = ACTION_SHOW_ALL;
8825 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008826 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 action = ACTION_GOTO;
8828 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008829 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 action = ACTION_SPLIT;
8831 break;
8832 }
8833
8834 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008835 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 {
8837 n = getdigits(&eap->arg);
8838 eap->arg = skipwhite(eap->arg);
8839 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008840 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 {
8842 whole = FALSE;
8843 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008844 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 if (*p)
8846 {
8847 *p++ = NUL;
8848 p = skipwhite(p);
8849
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008850 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008851 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008852 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008854 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 }
8856 }
8857 if (!eap->skip)
8858 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8859 whole, !eap->forceit,
8860 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8861 n, action, eap->line1, eap->line2);
8862}
8863#endif
8864
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865
Bram Moolenaar4033c552017-09-16 20:54:51 +02008866#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867/*
8868 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8869 */
8870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008871ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008873 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8875}
8876
8877/*
8878 * ":pedit"
8879 */
8880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008881ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882{
8883 win_T *curwin_save = curwin;
8884
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008885 if (ERROR_IF_ANY_POPUP_WINDOW)
8886 return;
8887
Bram Moolenaar026587b2019-08-17 15:08:00 +02008888 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008890 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008891
Bram Moolenaar026587b2019-08-17 15:08:00 +02008892 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008894
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 if (curwin != curwin_save && win_valid(curwin_save))
8896 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008897 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 validate_cursor();
8899 redraw_later(VALID);
8900 win_enter(curwin_save, TRUE);
8901 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008902# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008903 else if (WIN_IS_POPUP(curwin))
8904 {
8905 // can't keep focus in popup window
8906 win_enter(firstwin, TRUE);
8907 }
8908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 g_do_tagpreview = 0;
8910}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912
8913/*
8914 * ":stag", ":stselect" and ":stjump".
8915 */
8916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008917ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918{
8919 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008920 postponed_split_flags = cmdmod.cmod_split;
8921 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8923 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008924 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926
8927/*
8928 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8929 */
8930 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008931ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932{
8933 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8934}
8935
8936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008937ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938{
8939 int cmd;
8940
8941 switch (name[1])
8942 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008943 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008945 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008947 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008949 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008951 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008953 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008955 case 'f': // ":tfirst"
8956 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008958 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008960 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008962 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008964 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 return;
8966 }
8967#endif
8968 cmd = DT_TAG;
8969 break;
8970 }
8971
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008972 if (name[0] == 'l')
8973 {
8974#ifndef FEAT_QUICKFIX
8975 ex_ni(eap);
8976 return;
8977#else
8978 cmd = DT_LTAG;
8979#endif
8980 }
8981
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8983 eap->forceit, TRUE);
8984}
8985
8986/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008987 * Check "str" for starting with a special cmdline variable.
8988 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8989 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8990 */
8991 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008992find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008993{
8994 int len;
8995 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008996 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008997 "%",
8998#define SPEC_PERC 0
8999 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009000#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009001 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02009002#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009003 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02009004#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009005 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02009006#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009007 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009008#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009009 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009010#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009011 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02009012#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02009013 "<stack>", // call stack
9014#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01009015 "<script>", // script file name
9016#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009017 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01009018#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009019 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009020#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009021 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009022#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009023 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009024#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02009025 "<SID>", // script ID: <SNR>123_
9026#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009027#ifdef FEAT_CLIENTSERVER
9028 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02009029# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009030#endif
9031 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009032
K.Takataeeec2542021-06-02 13:28:16 +02009033 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009034 {
9035 len = (int)STRLEN(spec_str[i]);
9036 if (STRNCMP(src, spec_str[i], len) == 0)
9037 {
9038 *usedlen = len;
9039 return i;
9040 }
9041 }
9042 return -1;
9043}
9044
9045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 * Evaluate cmdline variables.
9047 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009048 * change "%" to curbuf->b_ffname
9049 * "#" to curwin->w_alt_fnum
9050 * "%%" to curwin->w_alt_fnum in Vim9 script
9051 * "<cword>" to word under the cursor
9052 * "<cWORD>" to WORD under the cursor
9053 * "<cexpr>" to C-expression under the cursor
9054 * "<cfile>" to path name under the cursor
9055 * "<sfile>" to sourced file name
9056 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009057 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009058 * "<slnum>" to sourced file line number
9059 * "<afile>" to file name for autocommand
9060 * "<abuf>" to buffer number for autocommand
9061 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 *
9063 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9064 * "" for error without a message) and NULL is returned.
9065 * Returns an allocated string if a valid match was found.
9066 * Returns NULL if no match was found. "usedlen" then still contains the
9067 * number of characters to skip.
9068 */
9069 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009070eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009071 char_u *src, // pointer into commandline
9072 char_u *srcstart, // beginning of valid memory for src
9073 int *usedlen, // characters after src that are used
9074 linenr_T *lnump, // line number for :e command, or NULL
9075 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009076 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009077 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009078 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
9080 int i;
9081 char_u *s;
9082 char_u *result;
9083 char_u *resultbuf = NULL;
9084 int resultlen;
9085 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009086 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009088 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
9092 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009093 if (escaped != NULL)
9094 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095
9096 /*
9097 * Check if there is something to do.
9098 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009099 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009100 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 {
9102 *usedlen = 1;
9103 return NULL;
9104 }
9105
9106 /*
9107 * Skip when preceded with a backslash "\%" and "\#".
9108 * Note: In "\\%" the % is also not recognized!
9109 */
9110 if (src > srcstart && src[-1] == '\\')
9111 {
9112 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009113 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 return NULL;
9115 }
9116
9117 /*
9118 * word or WORD under cursor
9119 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009120 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9121 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009123 resultlen = find_ident_under_cursor(&result,
9124 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9125 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9126 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 if (resultlen == 0)
9128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009129 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 return NULL;
9131 }
9132 }
9133
9134 /*
9135 * '#': Alternate file name
9136 * '%': Current file name
9137 * File name under the cursor
9138 * File name for autocommand
9139 * and following modifiers
9140 */
9141 else
9142 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009143 int off = 0;
9144
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 switch (spec_idx)
9146 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009147 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009148#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009149 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009152 // '%': current file
9153 if (curbuf->b_fname == NULL)
9154 {
9155 result = (char_u *)"";
9156 valid = 0; // Must have ":p:h" to be valid
9157 }
9158 else
9159 {
9160 result = curbuf->b_fname;
9161 tilde_file = STRCMP(result, "~") == 0;
9162 }
9163 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009165#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009166 // "%%" alternate file
9167 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009168#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009169 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009170 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009171 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009173 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 result = arg_all();
9175 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009176 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009177 if (escaped != NULL)
9178 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 break;
9181 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009182 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009183 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009184 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009186 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009187 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009188 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009189 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009191 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009193 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009194 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009195 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009196 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009197 return NULL;
9198 }
9199#ifdef FEAT_EVAL
9200 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9201 (long)i);
9202 if (result == NULL)
9203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009204 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009205 return NULL;
9206 }
9207#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009208 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 }
9212 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009213 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009214 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9215 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009216 buf = buflist_findnr(i);
9217 if (buf == NULL)
9218 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009219 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009220 return NULL;
9221 }
9222 if (lnump != NULL)
9223 *lnump = ECMD_LAST;
9224 if (buf->b_fname == NULL)
9225 {
9226 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009227 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009228 }
9229 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009230 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009231 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009232 tilde_file = STRCMP(result, "~") == 0;
9233 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 break;
9236
9237#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009238 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009239 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 if (result == NULL)
9241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009242 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 return NULL;
9244 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009245 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 break;
9247#endif
9248
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009249 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009251 if (result != NULL && !autocmd_fname_full)
9252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009253 // Still need to turn the fname into a full path. It is
9254 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009255 autocmd_fname_full = TRUE;
9256 result = FullName_save(autocmd_fname, FALSE);
9257 vim_free(autocmd_fname);
9258 autocmd_fname = result;
9259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 if (result == NULL)
9261 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009262 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 return NULL;
9264 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009265 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 break;
9267
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009268 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 if (autocmd_bufnr <= 0)
9270 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009271 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 return NULL;
9273 }
9274 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9275 result = strbuf;
9276 break;
9277
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009278 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 result = autocmd_match;
9280 if (result == NULL)
9281 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009282 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 return NULL;
9284 }
9285 break;
9286
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009287 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009288 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 if (result == NULL)
9290 {
LemonBoy6013d002022-04-09 21:42:10 +01009291 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9292 return NULL;
9293 }
9294 resultbuf = result; // remember allocated string
9295 break;
9296 case SPEC_STACK: // call stack
9297 result = estack_sfile(ESTACK_STACK);
9298 if (result == NULL)
9299 {
9300 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9301 return NULL;
9302 }
9303 resultbuf = result; // remember allocated string
9304 break;
9305 case SPEC_SCRIPT: // script file name
9306 result = estack_sfile(ESTACK_SCRIPT);
9307 if (result == NULL)
9308 {
9309 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 return NULL;
9311 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009312 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009314
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009315 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009316 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009317 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009318 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009319 return NULL;
9320 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009321 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009322 result = strbuf;
9323 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009324
9325#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009326 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009327 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009328 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009329 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009330 return NULL;
9331 }
9332 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009333 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009334 result = strbuf;
9335 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009336
Bram Moolenaar90944302020-08-01 20:45:11 +02009337 case SPEC_SID:
9338 if (current_sctx.sc_sid <= 0)
9339 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009340 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009341 return NULL;
9342 }
9343 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9344 result = strbuf;
9345 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009346#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009347
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009348#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009349 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009350 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9351 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 result = strbuf;
9353 break;
9354#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009355
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009356 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009357 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009358 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 }
9360
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009361 resultlen = (int)STRLEN(result); // length of new string
9362 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 {
9364 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 resultlen = (int)(s - result);
9367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 else if (!skip_mod)
9369 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009370 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 &resultlen);
9372 if (result == NULL)
9373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009374 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 return NULL;
9376 }
9377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 }
9379
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009380 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009382 if (empty_is_error)
9383 {
9384 if (valid != VALID_HEAD + VALID_PATH)
9385 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9386 else
9387 *errormsg = _(e_evaluates_to_an_empty_string);
9388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 result = NULL;
9390 }
9391 else
9392 result = vim_strnsave(result, resultlen);
9393 vim_free(resultbuf);
9394 return result;
9395}
9396
9397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 * Expand the <sfile> string in "arg".
9399 *
9400 * Returns an allocated string, or NULL for any error.
9401 */
9402 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009403expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009405 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 int len;
9407 char_u *result;
9408 char_u *newres;
9409 char_u *repl;
9410 int srclen;
9411 char_u *p;
9412
9413 result = vim_strsave(arg);
9414 if (result == NULL)
9415 return NULL;
9416
9417 for (p = result; *p; )
9418 {
9419 if (STRNCMP(p, "<sfile>", 7) != 0)
9420 ++p;
9421 else
9422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009423 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009424 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 if (errormsg != NULL)
9426 {
9427 if (*errormsg)
9428 emsg(errormsg);
9429 vim_free(result);
9430 return NULL;
9431 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009432 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 {
9434 p += srclen;
9435 continue;
9436 }
9437 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9438 newres = alloc(len);
9439 if (newres == NULL)
9440 {
9441 vim_free(repl);
9442 vim_free(result);
9443 return NULL;
9444 }
9445 mch_memmove(newres, result, (size_t)(p - result));
9446 STRCPY(newres + (p - result), repl);
9447 len = (int)STRLEN(newres);
9448 STRCAT(newres, p + srclen);
9449 vim_free(repl);
9450 vim_free(result);
9451 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009452 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 }
9454 }
9455
9456 return result;
9457}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009460/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009461 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009462 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009465dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 if (fname == NULL)
9468 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009469 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470}
9471#endif
9472
9473/*
9474 * ":behave {mswin,xterm}"
9475 */
9476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009477ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478{
9479 if (STRCMP(eap->arg, "mswin") == 0)
9480 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009481 set_option_value_give_err((char_u *)"selection",
9482 0L, (char_u *)"exclusive", 0);
9483 set_option_value_give_err((char_u *)"selectmode",
9484 0L, (char_u *)"mouse,key", 0);
9485 set_option_value_give_err((char_u *)"mousemodel",
9486 0L, (char_u *)"popup", 0);
9487 set_option_value_give_err((char_u *)"keymodel",
9488 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 }
9490 else if (STRCMP(eap->arg, "xterm") == 0)
9491 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009492 set_option_value_give_err((char_u *)"selection",
9493 0L, (char_u *)"inclusive", 0);
9494 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9495 set_option_value_give_err((char_u *)"mousemodel",
9496 0L, (char_u *)"extend", 0);
9497 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 }
9499 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009500 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501}
9502
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503static int filetype_detect = FALSE;
9504static int filetype_plugin = FALSE;
9505static int filetype_indent = FALSE;
9506
9507/*
9508 * ":filetype [plugin] [indent] {on,off,detect}"
9509 * on: Load the filetype.vim file to install autocommands for file types.
9510 * off: Load the ftoff.vim file to remove all autocommands for file types.
9511 * plugin on: load filetype.vim and ftplugin.vim
9512 * plugin off: load ftplugof.vim
9513 * indent on: load filetype.vim and indent.vim
9514 * indent off: load indoff.vim
9515 */
9516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009517ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518{
9519 char_u *arg = eap->arg;
9520 int plugin = FALSE;
9521 int indent = FALSE;
9522
9523 if (*eap->arg == NUL)
9524 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009525 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009526 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 filetype_detect ? "ON" : "OFF",
9528 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9529 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9530 return;
9531 }
9532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009533 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 for (;;)
9535 {
9536 if (STRNCMP(arg, "plugin", 6) == 0)
9537 {
9538 plugin = TRUE;
9539 arg = skipwhite(arg + 6);
9540 continue;
9541 }
9542 if (STRNCMP(arg, "indent", 6) == 0)
9543 {
9544 indent = TRUE;
9545 arg = skipwhite(arg + 6);
9546 continue;
9547 }
9548 break;
9549 }
9550 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9551 {
9552 if (*arg == 'o' || !filetype_detect)
9553 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009554 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 filetype_detect = TRUE;
9556 if (plugin)
9557 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009558 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 filetype_plugin = TRUE;
9560 }
9561 if (indent)
9562 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009563 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 filetype_indent = TRUE;
9565 }
9566 }
9567 if (*arg == 'd')
9568 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009569 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009570 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 }
9572 }
9573 else if (STRCMP(arg, "off") == 0)
9574 {
9575 if (plugin || indent)
9576 {
9577 if (plugin)
9578 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009579 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 filetype_plugin = FALSE;
9581 }
9582 if (indent)
9583 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009584 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 filetype_indent = FALSE;
9586 }
9587 }
9588 else
9589 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009590 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 filetype_detect = FALSE;
9592 }
9593 }
9594 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009595 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596}
9597
9598/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009599 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 */
9601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009602ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603{
9604 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009605 {
9606 char_u *arg = eap->arg;
9607
9608 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9609 arg += 9;
9610
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009611 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
Bram Moolenaar3e545692017-06-04 19:00:32 +02009612 if (arg != eap->arg)
9613 did_filetype = FALSE;
9614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009618ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619{
9620#ifdef FEAT_DIGRAPHS
9621 if (*eap->arg != NUL)
9622 putdigraph(eap->arg);
9623 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009624 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625#else
mityu61065042021-07-19 20:07:21 +02009626 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627#endif
9628}
9629
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009630#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9631 void
9632set_no_hlsearch(int flag)
9633{
9634 no_hlsearch = flag;
9635# ifdef FEAT_EVAL
9636 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9637# endif
9638}
9639
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640/*
9641 * ":nohlsearch"
9642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009644ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009646 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009647 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649#endif
9650
9651#ifdef FEAT_CRYPT
9652/*
9653 * ":X": Get crypt key
9654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009656ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009658 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009659 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660}
9661#endif
9662
9663#ifdef FEAT_FOLDING
9664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009665ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666{
9667 if (foldManualAllowed(TRUE))
9668 foldCreate(eap->line1, eap->line2);
9669}
9670
9671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009672ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673{
9674 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9675 eap->forceit, FALSE);
9676}
9677
9678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009679ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680{
9681 linenr_T lnum;
9682
Bram Moolenaar4facea32019-10-12 20:17:40 +02009683# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009684 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009685# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009686
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009687 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9689 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9690 ml_setmarked(lnum);
9691
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009692 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009694 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009695# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009696 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698}
9699#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009700
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009701#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009702/*
9703 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9704 * instead of a quickfix command.
9705 */
9706 int
9707is_loclist_cmd(int cmdidx)
9708{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009709 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009710 return FALSE;
9711 return cmdnames[cmdidx].cmd_name[0] == 'l';
9712}
9713#endif
9714
Bram Moolenaar4facea32019-10-12 20:17:40 +02009715#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009716 int
9717get_pressedreturn(void)
9718{
9719 return ex_pressedreturn;
9720}
9721
9722 void
9723set_pressedreturn(int val)
9724{
9725 ex_pressedreturn = val;
9726}
9727#endif