blob: fde68be723aab85b6469c63ed1bf4ce35dbf3386 [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);
112static void ex_stop(exarg_T *eap);
113static void ex_exit(exarg_T *eap);
114static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100116static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#else
118# define ex_goto ex_ni
119#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_shell(exarg_T *eap);
121static void ex_preserve(exarg_T *eap);
122static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_mode(exarg_T *eap);
124static void ex_wrongmodifier(exarg_T *eap);
125static void ex_find(exarg_T *eap);
126static void ex_open(exarg_T *eap);
127static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#ifndef FEAT_GUI
129# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100132#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#else
135# define ex_tearoff ex_ni
136#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100137#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
138 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_popup ex_ni
142#endif
143#ifndef FEAT_GUI_MSWIN
144# define ex_simalt ex_ni
145#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000146#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define gui_mch_find_dialog ex_ni
148# define gui_mch_replace_dialog ex_ni
149#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000150#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define ex_helpfind ex_ni
152#endif
153#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100154# define ex_cscope ex_ni
155# define ex_scscope ex_ni
156# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifndef FEAT_SYN_HL
159# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200160# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000161#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200162#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200163# define ex_syntime ex_ni
164#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000165#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000166# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000167# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000168# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000169# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000170# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000171#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200172#ifndef FEAT_PERSISTENT_UNDO
173# define ex_rundo ex_ni
174# define ex_wundo ex_ni
175#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176#ifndef FEAT_LUA
177# define ex_lua ex_script_ni
178# define ex_luado ex_ni
179# define ex_luafile ex_ni
180#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000181#ifndef FEAT_MZSCHEME
182# define ex_mzscheme ex_script_ni
183# define ex_mzfile ex_ni
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifndef FEAT_PERL
186# define ex_perl ex_script_ni
187# define ex_perldo ex_ni
188#endif
189#ifndef FEAT_PYTHON
190# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200191# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define ex_pyfile ex_ni
193#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200195# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200196# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define ex_py3file ex_ni
198#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100199#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
200# define ex_pyx ex_script_ni
201# define ex_pyxdo ex_ni
202# define ex_pyxfile ex_ni
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_TCL
205# define ex_tcl ex_script_ni
206# define ex_tcldo ex_ni
207# define ex_tclfile ex_ni
208#endif
209#ifndef FEAT_RUBY
210# define ex_ruby ex_script_ni
211# define ex_rubydo ex_ni
212# define ex_rubyfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_KEYMAP
215# define ex_loadkeymap ex_ni
216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_swapname(exarg_T *eap);
218static void ex_syncbind(exarg_T *eap);
219static void ex_read(exarg_T *eap);
220static void ex_pwd(exarg_T *eap);
221static void ex_equal(exarg_T *eap);
222static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_winpos ex_ni
229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_operators(exarg_T *eap);
231static void ex_put(exarg_T *eap);
232static void ex_copymove(exarg_T *eap);
233static void ex_submagic(exarg_T *eap);
234static void ex_join(exarg_T *eap);
235static void ex_at(exarg_T *eap);
236static void ex_bang(exarg_T *eap);
237static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_wundo(exarg_T *eap);
240static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redo(exarg_T *eap);
243static void ex_later(exarg_T *eap);
244static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100246static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_startinsert(exarg_T *eap);
250static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_checkpath(exarg_T *eap);
253static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#else
255# define ex_findpat ex_ni
256# define ex_checkpath ex_ni
257#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200258#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#else
261# define ex_psearch ex_ni
262#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_tag(exarg_T *eap);
264static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200266# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200267# define ex_break ex_ni
268# define ex_breakadd ex_ni
269# define ex_breakdel ex_ni
270# define ex_breaklist ex_ni
271# define ex_call ex_ni
272# define ex_catch ex_ni
273# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200274# define ex_continue ex_ni
275# define ex_debug ex_ni
276# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200278# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_echo ex_ni
281# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200283# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200284# define ex_endfunction ex_ni
285# define ex_endif ex_ni
286# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200288# define ex_eval ex_ni
289# define ex_execute ex_ni
Bram Moolenaar340c59e2021-04-24 20:37:03 +0200290# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200291# define ex_finally ex_ni
292# define ex_finish ex_ni
293# define ex_function ex_ni
294# define ex_if ex_ni
295# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200296# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200297# define ex_lockvar ex_ni
298# define ex_oldfiles ex_ni
299# define ex_options ex_ni
300# define ex_packadd ex_ni
301# define ex_packloadall ex_ni
302# define ex_return ex_ni
303# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# define ex_throw ex_ni
305# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000307# define ex_unlockvar ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200308# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309# define ex_import ex_ni
310# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200312#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313# define ex_loadview ex_ni
314#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200315#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316# define ex_viminfo ex_ni
317#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_filetype(exarg_T *eap);
320static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000322# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323# define ex_diffpatch ex_ni
324# define ex_diffgetput ex_ni
325# define ex_diffsplit ex_ni
326# define ex_diffthis ex_ni
327# define ex_diffupdate ex_ni
328#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100331static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#else
333# define ex_nohlsearch ex_ni
334# define ex_match ex_ni
335#endif
336#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#else
339# define ex_X ex_ni
340#endif
341#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_fold(exarg_T *eap);
343static void ex_foldopen(exarg_T *eap);
344static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#else
346# define ex_fold ex_ni
347# define ex_foldopen ex_ni
348# define ex_folddo ex_ni
349#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100350#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351# define ex_language ex_ni
352#endif
353#ifndef FEAT_SIGNS
354# define ex_sign ex_ni
355#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200359# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifndef FEAT_JUMPLIST
363# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200364# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365# define ex_changes ex_ni
366#endif
367
Bram Moolenaar05159a02005-02-26 23:04:13 +0000368#ifndef FEAT_PROFILE
369# define ex_profile ex_ni
370#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200371#ifndef FEAT_TERMINAL
372# define ex_terminal ex_ni
373#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200374#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
375# define ex_xrestore ex_ni
376#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100377#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200378# define ex_popupclear ex_ni
379#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381/*
382 * Declare cmdnames[].
383 */
384#define DO_DECLARE_EXCMD
385#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200386#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100387
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static char_u dollar_command[2] = {'$', 0};
389
390
391#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100392// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393typedef struct
394{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100395 char_u *line; // command line
396 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397} wcmd_T;
398
399/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000402 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000404struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100406 garray_T *lines_gap; // growarray with line info
407 int current_line; // last read line from growarray
408 int repeating; // TRUE when looping a second time
409 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200410 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 void *cookie;
412};
413
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200414static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100415static int store_loop_line(garray_T *gap, char_u *line);
416static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000417
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100418// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000419struct dbg_stuff
420{
421 int trylevel;
422 int force_abort;
423 except_T *caught_stack;
424 char_u *vv_exception;
425 char_u *vv_throwpoint;
426 int did_emsg;
427 int got_int;
428 int did_throw;
429 int need_rethrow;
430 int check_cstack;
431 except_T *current_exception;
432};
433
Bram Moolenaared203462004-06-16 11:19:22 +0000434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100435save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000436{
437 dsp->trylevel = trylevel; trylevel = 0;
438 dsp->force_abort = force_abort; force_abort = FALSE;
439 dsp->caught_stack = caught_stack; caught_stack = NULL;
440 dsp->vv_exception = v_exception(NULL);
441 dsp->vv_throwpoint = v_throwpoint(NULL);
442
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100443 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000444 dsp->did_emsg = did_emsg; did_emsg = FALSE;
445 dsp->got_int = got_int; got_int = FALSE;
446 dsp->did_throw = did_throw; did_throw = FALSE;
447 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
448 dsp->check_cstack = check_cstack; check_cstack = FALSE;
449 dsp->current_exception = current_exception; current_exception = NULL;
450}
451
452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100453restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000454{
455 suppress_errthrow = FALSE;
456 trylevel = dsp->trylevel;
457 force_abort = dsp->force_abort;
458 caught_stack = dsp->caught_stack;
459 (void)v_exception(dsp->vv_exception);
460 (void)v_throwpoint(dsp->vv_throwpoint);
461 did_emsg = dsp->did_emsg;
462 got_int = dsp->got_int;
463 did_throw = dsp->did_throw;
464 need_rethrow = dsp->need_rethrow;
465 check_cstack = dsp->check_cstack;
466 current_exception = dsp->current_exception;
467}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#endif
469
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470/*
471 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
472 * command is given.
473 */
474 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100475do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100476 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477{
478 int save_msg_scroll;
479 int prev_msg_row;
480 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100481 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482
483 if (improved)
484 exmode_active = EXMODE_VIM;
485 else
486 exmode_active = EXMODE_NORMAL;
487 State = NORMAL;
488
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100489 // When using ":global /pat/ visual" and then "Q" we return to continue
490 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000491 if (global_busy)
492 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
494 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100495 ++RedrawingDisabled; // don't redisplay the window
496 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100498 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 ++hold_gui_events;
500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
Bram Moolenaar32526b32019-01-19 17:43:09 +0100502 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 while (exmode_active)
504 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100505 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000506 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
507 {
508 exmode_active = FALSE;
509 break;
510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 msg_scroll = TRUE;
512 need_wait_return = FALSE;
513 ex_pressedreturn = FALSE;
514 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100515 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 prev_msg_row = msg_row;
517 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (improved)
519 {
520 cmdline_row = msg_row;
521 do_cmdline(NULL, getexline, NULL, 0);
522 }
523 else
524 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
525 lines_left = Rows - 1;
526
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100528 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100531 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 if (ex_pressedreturn)
535 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100536 // go up one line, to overwrite the ":<CR>" line, so the
537 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000538 msg_row = prev_msg_row;
539 if (prev_msg_row == Rows - 1)
540 msg_row--;
541 }
542 msg_col = 0;
543 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
544 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100547 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 {
549 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100550 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100552 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 }
555
556#ifdef FEAT_GUI
557 --hold_gui_events;
558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 --RedrawingDisabled;
560 --no_wait_return;
561 update_screen(CLEAR);
562 need_wait_return = FALSE;
563 msg_scroll = save_msg_scroll;
564}
565
566/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200567 * Print the executed command for when 'verbose' is set.
568 * When "lnum" is 0 only print the command.
569 */
570 static void
571msg_verbose_cmd(linenr_T lnum, char_u *cmd)
572{
573 ++no_wait_return;
574 verbose_enter_scroll();
575
576 if (lnum == 0)
577 smsg(_("Executing: %s"), cmd);
578 else
579 smsg(_("line %ld: %s"), (long)lnum, cmd);
580 if (msg_silent == 0)
581 msg_puts("\n"); // don't overwrite this
582
583 verbose_leave_scroll();
584 --no_wait_return;
585}
586
587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 * Execute a simple command line. Used for translated commands like "*".
589 */
590 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100591do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 return do_cmdline(cmd, NULL, NULL,
594 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
595}
596
597/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100598 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
599 * This allows for using a range without ":" in Vim9 script.
600 */
601 int
602do_cmd_argument(char_u *cmd)
603{
604 return do_cmdline(cmd, NULL, NULL,
605 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
606}
607
608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 * do_cmdline(): execute one Ex command line
610 *
611 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100612 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 * 2. Split up in parts separated with '|'.
614 *
615 * This function can be called recursively!
616 *
617 * flags:
618 * DOCMD_VERBOSE - The command will be included in the error message.
619 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100620 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 * DOCMD_KEYTYPED - Don't reset KeyTyped.
622 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
623 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
624 *
625 * return FAIL if cmdline could not be executed, OK otherwise
626 */
627 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100628do_cmdline(
629 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200630 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100631 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100632 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 char_u *next_cmdline; // next cmd to execute
635 char_u *cmdline_copy = NULL; // copy of cmd line
636 int used_getline = FALSE; // used "fgetline" to obtain command
637 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100639 int count = 0; // line number count
640 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 int retval = OK;
642#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100643 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100644 garray_T lines_ga; // keep lines for ":while"/":for"
645 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200646 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100647 char_u *fname = NULL; // function or script name
648 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
649 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
650 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200652 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200653 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200656 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000658 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000660 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100662# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663# define cmd_cookie cookie
664#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200666#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100667 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
668 // location for storing error messages to be converted to an exception.
669 // This ensures that the do_errthrow() call in do_one_cmd() does not
670 // combine the messages stored by an earlier invocation of do_one_cmd()
671 // with the command name of the later one. This would happen when
672 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 saved_msg_list = msg_list;
674 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#endif
676
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100677 // It's possible to create an endless loop with ":execute", catch that
678 // here. The value of 200 allows nested function calls, ":source", etc.
679 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100680 if (call_depth >= 200
681#ifdef FEAT_EVAL
682 && call_depth >= p_mfd
683#endif
684 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100686 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100688 // When converting to an exception, we do not include the command name
689 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100690 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 msg_list = saved_msg_list;
692#endif
693 return FAIL;
694 }
695 ++call_depth;
696
697#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100698 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 cstack.cs_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
701
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100702 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100704 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100705 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000706 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 ++ex_nesting_level;
708
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100709 // Get the function or script name and the address where the next breakpoint
710 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000711 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
713 fname = func_name(real_cookie);
714 breakpoint = func_breakpoint(real_cookie);
715 dbg_tick = func_dbg_tick(real_cookie);
716 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100717 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100719 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 breakpoint = source_breakpoint(real_cookie);
721 dbg_tick = source_dbg_tick(real_cookie);
722 }
723
724 /*
725 * Initialize "force_abort" and "suppress_errthrow" at the top level.
726 */
727 if (!recursive)
728 {
729 force_abort = FALSE;
730 suppress_errthrow = FALSE;
731 }
732
733 /*
734 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000735 * exception handling (used when debugging). Otherwise clear it to avoid
736 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000738 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000739 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000740 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200741 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742
743 initial_trylevel = trylevel;
744
745 /*
746 * "did_throw" will be set to TRUE when an exception is being thrown.
747 */
748 did_throw = FALSE;
749#endif
750 /*
751 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000752 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 * If force_abort is set, we cancel everything.
754 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100755#ifdef FEAT_EVAL
756 did_emsg_cumul += did_emsg;
757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 did_emsg = FALSE;
759
760 /*
761 * KeyTyped is only set when calling vgetc(). Reset it here when not
762 * calling vgetc() (sourced command lines).
763 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100764 if (!(flags & DOCMD_KEYTYPED)
765 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 KeyTyped = FALSE;
767
768 /*
769 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000770 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 * - for multiple commands on one line, separated with '|'
772 * - when repeating until there are no more lines (for ":source")
773 */
774 next_cmdline = cmdline;
775 do
776 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779#endif
780
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100781 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (next_cmdline == NULL
783#ifdef FEAT_EVAL
784 && !force_abort
785 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000786 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787#endif
788 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100789 {
790#ifdef FEAT_EVAL
791 did_emsg_cumul += did_emsg;
792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000797 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100798 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 * 3. If a line is given: Make a copy, so we can mess with it.
800 */
801
802#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100803 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000804 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100806 // Each '|' separated command is stored separately in lines_ga, to
807 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100808 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100810 // Check if a function has returned or, unless it has an unclosed
811 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000815 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816 func_line_end(real_cookie);
817# endif
818 if (func_has_ended(real_cookie))
819 {
820 retval = FAIL;
821 break;
822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000824#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000825 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000827 script_line_end();
828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100830 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100831 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {
833 retval = FAIL;
834 break;
835 }
836
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100837 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (breakpoint != NULL && dbg_tick != NULL
839 && *dbg_tick != debug_tick)
840 {
841 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100842 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100843 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 *dbg_tick = debug_tick;
845 }
846
847 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100848 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100850 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100852 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100854 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100855 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100857 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100858 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 *dbg_tick = debug_tick;
860 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000861# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000862 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000863 {
864 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100865 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100866 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000867 script_line_start();
868 }
869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#endif
872
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (next_cmdline == NULL)
875 {
876 /*
877 * Need to set msg_didout for the first line after an ":if",
878 * otherwise the ":if" will be overwritten.
879 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100880 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100882 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#ifdef FEAT_EVAL
884 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
885#else
886 0
887#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100888 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
889 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100891 // Don't call wait_return for aborted command line. The NULL
892 // returned for the end of a sourced file or executed function
893 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (KeyTyped && !(flags & DOCMD_REPEAT))
895 need_wait_return = FALSE;
896 retval = FAIL;
897 break;
898 }
899 used_getline = TRUE;
900
901 /*
902 * Keep the first typed line. Clear it when more lines are typed.
903 */
904 if (flags & DOCMD_KEEPLINE)
905 {
906 vim_free(repeat_cmdline);
907 if (count == 0)
908 repeat_cmdline = vim_strsave(next_cmdline);
909 else
910 repeat_cmdline = NULL;
911 }
912 }
913
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100914 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 else if (cmdline_copy == NULL)
916 {
917 next_cmdline = vim_strsave(next_cmdline);
918 if (next_cmdline == NULL)
919 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200920 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 retval = FAIL;
922 break;
923 }
924 }
925 cmdline_copy = next_cmdline;
926
927#ifdef FEAT_EVAL
928 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200929 * Inside a while/for loop, and when the command looks like a ":while"
930 * or ":for", the line is stored, because we may need it later when
931 * looping.
932 *
933 * When there is a '|' and another command, it is stored separately,
934 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000935 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200936 *
937 * Pass a different "fgetline" function to do_one_cmd() below,
938 * that it stores lines in or reads them from "lines_ga". Makes it
939 * possible to define a function inside a while/for loop and handles
940 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200942 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200944 cmd_getline = get_loop_line;
945 cmd_cookie = (void *)&cmd_loop_cookie;
946 cmd_loop_cookie.lines_gap = &lines_ga;
947 cmd_loop_cookie.current_line = current_line;
948 cmd_loop_cookie.getline = fgetline;
949 cmd_loop_cookie.cookie = cookie;
950 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
951
952 // Save the current line when encountering it the first time.
953 if (current_line == lines_ga.ga_len
954 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
956 retval = FAIL;
957 break;
958 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200959 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200961 else
962 {
963 cmd_getline = fgetline;
964 cmd_cookie = cookie;
965 }
966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 did_endif = FALSE;
968#endif
969
970 if (count++ == 0)
971 {
972 /*
973 * All output from the commands is put below each other, without
974 * waiting for a return. Don't do this when executing commands
975 * from a script or when being called recursive (e.g. for ":e
976 * +command file").
977 */
978 if (!(flags & DOCMD_NOWAIT) && !recursive)
979 {
980 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100981 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100983 msg_scroll = TRUE; // put messages below each other
984 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 ++RedrawingDisabled;
986 did_inc = TRUE;
987 }
988 }
989
Bram Moolenaar823654b2020-05-29 23:03:09 +0200990 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100991 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
993 /*
994 * 2. Execute one '|' separated command.
995 * do_one_cmd() will return NULL if there is no trailing '|'.
996 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
997 */
998 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100999 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000#ifdef FEAT_EVAL
1001 &cstack,
1002#endif
1003 cmd_getline, cmd_cookie);
1004 --recursive;
1005
1006#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001007 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001008 // Use "current_line" from "cmd_loop_cookie", it may have been
1009 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001010 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011#endif
1012
1013 if (next_cmdline == NULL)
1014 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001015 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 /*
1018 * If the command was typed, remember it for the ':' register.
1019 * Do this AFTER executing the command to make :@: work.
1020 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001021 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 && new_last_cmdline != NULL)
1023 {
1024 vim_free(last_cmdline);
1025 last_cmdline = new_last_cmdline;
1026 new_last_cmdline = NULL;
1027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 else
1030 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001031 // need to copy the command after the '|' to cmdline_copy, for the
1032 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001033 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 next_cmdline = cmdline_copy;
1035 }
1036
1037
1038#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001039 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001041 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001043 {
1044 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 {
1050 ++current_line;
1051
1052 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 * An ":endwhile", ":endfor" and ":continue" is handled here.
1054 * If we were executing commands, jump back to the ":while" or
1055 * ":for".
1056 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001058 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001060 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001062 // Jump back to the matching ":while" or ":for". Be careful
1063 // not to use a cs_line[] from an entry that isn't a ":while"
1064 // or ":for": It would make "current_line" invalid and can
1065 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (!did_emsg && !got_int && !did_throw
1067 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 && (cstack.cs_flags[cstack.cs_idx]
1069 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 && cstack.cs_line[cstack.cs_idx] >= 0
1071 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1072 {
1073 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001074 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001076 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001078 // Check for the next breakpoint at or after the ":while"
1079 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 if (breakpoint != NULL)
1081 {
1082 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001083 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 fname,
1085 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1086 *dbg_tick = debug_tick;
1087 }
1088 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001089 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001091 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001093 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1094 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 }
1096 }
1097
1098 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001099 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001104 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 }
1106 }
1107
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001108 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001109 if (breakpoint != NULL && has_watchexpr())
1110 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001111 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001112 *dbg_tick = debug_tick;
1113 }
1114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 /*
1116 * When not inside any ":while" loop, clear remembered lines.
1117 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
1120 if (lines_ga.ga_len > 0)
1121 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001122 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1124 free_cmdlines(&lines_ga);
1125 }
1126 current_line = 0;
1127 }
1128
1129 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1131 * being restored at the ":endtry". Reset them here and set the
1132 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1133 * This includes the case where a missing ":endif", ":endwhile" or
1134 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001136 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 cstack.cs_lflags &= ~CSL_HAD_FINA;
1139 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1140 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 did_throw ? (void *)current_exception : NULL);
1142 did_emsg = got_int = did_throw = FALSE;
1143 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1144 }
1145
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001146 // Update global "trylevel" for recursive calls to do_cmdline() from
1147 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 trylevel = initial_trylevel + cstack.cs_trylevel;
1149
1150 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001151 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 * files) is aborted because of an error, an interrupt, or an uncaught
1153 * exception, cancel everything. If it is left normally, reset
1154 * force_abort to get the non-EH compatible abortion behavior for
1155 * the rest of the script.
1156 */
1157 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1158 force_abort = FALSE;
1159
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001160 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001162#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164 }
1165 /*
1166 * Continue executing command lines when:
1167 * - no CTRL-C typed, no aborting error, no exception thrown or try
1168 * conditionals need to be checked for executing finally clauses or
1169 * catching an interrupt exception
1170 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 * looping for ":source" command or function call.
1173 */
1174 while (!((got_int
1175#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001176 || (did_emsg && (force_abort || in_vim9script()))
1177 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#endif
1179 )
1180#ifdef FEAT_EVAL
1181 && cstack.cs_trylevel == 0
1182#endif
1183 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001184 && !(did_emsg
1185#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001186 // Keep going when inside try/catch, so that the error can be
1187 // deal with, except when it is a syntax error, it may cause
1188 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001189 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1190#endif
1191 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001192 && (getline_equal(fgetline, cookie, getexmodeline)
1193 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && (next_cmdline != NULL
1195#ifdef FEAT_EVAL
1196 || cstack.cs_idx >= 0
1197#endif
1198 || (flags & DOCMD_REPEAT)));
1199
1200 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001201 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202#ifdef FEAT_EVAL
1203 free_cmdlines(&lines_ga);
1204 ga_clear(&lines_ga);
1205
1206 if (cstack.cs_idx >= 0)
1207 {
1208 /*
1209 * If a sourced file or executed function ran to its end, report the
1210 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001211 * In Vim9 script do not give a second error, executing aborts after
1212 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 */
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001214 if (!got_int && !did_throw && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001215 && ((getline_equal(fgetline, cookie, getsourceline)
1216 && !source_finished(fgetline, cookie))
1217 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 && !func_has_ended(real_cookie))))
1219 {
1220 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001225 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001227 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
1229
1230 /*
1231 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1232 * ":endtry" in a sourced file or executed function. If the try
1233 * conditional is in its finally clause, ignore anything pending.
1234 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001235 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 */
1237 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001238 {
1239 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1240
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001241 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001242 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001243 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1244 &cstack.cs_looplevel);
1245 }
1246 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 trylevel = initial_trylevel;
1248 }
1249
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001250 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1251 // lack was reported above and the error message is to be converted to an
1252 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001253 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 ? (char_u *)"endfunction" : (char_u *)NULL);
1255
1256 if (trylevel == 0)
1257 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001258 // Just in case did_throw got set but current_exception wasn't.
1259 if (current_exception == NULL)
1260 did_throw = FALSE;
1261
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 /*
1263 * When an exception is being thrown out of the outermost try
1264 * conditional, discard the uncaught exception, disable the conversion
1265 * of interrupts or errors to exceptions, and ensure that no more
1266 * commands are executed.
1267 */
1268 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001269 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
1271 /*
1272 * On an interrupt or an aborting error not converted to an exception,
1273 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001274 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 * when force_abort is set and did_emsg unset in case of an interrupt
1276 * from a finally clause after an error.
1277 */
1278 else if (got_int || (did_emsg && force_abort))
1279 suppress_errthrow = TRUE;
1280 }
1281
1282 /*
1283 * The current cstack will be freed when do_cmdline() returns. An uncaught
1284 * exception will have to be rethrown in the previous cstack. If a function
1285 * has just returned or a script file was just finished and the previous
1286 * cstack belongs to the same function or, respectively, script file, it
1287 * will have to be checked for finally clauses to be executed due to the
1288 * ":return" or ":finish". This is done in do_one_cmd().
1289 */
1290 if (did_throw)
1291 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001292 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001294 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 && ex_nesting_level > func_level(real_cookie) + 1))
1296 {
1297 if (!did_throw)
1298 check_cstack = TRUE;
1299 }
1300 else
1301 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001302 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001303 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 --ex_nesting_level;
1305 /*
1306 * Go to debug mode when returning from a function in which we are
1307 * single-stepping.
1308 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001309 if ((getline_equal(fgetline, cookie, getsourceline)
1310 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001312 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 ? (char_u *)_("End of sourced file")
1314 : (char_u *)_("End of function"));
1315 }
1316
1317 /*
1318 * Restore the exception environment (done after returning from the
1319 * debugger).
1320 */
1321 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001322 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
1324 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001325
1326 // Cleanup if "cs_emsg_silent_list" remains.
1327 if (cstack.cs_emsg_silent_list != NULL)
1328 {
1329 eslist_T *elem, *temp;
1330
1331 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1332 {
1333 temp = elem->next;
1334 vim_free(elem);
1335 }
1336 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001337#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339 /*
1340 * If there was too much output to fit on the command line, ask the user to
1341 * hit return before redrawing the screen. With the ":global" command we do
1342 * this only once after the command is finished.
1343 */
1344 if (did_inc)
1345 {
1346 --RedrawingDisabled;
1347 --no_wait_return;
1348 msg_scroll = FALSE;
1349
1350 /*
1351 * When just finished an ":if"-":else" which was typed, no need to
1352 * wait for hit-return. Also for an error situation.
1353 */
1354 if (retval == FAIL
1355#ifdef FEAT_EVAL
1356 || (did_endif && KeyTyped && !did_emsg)
1357#endif
1358 )
1359 {
1360 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001361 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363 else if (need_wait_return)
1364 {
1365 /*
1366 * The msg_start() above clears msg_didout. The wait_return we do
1367 * here should not overwrite the command that may be shown before
1368 * doing that.
1369 */
1370 msg_didout |= msg_didout_before_start;
1371 wait_return(FALSE);
1372 }
1373 }
1374
Bram Moolenaar2a942252012-11-28 23:03:07 +01001375#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001376 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001377#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 /*
1379 * Reset if_level, in case a sourced script file contains more ":if" than
1380 * ":endif" (could be ":if x | foo | endif").
1381 */
1382 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001383#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 --call_depth;
1386 return retval;
1387}
1388
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001389#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001390/*
1391 * Handle when "did_throw" is set after executing commands.
1392 */
1393 void
1394handle_did_throw()
1395{
1396 char *p = NULL;
1397 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001398 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001399
1400 /*
1401 * If the uncaught exception is a user exception, report it as an
1402 * error. If it is an error exception, display the saved error
1403 * message now. For an interrupt exception, do nothing; the
1404 * interrupt message is given elsewhere.
1405 */
1406 switch (current_exception->type)
1407 {
1408 case ET_USER:
1409 vim_snprintf((char *)IObuff, IOSIZE,
1410 _("E605: Exception not caught: %s"),
1411 current_exception->value);
1412 p = (char *)vim_strsave(IObuff);
1413 break;
1414 case ET_ERROR:
1415 messages = current_exception->messages;
1416 current_exception->messages = NULL;
1417 break;
1418 case ET_INTERRUPT:
1419 break;
1420 }
1421
1422 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1423 current_exception->throw_lnum);
1424 ESTACK_CHECK_SETUP
1425 current_exception->throw_name = NULL;
1426
1427 discard_current_exception(); // uses IObuff if 'verbose'
1428 suppress_errthrow = TRUE;
1429 force_abort = TRUE;
1430
1431 if (messages != NULL)
1432 {
1433 do
1434 {
1435 msglist_T *next = messages->next;
1436 int save_compiling = estack_compiling;
1437
1438 estack_compiling = messages->msg_compiling;
1439 emsg(messages->msg);
1440 vim_free(messages->msg);
1441 vim_free(messages->sfile);
1442 vim_free(messages);
1443 messages = next;
1444 estack_compiling = save_compiling;
1445 }
1446 while (messages != NULL);
1447 }
1448 else if (p != NULL)
1449 {
1450 emsg(p);
1451 vim_free(p);
1452 }
1453 vim_free(SOURCING_NAME);
1454 ESTACK_CHECK_NOW
1455 estack_pop();
1456}
1457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001459 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 */
1461 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001462get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001464 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 wcmd_T *wp;
1466 char_u *line;
1467
1468 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1469 {
1470 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001471 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001473 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001475 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001477 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001478 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 ++cp->current_line;
1480
1481 return line;
1482 }
1483
1484 KeyTyped = FALSE;
1485 ++cp->current_line;
1486 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001487 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return vim_strsave(wp->line);
1489}
1490
1491/*
1492 * Store a line in "gap" so that a ":while" loop can execute it again.
1493 */
1494 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001495store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 if (ga_grow(gap, 1) == FAIL)
1498 return FAIL;
1499 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001500 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 return OK;
1503}
1504
1505/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001506 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 */
1508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001509free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
1511 while (gap->ga_len > 0)
1512 {
1513 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1514 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 }
1516}
1517#endif
1518
1519/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001520 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1521 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001524getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001525 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001526 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001527 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001530 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001531 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001533 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1534 // function that's originally used to obtain the lines. This may be
1535 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001536 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001537 cp = (struct loop_cookie *)cookie;
1538 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
1540 gp = cp->getline;
1541 cp = cp->cookie;
1542 }
1543 return gp == func;
1544#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001545 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546#endif
1547}
1548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001550 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 * getline function. Otherwise return "cookie".
1552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001554getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001555 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001556 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557{
Bram Moolenaar13505972019-01-24 15:04:48 +01001558#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001559 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001560 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001562 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1563 // cookie that's originally used to obtain the lines. This may be nested
1564 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001565 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001566 cp = (struct loop_cookie *)cookie;
1567 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
1569 gp = cp->getline;
1570 cp = cp->cookie;
1571 }
1572 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001573#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaard5053d02020-06-28 15:51:16 +02001578#if defined(FEAT_EVAL) || defined(PROT)
1579/*
1580 * Get the next line source line without advancing.
1581 */
1582 char_u *
1583getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001584 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001585 void *cookie) // argument for fgetline()
1586{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001587 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001588 struct loop_cookie *cp;
1589 wcmd_T *wp;
1590
1591 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1592 // cookie that's originally used to obtain the lines. This may be nested
1593 // several levels.
1594 gp = fgetline;
1595 cp = (struct loop_cookie *)cookie;
1596 while (gp == get_loop_line)
1597 {
1598 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1599 {
1600 // executing lines a second time, use the stored copy
1601 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1602 return wp->line;
1603 }
1604 gp = cp->getline;
1605 cp = cp->cookie;
1606 }
1607 if (gp == getsourceline)
1608 return source_nextline(cp);
1609 return NULL;
1610}
1611#endif
1612
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613
1614/*
1615 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1616 * ":bwipeout", etc.
1617 * Returns the buffer number.
1618 */
1619 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001620compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001621{
1622 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001623 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001624 int count = offset;
1625
1626 buf = firstbuf;
1627 while (buf->b_next != NULL && buf->b_fnum < lnum)
1628 buf = buf->b_next;
1629 while (count != 0)
1630 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001631 count += (offset < 0) ? 1 : -1;
1632 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1633 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001634 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001635 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001636 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001637 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001638 while (buf->b_ml.ml_mfp == NULL)
1639 {
1640 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1641 if (nextbuf == NULL)
1642 break;
1643 buf = nextbuf;
1644 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001645 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001646 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001647 if (addr_type == ADDR_LOADED_BUFFERS)
1648 while (buf->b_ml.ml_mfp == NULL)
1649 {
1650 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1651 if (nextbuf == NULL)
1652 break;
1653 buf = nextbuf;
1654 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001655 return buf->b_fnum;
1656}
1657
Bram Moolenaarb7316892019-05-01 18:08:42 +02001658/*
1659 * Return the window number of "win".
1660 * When "win" is NULL return the number of windows.
1661 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001662 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001663current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001664{
1665 win_T *wp;
1666 int nr = 0;
1667
Bram Moolenaar29323592016-07-24 22:04:11 +02001668 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001669 {
1670 ++nr;
1671 if (wp == win)
1672 break;
1673 }
1674 return nr;
1675}
1676
1677 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001678current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001679{
1680 tabpage_T *tp;
1681 int nr = 0;
1682
Bram Moolenaar29323592016-07-24 22:04:11 +02001683 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001684 {
1685 ++nr;
1686 if (tp == tab)
1687 break;
1688 }
1689 return nr;
1690}
1691
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001692 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001693comment_start(char_u *p, int starts_with_colon UNUSED)
1694{
1695#ifdef FEAT_EVAL
1696 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001697 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001698#endif
1699 return *p == '"';
1700}
1701
Bram Moolenaarf240e182014-11-27 18:33:02 +01001702# define CURRENT_WIN_NR current_win_nr(curwin)
1703# define LAST_WIN_NR current_win_nr(NULL)
1704# define CURRENT_TAB_NR current_tab_nr(curtab)
1705# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707/*
1708 * Execute one Ex command.
1709 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001710 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1711 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 *
1713 * 1. skip comment lines and leading space
1714 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001715 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001716 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001717 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001718 * 6. parse arguments
1719 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001721 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 *
1723 * This function may be called recursively!
1724 */
1725#if (_MSC_VER == 1200)
1726/*
Bram Moolenaared203462004-06-16 11:19:22 +00001727 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001729 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001732do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001733 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001734 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001736 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001738 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001739 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001741 char_u *p;
1742 linenr_T lnum;
1743 long n;
1744 char *errormsg = NULL; // error message
1745 char_u *after_modifier = NULL;
1746 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001747 cmdmod_T save_cmdmod;
1748 int save_reg_executing = reg_executing;
1749 int ni; // set when Not Implemented
1750 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001751 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001752#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001753 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001754 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001755 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001756#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001757 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
Bram Moolenaara80faa82020-04-12 19:37:17 +02001759 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 ea.line1 = 1;
1761 ea.line2 = 1;
1762#ifdef FEAT_EVAL
1763 ++ex_nesting_level;
1764#endif
1765
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001766 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 if (quitmore
1768#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001769 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001770 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001771#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001772 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001773 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 --quitmore;
1775
1776 /*
1777 * Reset browse, confirm, etc.. They are restored when returning, for
1778 * recursive calls.
1779 */
1780 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001782 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001783 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1784 goto doend;
1785
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001786/*
1787 * 1. Skip comment lines and leading white space and colons.
1788 * 2. Handle command modifiers.
1789 */
1790 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001792 ea.cmdlinep = cmdlinep;
1793 ea.getline = fgetline;
1794 ea.cookie = cookie;
1795#ifdef FEAT_EVAL
1796 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001797 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001799 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001800 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001801 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001802#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001803 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001804#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001805 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807#ifdef FEAT_EVAL
1808 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1809 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1810#else
1811 ea.skip = (if_level > 0);
1812#endif
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001815 * 3. Skip over the range to find the command. Let "p" point to after it.
1816 *
1817 * We need the command to know what kind of range it uses.
1818 */
1819 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001820#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001821 // In Vim9 script a colon is required before the range. This may also be
1822 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001823 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001824 {
1825 may_have_range = FALSE;
1826 for (p = ea.cmd; p >= *cmdlinep; --p)
1827 {
1828 if (*p == ':')
1829 may_have_range = TRUE;
1830 if (p < ea.cmd && !VIM_ISWHITE(*p))
1831 break;
1832 }
1833 }
1834 else
1835 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001836 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001837#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001838 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839
1840#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001841 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001842 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001843 if (ea.cmd == cmd + 1 && *cmd == '$')
1844 // should be "$VAR = val"
1845 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001846 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001847 if (ea.cmdidx == CMD_SIZE)
1848 {
1849 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1850
1851 // If a ':' before the range is missing, give a clearer error
1852 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001853 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001854 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001855 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001856 goto doend;
1857 }
1858 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 else
1861#endif
1862 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001863
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001864#ifdef FEAT_EVAL
1865# ifdef FEAT_PROFILE
1866 // Count this line for profiling if skip is TRUE.
1867 if (do_profiling == PROF_YES
1868 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1869 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1870 {
1871 int skip = did_emsg || got_int || did_throw;
1872
1873 if (ea.cmdidx == CMD_catch)
1874 skip = !skip && !(cstack->cs_idx >= 0
1875 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1876 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1877 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1878 skip = skip || !(cstack->cs_idx >= 0
1879 && !(cstack->cs_flags[cstack->cs_idx]
1880 & (CSF_ACTIVE | CSF_TRUE)));
1881 else if (ea.cmdidx == CMD_finally)
1882 skip = FALSE;
1883 else if (ea.cmdidx != CMD_endif
1884 && ea.cmdidx != CMD_endfor
1885 && ea.cmdidx != CMD_endtry
1886 && ea.cmdidx != CMD_endwhile)
1887 skip = ea.skip;
1888
1889 if (!skip)
1890 {
1891 if (getline_equal(fgetline, cookie, get_func_line))
1892 func_line_exec(getline_cookie(fgetline, cookie));
1893 else if (getline_equal(fgetline, cookie, getsourceline))
1894 script_line_exec();
1895 }
1896 }
1897# endif
1898
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001899 // May go to debug mode. If this happens and the ">quit" debug command is
1900 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001901 dbg_check_breakpoint(&ea);
1902 if (!ea.skip && got_int)
1903 {
1904 ea.skip = TRUE;
1905 (void)do_intthrow(cstack);
1906 }
1907#endif
1908
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001909/*
1910 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 *
1912 * where 'addr' is:
1913 *
1914 * % (entire file)
1915 * $ [+-NUM]
1916 * 'x [+-NUM] (where x denotes a currently defined mark)
1917 * . [+-NUM]
1918 * [+-NUM]..
1919 * NUM
1920 *
1921 * The ea.cmd pointer is updated to point to the first character following the
1922 * range spec. If an initial address is found, but no second, the upper bound
1923 * is equal to the lower.
1924 */
1925
Bram Moolenaar25190db2019-05-04 15:05:28 +02001926 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001927 if (!IS_USER_CMDIDX(ea.cmdidx))
1928 {
1929 if (ea.cmdidx != CMD_SIZE)
1930 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1931 else
1932 ea.addr_type = ADDR_LINES;
1933
Bram Moolenaar25190db2019-05-04 15:05:28 +02001934 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001935 if (ea.cmdidx == CMD_wincmd && p != NULL)
1936 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001937#ifdef FEAT_QUICKFIX
1938 // :.cc in quickfix window uses line number
1939 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1940 ea.addr_type = ADDR_OTHER;
1941#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001942 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001943
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001944 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001945#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001946 if (!may_have_range)
1947 ea.line1 = ea.line2 = default_address(&ea);
1948 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001949#endif
1950 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1951 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001954 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 */
1956
1957 /*
1958 * Skip ':' and any white space
1959 */
1960 ea.cmd = skipwhite(ea.cmd);
1961 while (*ea.cmd == ':')
1962 ea.cmd = skipwhite(ea.cmd + 1);
1963
1964 /*
1965 * If we got a line, but no command, then go to the line.
1966 * If we find a '|' or '\n' we set ea.nextcmd.
1967 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001968 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1969 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 /*
1972 * strange vi behaviour:
1973 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001974 * ":3|..." prints line 3 (not in Vim9 script)
1975 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001977 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 goto doend;
Bram Moolenaarb8554302021-02-15 21:30:30 +01001979 if ((*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
1980#ifdef FEAT_EVAL
1981 && !vim9script
1982#endif
1983 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
1985 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001986 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if ((errormsg = invalid_range(&ea)) == NULL)
1988 {
1989 correct_range(&ea);
1990 ex_print(&ea);
1991 }
1992 }
1993 else if (ea.addr_count != 0)
1994 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001995 if (ea.line2 > curbuf->b_ml.ml_line_count)
1996 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 // With '-' in 'cpoptions' a line number past the file is an
1998 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001999 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2000 ea.line2 = -1;
2001 else
2002 ea.line2 = curbuf->b_ml.ml_line_count;
2003 }
2004
2005 if (ea.line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002006 errormsg = _(e_invalid_range);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 else
2008 {
2009 if (ea.line2 == 0)
2010 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 else
2012 curwin->w_cursor.lnum = ea.line2;
2013 beginline(BL_SOL | BL_FIX);
2014 }
2015 }
2016 goto doend;
2017 }
2018
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002019 // If this looks like an undefined user command and there are CmdUndefined
2020 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002021 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2022 && ASCII_ISUPPER(*ea.cmd)
2023 && has_cmdundefined())
2024 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002025 int ret;
2026
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002027 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002028 while (ASCII_ISALNUM(*p))
2029 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002030 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002031 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2032 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002033 // If the autocommands did something and didn't cause an error, try
2034 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002035 p = (ret
2036#ifdef FEAT_EVAL
2037 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002038#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002040 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002041
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (p == NULL)
2043 {
2044 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002045 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 goto doend;
2047 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002048 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002049 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2050 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
2052 errormsg = uc_fun_cmd();
2053 goto doend;
2054 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002055
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (ea.cmdidx == CMD_SIZE)
2057 {
2058 if (!ea.skip)
2059 {
2060 STRCPY(IObuff, _("E492: Not an editor command"));
2061 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002062 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002063 // If the modifier was parsed OK the error must be in the
2064 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002065 if (after_modifier != NULL)
2066 append_command(after_modifier);
2067 else
2068 append_command(*cmdlinep);
2069 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002070 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002071 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
2073 goto doend;
2074 }
2075
Bram Moolenaar958636c2014-10-21 20:01:58 +02002076 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2077 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002078#ifdef HAVE_EX_SCRIPT_NI
2079 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2080#endif
2081 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
2083#ifndef FEAT_EVAL
2084 /*
2085 * When the expression evaluation is disabled, recognize the ":if" and
2086 * ":endif" commands and ignore everything in between it.
2087 */
2088 if (ea.cmdidx == CMD_if)
2089 ++if_level;
2090 if (if_level)
2091 {
2092 if (ea.cmdidx == CMD_endif)
2093 --if_level;
2094 goto doend;
2095 }
2096
2097#endif
2098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002100 if (*p == '!' && ea.cmdidx != CMD_substitute
2101 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
2103 ++p;
2104 ea.forceit = TRUE;
2105 }
2106 else
2107 ea.forceit = FALSE;
2108
2109/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002110 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002112 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002113 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
2115 if (!ea.skip)
2116 {
2117#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002118 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002120 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002121 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 goto doend;
2123 }
2124#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002125 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002126 {
2127 errormsg = _("E981: Command not allowed in rvim");
2128 goto doend;
2129 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002130 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002132 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002133 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 goto doend;
2135 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002136
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002137 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002139#ifdef FEAT_CMDWIN
2140 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2141 {
2142 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002143 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002144 goto doend;
2145 }
2146#endif
2147 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2148 {
2149 // Command not allowed when text is locked
2150 errormsg = _(get_text_locked_msg());
2151 goto doend;
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002155 // Disallow editing another buffer when "curbuf_lock" is set.
2156 // Do allow ":checktime" (it is postponed).
2157 // Do allow ":edit" (check for an argument later).
2158 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002159 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002160 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002161 && ea.cmdidx != CMD_edit
2162 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002163 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002164 && curbuf_locked())
2165 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002167 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002169 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002170 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 goto doend;
2172 }
2173 }
2174
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002175 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002177 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 goto doend;
2179 }
2180
2181 /*
2182 * Don't complain about the range if it is not used
2183 * (could happen if line_count is accidentally set to 0).
2184 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002185 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
2187 /*
2188 * If the range is backwards, ask for confirmation and, if given, swap
2189 * ea.line1 & ea.line2 so it's forwards again.
2190 * When global command is busy, don't ask, will fail below.
2191 */
2192 if (!global_busy && ea.line1 > ea.line2)
2193 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002194 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002196 if (sourcing || exmode_active)
2197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002198 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002199 goto doend;
2200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (ask_yesno((char_u *)
2202 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002203 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205 lnum = ea.line1;
2206 ea.line1 = ea.line2;
2207 ea.line2 = lnum;
2208 }
2209 if ((errormsg = invalid_range(&ea)) != NULL)
2210 goto doend;
2211 }
2212
Bram Moolenaarb7316892019-05-01 18:08:42 +02002213 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2214 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 ea.line2 = 1;
2216
2217 correct_range(&ea);
2218
2219#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002220 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002221 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002223 // Put the first line at the start of a closed fold, put the last line
2224 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 (void)hasFolding(ea.line1, &ea.line1, NULL);
2226 (void)hasFolding(ea.line2, NULL, &ea.line2);
2227 }
2228#endif
2229
2230#ifdef FEAT_QUICKFIX
2231 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002232 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 * option here, so things like % get expanded.
2234 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002235 p = replace_makeprg(&ea, p, cmdlinep);
2236 if (p == NULL)
2237 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238#endif
2239
2240 /*
2241 * Skip to start of argument.
2242 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2243 */
2244 if (ea.cmdidx == CMD_bang)
2245 ea.arg = p;
2246 else
2247 ea.arg = skipwhite(p);
2248
Bram Moolenaar379fb762018-08-30 15:58:28 +02002249 // ":file" cannot be run with an argument when "curbuf_lock" is set
2250 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2251 goto doend;
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 /*
2254 * Check for "++opt=val" argument.
2255 * Must be first, allow ":w ++enc=utf8 !cmd"
2256 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002257 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2259 if (getargopt(&ea) == FAIL && !ni)
2260 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002261 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 goto doend;
2263 }
2264
2265 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2266 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002267 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002269 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002271 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 goto doend;
2273 }
2274 ea.arg = skipwhite(ea.arg + 1);
2275 ea.append = TRUE;
2276 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002277 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
2279 ++ea.arg;
2280 ea.usefilter = TRUE;
2281 }
2282 }
2283
2284 if (ea.cmdidx == CMD_read)
2285 {
2286 if (ea.forceit)
2287 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002288 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 ea.forceit = FALSE;
2290 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002291 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
2293 ++ea.arg;
2294 ea.usefilter = TRUE;
2295 }
2296 }
2297
2298 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2299 {
2300 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002301 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 ++ea.arg;
2304 ++ea.amount;
2305 }
2306 ea.arg = skipwhite(ea.arg);
2307 }
2308
2309 /*
2310 * Check for "+command" argument, before checking for next command.
2311 * Don't do this for ":read !cmd" and ":write !cmd".
2312 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002313 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2315
2316 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002317 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 * Don't do this for ":read !cmd" and ":write !cmd".
2319 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002320 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 separate_nextcmd(&ea);
2322
2323 /*
2324 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002325 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2326 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002328 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002329 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002330 || ea.cmdidx == CMD_global
2331 || ea.cmdidx == CMD_vglobal
2332 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
2334 for (p = ea.arg; *p; ++p)
2335 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002336 // Remove one backslash before a newline, so that it's possible to
2337 // pass a newline to the shell and also a newline that is preceded
2338 // with a backslash. This makes it impossible to end a shell
2339 // command in a backslash, but that doesn't appear useful.
2340 // Halving the number of backslashes is incompatible with previous
2341 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002343 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 else if (*p == '\n')
2345 {
2346 ea.nextcmd = p + 1;
2347 *p = NUL;
2348 break;
2349 }
2350 }
2351 }
2352
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002353 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002354 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002356 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002357 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002359 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002360 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002361 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002363#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002364 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002365 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002367 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002368 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002371 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2372 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002373 {
2374 ea.regname = *ea.arg++;
2375#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002376 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002377 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2378 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002379 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002380 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002381 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002382 }
2383#endif
2384 ea.arg = skipwhite(ea.arg);
2385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
2387
2388 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002389 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 * count, it's a buffer name.
2391 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002392 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002393 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002394 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
2396 n = getdigits(&ea.arg);
2397 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002398 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002400 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 goto doend;
2402 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002403 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
2405 ea.line2 = n;
2406 if (ea.addr_count == 0)
2407 ea.addr_count = 1;
2408 }
2409 else
2410 {
2411 ea.line1 = ea.line2;
2412 ea.line2 += n - 1;
2413 ++ea.addr_count;
2414 /*
2415 * Be vi compatible: no error message for out of range.
2416 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002417 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 ea.line2 = curbuf->b_ml.ml_line_count;
2419 }
2420 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002421
2422 /*
2423 * Check for flags: 'l', 'p' and '#'.
2424 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002425 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002426 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002427 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2428 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002430 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002431 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 goto doend;
2433 }
2434
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002435 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002437 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 goto doend;
2439 }
2440
2441#ifdef FEAT_EVAL
2442 /*
2443 * Skip the command when it's not going to be executed.
2444 * The commands like :if, :endif, etc. always need to be executed.
2445 * Also make an exception for commands that handle a trailing command
2446 * themselves.
2447 */
2448 if (ea.skip)
2449 {
2450 switch (ea.cmdidx)
2451 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002452 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 case CMD_while:
2454 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002455 case CMD_for:
2456 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 case CMD_if:
2458 case CMD_elseif:
2459 case CMD_else:
2460 case CMD_endif:
2461 case CMD_try:
2462 case CMD_catch:
2463 case CMD_finally:
2464 case CMD_endtry:
2465 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002466 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 break;
2468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002469 // Commands that handle '|' themselves. Check: A command should
2470 // either have the EX_TRLBAR flag, appear in this list or appear in
2471 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 case CMD_aboveleft:
2473 case CMD_and:
2474 case CMD_belowright:
2475 case CMD_botright:
2476 case CMD_browse:
2477 case CMD_call:
2478 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002479 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 case CMD_delfunction:
2481 case CMD_djump:
2482 case CMD_dlist:
2483 case CMD_dsearch:
2484 case CMD_dsplit:
2485 case CMD_echo:
2486 case CMD_echoerr:
2487 case CMD_echomsg:
2488 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002489 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002491 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002492 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 case CMD_help:
2494 case CMD_hide:
2495 case CMD_ijump:
2496 case CMD_ilist:
2497 case CMD_isearch:
2498 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002499 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 case CMD_keepjumps:
2501 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002502 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 case CMD_leftabove:
2504 case CMD_let:
2505 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002506 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002507 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002509 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002510 case CMD_noautocmd:
2511 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 case CMD_perl:
2513 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002514 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002515 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002516 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 case CMD_return:
2518 case CMD_rightbelow:
2519 case CMD_ruby:
2520 case CMD_silent:
2521 case CMD_smagic:
2522 case CMD_snomagic:
2523 case CMD_substitute:
2524 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002525 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 case CMD_tcl:
2527 case CMD_throw:
2528 case CMD_tilde:
2529 case CMD_topleft:
2530 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002531 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002532 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 case CMD_verbose:
2534 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002535 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 break;
2537
2538 default: goto doend;
2539 }
2540 }
2541#endif
2542
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002543 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2546 goto doend;
2547 }
2548
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 /*
2550 * Accept buffer name. Cannot be used at the same time with a buffer
2551 * number. Don't do this for a user command.
2552 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002553 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002554 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
2556 /*
2557 * :bdelete, :bwipeout and :bunload take several arguments, separated
2558 * by spaces: find next space (skipping over escaped characters).
2559 * The others take one argument: ignore trailing spaces.
2560 */
2561 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2562 || ea.cmdidx == CMD_bunload)
2563 p = skiptowhite_esc(ea.arg);
2564 else
2565 {
2566 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002567 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 --p;
2569 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002570 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002571 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002572 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 goto doend;
2574 ea.addr_count = 1;
2575 ea.arg = skipwhite(p);
2576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002578 // The :try command saves the emsg_silent flag, reset it here when
2579 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002580 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002581 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002582 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002583 if (emsg_silent < 0)
2584 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002585 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002586 }
2587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002589 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
Bram Moolenaar958636c2014-10-21 20:01:58 +02002592 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
2594 /*
2595 * Execute a user-defined command.
2596 */
2597 do_ucmd(&ea);
2598 }
2599 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 {
2601 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002602 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 */
2604 ea.errmsg = NULL;
2605 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2606 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002607 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
2609
2610#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002612 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002613 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002614 && current_sctx.sc_sid > 0
2615 && ea.cmdidx != CMD_endif
2616 && (cstack->cs_idx < 0
2617 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002618 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 /*
2621 * If the command just executed called do_cmdline(), any throw or ":return"
2622 * or ":finish" encountered there must also check the cstack of the still
2623 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2624 * exception, or reanimate a returned function or finished script file and
2625 * return or finish it again.
2626 */
2627 if (need_rethrow)
2628 do_throw(cstack);
2629 else if (check_cstack)
2630 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002631 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002633 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 && current_func_returned())
2635 do_return(&ea, TRUE, FALSE, NULL);
2636 }
2637 need_rethrow = check_cstack = FALSE;
2638#endif
2639
2640doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002641 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002644 curwin->w_cursor.col = 0;
2645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
2647 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2648 {
2649 if (sourcing)
2650 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002651 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
2653 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002654 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002656 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658 emsg(errormsg);
2659 }
2660#ifdef FEAT_EVAL
2661 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002662 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2663 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002664
2665 if (did_set_expr_line)
2666 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667#endif
2668
Bram Moolenaare1004402020-10-24 20:49:43 +02002669 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002671 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002673 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 ea.nextcmd = NULL;
2675
2676#ifdef FEAT_EVAL
2677 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002678 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679#endif
2680
2681 return ea.nextcmd;
2682}
2683#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002684 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685#endif
2686
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002687static char ex_error_buf[MSG_BUF_LEN];
2688
2689/*
2690 * Return an error message with argument included.
2691 * Uses a static buffer, only the last error will be kept.
2692 * "msg" will be translated, caller should use N_().
2693 */
2694 char *
2695ex_errmsg(char *msg, char_u *arg)
2696{
2697 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2698 return ex_error_buf;
2699}
2700
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002702 * Check for an Ex command with optional tail.
2703 * If there is a match advance "pp" to the argument and return TRUE.
2704 * If "noparen" is TRUE do not recognize the command followed by "(".
2705 */
2706 static int
2707checkforcmd_opt(
2708 char_u **pp, // start of command
2709 char *cmd, // name of command
2710 int len, // required length
2711 int noparen)
2712{
2713 int i;
2714
2715 for (i = 0; cmd[i] != NUL; ++i)
2716 if (((char_u *)cmd)[i] != (*pp)[i])
2717 break;
2718 if (i >= len && !isalpha((*pp)[i])
2719 && (*pp)[i] != '_' && (!noparen || (*pp)[i] != '('))
2720 {
2721 *pp = skipwhite(*pp + i);
2722 return TRUE;
2723 }
2724 return FALSE;
2725}
2726
2727/*
2728 * Check for an Ex command with optional tail.
2729 * If there is a match advance "pp" to the argument and return TRUE.
2730 */
2731 int
2732checkforcmd(
2733 char_u **pp, // start of command
2734 char *cmd, // name of command
2735 int len) // required length
2736{
2737 return checkforcmd_opt(pp, cmd, len, FALSE);
2738}
2739
2740/*
2741 * Check for an Ex command with optional tail, not followed by "(".
2742 * If there is a match advance "pp" to the argument and return TRUE.
2743 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002744 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002745checkforcmd_noparen(
2746 char_u **pp, // start of command
2747 char *cmd, // name of command
2748 int len) // required length
2749{
2750 return checkforcmd_opt(pp, cmd, len, TRUE);
2751}
2752
2753/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002754 * Parse and skip over command modifiers:
2755 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002756 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002757 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002758 * When "skip_only" is TRUE the global variables are not changed, except for
2759 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002760 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2761 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002762 * Call apply_cmdmod() to get the side effects of the modifiers:
2763 * - Increment "sandbox" for ":sandbox"
2764 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002765 * - set msg_silent for ":silent"
2766 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002767 * Return FAIL when the command is not to be executed.
2768 * May set "errormsg" to an error message.
2769 */
2770 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002771parse_command_modifiers(
2772 exarg_T *eap,
2773 char **errormsg,
2774 cmdmod_T *cmod,
2775 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002776{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002777 char_u *p;
2778 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002779
Bram Moolenaare1004402020-10-24 20:49:43 +02002780 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002781
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002782 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002783 for (;;)
2784 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002785 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002786 {
2787 if (*eap->cmd == ':')
2788 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002789 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002790 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002791
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002792 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002793 if (*eap->cmd == NUL && exmode_active
2794 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2795 || getline_equal(eap->getline, eap->cookie, getexline))
2796 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2797 {
2798 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002799 if (!skip_only)
2800 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002801 }
2802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002803 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002804 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002805 return FAIL;
2806 if (*eap->cmd == NUL)
2807 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002808 if (!skip_only)
2809 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 return FAIL;
2811 }
2812
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002813 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002814
2815 // In Vim9 script a variable can shadow a command modifier:
2816 // verbose = 123
2817 // verbose += 123
2818 // silent! verbose = func()
2819 // verbose.member = 2
2820 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002821 // But not:
2822 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002823 if (in_vim9script())
2824 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002825 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002826
2827 for (s = p; ASCII_ISALPHA(*s); ++s)
2828 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002829 n = skipwhite(s);
2830 if (vim_strchr((char_u *)".=", *n) != NULL
2831 || *s == '['
2832 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002833 break;
2834 }
2835
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 switch (*p)
2837 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002838 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002839 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002841 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 continue;
2843
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002844 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002845 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002846 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 continue;
2848 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002849 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002850 {
2851#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002852 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002853#endif
2854 continue;
2855 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002856 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002858 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002859 continue;
2860
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002861 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002862 break;
2863#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002864 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002865#endif
2866 continue;
2867
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002868 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002869 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002870 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002871 continue;
2872 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002873 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002874 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002875 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 continue;
2877 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002878 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002879 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002880 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002881 continue;
2882 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002883 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002885 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 continue;
2887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002888 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002890 char_u *reg_pat;
2891 char_u *nulp = NULL;
2892 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002893
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002894 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaareffed932018-08-14 13:38:17 +02002895 || *p == NUL || ends_excmd(*p))
2896 break;
2897 if (*p == '!')
2898 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002899 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002900 p = skipwhite(p + 1);
2901 if (*p == NUL || ends_excmd(*p))
2902 break;
2903 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002904#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002905 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002906 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002907 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002908#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002909 if (skip_only)
2910 p = skip_vimgrep_pat(p, NULL, NULL);
2911 else
2912 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002913 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2914 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002915 if (p == NULL || *p == NUL)
2916 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002917 if (!skip_only)
2918 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002919 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002920 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002921 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002922 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002923 // restore the character overwritten by NUL
2924 if (nulp != NULL)
2925 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002926 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002927 eap->cmd = p;
2928 continue;
2929 }
2930
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002931 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002932 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002933 || *p == NUL || ends_excmd(*p))
2934 break;
2935 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002936 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002937 continue;
2938
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002939 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002940 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002941 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002942 continue;
2943 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002944 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2945 {
2946 if (ends_excmd2(p, eap->cmd))
2947 {
2948 *errormsg =
2949 _(e_vim9cmd_must_be_followed_by_command);
2950 return FAIL;
2951 }
2952 cmod->cmod_flags |= CMOD_LEGACY;
2953 continue;
2954 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002955
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002956 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002957 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002958 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002959 continue;
2960
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002961 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002962 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002963 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002964 continue;
2965 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002966 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002967 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002968 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002969 continue;
2970
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002971 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02002972 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002973 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002974 continue;
2975
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002976 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002977 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002978 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002979 continue;
2980 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002981 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002982 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002983 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002984 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2985 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002986 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 }
2990 continue;
2991
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002992 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002993 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002994 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002995 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002996 long tabnr = get_address(eap, &eap->cmd,
2997 ADDR_TABS, eap->skip,
2998 skip_only, FALSE, 1);
2999 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003000 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003001 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003003 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3004 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003005 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003006 return FAIL;
3007 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003008 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003010 }
3011 eap->cmd = p;
3012 continue;
3013 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003014 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003015 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003016 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003017 continue;
3018
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003019 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003020 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003021 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003022 continue;
3023
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003024 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003025 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003026 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003027 continue;
3028 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003029 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003030 {
3031 if (ends_excmd2(p, eap->cmd))
3032 {
3033 *errormsg =
3034 _(e_vim9cmd_must_be_followed_by_command);
3035 return FAIL;
3036 }
3037 cmod->cmod_flags |= CMOD_VIM9CMD;
3038 continue;
3039 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003040 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003041 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003042 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003043 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003044 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003045 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003046 eap->cmd = p;
3047 continue;
3048 }
3049 break;
3050 }
3051
3052 return OK;
3053}
3054
3055/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003056 * Return TRUE if "cmod" has anything set.
3057 */
3058 int
3059has_cmdmod(cmdmod_T *cmod)
3060{
3061 return cmod->cmod_flags != 0
3062 || cmod->cmod_split != 0
3063 || cmod->cmod_verbose != 0
3064 || cmod->cmod_tab != 0
3065 || cmod->cmod_filter_regmatch.regprog != NULL;
3066}
3067
3068/*
3069 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3070 */
3071 int
3072cmdmod_error(void)
3073{
3074 if (in_vim9script() && has_cmdmod(&cmdmod))
3075 {
3076 emsg(_(e_misplaced_command_modifier));
3077 return TRUE;
3078 }
3079 return FALSE;
3080}
3081
3082/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003083 * Apply the command modifiers. Saves current state in "cmdmod", call
3084 * undo_cmdmod() later.
3085 */
3086 void
3087apply_cmdmod(cmdmod_T *cmod)
3088{
3089#ifdef HAVE_SANDBOX
3090 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3091 {
3092 ++sandbox;
3093 cmod->cmod_did_sandbox = TRUE;
3094 }
3095#endif
3096 if (cmod->cmod_verbose > 0)
3097 {
3098 if (cmod->cmod_verbose_save == 0)
3099 cmod->cmod_verbose_save = p_verbose + 1;
3100 p_verbose = cmod->cmod_verbose;
3101 }
3102
3103 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3104 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003105 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003106 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003107 cmod->cmod_save_msg_scroll = msg_scroll;
3108 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003109 if (cmod->cmod_flags & CMOD_SILENT)
3110 ++msg_silent;
3111 if (cmod->cmod_flags & CMOD_UNSILENT)
3112 msg_silent = 0;
3113
3114 if (cmod->cmod_flags & CMOD_ERRSILENT)
3115 {
3116 ++emsg_silent;
3117 ++cmod->cmod_did_esilent;
3118 }
3119
Bram Moolenaare1004402020-10-24 20:49:43 +02003120 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003121 {
3122 // Set 'eventignore' to "all".
3123 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003124 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003125 set_string_option_direct((char_u *)"ei", -1,
3126 (char_u *)"all", OPT_FREE, SID_NONE);
3127 }
3128}
3129
3130/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003131 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003132 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003133 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003134undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003135{
Bram Moolenaare1004402020-10-24 20:49:43 +02003136 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003137 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003138 p_verbose = cmod->cmod_verbose_save - 1;
3139 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003140 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003141
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003142#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003143 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003144 {
3145 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003146 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003147 }
3148#endif
3149
Bram Moolenaare1004402020-10-24 20:49:43 +02003150 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003151 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003152 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003153 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3154 OPT_FREE, SID_NONE);
3155 free_string_option(cmod->cmod_save_ei);
3156 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003157 }
3158
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003159 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003160
Bram Moolenaare1004402020-10-24 20:49:43 +02003161 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003162 {
3163 // messages could be enabled for a serious error, need to check if the
3164 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003165 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3166 msg_silent = cmod->cmod_save_msg_silent - 1;
3167 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003168 if (emsg_silent < 0)
3169 emsg_silent = 0;
3170 // Restore msg_scroll, it's set by file I/O commands, even when no
3171 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003172 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003173
3174 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3175 // somewhere in the line. Put it back in the first column.
3176 if (redirecting())
3177 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003178
Bram Moolenaare1004402020-10-24 20:49:43 +02003179 cmod->cmod_save_msg_silent = 0;
3180 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003181 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003182}
3183
3184/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003185 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003186 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003187 * Return FAIL and set "errormsg" or return OK.
3188 */
3189 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003190parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003191{
3192 int address_count = 1;
3193 linenr_T lnum;
3194
3195 // Repeat for all ',' or ';' separated addresses.
3196 for (;;)
3197 {
3198 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003199 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003200 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003201 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003202 eap->addr_count == 0, address_count++);
3203 if (eap->cmd == NULL) // error detected
3204 return FAIL;
3205 if (lnum == MAXLNUM)
3206 {
3207 if (*eap->cmd == '%') // '%' - all lines
3208 {
3209 ++eap->cmd;
3210 switch (eap->addr_type)
3211 {
3212 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003213 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003214 eap->line1 = 1;
3215 eap->line2 = curbuf->b_ml.ml_line_count;
3216 break;
3217 case ADDR_LOADED_BUFFERS:
3218 {
3219 buf_T *buf = firstbuf;
3220
3221 while (buf->b_next != NULL
3222 && buf->b_ml.ml_mfp == NULL)
3223 buf = buf->b_next;
3224 eap->line1 = buf->b_fnum;
3225 buf = lastbuf;
3226 while (buf->b_prev != NULL
3227 && buf->b_ml.ml_mfp == NULL)
3228 buf = buf->b_prev;
3229 eap->line2 = buf->b_fnum;
3230 break;
3231 }
3232 case ADDR_BUFFERS:
3233 eap->line1 = firstbuf->b_fnum;
3234 eap->line2 = lastbuf->b_fnum;
3235 break;
3236 case ADDR_WINDOWS:
3237 case ADDR_TABS:
3238 if (IS_USER_CMDIDX(eap->cmdidx))
3239 {
3240 eap->line1 = 1;
3241 eap->line2 = eap->addr_type == ADDR_WINDOWS
3242 ? LAST_WIN_NR : LAST_TAB_NR;
3243 }
3244 else
3245 {
3246 // there is no Vim command which uses '%' and
3247 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003248 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003249 return FAIL;
3250 }
3251 break;
3252 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003253 case ADDR_UNSIGNED:
3254 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003255 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003256 return FAIL;
3257 case ADDR_ARGUMENTS:
3258 if (ARGCOUNT == 0)
3259 eap->line1 = eap->line2 = 0;
3260 else
3261 {
3262 eap->line1 = 1;
3263 eap->line2 = ARGCOUNT;
3264 }
3265 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003266 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003267#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003268 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003269 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003270 if (eap->line2 == 0)
3271 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003272#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003273 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003274 case ADDR_NONE:
3275 // Will give an error later if a range is found.
3276 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003277 }
3278 ++eap->addr_count;
3279 }
3280 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3281 {
3282 pos_T *fp;
3283
3284 // '*' - visual area
3285 if (eap->addr_type != ADDR_LINES)
3286 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003287 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003288 return FAIL;
3289 }
3290
3291 ++eap->cmd;
3292 if (!eap->skip)
3293 {
3294 fp = getmark('<', FALSE);
3295 if (check_mark(fp) == FAIL)
3296 return FAIL;
3297 eap->line1 = fp->lnum;
3298 fp = getmark('>', FALSE);
3299 if (check_mark(fp) == FAIL)
3300 return FAIL;
3301 eap->line2 = fp->lnum;
3302 ++eap->addr_count;
3303 }
3304 }
3305 }
3306 else
3307 eap->line2 = lnum;
3308 eap->addr_count++;
3309
3310 if (*eap->cmd == ';')
3311 {
3312 if (!eap->skip)
3313 {
3314 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003315 // Don't leave the cursor on an illegal line or column, but do
3316 // accept zero as address, so 0;/PATTERN/ works correctly.
3317 if (eap->line2 > 0)
3318 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003319 }
3320 }
3321 else if (*eap->cmd != ',')
3322 break;
3323 ++eap->cmd;
3324 }
3325
3326 // One address given: set start and end lines.
3327 if (eap->addr_count == 1)
3328 {
3329 eap->line1 = eap->line2;
3330 // ... but only implicit: really no address given
3331 if (lnum == MAXLNUM)
3332 eap->addr_count = 0;
3333 }
3334 return OK;
3335}
3336
3337/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003338 * Append "cmd" to the error message in IObuff.
3339 * Takes care of limiting the length and handling 0xa0, which would be
3340 * invisible otherwise.
3341 */
3342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003343append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003344{
3345 char_u *s = cmd;
3346 char_u *d;
3347
3348 STRCAT(IObuff, ": ");
3349 d = IObuff + STRLEN(IObuff);
3350 while (*s != NUL && d - IObuff < IOSIZE - 7)
3351 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003352 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003353 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003354 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003355 STRCPY(d, "<a0>");
3356 d += 4;
3357 }
3358 else
3359 MB_COPY_CHAR(s, d);
3360 }
3361 *d = NUL;
3362}
3363
3364/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003365 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3366 * the name. Otherwise just return "start".
3367 */
3368 char_u *
3369skip_option_env_lead(char_u *start)
3370{
3371 char_u *name = start;
3372
3373 if (*start == '&')
3374 {
3375 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3376 name += 3;
3377 else
3378 name += 1;
3379 }
3380 else if (*start == '$')
3381 name += 1;
3382 return name;
3383}
3384
3385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003387 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003388 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003389 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 *
3391 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3392 * assignment without "let". Sets eap->cmdidx to the command while returning
3393 * "eap->cmd".
3394 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 * Returns NULL for an ambiguous user command.
3396 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 char_u *
3398find_ex_command(
3399 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003400 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003401 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003402 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403{
3404 int len;
3405 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003406 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003407#ifndef FEAT_EVAL
3408 int vim9 = FALSE;
3409#else
3410 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 /*
3413 * Recognize a Vim9 script function/method call and assignment:
3414 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3415 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003416 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003417 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003419 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420
Bram Moolenaar83144542020-08-02 20:40:43 +02003421 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003422 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003424 int oplen;
3425 int heredoc;
3426 char_u *swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003428 if (
3429 // "(..." is an expression.
3430 // "funcname(" is always a function call.
3431 *p == '('
3432 || (p == eap->cmd
3433 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003434 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003435 *eap->cmd == '{'
3436 // "'string'->func()" is an expression.
3437 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003438 // '"string"->func()' is an expression.
3439 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003440 // "g:varname" is an expression.
3441 || eap->cmd[1] == ':'
3442 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003443 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003444 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003445 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003446 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3447 {
3448 // "{" by itself is the start of a block.
3449 eap->cmdidx = CMD_block;
3450 return eap->cmd + 1;
3451 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003452 eap->cmdidx = CMD_eval;
3453 return eap->cmd;
3454 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003455
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003456 if (p != eap->cmd && (
3457 // "varname[]" is an expression.
3458 *p == '['
3459 // "varname.key" is an expression.
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02003460 || (*p == '.' && (ASCII_ISALPHA(p[1])
3461 || p[1] == '_'))))
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003462 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003463 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003464
3465 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003466 // Skip over the whole thing, it can be:
3467 // name.member = val
3468 // name[a : b] = val
3469 // name[idx] = val
3470 // name[idx].member = val
3471 // etc.
3472 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003473 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003474 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003475 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003476 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003477 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003478 || (after[0] == '.' && after[1] == '.'
3479 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003480 eap->cmdidx = CMD_var;
3481 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003482 --emsg_silent;
3483 return eap->cmd;
3484 }
3485
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003486 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3487 // an assignment.
3488 // If there is no line break inside the "[...]" then "p" is
3489 // advanced to after the "]" by to_name_const_end(): check if a "="
3490 // follows.
3491 // If "[...]" has a line break "p" still points at the "[" and it
3492 // can't be an assignment.
3493 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003494 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003495 char_u *eq;
3496
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003497 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003498 if (p == eap->cmd && *p == '[')
3499 {
3500 int count = 0;
3501 int semicolon = FALSE;
3502
3503 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3504 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003505 eq = p;
3506 if (eq != NULL)
3507 {
3508 eq = skipwhite(eq);
3509 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3510 ++eq;
3511 }
3512 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003513 {
3514 eap->cmdidx = CMD_eval;
3515 return eap->cmd;
3516 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003517 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003518 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003519 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003520 return eap->cmd;
3521 }
3522 }
3523
3524 // Recognize an assignment if we recognize the variable name:
3525 // "g:var = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003526 // "var = expr" where "var" is a variable name or we are skipping
3527 // (variable declaration might have been skipped).
Bram Moolenaar83144542020-08-02 20:40:43 +02003528 if (*eap->cmd == '@')
3529 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003530 oplen = assignment_len(skipwhite(p), &heredoc);
3531 if (oplen > 0)
3532 {
3533 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3534 || *eap->cmd == '&'
3535 || *eap->cmd == '$'
3536 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003537 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003538 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003539 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003540 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003541 return eap->cmd;
3542 }
3543 }
3544
3545 // Recognize using a type for a w:, b:, t: or g: variable:
3546 // "w:varname: number = 123".
3547 if (eap->cmd[1] == ':' && *p == ':')
3548 {
3549 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003550 return eap->cmd;
3551 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003552 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003553
3554 // If it is an ID it might be a variable with an operator on the next
3555 // line, if the variable exists it can't be an Ex command.
3556 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003557 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003558 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3559 {
3560 eap->cmdidx = CMD_eval;
3561 return eap->cmd;
3562 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003563
3564 // Check for "++nr" and "--nr".
3565 if (p == eap->cmd && p[0] == p[1] && (*p == '+' || *p == '-'))
3566 {
3567 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3568 return eap->cmd + 2;
3569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 }
3571#endif
3572
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 /*
3574 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003575 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003576 * - The 'k' command can directly be followed by any character.
3577 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003579 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003581 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 */
3583 p = eap->cmd;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003584 if (!vim9 && *p == 'k')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
3586 eap->cmdidx = CMD_k;
3587 ++p;
3588 }
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003589 else if (!vim9
3590 && p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003591 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3592 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 || p[1] == 'g'
3594 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3595 || p[1] == 'I'
3596 || (p[1] == 'r' && p[2] != 'e')))
3597 {
3598 eap->cmdidx = CMD_substitute;
3599 ++p;
3600 }
3601 else
3602 {
3603 while (ASCII_ISALPHA(*p))
3604 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003605 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003606 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003608 while (ASCII_ISALNUM(*p))
3609 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 }
3611 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3612 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003613 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 ++p;
3615 while (ASCII_ISALPHA(*p))
3616 ++p;
3617 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003619 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003620 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 ++p;
3622 len = (int)(p - eap->cmd);
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003623 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003624 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003625 // Check for ":dl", ":dell", etc. to ":deletel": that's
3626 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003627 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003628 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003629 break;
3630 if (i == len - 1)
3631 {
3632 --len;
3633 if (p[-1] == 'l')
3634 eap->flags |= EXFLAG_LIST;
3635 else
3636 eap->flags |= EXFLAG_PRINT;
3637 }
3638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003640 if (ASCII_ISLOWER(eap->cmd[0]))
3641 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003642 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003643 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003644
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003645 if (command_count != (int)CMD_SIZE)
3646 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003647 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003648 getout(1);
3649 }
3650
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003651 // Use a precomputed index for fast look-up in cmdnames[]
3652 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003653 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3654 if (ASCII_ISLOWER(c2))
3655 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3656 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003657 else if (ASCII_ISUPPER(eap->cmd[0]))
3658 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003660 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
3662 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3663 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3664 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3665 (size_t)len) == 0)
3666 {
3667#ifdef FEAT_EVAL
3668 if (full != NULL
3669 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3670 *full = TRUE;
3671#endif
3672 break;
3673 }
3674
Bram Moolenaar95388e32020-11-20 21:07:00 +01003675 // Not not recognize ":*" as the star command unless '*' is in
3676 // 'cpoptions'.
3677 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3678 p = eap->cmd;
3679
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003680 // Look for a user defined command as a last resort. Let ":Print" be
3681 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003682 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3683 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003685 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 while (ASCII_ISALNUM(*p))
3687 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003688 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003690 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 eap->cmdidx = CMD_SIZE;
3692 }
3693
Bram Moolenaar373863e2020-09-26 17:20:53 +02003694 // ":fina" means ":finally" for backwards compatibility.
3695 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3696 eap->cmdidx = CMD_finally;
3697
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003698#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003699 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003700 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003701 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003702 && (eap->cmdidx < 0 ||
3703 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003704 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003705 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3706
3707 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003708 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003709 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003710 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003711#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003712
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 return p;
3714}
3715
3716#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003717static struct cmdmod
3718{
3719 char *name;
3720 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003721 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003722} cmdmods[] = {
3723 {"aboveleft", 3, FALSE},
3724 {"belowright", 3, FALSE},
3725 {"botright", 2, FALSE},
3726 {"browse", 3, FALSE},
3727 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003728 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003729 {"hide", 3, FALSE},
3730 {"keepalt", 5, FALSE},
3731 {"keepjumps", 5, FALSE},
3732 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003733 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003734 {"leftabove", 5, FALSE},
3735 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003736 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003737 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003738 {"rightbelow", 6, FALSE},
3739 {"sandbox", 3, FALSE},
3740 {"silent", 3, FALSE},
3741 {"tab", 3, TRUE},
3742 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003743 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003744 {"verbose", 4, TRUE},
3745 {"vertical", 4, FALSE},
3746};
3747
3748/*
3749 * Return length of a command modifier (including optional count).
3750 * Return zero when it's not a modifier.
3751 */
3752 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003753modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003754{
3755 int i, j;
3756 char_u *p = cmd;
3757
3758 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003759 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003760 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003761 {
3762 for (j = 0; p[j] != NUL; ++j)
3763 if (p[j] != cmdmods[i].name[j])
3764 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003765 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003766 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003767 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003768 }
3769 return 0;
3770}
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772/*
3773 * Return > 0 if an Ex command "name" exists.
3774 * Return 2 if there is an exact match.
3775 * Return 3 if there is an ambiguous match.
3776 */
3777 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003778cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
3780 exarg_T ea;
3781 int full = FALSE;
3782 int i;
3783 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003784 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003786 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003787 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
3789 for (j = 0; name[j] != NUL; ++j)
3790 if (name[j] != cmdmods[i].name[j])
3791 break;
3792 if (name[j] == NUL && j >= cmdmods[i].minlen)
3793 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3794 }
3795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003796 // Check built-in commands and user defined commands.
3797 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003798 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003801 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003803 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3804 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003805 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003806 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3808}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003809
3810/*
3811 * "fullcommand" function
3812 */
3813 void
3814f_fullcommand(typval_T *argvars, typval_T *rettv)
3815{
3816 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003817 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003818 char_u *p;
3819
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003820 rettv->v_type = VAR_STRING;
3821 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003822
3823 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3824 return;
3825
3826 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003827 if (name == NULL)
3828 return;
3829
3830 while (*name != NUL && *name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003831 name++;
3832 name = skip_range(name, TRUE, NULL);
3833
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003834 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3835 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003836 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003837 p = find_ex_command(&ea, NULL, NULL, NULL);
3838 if (p == NULL || ea.cmdidx == CMD_SIZE)
3839 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003840 if (in_vim9script())
3841 {
3842 int res;
3843
3844 ++emsg_silent;
3845 res = not_in_vim9(&ea);
3846 --emsg_silent;
3847
3848 if (res == FAIL)
3849 return;
3850 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003851
3852 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
3853 ? get_user_commands(NULL, ea.useridx)
3854 : cmdnames[ea.cmdidx].cmd_name);
3855}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856#endif
3857
Bram Moolenaard0190392019-08-23 21:17:35 +02003858 cmdidx_T
3859excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860{
Bram Moolenaard0190392019-08-23 21:17:35 +02003861 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
Bram Moolenaard0190392019-08-23 21:17:35 +02003863 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3864 idx = (cmdidx_T)((int)idx + 1))
3865 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 break;
3867
Bram Moolenaard0190392019-08-23 21:17:35 +02003868 return idx;
3869}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870
Bram Moolenaard0190392019-08-23 21:17:35 +02003871 long
3872excmd_get_argt(cmdidx_T idx)
3873{
3874 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875}
3876
3877/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003878 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 *
3880 * Backslashed delimiters after / or ? will be skipped, and commands will
3881 * not be expanded between /'s and ?'s or after "'".
3882 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003883 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 * Returns the "cmd" pointer advanced to beyond the range.
3885 */
3886 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003887skip_range(
3888 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003889 int skip_star, // skip "*" used for Visual range
3890 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003892 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003894 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003896 if (*cmd == '\\')
3897 {
3898 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3899 ++cmd;
3900 else
3901 break;
3902 }
3903 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
3905 if (*++cmd == NUL && ctx != NULL)
3906 *ctx = EXPAND_NOTHING;
3907 }
3908 else if (*cmd == '/' || *cmd == '?')
3909 {
3910 delim = *cmd++;
3911 while (*cmd != NUL && *cmd != delim)
3912 if (*cmd++ == '\\' && *cmd != NUL)
3913 ++cmd;
3914 if (*cmd == NUL && ctx != NULL)
3915 *ctx = EXPAND_NOTHING;
3916 }
3917 if (*cmd != NUL)
3918 ++cmd;
3919 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003920
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003921 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003922 while (*cmd == ':')
3923 cmd = skipwhite(cmd + 1);
3924
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003925 // Skip "*" used for Visual range.
3926 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3927 cmd = skipwhite(cmd + 1);
3928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 return cmd;
3930}
3931
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003932 static void
3933addr_error(cmd_addr_T addr_type)
3934{
3935 if (addr_type == ADDR_NONE)
3936 emsg(_(e_norange));
3937 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003938 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003939}
3940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003942 * Return the default address for an address type.
3943 */
3944 static linenr_T
3945default_address(exarg_T *eap)
3946{
3947 linenr_T lnum = 0;
3948
3949 switch (eap->addr_type)
3950 {
3951 case ADDR_LINES:
3952 case ADDR_OTHER:
3953 // Default is the cursor line number. Avoid using an invalid
3954 // line number though.
3955 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3956 lnum = curbuf->b_ml.ml_line_count;
3957 else
3958 lnum = curwin->w_cursor.lnum;
3959 break;
3960 case ADDR_WINDOWS:
3961 lnum = CURRENT_WIN_NR;
3962 break;
3963 case ADDR_ARGUMENTS:
3964 lnum = curwin->w_arg_idx + 1;
3965 if (lnum > ARGCOUNT)
3966 lnum = ARGCOUNT;
3967 break;
3968 case ADDR_LOADED_BUFFERS:
3969 case ADDR_BUFFERS:
3970 lnum = curbuf->b_fnum;
3971 break;
3972 case ADDR_TABS:
3973 lnum = CURRENT_TAB_NR;
3974 break;
3975 case ADDR_TABS_RELATIVE:
3976 case ADDR_UNSIGNED:
3977 lnum = 1;
3978 break;
3979 case ADDR_QUICKFIX:
3980#ifdef FEAT_QUICKFIX
3981 lnum = qf_get_cur_idx(eap);
3982#endif
3983 break;
3984 case ADDR_QUICKFIX_VALID:
3985#ifdef FEAT_QUICKFIX
3986 lnum = qf_get_cur_valid_idx(eap);
3987#endif
3988 break;
3989 case ADDR_NONE:
3990 // Will give an error later if a range is found.
3991 break;
3992 }
3993 return lnum;
3994}
3995
3996/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003997 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 *
3999 * Set ptr to the next character after the part that was interpreted.
4000 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004001 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 *
4003 * Return MAXLNUM when no Ex address was found.
4004 */
4005 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004006get_address(
4007 exarg_T *eap UNUSED,
4008 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004009 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004010 int skip, // only skip the address, don't use it
4011 int silent, // no errors or side effects
4012 int to_other_file, // flag: may jump to other file
4013 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
4015 int c;
4016 int i;
4017 long n;
4018 char_u *cmd;
4019 pos_T pos;
4020 pos_T *fp;
4021 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004022 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
4024 cmd = skipwhite(*ptr);
4025 lnum = MAXLNUM;
4026 do
4027 {
4028 switch (*cmd)
4029 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004030 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004031 ++cmd;
4032 switch (addr_type)
4033 {
4034 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004035 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 lnum = curwin->w_cursor.lnum;
4037 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004038 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004039 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004040 break;
4041 case ADDR_ARGUMENTS:
4042 lnum = curwin->w_arg_idx + 1;
4043 break;
4044 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004045 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004046 lnum = curbuf->b_fnum;
4047 break;
4048 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004049 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004050 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004051 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004052 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004053 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004054 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004055 cmd = NULL;
4056 goto error;
4057 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004058 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004059#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004060 lnum = qf_get_cur_idx(eap);
4061#endif
4062 break;
4063 case ADDR_QUICKFIX_VALID:
4064#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004065 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004066#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004067 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004068 }
4069 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004071 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004072 ++cmd;
4073 switch (addr_type)
4074 {
4075 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004076 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 lnum = curbuf->b_ml.ml_line_count;
4078 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004079 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004080 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004081 break;
4082 case ADDR_ARGUMENTS:
4083 lnum = ARGCOUNT;
4084 break;
4085 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004086 buf = lastbuf;
4087 while (buf->b_ml.ml_mfp == NULL)
4088 {
4089 if (buf->b_prev == NULL)
4090 break;
4091 buf = buf->b_prev;
4092 }
4093 lnum = buf->b_fnum;
4094 break;
4095 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004096 lnum = lastbuf->b_fnum;
4097 break;
4098 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004099 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004100 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004101 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004102 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004103 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004104 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004105 cmd = NULL;
4106 goto error;
4107 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004108 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004109#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004110 lnum = qf_get_size(eap);
4111 if (lnum == 0)
4112 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004113#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004114 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004115 case ADDR_QUICKFIX_VALID:
4116#ifdef FEAT_QUICKFIX
4117 lnum = qf_get_valid_size(eap);
4118 if (lnum == 0)
4119 lnum = 1;
4120#endif
4121 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004122 }
4123 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004125 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004126 if (*++cmd == NUL)
4127 {
4128 cmd = NULL;
4129 goto error;
4130 }
4131 if (addr_type != ADDR_LINES)
4132 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004133 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004134 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004135 goto error;
4136 }
4137 if (skip)
4138 ++cmd;
4139 else
4140 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004141 // Only accept a mark in another file when it is
4142 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004143 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4144 ++cmd;
4145 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004146 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004147 lnum = curwin->w_cursor.lnum;
4148 else
4149 {
4150 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
4152 cmd = NULL;
4153 goto error;
4154 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004155 lnum = fp->lnum;
4156 }
4157 }
4158 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
4160 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004161 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004162 c = *cmd++;
4163 if (addr_type != ADDR_LINES)
4164 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004165 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004166 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004167 goto error;
4168 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004169 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004170 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004171 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004172 if (*cmd == c)
4173 ++cmd;
4174 }
4175 else
4176 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004177 int flags;
4178
4179 pos = curwin->w_cursor; // save curwin->w_cursor
4180
4181 // When '/' or '?' follows another address, start from
4182 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004183 if (lnum != MAXLNUM)
4184 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004185
4186 // Start a forward search at the end of the line (unless
4187 // before the first line).
4188 // Start a backward search at the start of the line.
4189 // This makes sure we never match in the current
4190 // line, and can match anywhere in the
4191 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004192 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004193 curwin->w_cursor.col = MAXCOL;
4194 else
4195 curwin->w_cursor.col = 0;
4196 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004197 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004198 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004199 {
4200 curwin->w_cursor = pos;
4201 cmd = NULL;
4202 goto error;
4203 }
4204 lnum = curwin->w_cursor.lnum;
4205 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004206 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004207 cmd += searchcmdlen;
4208 }
4209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004211 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004212 ++cmd;
4213 if (addr_type != ADDR_LINES)
4214 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004215 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004216 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004217 goto error;
4218 }
4219 if (*cmd == '&')
4220 i = RE_SUBST;
4221 else if (*cmd == '?' || *cmd == '/')
4222 i = RE_SEARCH;
4223 else
4224 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004225 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004226 cmd = NULL;
4227 goto error;
4228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004230 if (!skip)
4231 {
4232 /*
4233 * When search follows another address, start from
4234 * there.
4235 */
4236 if (lnum != MAXLNUM)
4237 pos.lnum = lnum;
4238 else
4239 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004241 /*
4242 * Start the search just like for the above
4243 * do_search().
4244 */
4245 if (*cmd != '?')
4246 pos.col = MAXCOL;
4247 else
4248 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004249 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004250 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004251 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004252 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004253 lnum = pos.lnum;
4254 else
4255 {
4256 cmd = NULL;
4257 goto error;
4258 }
4259 }
4260 ++cmd;
4261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
4263 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004264 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004265 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267
4268 for (;;)
4269 {
4270 cmd = skipwhite(cmd);
4271 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4272 break;
4273
4274 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004275 {
4276 switch (addr_type)
4277 {
4278 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004279 case ADDR_OTHER:
4280 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004281 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004282 break;
4283 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004284 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004285 break;
4286 case ADDR_ARGUMENTS:
4287 lnum = curwin->w_arg_idx + 1;
4288 break;
4289 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004290 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004291 lnum = curbuf->b_fnum;
4292 break;
4293 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004294 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004295 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004296 case ADDR_TABS_RELATIVE:
4297 lnum = 1;
4298 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004299 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004300#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004301 lnum = qf_get_cur_idx(eap);
4302#endif
4303 break;
4304 case ADDR_QUICKFIX_VALID:
4305#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004306 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004307#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004308 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004309 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004310 case ADDR_UNSIGNED:
4311 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004312 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004313 }
4314 }
4315
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004317 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 else
4319 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004320 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 n = 1;
4322 else
4323 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004324
4325 if (addr_type == ADDR_TABS_RELATIVE)
4326 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004327 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004328 cmd = NULL;
4329 goto error;
4330 }
4331 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004332 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004333 lnum = compute_buffer_local_count(
4334 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004336 {
4337#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004338 // Relative line addressing, need to adjust for folded lines
4339 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004340 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4341 && address_count >= 2)
4342 (void)hasFolding(lnum, NULL, &lnum);
4343#endif
4344 if (i == '-')
4345 lnum -= n;
4346 else
4347 lnum += n;
4348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350 } while (*cmd == '/' || *cmd == '?');
4351
4352error:
4353 *ptr = cmd;
4354 return lnum;
4355}
4356
4357/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004358 * Set eap->line1 and eap->line2 to the whole range.
4359 * Used for commands with the EX_DFLALL flag and no range given.
4360 */
4361 static void
4362address_default_all(exarg_T *eap)
4363{
4364 eap->line1 = 1;
4365 switch (eap->addr_type)
4366 {
4367 case ADDR_LINES:
4368 case ADDR_OTHER:
4369 eap->line2 = curbuf->b_ml.ml_line_count;
4370 break;
4371 case ADDR_LOADED_BUFFERS:
4372 {
4373 buf_T *buf = firstbuf;
4374
4375 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4376 buf = buf->b_next;
4377 eap->line1 = buf->b_fnum;
4378 buf = lastbuf;
4379 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4380 buf = buf->b_prev;
4381 eap->line2 = buf->b_fnum;
4382 }
4383 break;
4384 case ADDR_BUFFERS:
4385 eap->line1 = firstbuf->b_fnum;
4386 eap->line2 = lastbuf->b_fnum;
4387 break;
4388 case ADDR_WINDOWS:
4389 eap->line2 = LAST_WIN_NR;
4390 break;
4391 case ADDR_TABS:
4392 eap->line2 = LAST_TAB_NR;
4393 break;
4394 case ADDR_TABS_RELATIVE:
4395 eap->line2 = 1;
4396 break;
4397 case ADDR_ARGUMENTS:
4398 if (ARGCOUNT == 0)
4399 eap->line1 = eap->line2 = 0;
4400 else
4401 eap->line2 = ARGCOUNT;
4402 break;
4403 case ADDR_QUICKFIX_VALID:
4404#ifdef FEAT_QUICKFIX
4405 eap->line2 = qf_get_valid_size(eap);
4406 if (eap->line2 == 0)
4407 eap->line2 = 1;
4408#endif
4409 break;
4410 case ADDR_NONE:
4411 case ADDR_UNSIGNED:
4412 case ADDR_QUICKFIX:
4413 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4414 break;
4415 }
4416}
4417
4418
4419/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004420 * Get flags from an Ex command argument.
4421 */
4422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004423get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004424{
4425 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4426 {
4427 if (*eap->arg == 'l')
4428 eap->flags |= EXFLAG_LIST;
4429 else if (*eap->arg == 'p')
4430 eap->flags |= EXFLAG_PRINT;
4431 else
4432 eap->flags |= EXFLAG_NR;
4433 eap->arg = skipwhite(eap->arg + 1);
4434 }
4435}
4436
4437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 * Function called for command which is Not Implemented. NI!
4439 */
4440 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004441ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442{
4443 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004444 eap->errmsg =
4445 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446}
4447
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004448#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449/*
4450 * Function called for script command which is Not Implemented. NI!
4451 * Skips over ":perl <<EOF" constructs.
4452 */
4453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004454ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455{
4456 if (!eap->skip)
4457 ex_ni(eap);
4458 else
4459 vim_free(script_get(eap, eap->arg));
4460}
4461#endif
4462
4463/*
4464 * Check range in Ex command for validity.
4465 * Return NULL when valid, error message when invalid.
4466 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004467 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004468invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004470 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if ( eap->line1 < 0
4473 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004474 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004475 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004476
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004477 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004478 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004479 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004480 {
4481 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004482 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004483#ifdef FEAT_DIFF
4484 + (eap->cmdidx == CMD_diffget)
4485#endif
4486 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004487 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004488 break;
4489 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004490 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004491 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004492 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004493 break;
4494 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004495 // Only a boundary check, not whether the buffers actually
4496 // exist.
4497 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004498 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004499 break;
4500 case ADDR_LOADED_BUFFERS:
4501 buf = firstbuf;
4502 while (buf->b_ml.ml_mfp == NULL)
4503 {
4504 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004505 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004506 buf = buf->b_next;
4507 }
4508 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004509 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004510 buf = lastbuf;
4511 while (buf->b_ml.ml_mfp == NULL)
4512 {
4513 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004514 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004515 buf = buf->b_prev;
4516 }
4517 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004518 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004519 break;
4520 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004521 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004522 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004523 break;
4524 case ADDR_TABS:
4525 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004526 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004527 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004528 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004529 case ADDR_OTHER:
4530 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004531 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004532 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004533#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004534 // No error for value that is too big, will use the last entry.
4535 if (eap->line2 <= 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004536 return _(e_invalid_range);
Bram Moolenaare906c502015-09-09 21:10:39 +02004537#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004538 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004539 case ADDR_QUICKFIX_VALID:
4540#ifdef FEAT_QUICKFIX
4541 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4542 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004543 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004544#endif
4545 break;
4546 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004547 case ADDR_NONE:
4548 // Will give an error elsewhere.
4549 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004550 }
4551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 return NULL;
4553}
4554
4555/*
4556 * Correct the range for zero line number, if required.
4557 */
4558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004559correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004561 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
4563 if (eap->line1 == 0)
4564 eap->line1 = 1;
4565 if (eap->line2 == 0)
4566 eap->line2 = 1;
4567 }
4568}
4569
Bram Moolenaar748bf032005-02-02 23:04:36 +00004570#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004571/*
4572 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4573 * pattern. Otherwise return eap->arg.
4574 */
4575 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004576skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004577{
4578 char_u *p = eap->arg;
4579
Bram Moolenaara37420f2006-02-04 22:37:47 +00004580 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4581 || eap->cmdidx == CMD_vimgrepadd
4582 || eap->cmdidx == CMD_lvimgrepadd
4583 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004584 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004585 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004586 if (p == NULL)
4587 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004588 }
4589 return p;
4590}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004591
4592/*
4593 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4594 * in the command line, so that things like % get expanded.
4595 */
4596 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004597replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004598{
4599 char_u *new_cmdline;
4600 char_u *program;
4601 char_u *pos;
4602 char_u *ptr;
4603 int len;
4604 int i;
4605
4606 /*
4607 * Don't do it when ":vimgrep" is used for ":grep".
4608 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004609 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4610 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4611 || eap->cmdidx == CMD_grepadd
4612 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004613 && !grep_internal(eap->cmdidx))
4614 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004615 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4616 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004617 {
4618 if (*curbuf->b_p_gp == NUL)
4619 program = p_gp;
4620 else
4621 program = curbuf->b_p_gp;
4622 }
4623 else
4624 {
4625 if (*curbuf->b_p_mp == NUL)
4626 program = p_mp;
4627 else
4628 program = curbuf->b_p_mp;
4629 }
4630
4631 p = skipwhite(p);
4632
4633 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4634 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004635 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004636 i = 1;
4637 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4638 ++i;
4639 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004640 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004641 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004642 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004643 ptr = new_cmdline;
4644 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4645 {
4646 i = (int)(pos - program);
4647 STRNCPY(ptr, program, i);
4648 STRCPY(ptr += i, p);
4649 ptr += len;
4650 program = pos + 2;
4651 }
4652 STRCPY(ptr, program);
4653 }
4654 else
4655 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004656 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004657 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004658 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004659 STRCPY(new_cmdline, program);
4660 STRCAT(new_cmdline, " ");
4661 STRCAT(new_cmdline, p);
4662 }
4663 msg_make(p);
4664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004665 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004666 vim_free(*cmdlinep);
4667 *cmdlinep = new_cmdline;
4668 p = new_cmdline;
4669 }
4670 return p;
4671}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004672#endif
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674/*
4675 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004676 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 * Return FAIL for failure, OK otherwise.
4678 */
4679 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004680expand_filename(
4681 exarg_T *eap,
4682 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004683 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004685 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 char_u *repl;
4687 int srclen;
4688 char_u *p;
4689 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004690 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691
Bram Moolenaar748bf032005-02-02 23:04:36 +00004692#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004693 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004694 p = skip_grep_pat(eap);
4695#else
4696 p = eap->arg;
4697#endif
4698
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 /*
4700 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4701 * the file name contains a wildcard it should not cause expanding.
4702 * (it will be expanded anyway if there is a wildcard before replacing).
4703 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004704 has_wildcards = mch_has_wildcard(p);
4705 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004707#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004708 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004709 if (p[0] == '`' && p[1] == '=')
4710 {
4711 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004712 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004713 if (*p == '`')
4714 ++p;
4715 continue;
4716 }
4717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 /*
4719 * Quick check if this cannot be the start of a special string.
4720 * Also removes backslash before '%', '#' and '<'.
4721 */
4722 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4723 {
4724 ++p;
4725 continue;
4726 }
4727
4728 /*
4729 * Try to find a match at this position.
4730 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004731 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4732 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004733 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004735 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 {
4737 p += srclen;
4738 continue;
4739 }
4740
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004741 // Wildcards won't be expanded below, the replacement is taken
4742 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004743 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4744 {
4745 char_u *l = repl;
4746
4747 repl = expand_env_save(repl);
4748 vim_free(l);
4749 }
4750
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004751 // Need to escape white space et al. with a backslash.
4752 // Don't do this for:
4753 // - replacement that already has been escaped: "##"
4754 // - shell commands (may have to use quotes instead).
4755 // - non-unix systems when there is a single argument (spaces don't
4756 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004758 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 && eap->cmdidx != CMD_grep
4761 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004762 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004763 && eap->cmdidx != CMD_lgrep
4764 && eap->cmdidx != CMD_lgrepadd
4765 && eap->cmdidx != CMD_lmake
4766 && eap->cmdidx != CMD_make
4767 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004769 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770#endif
4771 )
4772 {
4773 char_u *l;
4774#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004775 // Don't escape a backslash here, because rem_backslash() doesn't
4776 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 static char_u *nobslash = (char_u *)" \t\"|";
4778# define ESCAPE_CHARS nobslash
4779#else
4780# define ESCAPE_CHARS escape_chars
4781#endif
4782
4783 for (l = repl; *l; ++l)
4784 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4785 {
4786 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4787 if (l != NULL)
4788 {
4789 vim_free(repl);
4790 repl = l;
4791 }
4792 break;
4793 }
4794 }
4795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004796 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004797 if ((eap->usefilter || eap->cmdidx == CMD_bang
4798 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004799 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
4801 char_u *l;
4802
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004803 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 if (l != NULL)
4805 {
4806 vim_free(repl);
4807 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809 }
4810
4811 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4812 vim_free(repl);
4813 if (p == NULL)
4814 return FAIL;
4815 }
4816
4817 /*
4818 * One file argument: Expand wildcards.
4819 * Don't do this with ":r !command" or ":w !command".
4820 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004821 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
4823 /*
4824 * May do this twice:
4825 * 1. Replace environment variables.
4826 * 2. Replace any other wildcards, remove backslashes.
4827 */
4828 for (n = 1; n <= 2; ++n)
4829 {
4830 if (n == 2)
4831 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 /*
4833 * Halve the number of backslashes (this is Vi compatible).
4834 * For Unix and OS/2, when wildcards are expanded, this is
4835 * done by ExpandOne() below.
4836 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004837#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 if (!has_wildcards)
4839#endif
4840 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842
4843 if (has_wildcards)
4844 {
4845 if (n == 1)
4846 {
4847 /*
4848 * First loop: May expand environment variables. This
4849 * can be done much faster with expand_env() than with
4850 * something else (e.g., calling a shell).
4851 * After expanding environment variables, check again
4852 * if there are still wildcards present.
4853 */
4854 if (vim_strchr(eap->arg, '$') != NULL
4855 || vim_strchr(eap->arg, '~') != NULL)
4856 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004857 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004858 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 has_wildcards = mch_has_wildcard(NameBuff);
4860 p = NameBuff;
4861 }
4862 else
4863 p = NULL;
4864 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004865 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 {
4867 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004868 int options = WILD_LIST_NOTFOUND
4869 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
4871 ExpandInit(&xpc);
4872 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004873 if (p_wic)
4874 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004876 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 if (p == NULL)
4878 return FAIL;
4879 }
4880 if (p != NULL)
4881 {
4882 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4883 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004884 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 vim_free(p);
4886 }
4887 }
4888 }
4889 }
4890 return OK;
4891}
4892
4893/*
4894 * Replace part of the command line, keeping eap->cmd, eap->arg and
4895 * eap->nextcmd correct.
4896 * "src" points to the part that is to be replaced, of length "srclen".
4897 * "repl" is the replacement string.
4898 * Returns a pointer to the character after the replaced string.
4899 * Returns NULL for failure.
4900 */
4901 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004902repl_cmdline(
4903 exarg_T *eap,
4904 char_u *src,
4905 int srclen,
4906 char_u *repl,
4907 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908{
4909 int len;
4910 int i;
4911 char_u *new_cmdline;
4912
4913 /*
4914 * The new command line is build in new_cmdline[].
4915 * First allocate it.
4916 * Careful: a "+cmd" argument may have been NUL terminated.
4917 */
4918 len = (int)STRLEN(repl);
4919 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004920 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004921 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004922 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004923 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924
4925 /*
4926 * Copy the stuff before the expanded part.
4927 * Copy the expanded stuff.
4928 * Copy what came after the expanded part.
4929 * Copy the next commands, if there are any.
4930 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004931 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004933
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004935 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004937 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004939 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
4941 i = (int)STRLEN(new_cmdline) + 1;
4942 STRCPY(new_cmdline + i, eap->nextcmd);
4943 eap->nextcmd = new_cmdline + i;
4944 }
4945 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4946 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4947 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4948 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4949 vim_free(*cmdlinep);
4950 *cmdlinep = new_cmdline;
4951
4952 return src;
4953}
4954
4955/*
4956 * Check for '|' to separate commands and '"' to start comments.
4957 */
4958 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004959separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960{
4961 char_u *p;
4962
Bram Moolenaar86b68352004-12-27 21:59:20 +00004963#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004964 p = skip_grep_pat(eap);
4965#else
4966 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004967#endif
4968
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004969 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
4971 if (*p == Ctrl_V)
4972 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004973 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004974 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004976 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004977 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004978 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 break;
4980 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004981
4982#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004983 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004984 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004985 {
4986 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004987 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004988 if (*p == NUL) // stop at NUL after CTRL-V
4989 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004990 }
4991#endif
4992
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004993 // Check for '"': start of comment or '|': next command
4994 // :@" and :*" do not start a comment!
4995 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004996 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004997#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004998 && !in_vim9script()
4999#endif
5000 && !(eap->argt & EX_NOTRLCOM)
5001 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5002 || p != eap->arg)
5003 && (eap->cmdidx != CMD_redir
5004 || p != eap->arg + 1 || p[-1] != '@'))
5005#ifdef FEAT_EVAL
5006 || (*p == '#'
5007 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005008 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005009 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 || *p == '|' || *p == '\n')
5012 {
5013 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005014 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 * AND 'b' is present in 'cpoptions'.
5016 */
5017 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005018 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005020 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 --p;
5022 }
5023 else
5024 {
5025 eap->nextcmd = check_nextcmd(p);
5026 *p = NUL;
5027 break;
5028 }
5029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005031
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005032 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 del_trailing_spaces(eap->arg);
5034}
5035
5036/*
5037 * get + command from ex argument
5038 */
5039 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005040getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041{
5042 char_u *arg = *argp;
5043 char_u *command = NULL;
5044
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005045 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
5047 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005048 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 command = dollar_command;
5050 else
5051 {
5052 command = arg;
5053 arg = skip_cmd_arg(command, TRUE);
5054 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005055 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005058 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 *argp = arg;
5060 }
5061 return command;
5062}
5063
5064/*
5065 * Find end of "+command" argument. Skip over "\ " and "\\".
5066 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005067 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005068skip_cmd_arg(
5069 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005070 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071{
5072 while (*p && !vim_isspace(*p))
5073 {
5074 if (*p == '\\' && p[1] != NUL)
5075 {
5076 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005077 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 else
5079 ++p;
5080 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005081 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
5083 return p;
5084}
5085
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005086 int
5087get_bad_opt(char_u *p, exarg_T *eap)
5088{
5089 if (STRICMP(p, "keep") == 0)
5090 eap->bad_char = BAD_KEEP;
5091 else if (STRICMP(p, "drop") == 0)
5092 eap->bad_char = BAD_DROP;
5093 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5094 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005095 else
5096 return FAIL;
5097 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005098}
5099
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100/*
5101 * Get "++opt=arg" argument.
5102 * Return FAIL or OK.
5103 */
5104 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005105getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106{
5107 char_u *arg = eap->arg + 2;
5108 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005109 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005112 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5114 {
5115 if (*arg == 'n')
5116 {
5117 arg += 2;
5118 eap->force_bin = FORCE_NOBIN;
5119 }
5120 else
5121 eap->force_bin = FORCE_BIN;
5122 if (!checkforcmd(&arg, "binary", 3))
5123 return FAIL;
5124 eap->arg = skipwhite(arg);
5125 return OK;
5126 }
5127
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005128 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005129 if (STRNCMP(arg, "edit", 4) == 0)
5130 {
5131 eap->read_edit = TRUE;
5132 eap->arg = skipwhite(arg + 4);
5133 return OK;
5134 }
5135
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (STRNCMP(arg, "ff", 2) == 0)
5137 {
5138 arg += 2;
5139 pp = &eap->force_ff;
5140 }
5141 else if (STRNCMP(arg, "fileformat", 10) == 0)
5142 {
5143 arg += 10;
5144 pp = &eap->force_ff;
5145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 else if (STRNCMP(arg, "enc", 3) == 0)
5147 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005148 if (STRNCMP(arg, "encoding", 8) == 0)
5149 arg += 8;
5150 else
5151 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 pp = &eap->force_enc;
5153 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005154 else if (STRNCMP(arg, "bad", 3) == 0)
5155 {
5156 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005157 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
5160 if (pp == NULL || *arg != '=')
5161 return FAIL;
5162
5163 ++arg;
5164 *pp = (int)(arg - eap->cmd);
5165 arg = skip_cmd_arg(arg, FALSE);
5166 eap->arg = skipwhite(arg);
5167 *arg = NUL;
5168
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (pp == &eap->force_ff)
5170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5172 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005173 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005175 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005177 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5179 *p = TOLOWER_ASC(*p);
5180 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005181 else
5182 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005183 // Check ++bad= argument. Must be a single-byte character, "keep" or
5184 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005185 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005186 return FAIL;
5187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
5189 return OK;
5190}
5191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005193ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
5195 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005196 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 * directory for security reasons.
5198 */
5199 if (secure)
5200 {
5201 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005202 eap->errmsg =
5203 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
5205 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005206 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 else
5208 do_augroup(eap->arg, eap->forceit);
5209}
5210
5211/*
5212 * ":doautocmd": Apply the automatic commands to the current buffer.
5213 */
5214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005215ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005217 char_u *arg = eap->arg;
5218 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005219 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005220
Bram Moolenaar1610d052016-06-09 22:53:01 +02005221 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005222 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005223 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005224 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227/*
5228 * :[N]bunload[!] [N] [bufname] unload buffer
5229 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5230 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5231 */
5232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005233ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005235 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005236 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 eap->errmsg = do_bufdel(
5238 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5239 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5240 : DOBUF_UNLOAD, eap->arg,
5241 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5242}
5243
5244/*
5245 * :[N]buffer [N] to buffer N
5246 * :[N]sbuffer [N] to buffer N
5247 */
5248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005249ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005251 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005252 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005254 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 else
5256 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005257 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5259 else
5260 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005261 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005262 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
5264}
5265
5266/*
5267 * :[N]bmodified [N] to next mod. buffer
5268 * :[N]sbmodified [N] to next mod. buffer
5269 */
5270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005271ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
5273 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005274 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005275 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276}
5277
5278/*
5279 * :[N]bnext [N] to next buffer
5280 * :[N]sbnext [N] split and to next buffer
5281 */
5282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005283ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005285 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005286 return;
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005289 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005290 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291}
5292
5293/*
5294 * :[N]bNext [N] to previous buffer
5295 * :[N]bprevious [N] to previous buffer
5296 * :[N]sbNext [N] split and to previous buffer
5297 * :[N]sbprevious [N] split and to previous buffer
5298 */
5299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005300ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005302 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005303 return;
5304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005306 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005307 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308}
5309
5310/*
5311 * :brewind to first buffer
5312 * :bfirst to first buffer
5313 * :sbrewind split and to first buffer
5314 * :sbfirst split and to first buffer
5315 */
5316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005317ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005319 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005320 return;
5321
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005323 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005324 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325}
5326
5327/*
5328 * :blast to last buffer
5329 * :sblast split and to last buffer
5330 */
5331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005332ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005334 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005335 return;
5336
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005338 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005339 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341
Bram Moolenaar7a092242020-04-16 22:10:49 +02005342/*
5343 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005344 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005347ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005349 int comment_char = '"';
5350
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005351#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005352 if (in_vim9script())
5353 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005354#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005355 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005356}
5357
5358/*
5359 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5360 * to "cmd_start" or has a white space character before it.
5361 */
5362 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005363ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005364{
5365 int c = *cmd;
5366
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005367 if (c == NUL || c == '|' || c == '\n')
5368 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005369#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005370 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005371 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5372 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005373 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005374#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005375 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376}
5377
5378#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5379 || defined(PROTO)
5380/*
5381 * Return the next command, after the first '|' or '\n'.
5382 * Return NULL if not found.
5383 */
5384 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005385find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386{
5387 while (*p != '|' && *p != '\n')
5388 {
5389 if (*p == NUL)
5390 return NULL;
5391 ++p;
5392 }
5393 return (p + 1);
5394}
5395#endif
5396
5397/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005398 * Check if *p is a separator between Ex commands, skipping over white space.
5399 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 */
5401 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005402check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005404 char_u *s = skipwhite(p);
5405
5406 if (*s == '|' || *s == '\n')
5407 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 else
5409 return NULL;
5410}
5411
5412/*
5413 * - if there are more files to edit
5414 * - and this is the last window
5415 * - and forceit not used
5416 * - and not repeated twice on a row
5417 * return FAIL and give error message if 'message' TRUE
5418 * return OK otherwise
5419 */
5420 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005421check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005422 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005423 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
5425 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5426
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005427 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005428 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
5430 if (message)
5431 {
5432#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005433 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5434 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005436 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005438 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5439 NGETTEXT("%d more file to edit. Quit anyway?",
5440 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5442 return OK;
5443 return FAIL;
5444 }
5445#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005446 semsg(NGETTEXT("E173: %d more file to edit",
5447 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005448 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 }
5450 return FAIL;
5451 }
5452 return OK;
5453}
5454
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455/*
5456 * Function given to ExpandGeneric() to obtain the list of command names.
5457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005459get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460{
5461 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 return cmdnames[idx].cmd_name;
5464}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005467ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
Bram Moolenaare6850792010-05-14 15:28:44 +02005469 if (*eap->arg == NUL)
5470 {
5471#ifdef FEAT_EVAL
5472 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5473 char_u *p = NULL;
5474
5475 if (expr != NULL)
5476 {
5477 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005478 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005479 --emsg_off;
5480 vim_free(expr);
5481 }
5482 if (p != NULL)
5483 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005484 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005485 vim_free(p);
5486 }
5487 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005488 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005489#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005490 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005491#endif
5492 }
5493 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005494 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005495
5496#ifdef FEAT_VTP
5497 else if (has_vtp_working())
5498 {
5499 // background color change requires clear + redraw
5500 update_screen(CLEAR);
5501 redrawcmd();
5502 }
5503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504}
5505
5506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005507ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508{
5509 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005510 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 do_highlight(eap->arg, eap->forceit, FALSE);
5512}
5513
5514
5515/*
5516 * Call this function if we thought we were going to exit, but we won't
5517 * (because of an error). May need to restore the terminal mode.
5518 */
5519 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005520not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
5522 exiting = FALSE;
5523 settmode(TMODE_RAW);
5524}
5525
Bram Moolenaar3552e742021-05-29 12:21:58 +02005526 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005527before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5528{
5529 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5530
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005531 // Bail out when autocommands closed the window.
5532 // Refuse to quit when the buffer in the last window is being closed (can
5533 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005534 if (!win_valid(wp)
5535 || curbuf_locked()
5536 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5537 return TRUE;
5538
5539 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5540 {
5541 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005542 // Refuse to quit when locked or when the window was closed or the
5543 // buffer in the last window is being closed (can only happen in
5544 // autocommands).
5545 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005546 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5547 return TRUE;
5548 }
5549
5550 return FALSE;
5551}
5552
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005554 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005555 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005556 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005558 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005559ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005561 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005562
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563#ifdef FEAT_CMDWIN
5564 if (cmdwin_type != 0)
5565 {
5566 cmdwin_result = Ctrl_C;
5567 return;
5568 }
5569#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005570 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005571 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005572 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005573 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005574 return;
5575 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005576 if (eap->addr_count > 0)
5577 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005578 int wnr = eap->line2;
5579
5580 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5581 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005582 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005583 }
5584 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005585 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005586
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005587 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005588 if (curbuf_locked())
5589 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005590
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005591 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005592 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005593 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
5595#ifdef FEAT_NETBEANS_INTG
5596 netbeansForcedQuit = eap->forceit;
5597#endif
5598
5599 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005600 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 */
5602 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5603 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005604 if ((!buf_hide(wp->w_buffer)
5605 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005606 | (eap->forceit ? CCGD_FORCEIT : 0)
5607 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005609 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 {
5611 not_exiting();
5612 }
5613 else
5614 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005615 // quit last window
5616 // Note: only_one_window() returns true, even so a help window is
5617 // still open. In that case only quit, if no address has been
5618 // specified. Example:
5619 // :h|wincmd w|1q - don't quit
5620 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005621 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005623 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005624#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005626#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005627 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005628 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630}
5631
5632/*
5633 * ":cquit".
5634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005636ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005638 // this does not always pass on the exit code to the Manx compiler. why?
5639 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640}
5641
5642/*
5643 * ":qall": try to quit all windows
5644 */
5645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005646ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
5648# ifdef FEAT_CMDWIN
5649 if (cmdwin_type != 0)
5650 {
5651 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005652 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 else
5654 cmdwin_result = K_XF2;
5655 return;
5656 }
5657# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005658
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005659 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005660 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005661 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005662 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005663 return;
5664 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005665
5666 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005667 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005668
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005670 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 getout(0);
5672 not_exiting();
5673}
5674
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675/*
5676 * ":close": close current window, unless it is the last one
5677 */
5678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005679ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005681 win_T *win;
5682 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005683#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005685 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005687#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005688 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005689 {
5690 if (eap->addr_count == 0)
5691 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005692 else
5693 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005694 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005695 {
5696 winnr++;
5697 if (winnr == eap->line2)
5698 break;
5699 }
5700 if (win == NULL)
5701 win = lastwin;
5702 ex_win_close(eap->forceit, win, NULL);
5703 }
5704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705}
5706
Bram Moolenaar4033c552017-09-16 20:54:51 +02005707#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005708/*
5709 * ":pclose": Close any preview window.
5710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005712ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005713{
5714 win_T *win;
5715
Bram Moolenaar79648732019-07-18 21:43:07 +02005716 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005717 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005718 if (win->w_p_pvw)
5719 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005720 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005721 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005722 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005723# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005724 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005725 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005726# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005727}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005728#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005729
Bram Moolenaarf740b292006-02-16 22:11:02 +00005730/*
5731 * Close window "win" and take care of handling closing the last window for a
5732 * modified buffer.
5733 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005735ex_win_close(
5736 int forceit,
5737 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005738 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740 int need_hide;
5741 buf_T *buf = win->w_buffer;
5742
Bram Moolenaarcf844172020-06-26 19:44:06 +02005743 // Never close the autocommand window.
5744 if (win == aucmd_win)
5745 {
5746 emsg(_(e_autocmd_close));
5747 return;
5748 }
5749
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005751 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005753#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005754 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005756 bufref_T bufref;
5757
5758 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005760 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 return;
5762 need_hide = FALSE;
5763 }
5764 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005767 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 return;
5769 }
5770 }
5771
Bram Moolenaar4033c552017-09-16 20:54:51 +02005772#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005774#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005776 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005777 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005778 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005779 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005780 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781}
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005784 * Handle the argument for a tabpage related ex command.
5785 * Returns a tabpage number.
5786 * When an error is encountered then eap->errmsg is set.
5787 */
5788 static int
5789get_tabpage_arg(exarg_T *eap)
5790{
5791 int tab_number;
5792 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5793
5794 if (eap->arg && *eap->arg != NUL)
5795 {
5796 char_u *p = eap->arg;
5797 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005798 int relative = 0; // argument +N/-N means: go to N places to the
5799 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005800
5801 if (*p == '-')
5802 {
5803 relative = -1;
5804 p++;
5805 }
5806 else if (*p == '+')
5807 {
5808 relative = 1;
5809 p++;
5810 }
5811
5812 p_save = p;
5813 tab_number = getdigits(&p);
5814
5815 if (relative == 0)
5816 {
5817 if (STRCMP(p, "$") == 0)
5818 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005819 else if (STRCMP(p, "#") == 0)
5820 if (valid_tabpage(lastused_tabpage))
5821 tab_number = tabpage_index(lastused_tabpage);
5822 else
5823 {
5824 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5825 tab_number = 0;
5826 goto theend;
5827 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005828 else if (p == p_save || *p_save == '-' || *p != NUL
5829 || tab_number > LAST_TAB_NR)
5830 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005831 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005832 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005833 goto theend;
5834 }
5835 }
5836 else
5837 {
5838 if (*p_save == NUL)
5839 tab_number = 1;
5840 else if (p == p_save || *p_save == '-' || *p != NUL
5841 || tab_number == 0)
5842 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005843 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005844 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005845 goto theend;
5846 }
5847 tab_number = tab_number * relative + tabpage_index(curtab);
5848 if (!unaccept_arg0 && relative == -1)
5849 --tab_number;
5850 }
5851 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005852 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005853 }
5854 else if (eap->addr_count > 0)
5855 {
5856 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005857 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005858 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005859 tab_number = 0;
5860 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005861 else
5862 {
5863 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005864 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005865 {
5866 --tab_number;
5867 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005868 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01005869 }
5870 }
5871 }
5872 else
5873 {
5874 switch (eap->cmdidx)
5875 {
5876 case CMD_tabnext:
5877 tab_number = tabpage_index(curtab) + 1;
5878 if (tab_number > LAST_TAB_NR)
5879 tab_number = 1;
5880 break;
5881 case CMD_tabmove:
5882 tab_number = LAST_TAB_NR;
5883 break;
5884 default:
5885 tab_number = tabpage_index(curtab);
5886 }
5887 }
5888
5889theend:
5890 return tab_number;
5891}
5892
5893/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005894 * ":tabclose": close current tab page, unless it is the last one.
5895 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 */
5897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005898ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005900 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005901 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005902
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005903# ifdef FEAT_CMDWIN
5904 if (cmdwin_type != 0)
5905 cmdwin_result = K_IGNORE;
5906 else
5907# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005908 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005909 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005910 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005912 tab_number = get_tabpage_arg(eap);
5913 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005914 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005915 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005916 if (tp == NULL)
5917 {
5918 beep_flush();
5919 return;
5920 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005921 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005922 {
5923 tabpage_close_other(tp, eap->forceit);
5924 return;
5925 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005926 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005927 tabpage_close(eap->forceit);
5928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005930}
5931
5932/*
5933 * ":tabonly": close all tab pages except the current one
5934 */
5935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005936ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005937{
5938 tabpage_T *tp;
5939 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005940 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005941
5942# ifdef FEAT_CMDWIN
5943 if (cmdwin_type != 0)
5944 cmdwin_result = K_IGNORE;
5945 else
5946# endif
5947 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005948 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005949 else
5950 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005951 tab_number = get_tabpage_arg(eap);
5952 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005953 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005954 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005955 // Repeat this up to a 1000 times, because autocommands may
5956 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005957 for (done = 0; done < 1000; ++done)
5958 {
5959 FOR_ALL_TABPAGES(tp)
5960 if (tp->tp_topframe != topframe)
5961 {
5962 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005963 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005964 if (valid_tabpage(tp))
5965 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005966 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005967 break;
5968 }
5969 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005970 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005971 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005972 }
5973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
5976/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005977 * Close the current tab page.
5978 */
5979 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005980tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005981{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005982 // First close all the windows but the current one. If that worked then
5983 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005984 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005985 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005986 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005987 ex_win_close(forceit, curwin, NULL);
5988# ifdef FEAT_GUI
5989 need_mouse_correct = TRUE;
5990# endif
5991}
5992
5993/*
5994 * Close tab page "tp", which is not the current tab page.
5995 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005996 * Also takes care of the tab pages line disappearing when closing the
5997 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005998 */
5999 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006000tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006001{
6002 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006003 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006004 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006005
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006006 // Limit to 1000 windows, autocommands may add a window while we close
6007 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006008 while (++done < 1000)
6009 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006010 wp = tp->tp_firstwin;
6011 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006012
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006013 // Autocommands may delete the tab page under our fingers and we may
6014 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006015 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006016 break;
6017 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006018
Bram Moolenaar29323592016-07-24 22:04:11 +02006019 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006020
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006021 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006022 if (h != tabline_height())
6023 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006024}
6025
6026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 * ":only".
6028 */
6029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006030ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006032 win_T *wp;
6033 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034# ifdef FEAT_GUI
6035 need_mouse_correct = TRUE;
6036# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006037 if (eap->addr_count > 0)
6038 {
6039 wnr = eap->line2;
6040 for (wp = firstwin; --wnr > 0; )
6041 {
6042 if (wp->w_next == NULL)
6043 break;
6044 else
6045 wp = wp->w_next;
6046 }
6047 win_goto(wp);
6048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 close_others(TRUE, eap->forceit);
6050}
6051
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006053ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006055 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006056 if (!eap->skip)
6057 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006058#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006059 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006060#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006061 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006062 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006063 else
6064 {
6065 int winnr = 0;
6066 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006067
Bram Moolenaar2256c992016-11-15 21:17:07 +01006068 FOR_ALL_WINDOWS(win)
6069 {
6070 winnr++;
6071 if (winnr == eap->line2)
6072 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006073 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006074 if (win == NULL)
6075 win = lastwin;
6076 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 }
6079}
6080
6081/*
6082 * ":stop" and ":suspend": Suspend Vim.
6083 */
6084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006085ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
6087 /*
6088 * Disallow suspending for "rvim".
6089 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006090 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 {
6092 if (!eap->forceit)
6093 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006094 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 windgoto((int)Rows - 1, 0);
6096 out_char('\n');
6097 out_flush();
6098 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006099 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006101 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104#ifdef FEAT_TITLE
6105 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006106 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107#endif
6108 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006109 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006111 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006112 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 }
6114}
6115
6116/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006117 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 */
6119 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006120ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006122#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006123 if (not_in_vim9(eap) == FAIL)
6124 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126#ifdef FEAT_CMDWIN
6127 if (cmdwin_type != 0)
6128 {
6129 cmdwin_result = Ctrl_C;
6130 return;
6131 }
6132#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006133 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006134 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006135 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006136 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006137 return;
6138 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006139
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006141 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 */
6143 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6144 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006145
6146 // Write the buffer for ":wq" or when it was changed.
6147 // Trigger QuitPre and ExitPre.
6148 // Check if we can exit now, after autocommands have changed things.
6149 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6150 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006152 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 {
6154 not_exiting();
6155 }
6156 else
6157 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006158 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006160 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161# ifdef FEAT_GUI
6162 need_mouse_correct = TRUE;
6163# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006164 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006165 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 }
6167}
6168
6169/*
6170 * ":print", ":list", ":number".
6171 */
6172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006173ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006175 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006176 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006177 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006179 for ( ;!got_int; ui_breakcheck())
6180 {
6181 print_line(eap->line1,
6182 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6183 || (eap->flags & EXFLAG_NR)),
6184 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6185 if (++eap->line1 > eap->line2)
6186 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006187 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006188 }
6189 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006190 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006191 curwin->w_cursor.lnum = eap->line2;
6192 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 }
6194
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196}
6197
6198#ifdef FEAT_BYTEOFF
6199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006200ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
6202 goto_byte(eap->line2);
6203}
6204#endif
6205
6206/*
6207 * ":shell".
6208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006210ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 do_shell(NULL, 0);
6213}
6214
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006215#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006217static int drop_busy = FALSE;
6218static int drop_filec;
6219static char_u **drop_filev = NULL;
6220static int drop_split;
6221static void (*drop_callback)(void *);
6222static void *drop_cookie;
6223
6224 static void
6225handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226{
6227 exarg_T ea;
6228 int save_msg_scroll = msg_scroll;
6229
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006230 // Setting the argument list may cause screen updates and being called
6231 // recursively. Avoid that by setting drop_busy.
6232 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006234 // Check whether the current buffer is changed. If so, we will need
6235 // to split the current window or data could be lost.
6236 // We don't need to check if the 'hidden' option is set, as in this
6237 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006238 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {
6240 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006241 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 --emsg_off;
6243 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006244 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 if (win_split(0, 0) == FAIL)
6247 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006248 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006250 // When splitting the window, create a new alist. Otherwise the
6251 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 alist_unlink(curwin->w_alist);
6253 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255
6256 /*
6257 * Set up the new argument list.
6258 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006259 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
6261 /*
6262 * Move to the first file.
6263 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006264 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006265 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 ea.cmd = (char_u *)"next";
6267 do_argfile(&ea, 0);
6268
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006269 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6270 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 need_start_insertmode = FALSE;
6272
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006273 // Restore msg_scroll, otherwise a following command may cause scrolling
6274 // unexpectedly. The screen will be redrawn by the caller, thus
6275 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006277
6278 if (drop_callback != NULL)
6279 drop_callback(drop_cookie);
6280
6281 drop_filev = NULL;
6282 drop_busy = FALSE;
6283}
6284
6285/*
6286 * Handle a file drop. The code is here because a drop is *nearly* like an
6287 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006288 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006289 * code is very similar to :args and hence needs access to a lot of the static
6290 * functions in this file.
6291 *
6292 * The "filev" list must have been allocated using alloc(), as should each item
6293 * in the list. This function takes over responsibility for freeing the "filev"
6294 * list.
6295 */
6296 void
6297handle_drop(
6298 int filec, // the number of files dropped
6299 char_u **filev, // the list of files dropped
6300 int split, // force splitting the window
6301 void (*callback)(void *), // to be called after setting the argument
6302 // list
6303 void *cookie) // argument for "callback" (allocated)
6304{
6305 // Cannot handle recursive drops, finish the pending one.
6306 if (drop_busy)
6307 {
6308 FreeWild(filec, filev);
6309 vim_free(cookie);
6310 return;
6311 }
6312
6313 // When calling handle_drop() more than once in a row we only use the last
6314 // one.
6315 if (drop_filev != NULL)
6316 {
6317 FreeWild(drop_filec, drop_filev);
6318 vim_free(drop_cookie);
6319 }
6320
6321 drop_filec = filec;
6322 drop_filev = filev;
6323 drop_split = split;
6324 drop_callback = callback;
6325 drop_cookie = cookie;
6326
6327 // Postpone this when:
6328 // - editing the command line
6329 // - not possible to change the current buffer
6330 // - updating the screen
6331 // As it may change buffers and window structures that are in use and cause
6332 // freed memory to be used.
6333 if (text_locked() || curbuf_locked() || updating_screen)
6334 return;
6335
6336 handle_drop_internal();
6337}
6338
6339/*
6340 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6341 * reset: Handle a postponed drop.
6342 */
6343 void
6344handle_any_postponed_drop(void)
6345{
6346 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006347 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006348 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349}
6350#endif
6351
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 * ":preserve".
6354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006356ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006358 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 ml_preserve(curbuf, TRUE);
6360}
6361
6362/*
6363 * ":recover".
6364 */
6365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006366ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006368 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006370 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6371 | CCGD_MULTWIN
6372 | (eap->forceit ? CCGD_FORCEIT : 0)
6373 | CCGD_EXCMD)
6374
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 && (*eap->arg == NUL
6376 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006377 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 recoverymode = FALSE;
6379}
6380
6381/*
6382 * Command modifier used in a wrong way.
6383 */
6384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006385ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006387 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388}
6389
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390/*
6391 * :sview [+command] file split window with new file, read-only
6392 * :split [[+command] file] split window with current or new file
6393 * :vsplit [[+command] file] split window vertically with current or new file
6394 * :new [[+command] file] split window with no or new file
6395 * :vnew [[+command] file] split vertically window with no or new file
6396 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006397 *
6398 * :tabedit open new Tab page with empty window
6399 * :tabedit [+command] file open new Tab page and edit "file"
6400 * :tabnew [[+command] file] just like :tabedit
6401 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 */
6403 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006404ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006406 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006407#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006409#endif
6410#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006411 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006412 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006413#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006414 int use_tab = eap->cmdidx == CMD_tabedit
6415 || eap->cmdidx == CMD_tabfind
6416 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006418 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006419 return;
6420
Bram Moolenaar4033c552017-09-16 20:54:51 +02006421#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
Bram Moolenaar4033c552017-09-16 20:54:51 +02006425#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006426 // A ":split" in the quickfix window works like ":new". Don't want two
6427 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006428 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 {
6430 if (eap->cmdidx == CMD_split)
6431 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 if (eap->cmdidx == CMD_vsplit)
6433 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436
Bram Moolenaar4033c552017-09-16 20:54:51 +02006437#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006438 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 {
6440 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6441 FNAME_MESS, TRUE, curbuf->b_ffname);
6442 if (fname == NULL)
6443 goto theend;
6444 eap->arg = fname;
6445 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006446# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006447 else
6448# endif
6449#endif
6450#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006451 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 && eap->cmdidx != CMD_new)
6454 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006455 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006456# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006457 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006458# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006459 au_has_group((char_u *)"FileExplorer"))
6460 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006461 // No browsing supported but we do have the file explorer:
6462 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006463 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006464 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006465 }
6466 else
6467 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006468 fname = do_browse(0, (char_u *)(use_tab
6469 ? _("Edit File in new tab page")
6470 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006472 if (fname == NULL)
6473 goto theend;
6474 eap->arg = fname;
6475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006477 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006480 /*
6481 * Either open new tab page or split the window.
6482 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006483 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006484 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006485 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006486 : eap->addr_count == 0 ? 0
6487 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006488 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006489 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006491 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006492 if (curwin != old_curwin
6493 && win_valid(old_curwin)
6494 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006495 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006496 old_curwin->w_alt_fnum = curbuf->b_fnum;
6497 }
6498 }
6499 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6501 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006502 // Reset 'scrollbind' when editing another file, but keep it when
6503 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006504 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006505 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 else
6507 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 do_exedit(eap, old_curwin);
6509 }
6510
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006511# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006512 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006513# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006515# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516theend:
6517 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006520
6521/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006522 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006523 */
6524 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006525tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006526{
6527 exarg_T ea;
6528
Bram Moolenaara80faa82020-04-12 19:37:17 +02006529 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006530 ea.cmdidx = CMD_tabnew;
6531 ea.cmd = (char_u *)"tabn";
6532 ea.arg = (char_u *)"";
6533 ex_splitview(&ea);
6534}
6535
6536/*
6537 * :tabnext command
6538 */
6539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006540ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006541{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006542 int tab_number;
6543
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006544 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006545 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006546 switch (eap->cmdidx)
6547 {
6548 case CMD_tabfirst:
6549 case CMD_tabrewind:
6550 goto_tabpage(1);
6551 break;
6552 case CMD_tablast:
6553 goto_tabpage(9999);
6554 break;
6555 case CMD_tabprevious:
6556 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006557 if (eap->arg && *eap->arg != NUL)
6558 {
6559 char_u *p = eap->arg;
6560 char_u *p_save = p;
6561
6562 tab_number = getdigits(&p);
6563 if (p == p_save || *p_save == '-' || *p != NUL
6564 || tab_number == 0)
6565 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006566 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006567 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006568 return;
6569 }
6570 }
6571 else
6572 {
6573 if (eap->addr_count == 0)
6574 tab_number = 1;
6575 else
6576 {
6577 tab_number = eap->line2;
6578 if (tab_number < 1)
6579 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006580 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006581 return;
6582 }
6583 }
6584 }
6585 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006586 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006587 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006588 tab_number = get_tabpage_arg(eap);
6589 if (eap->errmsg == NULL)
6590 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006591 break;
6592 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006593}
6594
6595/*
6596 * :tabmove command
6597 */
6598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006599ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006600{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006601 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006602
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006603 tab_number = get_tabpage_arg(eap);
6604 if (eap->errmsg == NULL)
6605 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006606}
6607
6608/*
6609 * :tabs command: List tabs and their contents.
6610 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006611 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006612ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006613{
6614 tabpage_T *tp;
6615 win_T *wp;
6616 int tabcount = 1;
6617
6618 msg_start();
6619 msg_scroll = TRUE;
6620 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6621 {
6622 msg_putchar('\n');
6623 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006624 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006625 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006626 ui_breakcheck();
6627
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006628 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006629 wp = firstwin;
6630 else
6631 wp = tp->tp_firstwin;
6632 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6633 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006634 msg_putchar('\n');
6635 msg_putchar(wp == curwin ? '>' : ' ');
6636 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006637 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6638 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006639 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006640 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006641 else
6642 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6643 IObuff, IOSIZE, TRUE);
6644 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006645 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006646 ui_breakcheck();
6647 }
6648 }
6649}
6650
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651/*
6652 * ":mode": Set screen mode.
6653 * If no argument given, just get the screen size and redraw.
6654 */
6655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006656ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
6658 if (*eap->arg == NUL)
6659 shell_resized();
6660 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006661 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662}
6663
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664/*
6665 * ":resize".
6666 * set, increment or decrement current window height
6667 */
6668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006669ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670{
6671 int n;
6672 win_T *wp = curwin;
6673
6674 if (eap->addr_count > 0)
6675 {
6676 n = eap->line2;
6677 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6678 ;
6679 }
6680
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006681# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006683# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006685 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
6687 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006688 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006689 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 n = 9999;
6691 win_setwidth_win((int)n, wp);
6692 }
6693 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 {
6695 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006696 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006697 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 n = 9999;
6699 win_setheight_win((int)n, wp);
6700 }
6701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702
6703/*
6704 * ":find [+command] <file>" command.
6705 */
6706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006707ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708{
6709#ifdef FEAT_SEARCHPATH
6710 char_u *fname;
6711 int count;
6712
6713 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6714 TRUE, curbuf->b_ffname);
6715 if (eap->addr_count > 0)
6716 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006717 // Repeat finding the file "count" times. This matters when it
6718 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 count = eap->line2;
6720 while (fname != NULL && --count > 0)
6721 {
6722 vim_free(fname);
6723 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6724 FALSE, curbuf->b_ffname);
6725 }
6726 }
6727
6728 if (fname != NULL)
6729 {
6730 eap->arg = fname;
6731#endif
6732 do_exedit(eap, NULL);
6733#ifdef FEAT_SEARCHPATH
6734 vim_free(fname);
6735 }
6736#endif
6737}
6738
6739/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006740 * ":open" simulation: for now just work like ":visual".
6741 */
6742 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006743ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006744{
6745 regmatch_T regmatch;
6746 char_u *p;
6747
Bram Moolenaar65088802021-03-13 21:07:21 +01006748#ifdef FEAT_EVAL
6749 if (not_in_vim9(eap) == FAIL)
6750 return;
6751#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006752 curwin->w_cursor.lnum = eap->line2;
6753 beginline(BL_SOL | BL_FIX);
6754 if (*eap->arg == '/')
6755 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006756 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006757 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006758 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006759 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006760 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006761 if (regmatch.regprog != NULL)
6762 {
6763 regmatch.rm_ic = p_ic;
6764 p = ml_get_curline();
6765 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006766 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006767 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006768 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006769 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006770 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006771 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006772 eap->arg += STRLEN(eap->arg);
6773 }
6774 check_cursor();
6775
6776 eap->cmdidx = CMD_visual;
6777 do_exedit(eap, NULL);
6778}
6779
6780/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006781 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 */
6783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006784ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785{
6786 do_exedit(eap, NULL);
6787}
6788
6789/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006790 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006793do_exedit(
6794 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006795 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
6797 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006799 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006801 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6802 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006803 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 /*
6805 * ":vi" command ends Ex mode.
6806 */
6807 if (exmode_active && (eap->cmdidx == CMD_visual
6808 || eap->cmdidx == CMD_view))
6809 {
6810 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006811 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006813 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006814 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006815 if (global_busy)
6816 {
6817 int rd = RedrawingDisabled;
6818 int nwr = no_wait_return;
6819 int ms = msg_scroll;
6820#ifdef FEAT_GUI
6821 int he = hold_gui_events;
6822#endif
6823
6824 if (eap->nextcmd != NULL)
6825 {
6826 stuffReadbuff(eap->nextcmd);
6827 eap->nextcmd = NULL;
6828 }
6829
6830 if (exmode_was != EXMODE_VIM)
6831 settmode(TMODE_RAW);
6832 RedrawingDisabled = 0;
6833 no_wait_return = 0;
6834 need_wait_return = FALSE;
6835 msg_scroll = 0;
6836#ifdef FEAT_GUI
6837 hold_gui_events = 0;
6838#endif
6839 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006840 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006841
6842 main_loop(FALSE, TRUE);
6843
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006844 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006845 RedrawingDisabled = rd;
6846 no_wait_return = nwr;
6847 msg_scroll = ms;
6848#ifdef FEAT_GUI
6849 hold_gui_events = he;
6850#endif
6851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
6855
6856 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006857 || eap->cmdidx == CMD_tabnew
6858 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006859 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006861 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 setpcmark();
6863 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006864 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6865 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006867 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 || *eap->arg != NUL
6869#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006870 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871#endif
6872 )
6873 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006874 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6875 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006876 if (*eap->arg != NUL && curbuf_locked())
6877 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006878
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 n = readonlymode;
6880 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6881 readonlymode = TRUE;
6882 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006883 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6884 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006885 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6886 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6888 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006889 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6891 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6892 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006893 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006895 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006896 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006897 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6898 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006899 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006901 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 if (old_curwin != NULL)
6903 {
6904 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006905 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006907#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006908 cleanup_T cs;
6909
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006910 // Reset the error/interrupt/exception state here so that
6911 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006912 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006913#endif
6914#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006916#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006917 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006918
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006919#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006920 // Restore the error/interrupt/exception state if not
6921 // discarded by a new aborting error, interrupt, or
6922 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006923 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 }
6926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 }
6928 else if (readonlymode && curbuf->b_nwindows == 1)
6929 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006930 // When editing an already visited buffer, 'readonly' won't be set
6931 // but the previous value is kept. With ":view" and ":sview" we
6932 // want the file to be readonly, except when another window is
6933 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 curbuf->b_p_ro = TRUE;
6935 }
6936 readonlymode = n;
6937 }
6938 else
6939 {
6940 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01006941 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942#ifdef FEAT_TITLE
6943 n = curwin->w_arg_idx_invalid;
6944#endif
6945 check_arg_idx(curwin);
6946#ifdef FEAT_TITLE
6947 if (n != curwin->w_arg_idx_invalid)
6948 maketitle();
6949#endif
6950 }
6951
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 /*
6953 * if ":split file" worked, set alternate file name in old window to new
6954 * file
6955 */
6956 if (old_curwin != NULL
6957 && *eap->arg != NUL
6958 && curwin != old_curwin
6959 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006960 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006961 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
6964 ex_no_reprint = TRUE;
6965}
6966
6967#ifndef FEAT_GUI
6968/*
6969 * ":gui" and ":gvim" when there is no GUI.
6970 */
6971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006972ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006974 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975}
6976#endif
6977
Bram Moolenaar4f974752019-02-17 17:44:42 +01006978#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006980ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
6982 gui_make_tearoff(eap->arg);
6983}
6984#endif
6985
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006986#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6987 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006989ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006991# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6992 if (gui.in_use)
6993 gui_make_popup(eap->arg, eap->forceit);
6994# ifdef FEAT_TERM_POPUP_MENU
6995 else
6996# endif
6997# endif
6998# ifdef FEAT_TERM_POPUP_MENU
6999 pum_make_popup(eap->arg, eap->forceit);
7000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001}
7002#endif
7003
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007005ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007008 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007010 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011}
7012
7013/*
7014 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7015 * offset.
7016 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007019ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007022 win_T *save_curwin = curwin;
7023 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 long topline;
7025 long y;
7026 linenr_T old_linenr = curwin->w_cursor.lnum;
7027
7028 setpcmark();
7029
7030 /*
7031 * determine max topline
7032 */
7033 if (curwin->w_p_scb)
7034 {
7035 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007036 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {
7038 if (wp->w_p_scb && wp->w_buffer)
7039 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007040 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 if (topline > y)
7042 topline = y;
7043 }
7044 }
7045 if (topline < 1)
7046 topline = 1;
7047 }
7048 else
7049 {
7050 topline = 1;
7051 }
7052
7053
7054 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007055 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007057 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {
7059 if (curwin->w_p_scb)
7060 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007061 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 y = topline - curwin->w_topline;
7063 if (y > 0)
7064 scrollup(y, TRUE);
7065 else
7066 scrolldown(-y, TRUE);
7067 curwin->w_scbind_pos = topline;
7068 redraw_later(VALID);
7069 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 }
7072 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007073 curwin = save_curwin;
7074 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 if (curwin->w_p_scb)
7076 {
7077 did_syncbind = TRUE;
7078 checkpcmark();
7079 if (old_linenr != curwin->w_cursor.lnum)
7080 {
7081 char_u ctrl_o[2];
7082
7083 ctrl_o[0] = Ctrl_O;
7084 ctrl_o[1] = 0;
7085 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7086 }
7087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088}
7089
7090
7091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007092ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007094 int i;
7095 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7096 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007098 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 do_bang(1, eap, FALSE, FALSE, TRUE);
7100 else
7101 {
7102 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7103 return;
7104
7105#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007106 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {
7108 char_u *browseFile;
7109
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007110 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 NULL, NULL, NULL, curbuf);
7112 if (browseFile != NULL)
7113 {
7114 i = readfile(browseFile, NULL,
7115 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7116 vim_free(browseFile);
7117 }
7118 else
7119 i = OK;
7120 }
7121 else
7122#endif
7123 if (*eap->arg == NUL)
7124 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007125 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 return;
7127 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7128 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7129 }
7130 else
7131 {
7132 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7133 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7134 i = readfile(eap->arg, NULL,
7135 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7136
7137 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007138 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007140#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 if (!aborting())
7142#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007143 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 }
7145 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007146 {
7147 if (empty && exmode_active)
7148 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007149 // Delete the empty line that remains. Historically ex does
7150 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007151 if (eap->line2 == 0)
7152 lnum = curbuf->b_ml.ml_line_count;
7153 else
7154 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007155 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007156 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007157 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007158 if (curwin->w_cursor.lnum > 1
7159 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007160 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007161 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007162 }
7163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 }
7167}
7168
Bram Moolenaarea408852005-06-25 22:49:46 +00007169static char_u *prev_dir = NULL;
7170
7171#if defined(EXITFREE) || defined(PROTO)
7172 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007173free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007174{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007175 VIM_CLEAR(prev_dir);
7176 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007177}
7178#endif
7179
Bram Moolenaarf4258302013-06-02 18:20:17 +02007180/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007181 * Get the previous directory for the given chdir scope.
7182 */
7183 static char_u *
7184get_prevdir(cdscope_T scope)
7185{
7186 if (scope == CDSCOPE_WINDOW)
7187 return curwin->w_prevdir;
7188 else if (scope == CDSCOPE_TABPAGE)
7189 return curtab->tp_prevdir;
7190 return prev_dir;
7191}
7192
7193/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007194 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007195 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7196 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007197 */
7198 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007199post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007200{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007201 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007202 // Clear tab local directory for both :cd and :tcd
7203 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007204 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007205 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007206 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007207 char_u *pdir = get_prevdir(scope);
7208
7209 // If still in the global directory, need to remember current
7210 // directory as the global directory.
7211 if (globaldir == NULL && pdir != NULL)
7212 globaldir = vim_strsave(pdir);
7213
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007214 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007215 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007216 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007217 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007218 curtab->tp_localdir = vim_strsave(NameBuff);
7219 else
7220 curwin->w_localdir = vim_strsave(NameBuff);
7221 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007222 }
7223 else
7224 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007225 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007226 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007227 }
7228
7229 shorten_fnames(TRUE);
7230}
7231
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007232/*
7233 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007234 * chdir() function.
7235 * scope == CDSCOPE_WINDOW: changes the window-local directory
7236 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7237 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007238 * Returns TRUE if the directory is successfully changed.
7239 */
7240 int
7241changedir_func(
7242 char_u *new_dir,
7243 int forceit,
7244 cdscope_T scope)
7245{
7246 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007247 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007248 int dir_differs;
7249 int retval = FALSE;
7250
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007251 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007252 return FALSE;
7253
7254 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7255 {
7256 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7257 return FALSE;
7258 }
7259
7260 // ":cd -": Change to previous directory
7261 if (STRCMP(new_dir, "-") == 0)
7262 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007263 pdir = get_prevdir(scope);
7264 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007265 {
7266 emsg(_("E186: No previous directory"));
7267 return FALSE;
7268 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007269 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007270 }
7271
Bram Moolenaar002bc792020-06-05 22:33:42 +02007272 // Free the previous directory
7273 tofree = get_prevdir(scope);
7274
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007275 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007276 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007277 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007278 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007279 pdir = NULL;
7280 if (scope == CDSCOPE_WINDOW)
7281 curwin->w_prevdir = pdir;
7282 else if (scope == CDSCOPE_TABPAGE)
7283 curtab->tp_prevdir = pdir;
7284 else
7285 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007286
7287#if defined(UNIX) || defined(VMS)
7288 // for UNIX ":cd" means: go to home directory
7289 if (*new_dir == NUL)
7290 {
7291 // use NameBuff for home directory name
7292# ifdef VMS
7293 char_u *p;
7294
7295 p = mch_getenv((char_u *)"SYS$LOGIN");
7296 if (p == NULL || *p == NUL) // empty is the same as not set
7297 NameBuff[0] = NUL;
7298 else
7299 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7300# else
7301 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7302# endif
7303 new_dir = NameBuff;
7304 }
7305#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007306 dir_differs = new_dir == NULL || pdir == NULL
7307 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007308 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7309 emsg(_(e_failed));
7310 else
7311 {
7312 char_u *acmd_fname;
7313
7314 post_chdir(scope);
7315
7316 if (dir_differs)
7317 {
7318 if (scope == CDSCOPE_WINDOW)
7319 acmd_fname = (char_u *)"window";
7320 else if (scope == CDSCOPE_TABPAGE)
7321 acmd_fname = (char_u *)"tabpage";
7322 else
7323 acmd_fname = (char_u *)"global";
7324 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7325 curbuf);
7326 }
7327 retval = TRUE;
7328 }
7329 vim_free(tofree);
7330
7331 return retval;
7332}
Bram Moolenaarea408852005-06-25 22:49:46 +00007333
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007335 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007337 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007338ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007340 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341
7342 new_dir = eap->arg;
7343#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007344 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 if (*new_dir == NUL)
7346 ex_pwd(NULL);
7347 else
7348#endif
7349 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007350 cdscope_T scope = CDSCOPE_GLOBAL;
7351
7352 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7353 scope = CDSCOPE_WINDOW;
7354 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7355 scope = CDSCOPE_TABPAGE;
7356
7357 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007358 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007359 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007360 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 ex_pwd(eap);
7362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 }
7364}
7365
7366/*
7367 * ":pwd".
7368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007370ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371{
7372 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7373 {
7374#ifdef BACKSLASH_IN_FILENAME
7375 slash_adjust(NameBuff);
7376#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007377 if (p_verbose > 0)
7378 {
7379 char *context = "global";
7380
7381 if (curwin->w_localdir != NULL)
7382 context = "window";
7383 else if (curtab->tp_localdir != NULL)
7384 context = "tabpage";
7385 smsg("[%s] %s", context, (char *)NameBuff);
7386 }
7387 else
7388 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 }
7390 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007391 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392}
7393
7394/*
7395 * ":=".
7396 */
7397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007398ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007400 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007401 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402}
7403
7404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007405ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007407 int n;
7408 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409
7410 if (cursor_valid())
7411 {
7412 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7413 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007414 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007416
7417 len = eap->line2;
7418 switch (*eap->arg)
7419 {
7420 case 'm': break;
7421 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007422 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007423 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007424
7425 // Hide the cursor if invoked with !
7426 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427}
7428
7429/*
7430 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007431 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 */
7433 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007434do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435{
Bram Moolenaar52953192019-08-16 10:27:13 +02007436 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007437 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007438# ifdef ELAPSED_FUNC
7439 elapsed_T start_tv;
7440
7441 // Remember at what time we started, so that we know how much longer we
7442 // should wait after waiting for a bit.
7443 ELAPSED_INIT(start_tv);
7444# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007446 if (hide_cursor)
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007447 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007448 else
7449 cursor_on();
7450
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007451 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007452 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007454 wait_now = msec - done > 1000L ? 1000L : msec - done;
7455#ifdef FEAT_TIMERS
7456 {
7457 long due_time = check_due_timer();
7458
7459 if (due_time > 0 && due_time < wait_now)
7460 wait_now = due_time;
7461 }
7462#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007463#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007464 if (has_any_channel() && wait_now > 20L)
7465 wait_now = 20L;
7466#endif
7467#ifdef FEAT_SOUND
7468 if (has_any_sound_callback() && wait_now > 20L)
7469 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007470#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007471 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007472
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007473#ifdef FEAT_JOB_CHANNEL
7474 if (has_any_channel())
7475 ui_breakcheck_force(TRUE);
7476 else
7477#endif
7478 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007479#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007480 // Process the netbeans and clientserver messages that may have been
7481 // received in the call to ui_breakcheck() when the GUI is in use. This
7482 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007483 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007484#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007485
7486# ifdef ELAPSED_FUNC
7487 // actual time passed
7488 done = ELAPSED_FUNC(start_tv);
7489# else
7490 // guestimate time passed (will actually be more)
7491 done += wait_now;
7492# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007494
7495 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7496 // input buffer, otherwise a following call to input() fails.
7497 if (got_int)
7498 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007499
7500 if (hide_cursor)
7501 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502}
7503
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504/*
7505 * ":winsize" command (obsolete).
7506 */
7507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007508ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
7510 int w, h;
7511 char_u *arg = eap->arg;
7512 char_u *p;
7513
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007514 if (!isdigit(*arg))
7515 {
7516 semsg(_(e_invarg2), arg);
7517 return;
7518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 w = getdigits(&arg);
7520 arg = skipwhite(arg);
7521 p = arg;
7522 h = getdigits(&arg);
7523 if (*p != NUL && *arg == NUL)
7524 set_shellsize(w, h, TRUE);
7525 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007526 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527}
7528
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007530ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531{
7532 int xchar = NUL;
7533 char_u *p;
7534
7535 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007537 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 if (eap->arg[1] == NUL)
7539 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007540 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 return;
7542 }
7543 xchar = eap->arg[1];
7544 p = eap->arg + 2;
7545 }
7546 else
7547 p = eap->arg + 1;
7548
7549 eap->nextcmd = check_nextcmd(p);
7550 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007551 if (*p != NUL && *p != (
7552#ifdef FEAT_EVAL
7553 in_vim9script() ? '#' :
7554#endif
7555 '"')
7556 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007557 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007558 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007560 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007561 postponed_split_flags = cmdmod.cmod_split;
7562 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7564 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007565 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 }
7567}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568
Bram Moolenaar843ee412004-06-30 16:16:41 +00007569#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570/*
7571 * ":winpos".
7572 */
7573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007574ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
7576 int x, y;
7577 char_u *arg = eap->arg;
7578 char_u *p;
7579
7580 if (*arg == NUL)
7581 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007582# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007583# ifdef VIMDLL
7584 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7585 mch_get_winpos(&x, &y) != FAIL)
7586# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007588# else
7589 if (mch_get_winpos(&x, &y) != FAIL)
7590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {
7592 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007593 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 }
7595 else
7596# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007597 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 }
7599 else
7600 {
7601 x = getdigits(&arg);
7602 arg = skipwhite(arg);
7603 p = arg;
7604 y = getdigits(&arg);
7605 if (*p == NUL || *arg != NUL)
7606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007607 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 return;
7609 }
7610# ifdef FEAT_GUI
7611 if (gui.in_use)
7612 gui_mch_set_winpos(x, y);
7613 else if (gui.starting)
7614 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007615 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 gui_win_x = x;
7617 gui_win_y = y;
7618 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007619# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 else
7621# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007622# endif
7623# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007624 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625# endif
7626# ifdef HAVE_TGETENT
7627 if (*T_CWP)
7628 term_set_winpos(x, y);
7629# endif
7630 }
7631}
7632#endif
7633
7634/*
7635 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7636 */
7637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007638ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639{
7640 oparg_T oa;
7641
7642 clear_oparg(&oa);
7643 oa.regname = eap->regname;
7644 oa.start.lnum = eap->line1;
7645 oa.end.lnum = eap->line2;
7646 oa.line_count = eap->line2 - eap->line1 + 1;
7647 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007649 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 {
7651 setpcmark();
7652 curwin->w_cursor.lnum = eap->line1;
7653 beginline(BL_SOL | BL_FIX);
7654 }
7655
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007656 if (VIsual_active)
7657 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007658
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 switch (eap->cmdidx)
7660 {
7661 case CMD_delete:
7662 oa.op_type = OP_DELETE;
7663 op_delete(&oa);
7664 break;
7665
7666 case CMD_yank:
7667 oa.op_type = OP_YANK;
7668 (void)op_yank(&oa, FALSE, TRUE);
7669 break;
7670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007671 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007672 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007674 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7675#else
7676 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007678 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 oa.op_type = OP_RSHIFT;
7680 else
7681 oa.op_type = OP_LSHIFT;
7682 op_shift(&oa, FALSE, eap->amount);
7683 break;
7684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007686 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687}
7688
7689/*
7690 * ":put".
7691 */
7692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007693ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007695 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 if (eap->line2 == 0)
7697 {
7698 eap->line2 = 1;
7699 eap->forceit = TRUE;
7700 }
7701 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007702 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007703 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704}
7705
7706/*
7707 * Handle ":copy" and ":move".
7708 */
7709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007710ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711{
7712 long n;
7713
Bram Moolenaar491799b2020-08-01 19:23:43 +02007714#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007715 if (not_in_vim9(eap) == FAIL)
7716 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007717#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007718 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007719 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {
7721 eap->nextcmd = NULL;
7722 return;
7723 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007724 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725
7726 /*
7727 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7728 */
7729 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7730 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007731 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 return;
7733 }
7734
7735 if (eap->cmdidx == CMD_move)
7736 {
7737 if (do_move(eap->line1, eap->line2, n) == FAIL)
7738 return;
7739 }
7740 else
7741 ex_copy(eap->line1, eap->line2, n);
7742 u_clearline();
7743 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007744 ex_may_print(eap);
7745}
7746
7747/*
7748 * Print the current line if flags were given to the Ex command.
7749 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007750 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007751ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007752{
7753 if (eap->flags != 0)
7754 {
7755 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7756 (eap->flags & EXFLAG_LIST));
7757 ex_no_reprint = TRUE;
7758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759}
7760
7761/*
7762 * ":smagic" and ":snomagic".
7763 */
7764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007765ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007767 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007769 magic_overruled = eap->cmdidx == CMD_smagic
7770 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007771 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007772 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773}
7774
7775/*
7776 * ":join".
7777 */
7778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007779ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780{
7781 curwin->w_cursor.lnum = eap->line1;
7782 if (eap->line1 == eap->line2)
7783 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007784 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 return;
7786 if (eap->line2 == curbuf->b_ml.ml_line_count)
7787 {
7788 beep_flush();
7789 return;
7790 }
7791 ++eap->line2;
7792 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007793 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007795 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796}
7797
7798/*
7799 * ":[addr]@r" or ":[addr]*r": execute register
7800 */
7801 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007802ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007805 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806
7807 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007808 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809
7810#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007811 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812#endif
7813
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007814 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 c = *eap->arg;
7816 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7817 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007818 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007819 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7820 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007821 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 else
7825 {
7826 int save_efr = exec_from_reg;
7827
7828 exec_from_reg = TRUE;
7829
7830 /*
7831 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007832 * Continue until the stuff buffer is empty and all added characters
7833 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007835 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7837
7838 exec_from_reg = save_efr;
7839 }
7840}
7841
7842/*
7843 * ":!".
7844 */
7845 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007846ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7849}
7850
7851/*
7852 * ":undo".
7853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007855ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007857 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007858 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007859 else
7860 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861}
7862
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007863#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007865ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007866{
7867 char_u hash[UNDO_HASH_SIZE];
7868
7869 u_compute_hash(hash);
7870 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7871}
7872
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007874ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007875{
7876 char_u hash[UNDO_HASH_SIZE];
7877
7878 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007879 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007880}
7881#endif
7882
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883/*
7884 * ":redo".
7885 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007887ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888{
7889 u_redo(1);
7890}
7891
7892/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007893 * ":earlier" and ":later".
7894 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007896ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007897{
7898 long count = 0;
7899 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007900 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007901 char_u *p = eap->arg;
7902
7903 if (*p == NUL)
7904 count = 1;
7905 else if (isdigit(*p))
7906 {
7907 count = getdigits(&p);
7908 switch (*p)
7909 {
7910 case 's': ++p; sec = TRUE; break;
7911 case 'm': ++p; sec = TRUE; count *= 60; break;
7912 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007913 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7914 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007915 }
7916 }
7917
7918 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007919 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007920 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007921 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7922 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007923}
7924
7925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 * ":redir": start/stop redirection.
7927 */
7928 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007929ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
7931 char *mode;
7932 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007933 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934
Bram Moolenaarba768492016-07-08 15:32:54 +02007935#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007936 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007938 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007939 return;
7940 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007941#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007942
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 if (STRICMP(eap->arg, "END") == 0)
7944 close_redir();
7945 else
7946 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007947 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007949 ++arg;
7950 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007952 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 mode = "a";
7954 }
7955 else
7956 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007957 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958
7959 close_redir();
7960
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007961 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007962 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 if (fname == NULL)
7964 return;
7965#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007966 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {
7968 char_u *browseFile;
7969
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007970 browseFile = do_browse(BROWSE_SAVE,
7971 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007972 fname, NULL, NULL,
7973 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007975 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 vim_free(fname);
7977 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007978 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 }
7980#endif
7981
7982 redir_fd = open_exfile(fname, eap->forceit, mode);
7983 vim_free(fname);
7984 }
7985#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007986 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007988 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007990 ++arg;
7991 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007993 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007994 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007996 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007998 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007999 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008000 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008001 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008003 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008004 if (*arg == '>')
8005 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008006 // Make register empty when not using @A-@Z and the
8007 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008008 if (*arg == NUL && !isupper(redir_reg))
8009 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008011 }
8012 if (*arg != NUL)
8013 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008014 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008015 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008016 }
8017 }
8018 else if (*arg == '=' && arg[1] == '>')
8019 {
8020 int append;
8021
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008022 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008023 close_redir();
8024 arg += 2;
8025
8026 if (*arg == '>')
8027 {
8028 ++arg;
8029 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 }
8031 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008032 append = FALSE;
8033
8034 if (var_redir_start(skipwhite(arg), append) == OK)
8035 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 }
8037#endif
8038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008039 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008042 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008044
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008045 // Make sure redirection is not off. Can happen for cmdline completion
8046 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008047 if (redir_fd != NULL
8048#ifdef FEAT_EVAL
8049 || redir_reg || redir_vname
8050#endif
8051 )
8052 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053}
8054
8055/*
8056 * ":redraw": force redraw
8057 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008058 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008059ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060{
8061 int r = RedrawingDisabled;
8062 int p = p_lz;
8063
8064 RedrawingDisabled = 0;
8065 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008066 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008068 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069#ifdef FEAT_TITLE
8070 if (need_maketitle)
8071 maketitle();
8072#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008073#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8074# ifdef VIMDLL
8075 if (!gui.in_use)
8076# endif
8077 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 RedrawingDisabled = r;
8080 p_lz = p;
8081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008082 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 msg_didout = FALSE;
8084 msg_col = 0;
8085
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008086 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008087 need_wait_return = FALSE;
8088
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 out_flush();
8090}
8091
8092/*
8093 * ":redrawstatus": force redraw of status line(s)
8094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008096ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 int r = RedrawingDisabled;
8099 int p = p_lz;
8100
8101 RedrawingDisabled = 0;
8102 p_lz = FALSE;
8103 if (eap->forceit)
8104 status_redraw_all();
8105 else
8106 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008107 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 RedrawingDisabled = r;
8109 p_lz = p;
8110 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111}
8112
Bram Moolenaare12bab32019-01-08 22:02:56 +01008113/*
8114 * ":redrawtabline": force redraw of the tabline
8115 */
8116 static void
8117ex_redrawtabline(exarg_T *eap UNUSED)
8118{
8119 int r = RedrawingDisabled;
8120 int p = p_lz;
8121
8122 RedrawingDisabled = 0;
8123 p_lz = FALSE;
8124
8125 draw_tabline();
8126
8127 RedrawingDisabled = r;
8128 p_lz = p;
8129 out_flush();
8130}
8131
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008133close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134{
8135 if (redir_fd != NULL)
8136 {
8137 fclose(redir_fd);
8138 redir_fd = NULL;
8139 }
8140#ifdef FEAT_EVAL
8141 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008142 if (redir_vname)
8143 {
8144 var_redir_stop();
8145 redir_vname = 0;
8146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147#endif
8148}
8149
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008150#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008151 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008152vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008153{
8154 if (vim_mkdir(name, prot) != 0)
8155 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008156 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008157 return FAIL;
8158 }
8159 return OK;
8160}
8161#endif
8162
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163/*
8164 * Open a file for writing for an Ex command, with some checks.
8165 * Return file descriptor, or NULL on failure.
8166 */
8167 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008168open_exfile(
8169 char_u *fname,
8170 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008171 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172{
8173 FILE *fd;
8174
8175#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008176 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 if (mch_isdir(fname))
8178 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008179 semsg(_(e_src_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 return NULL;
8181 }
8182#endif
8183 if (!forceit && *mode != 'a' && vim_fexists(fname))
8184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008185 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 return NULL;
8187 }
8188
8189 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008190 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191
8192 return fd;
8193}
8194
8195/*
8196 * ":mark" and ":k".
8197 */
8198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008199ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200{
8201 pos_T pos;
8202
Bram Moolenaar10b94212021-02-19 21:42:57 +01008203#ifdef FEAT_EVAL
8204 if (not_in_vim9(eap) == FAIL)
8205 return;
8206#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008207 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008208 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008209 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008210 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 else
8212 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008213 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 curwin->w_cursor.lnum = eap->line2;
8215 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008216 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008217 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008218 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 }
8220}
8221
8222/*
8223 * Update w_topline, w_leftcol and the cursor position.
8224 */
8225 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008226update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008228 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 update_topline();
8230 if (!curwin->w_p_wrap)
8231 validate_cursor();
8232 update_curswant();
8233}
8234
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008236 * Save the current State and go to Normal mode.
8237 * Return TRUE if the typeahead could be saved.
8238 */
8239 int
8240save_current_state(save_state_T *sst)
8241{
8242 sst->save_msg_scroll = msg_scroll;
8243 sst->save_restart_edit = restart_edit;
8244 sst->save_msg_didout = msg_didout;
8245 sst->save_State = State;
8246 sst->save_insertmode = p_im;
8247 sst->save_finish_op = finish_op;
8248 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008249 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008250
Bram Moolenaar4324d872020-12-01 20:12:24 +01008251 msg_scroll = FALSE; // no msg scrolling in Normal mode
8252 restart_edit = 0; // don't go to Insert mode
8253 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008254
Bram Moolenaara452b802020-12-01 21:47:59 +01008255 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008256 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008257
8258 /*
8259 * Save the current typeahead. This is required to allow using ":normal"
8260 * from an event handler and makes sure we don't hang when the argument
8261 * ends with half a command.
8262 */
8263 save_typeahead(&sst->tabuf);
8264 return sst->tabuf.typebuf_valid;
8265}
8266
8267 void
8268restore_current_state(save_state_T *sst)
8269{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008270 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008271 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008272
8273 msg_scroll = sst->save_msg_scroll;
8274 restart_edit = sst->save_restart_edit;
8275 p_im = sst->save_insertmode;
8276 finish_op = sst->save_finish_op;
8277 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008278 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008279 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008280 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008281
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008282 // Restore the state (needed when called from a function executed for
8283 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008284 State = sst->save_State;
8285#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008286 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008287#endif
8288}
8289
8290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 * ":normal[!] {commands}": Execute normal mode commands.
8292 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008293 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008294ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008296 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 char_u *arg = NULL;
8298 int l;
8299 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008301 if (ex_normal_lock > 0)
8302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008303 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008304 return;
8305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 if (ex_normal_busy >= p_mmd)
8307 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008308 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 return;
8310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 /*
8313 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8314 * this for the K_SPECIAL leading byte, otherwise special keys will not
8315 * work.
8316 */
8317 if (has_mbyte)
8318 {
8319 int len = 0;
8320
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008321 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 for (p = eap->arg; *p != NUL; ++p)
8323 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008324#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008325 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008327#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008328 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008329 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008330#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 )
8334 len += 2;
8335 }
8336 if (len > 0)
8337 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008338 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 if (arg != NULL)
8340 {
8341 len = 0;
8342 for (p = eap->arg; *p != NUL; ++p)
8343 {
8344 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008345#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 if (*p == CSI)
8347 {
8348 arg[len++] = KS_EXTRA;
8349 arg[len++] = (int)KE_CSI;
8350 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008351#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008352 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {
8354 arg[len++] = *++p;
8355 if (*p == K_SPECIAL)
8356 {
8357 arg[len++] = KS_SPECIAL;
8358 arg[len++] = KE_FILLER;
8359 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008360#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 else if (*p == CSI)
8362 {
8363 arg[len++] = KS_EXTRA;
8364 arg[len++] = (int)KE_CSI;
8365 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 }
8368 arg[len] = NUL;
8369 }
8370 }
8371 }
8372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008374 ++ex_normal_busy;
8375 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {
8377 /*
8378 * Repeat the :normal command for each line in the range. When no
8379 * range given, execute it just once, without positioning the cursor
8380 * first.
8381 */
8382 do
8383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 if (eap->addr_count != 0)
8385 {
8386 curwin->w_cursor.lnum = eap->line1++;
8387 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008388 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 }
8390
Bram Moolenaar13505972019-01-24 15:04:48 +01008391 exec_normal_cmd(arg != NULL
8392 ? arg
8393 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 }
8395 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8396 }
8397
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008398 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 update_topline_cursor();
8400
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008401 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008403 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008404#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008405 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008406#endif
8407
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409}
8410
8411/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008412 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 */
8414 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008415ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008417 if (eap->forceit)
8418 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008419 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008420 if (!curwin->w_cursor.lnum)
8421 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008422 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008423 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008424#ifdef FEAT_TERMINAL
8425 // Ignore this when running in an active terminal.
8426 if (term_job_running(curbuf->b_term))
8427 return;
8428#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008429
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008430 // Ignore the command when already in Insert mode. Inserting an
8431 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008432 if (State & INSERT)
8433 return;
8434
Bram Moolenaar64969662005-12-14 21:59:55 +00008435 if (eap->cmdidx == CMD_startinsert)
8436 restart_edit = 'a';
8437 else if (eap->cmdidx == CMD_startreplace)
8438 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008440 restart_edit = 'V';
8441
8442 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008444 if (eap->cmdidx == CMD_startinsert)
8445 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008446 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008448
8449 if (VIsual_active)
8450 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451}
8452
8453/*
8454 * ":stopinsert"
8455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008457ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458{
8459 restart_edit = 0;
8460 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008461 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008464/*
8465 * Execute normal mode command "cmd".
8466 * "remap" can be REMAP_NONE or REMAP_YES.
8467 */
8468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008469exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008470{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008471 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008472 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008473 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008474}
Bram Moolenaar25281632016-01-21 23:32:32 +01008475
Bram Moolenaar25281632016-01-21 23:32:32 +01008476/*
8477 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008478 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008479 */
8480 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008481exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008482{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008483 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008484 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008485
Bram Moolenaar905dd902019-04-07 14:21:47 +02008486 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8487 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008488 clear_oparg(&oa);
8489 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008490 while ((!stuff_empty()
8491 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008492 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008493 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008494 {
8495 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008496#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008497 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008498 && oa.op_type == OP_NOP && oa.regname == NUL
8499 && !VIsual_active)
8500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008501 // If terminal_loop() returns OK we got a key that is handled
8502 // in Normal model. With FAIL we first need to position the
8503 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008504 if (terminal_loop(TRUE) == OK)
8505 normal_cmd(&oa, TRUE);
8506 }
8507 else
8508#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008509 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008510 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008511 }
8512}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008513
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514#ifdef FEAT_FIND_ID
8515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008516ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517{
8518 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8519 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8520 (linenr_T)1, (linenr_T)MAXLNUM);
8521}
8522
Bram Moolenaar4033c552017-09-16 20:54:51 +02008523#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524/*
8525 * ":psearch"
8526 */
8527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008528ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529{
8530 g_do_tagpreview = p_pvh;
8531 ex_findpat(eap);
8532 g_do_tagpreview = 0;
8533}
8534#endif
8535
8536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008537ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539 int whole = TRUE;
8540 long n;
8541 char_u *p;
8542 int action;
8543
8544 switch (cmdnames[eap->cmdidx].cmd_name[2])
8545 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008546 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8548 action = ACTION_GOTO;
8549 else
8550 action = ACTION_SHOW;
8551 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008552 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 action = ACTION_SHOW_ALL;
8554 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008555 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 action = ACTION_GOTO;
8557 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008558 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 action = ACTION_SPLIT;
8560 break;
8561 }
8562
8563 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008564 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {
8566 n = getdigits(&eap->arg);
8567 eap->arg = skipwhite(eap->arg);
8568 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008569 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 {
8571 whole = FALSE;
8572 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008573 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 if (*p)
8575 {
8576 *p++ = NUL;
8577 p = skipwhite(p);
8578
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008579 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008580 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008581 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 else
8583 eap->nextcmd = check_nextcmd(p);
8584 }
8585 }
8586 if (!eap->skip)
8587 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8588 whole, !eap->forceit,
8589 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8590 n, action, eap->line1, eap->line2);
8591}
8592#endif
8593
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594
Bram Moolenaar4033c552017-09-16 20:54:51 +02008595#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596/*
8597 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8598 */
8599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008600ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008602 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8604}
8605
8606/*
8607 * ":pedit"
8608 */
8609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008610ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611{
8612 win_T *curwin_save = curwin;
8613
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008614 if (ERROR_IF_ANY_POPUP_WINDOW)
8615 return;
8616
Bram Moolenaar026587b2019-08-17 15:08:00 +02008617 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008619 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008620
Bram Moolenaar026587b2019-08-17 15:08:00 +02008621 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008623
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 if (curwin != curwin_save && win_valid(curwin_save))
8625 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008626 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 validate_cursor();
8628 redraw_later(VALID);
8629 win_enter(curwin_save, TRUE);
8630 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008631# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008632 else if (WIN_IS_POPUP(curwin))
8633 {
8634 // can't keep focus in popup window
8635 win_enter(firstwin, TRUE);
8636 }
8637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 g_do_tagpreview = 0;
8639}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641
8642/*
8643 * ":stag", ":stselect" and ":stjump".
8644 */
8645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008646ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647{
8648 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008649 postponed_split_flags = cmdmod.cmod_split;
8650 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8652 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008653 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655
8656/*
8657 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8658 */
8659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008660ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
8662 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8663}
8664
8665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008666ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667{
8668 int cmd;
8669
8670 switch (name[1])
8671 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008672 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008674 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008676 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008678 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008680 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008682 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008684 case 'f': // ":tfirst"
8685 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008687 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008689 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008691 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008693 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 return;
8695 }
8696#endif
8697 cmd = DT_TAG;
8698 break;
8699 }
8700
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008701 if (name[0] == 'l')
8702 {
8703#ifndef FEAT_QUICKFIX
8704 ex_ni(eap);
8705 return;
8706#else
8707 cmd = DT_LTAG;
8708#endif
8709 }
8710
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8712 eap->forceit, TRUE);
8713}
8714
8715/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008716 * Check "str" for starting with a special cmdline variable.
8717 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8718 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8719 */
8720 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008721find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008722{
8723 int len;
8724 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008725 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008726 "%",
8727#define SPEC_PERC 0
8728 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008729#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008730 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008731#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008732 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008733#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008734 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008735#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008736 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008737#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008738 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008739#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008740 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008741#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008742 "<stack>", // call stack
8743#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008744 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008745#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008746 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008747#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008748 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008749#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008750 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008751#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008752 "<SID>", // script ID: <SNR>123_
8753#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008754#ifdef FEAT_CLIENTSERVER
8755 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008756# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008757#endif
8758 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008759
K.Takataeeec2542021-06-02 13:28:16 +02008760 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008761 {
8762 len = (int)STRLEN(spec_str[i]);
8763 if (STRNCMP(src, spec_str[i], len) == 0)
8764 {
8765 *usedlen = len;
8766 return i;
8767 }
8768 }
8769 return -1;
8770}
8771
8772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 * Evaluate cmdline variables.
8774 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008775 * change "%" to curbuf->b_ffname
8776 * "#" to curwin->w_alt_fnum
8777 * "%%" to curwin->w_alt_fnum in Vim9 script
8778 * "<cword>" to word under the cursor
8779 * "<cWORD>" to WORD under the cursor
8780 * "<cexpr>" to C-expression under the cursor
8781 * "<cfile>" to path name under the cursor
8782 * "<sfile>" to sourced file name
8783 * "<stack>" to call stack
8784 * "<slnum>" to sourced file line number
8785 * "<afile>" to file name for autocommand
8786 * "<abuf>" to buffer number for autocommand
8787 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 *
8789 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8790 * "" for error without a message) and NULL is returned.
8791 * Returns an allocated string if a valid match was found.
8792 * Returns NULL if no match was found. "usedlen" then still contains the
8793 * number of characters to skip.
8794 */
8795 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008796eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008797 char_u *src, // pointer into commandline
8798 char_u *srcstart, // beginning of valid memory for src
8799 int *usedlen, // characters after src that are used
8800 linenr_T *lnump, // line number for :e command, or NULL
8801 char **errormsg, // pointer to error message
8802 int *escaped) // return value has escaped white space (can
8803 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805 int i;
8806 char_u *s;
8807 char_u *result;
8808 char_u *resultbuf = NULL;
8809 int resultlen;
8810 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008811 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008813 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816
8817 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008818 if (escaped != NULL)
8819 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820
8821 /*
8822 * Check if there is something to do.
8823 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008824 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008825 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 {
8827 *usedlen = 1;
8828 return NULL;
8829 }
8830
8831 /*
8832 * Skip when preceded with a backslash "\%" and "\#".
8833 * Note: In "\\%" the % is also not recognized!
8834 */
8835 if (src > srcstart && src[-1] == '\\')
8836 {
8837 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008838 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 return NULL;
8840 }
8841
8842 /*
8843 * word or WORD under cursor
8844 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008845 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8846 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008848 resultlen = find_ident_under_cursor(&result,
8849 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8850 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8851 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 if (resultlen == 0)
8853 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008854 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 return NULL;
8856 }
8857 }
8858
8859 /*
8860 * '#': Alternate file name
8861 * '%': Current file name
8862 * File name under the cursor
8863 * File name for autocommand
8864 * and following modifiers
8865 */
8866 else
8867 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008868 int off = 0;
8869
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 switch (spec_idx)
8871 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008872 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008873#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008874 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008877 // '%': current file
8878 if (curbuf->b_fname == NULL)
8879 {
8880 result = (char_u *)"";
8881 valid = 0; // Must have ":p:h" to be valid
8882 }
8883 else
8884 {
8885 result = curbuf->b_fname;
8886 tilde_file = STRCMP(result, "~") == 0;
8887 }
8888 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008890#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008891 // "%%" alternate file
8892 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008893#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008894 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008895 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008896 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008898 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 result = arg_all();
8900 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008901 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008902 if (escaped != NULL)
8903 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 break;
8906 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008907 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008908 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008909 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008911 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008912 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008913 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008914 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008916 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008918 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00008919 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008920 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008921 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008922 return NULL;
8923 }
8924#ifdef FEAT_EVAL
8925 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8926 (long)i);
8927 if (result == NULL)
8928 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008929 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008930 return NULL;
8931 }
8932#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008933 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 }
8937 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008938 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008939 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
8940 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008941 buf = buflist_findnr(i);
8942 if (buf == NULL)
8943 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008944 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008945 return NULL;
8946 }
8947 if (lnump != NULL)
8948 *lnump = ECMD_LAST;
8949 if (buf->b_fname == NULL)
8950 {
8951 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008952 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008953 }
8954 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008955 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008956 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008957 tilde_file = STRCMP(result, "~") == 0;
8958 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 break;
8961
8962#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008963 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008964 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 if (result == NULL)
8966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008967 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 return NULL;
8969 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008970 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 break;
8972#endif
8973
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008974 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008976 if (result != NULL && !autocmd_fname_full)
8977 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008978 // Still need to turn the fname into a full path. It is
8979 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008980 autocmd_fname_full = TRUE;
8981 result = FullName_save(autocmd_fname, FALSE);
8982 vim_free(autocmd_fname);
8983 autocmd_fname = result;
8984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 if (result == NULL)
8986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008987 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 return NULL;
8989 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008990 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 break;
8992
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008993 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 if (autocmd_bufnr <= 0)
8995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008996 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 return NULL;
8998 }
8999 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9000 result = strbuf;
9001 break;
9002
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009003 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 result = autocmd_match;
9005 if (result == NULL)
9006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009007 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 return NULL;
9009 }
9010 break;
9011
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009012 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02009013 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02009014 result = estack_sfile(spec_idx == SPEC_SFILE
9015 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 if (result == NULL)
9017 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02009018 *errormsg = spec_idx == SPEC_SFILE
9019 ? _("E498: no :source file name to substitute for \"<sfile>\"")
9020 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 return NULL;
9022 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009023 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009025
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009026 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009027 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009029 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009030 return NULL;
9031 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009032 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009033 result = strbuf;
9034 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009035
9036#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009037 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009038 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009039 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009040 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009041 return NULL;
9042 }
9043 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009044 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009045 result = strbuf;
9046 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009047
Bram Moolenaar90944302020-08-01 20:45:11 +02009048 case SPEC_SID:
9049 if (current_sctx.sc_sid <= 0)
9050 {
9051 *errormsg = _(e_usingsid);
9052 return NULL;
9053 }
9054 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9055 result = strbuf;
9056 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009057#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009058
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009059#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009060 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009061 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9062 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 result = strbuf;
9064 break;
9065#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009066
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009067 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009068 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009069 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 }
9071
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009072 resultlen = (int)STRLEN(result); // length of new string
9073 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 {
9075 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 resultlen = (int)(s - result);
9078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 else if (!skip_mod)
9080 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009081 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 &resultlen);
9083 if (result == NULL)
9084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009085 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 return NULL;
9087 }
9088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 }
9090
9091 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9092 {
9093 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009094 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009095 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009097 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 result = NULL;
9099 }
9100 else
9101 result = vim_strnsave(result, resultlen);
9102 vim_free(resultbuf);
9103 return result;
9104}
9105
9106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 * Expand the <sfile> string in "arg".
9108 *
9109 * Returns an allocated string, or NULL for any error.
9110 */
9111 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009112expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009114 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 int len;
9116 char_u *result;
9117 char_u *newres;
9118 char_u *repl;
9119 int srclen;
9120 char_u *p;
9121
9122 result = vim_strsave(arg);
9123 if (result == NULL)
9124 return NULL;
9125
9126 for (p = result; *p; )
9127 {
9128 if (STRNCMP(p, "<sfile>", 7) != 0)
9129 ++p;
9130 else
9131 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009132 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009133 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 if (errormsg != NULL)
9135 {
9136 if (*errormsg)
9137 emsg(errormsg);
9138 vim_free(result);
9139 return NULL;
9140 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009141 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 {
9143 p += srclen;
9144 continue;
9145 }
9146 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9147 newres = alloc(len);
9148 if (newres == NULL)
9149 {
9150 vim_free(repl);
9151 vim_free(result);
9152 return NULL;
9153 }
9154 mch_memmove(newres, result, (size_t)(p - result));
9155 STRCPY(newres + (p - result), repl);
9156 len = (int)STRLEN(newres);
9157 STRCAT(newres, p + srclen);
9158 vim_free(repl);
9159 vim_free(result);
9160 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009161 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 }
9163 }
9164
9165 return result;
9166}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009169/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009170 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009171 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009174dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 if (fname == NULL)
9177 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009178 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179}
9180#endif
9181
9182/*
9183 * ":behave {mswin,xterm}"
9184 */
9185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009186ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187{
9188 if (STRCMP(eap->arg, "mswin") == 0)
9189 {
9190 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9191 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9192 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9193 set_option_value((char_u *)"keymodel", 0L,
9194 (char_u *)"startsel,stopsel", 0);
9195 }
9196 else if (STRCMP(eap->arg, "xterm") == 0)
9197 {
9198 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9199 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9200 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9201 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9202 }
9203 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009204 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205}
9206
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207static int filetype_detect = FALSE;
9208static int filetype_plugin = FALSE;
9209static int filetype_indent = FALSE;
9210
9211/*
9212 * ":filetype [plugin] [indent] {on,off,detect}"
9213 * on: Load the filetype.vim file to install autocommands for file types.
9214 * off: Load the ftoff.vim file to remove all autocommands for file types.
9215 * plugin on: load filetype.vim and ftplugin.vim
9216 * plugin off: load ftplugof.vim
9217 * indent on: load filetype.vim and indent.vim
9218 * indent off: load indoff.vim
9219 */
9220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009221ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222{
9223 char_u *arg = eap->arg;
9224 int plugin = FALSE;
9225 int indent = FALSE;
9226
9227 if (*eap->arg == NUL)
9228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009229 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009230 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 filetype_detect ? "ON" : "OFF",
9232 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9233 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9234 return;
9235 }
9236
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009237 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 for (;;)
9239 {
9240 if (STRNCMP(arg, "plugin", 6) == 0)
9241 {
9242 plugin = TRUE;
9243 arg = skipwhite(arg + 6);
9244 continue;
9245 }
9246 if (STRNCMP(arg, "indent", 6) == 0)
9247 {
9248 indent = TRUE;
9249 arg = skipwhite(arg + 6);
9250 continue;
9251 }
9252 break;
9253 }
9254 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9255 {
9256 if (*arg == 'o' || !filetype_detect)
9257 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009258 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 filetype_detect = TRUE;
9260 if (plugin)
9261 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009262 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 filetype_plugin = TRUE;
9264 }
9265 if (indent)
9266 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009267 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 filetype_indent = TRUE;
9269 }
9270 }
9271 if (*arg == 'd')
9272 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009273 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009274 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 }
9276 }
9277 else if (STRCMP(arg, "off") == 0)
9278 {
9279 if (plugin || indent)
9280 {
9281 if (plugin)
9282 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009283 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 filetype_plugin = FALSE;
9285 }
9286 if (indent)
9287 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009288 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 filetype_indent = FALSE;
9290 }
9291 }
9292 else
9293 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009294 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 filetype_detect = FALSE;
9296 }
9297 }
9298 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009299 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300}
9301
9302/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009303 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 */
9305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009306ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307{
9308 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009309 {
9310 char_u *arg = eap->arg;
9311
9312 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9313 arg += 9;
9314
9315 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9316 if (arg != eap->arg)
9317 did_filetype = FALSE;
9318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009322ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323{
9324#ifdef FEAT_DIGRAPHS
9325 if (*eap->arg != NUL)
9326 putdigraph(eap->arg);
9327 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009328 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329#else
mityu61065042021-07-19 20:07:21 +02009330 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331#endif
9332}
9333
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009334#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9335 void
9336set_no_hlsearch(int flag)
9337{
9338 no_hlsearch = flag;
9339# ifdef FEAT_EVAL
9340 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9341# endif
9342}
9343
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344/*
9345 * ":nohlsearch"
9346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009348ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009350 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009351 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353#endif
9354
9355#ifdef FEAT_CRYPT
9356/*
9357 * ":X": Get crypt key
9358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009360ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009362 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009363 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364}
9365#endif
9366
9367#ifdef FEAT_FOLDING
9368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009369ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370{
9371 if (foldManualAllowed(TRUE))
9372 foldCreate(eap->line1, eap->line2);
9373}
9374
9375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009376ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377{
9378 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9379 eap->forceit, FALSE);
9380}
9381
9382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009383ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385 linenr_T lnum;
9386
Bram Moolenaar4facea32019-10-12 20:17:40 +02009387# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009388 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009389# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009390
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009391 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9393 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9394 ml_setmarked(lnum);
9395
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009396 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009398 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009399# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009400 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402}
9403#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009405#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009406/*
9407 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9408 * instead of a quickfix command.
9409 */
9410 int
9411is_loclist_cmd(int cmdidx)
9412{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009413 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009414 return FALSE;
9415 return cmdnames[cmdidx].cmd_name[0] == 'l';
9416}
9417#endif
9418
Bram Moolenaar4facea32019-10-12 20:17:40 +02009419#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009420 int
9421get_pressedreturn(void)
9422{
9423 return ex_pressedreturn;
9424}
9425
9426 void
9427set_pressedreturn(int val)
9428{
9429 ex_pressedreturn = val;
9430}
9431#endif