blob: 1c41036a4aa7b9f2cd4b6842f6cc410376205c94 [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 Moolenaare31ee862020-01-07 20:59:34 +0100667 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100669 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
670 // location for storing error messages to be converted to an exception.
671 // This ensures that the do_errthrow() call in do_one_cmd() does not
672 // combine the messages stored by an earlier invocation of do_one_cmd()
673 // with the command name of the later one. This would happen when
674 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 saved_msg_list = msg_list;
676 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677#endif
678
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100679 // It's possible to create an endless loop with ":execute", catch that
680 // here. The value of 200 allows nested function calls, ":source", etc.
681 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100682 if (call_depth >= 200
683#ifdef FEAT_EVAL
684 && call_depth >= p_mfd
685#endif
686 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100688 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100690 // When converting to an exception, we do not include the command name
691 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100692 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 msg_list = saved_msg_list;
694#endif
695 return FAIL;
696 }
697 ++call_depth;
698
699#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100700 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 cstack.cs_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
703
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100704 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100706 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100707 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000708 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 ++ex_nesting_level;
710
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100711 // Get the function or script name and the address where the next breakpoint
712 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000713 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
715 fname = func_name(real_cookie);
716 breakpoint = func_breakpoint(real_cookie);
717 dbg_tick = func_dbg_tick(real_cookie);
718 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100719 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100721 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 breakpoint = source_breakpoint(real_cookie);
723 dbg_tick = source_dbg_tick(real_cookie);
724 }
725
726 /*
727 * Initialize "force_abort" and "suppress_errthrow" at the top level.
728 */
729 if (!recursive)
730 {
731 force_abort = FALSE;
732 suppress_errthrow = FALSE;
733 }
734
735 /*
736 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000737 * exception handling (used when debugging). Otherwise clear it to avoid
738 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000740 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000741 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000742 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200743 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
745 initial_trylevel = trylevel;
746
747 /*
748 * "did_throw" will be set to TRUE when an exception is being thrown.
749 */
750 did_throw = FALSE;
751#endif
752 /*
753 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000754 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 * If force_abort is set, we cancel everything.
756 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100757#ifdef FEAT_EVAL
758 did_emsg_cumul += did_emsg;
759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 did_emsg = FALSE;
761
762 /*
763 * KeyTyped is only set when calling vgetc(). Reset it here when not
764 * calling vgetc() (sourced command lines).
765 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100766 if (!(flags & DOCMD_KEYTYPED)
767 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 KeyTyped = FALSE;
769
770 /*
771 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000772 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 * - for multiple commands on one line, separated with '|'
774 * - when repeating until there are no more lines (for ":source")
775 */
776 next_cmdline = cmdline;
777 do
778 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100780 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000781#endif
782
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100783 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (next_cmdline == NULL
785#ifdef FEAT_EVAL
786 && !force_abort
787 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000788 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789#endif
790 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100791 {
792#ifdef FEAT_EVAL
793 did_emsg_cumul += did_emsg;
794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
798 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000799 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100800 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 * 3. If a line is given: Make a copy, so we can mess with it.
802 */
803
804#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100805 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000806 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100808 // Each '|' separated command is stored separately in lines_ga, to
809 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100810 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100812 // Check if a function has returned or, unless it has an unclosed
813 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000817 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818 func_line_end(real_cookie);
819# endif
820 if (func_has_ended(real_cookie))
821 {
822 retval = FAIL;
823 break;
824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000827 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000829 script_line_end();
830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100833 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 retval = FAIL;
836 break;
837 }
838
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100839 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 if (breakpoint != NULL && dbg_tick != NULL
841 && *dbg_tick != debug_tick)
842 {
843 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100844 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100845 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 *dbg_tick = debug_tick;
847 }
848
849 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100850 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100852 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100854 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100856 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100857 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100859 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100860 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 *dbg_tick = debug_tick;
862 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000863# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000864 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000865 {
866 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100867 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000869 script_line_start();
870 }
871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#endif
874
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100875 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (next_cmdline == NULL)
877 {
878 /*
879 * Need to set msg_didout for the first line after an ":if",
880 * otherwise the ":if" will be overwritten.
881 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100882 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885#ifdef FEAT_EVAL
886 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
887#else
888 0
889#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100890 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
891 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100893 // Don't call wait_return for aborted command line. The NULL
894 // returned for the end of a sourced file or executed function
895 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (KeyTyped && !(flags & DOCMD_REPEAT))
897 need_wait_return = FALSE;
898 retval = FAIL;
899 break;
900 }
901 used_getline = TRUE;
902
903 /*
904 * Keep the first typed line. Clear it when more lines are typed.
905 */
906 if (flags & DOCMD_KEEPLINE)
907 {
908 vim_free(repeat_cmdline);
909 if (count == 0)
910 repeat_cmdline = vim_strsave(next_cmdline);
911 else
912 repeat_cmdline = NULL;
913 }
914 }
915
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100916 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 else if (cmdline_copy == NULL)
918 {
919 next_cmdline = vim_strsave(next_cmdline);
920 if (next_cmdline == NULL)
921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100922 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 retval = FAIL;
924 break;
925 }
926 }
927 cmdline_copy = next_cmdline;
928
929#ifdef FEAT_EVAL
930 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200931 * Inside a while/for loop, and when the command looks like a ":while"
932 * or ":for", the line is stored, because we may need it later when
933 * looping.
934 *
935 * When there is a '|' and another command, it is stored separately,
936 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200938 *
939 * Pass a different "fgetline" function to do_one_cmd() below,
940 * that it stores lines in or reads them from "lines_ga". Makes it
941 * possible to define a function inside a while/for loop and handles
942 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200944 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200946 cmd_getline = get_loop_line;
947 cmd_cookie = (void *)&cmd_loop_cookie;
948 cmd_loop_cookie.lines_gap = &lines_ga;
949 cmd_loop_cookie.current_line = current_line;
950 cmd_loop_cookie.getline = fgetline;
951 cmd_loop_cookie.cookie = cookie;
952 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
953
954 // Save the current line when encountering it the first time.
955 if (current_line == lines_ga.ga_len
956 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
958 retval = FAIL;
959 break;
960 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200961 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200963 else
964 {
965 cmd_getline = fgetline;
966 cmd_cookie = cookie;
967 }
968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 did_endif = FALSE;
970#endif
971
972 if (count++ == 0)
973 {
974 /*
975 * All output from the commands is put below each other, without
976 * waiting for a return. Don't do this when executing commands
977 * from a script or when being called recursive (e.g. for ":e
978 * +command file").
979 */
980 if (!(flags & DOCMD_NOWAIT) && !recursive)
981 {
982 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100983 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100985 msg_scroll = TRUE; // put messages below each other
986 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 ++RedrawingDisabled;
988 did_inc = TRUE;
989 }
990 }
991
Bram Moolenaar823654b2020-05-29 23:03:09 +0200992 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100993 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995 /*
996 * 2. Execute one '|' separated command.
997 * do_one_cmd() will return NULL if there is no trailing '|'.
998 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
999 */
1000 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001001 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002#ifdef FEAT_EVAL
1003 &cstack,
1004#endif
1005 cmd_getline, cmd_cookie);
1006 --recursive;
1007
1008#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001009 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001010 // Use "current_line" from "cmd_loop_cookie", it may have been
1011 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001012 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013#endif
1014
1015 if (next_cmdline == NULL)
1016 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001017 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 /*
1020 * If the command was typed, remember it for the ':' register.
1021 * Do this AFTER executing the command to make :@: work.
1022 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 && new_last_cmdline != NULL)
1025 {
1026 vim_free(last_cmdline);
1027 last_cmdline = new_last_cmdline;
1028 new_last_cmdline = NULL;
1029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 }
1031 else
1032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001033 // need to copy the command after the '|' to cmdline_copy, for the
1034 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001035 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 next_cmdline = cmdline_copy;
1037 }
1038
1039
1040#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001041 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001043 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001045 {
1046 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001050 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
1052 ++current_line;
1053
1054 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001055 * An ":endwhile", ":endfor" and ":continue" is handled here.
1056 * If we were executing commands, jump back to the ":while" or
1057 * ":for".
1058 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001060 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001064 // Jump back to the matching ":while" or ":for". Be careful
1065 // not to use a cs_line[] from an entry that isn't a ":while"
1066 // or ":for": It would make "current_line" invalid and can
1067 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (!did_emsg && !got_int && !did_throw
1069 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 && (cstack.cs_flags[cstack.cs_idx]
1071 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 && cstack.cs_line[cstack.cs_idx] >= 0
1073 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1074 {
1075 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001076 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001078 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001080 // Check for the next breakpoint at or after the ":while"
1081 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (breakpoint != NULL)
1083 {
1084 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001085 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 fname,
1087 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1088 *dbg_tick = debug_tick;
1089 }
1090 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001091 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001093 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001095 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1096 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098 }
1099
1100 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001105 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001106 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 }
1108 }
1109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001110 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001111 if (breakpoint != NULL && has_watchexpr())
1112 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001113 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001114 *dbg_tick = debug_tick;
1115 }
1116
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 /*
1118 * When not inside any ":while" loop, clear remembered lines.
1119 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001120 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
1122 if (lines_ga.ga_len > 0)
1123 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001124 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1126 free_cmdlines(&lines_ga);
1127 }
1128 current_line = 0;
1129 }
1130
1131 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1133 * being restored at the ":endtry". Reset them here and set the
1134 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1135 * This includes the case where a missing ":endif", ":endwhile" or
1136 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 cstack.cs_lflags &= ~CSL_HAD_FINA;
1141 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1142 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 did_throw ? (void *)current_exception : NULL);
1144 did_emsg = got_int = did_throw = FALSE;
1145 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1146 }
1147
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001148 // Update global "trylevel" for recursive calls to do_cmdline() from
1149 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 trylevel = initial_trylevel + cstack.cs_trylevel;
1151
1152 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001153 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 * files) is aborted because of an error, an interrupt, or an uncaught
1155 * exception, cancel everything. If it is left normally, reset
1156 * force_abort to get the non-EH compatible abortion behavior for
1157 * the rest of the script.
1158 */
1159 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1160 force_abort = FALSE;
1161
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001162 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001164#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
1166 }
1167 /*
1168 * Continue executing command lines when:
1169 * - no CTRL-C typed, no aborting error, no exception thrown or try
1170 * conditionals need to be checked for executing finally clauses or
1171 * catching an interrupt exception
1172 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 * looping for ":source" command or function call.
1175 */
1176 while (!((got_int
1177#ifdef FEAT_EVAL
1178 || (did_emsg && force_abort) || did_throw
1179#endif
1180 )
1181#ifdef FEAT_EVAL
1182 && cstack.cs_trylevel == 0
1183#endif
1184 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001185 && !(did_emsg
1186#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001187 // Keep going when inside try/catch, so that the error can be
1188 // deal with, except when it is a syntax error, it may cause
1189 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001190 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1191#endif
1192 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001193 && (getline_equal(fgetline, cookie, getexmodeline)
1194 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 && (next_cmdline != NULL
1196#ifdef FEAT_EVAL
1197 || cstack.cs_idx >= 0
1198#endif
1199 || (flags & DOCMD_REPEAT)));
1200
1201 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001202 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#ifdef FEAT_EVAL
1204 free_cmdlines(&lines_ga);
1205 ga_clear(&lines_ga);
1206
1207 if (cstack.cs_idx >= 0)
1208 {
1209 /*
1210 * If a sourced file or executed function ran to its end, report the
1211 * unclosed conditional.
1212 */
1213 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001214 && ((getline_equal(fgetline, cookie, getsourceline)
1215 && !source_finished(fgetline, cookie))
1216 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 && !func_has_ended(real_cookie))))
1218 {
1219 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001220 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001222 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001224 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 }
1228
1229 /*
1230 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1231 * ":endtry" in a sourced file or executed function. If the try
1232 * conditional is in its finally clause, ignore anything pending.
1233 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001234 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 */
1236 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001237 {
1238 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1239
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001240 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001241 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001242 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1243 &cstack.cs_looplevel);
1244 }
1245 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 trylevel = initial_trylevel;
1247 }
1248
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001249 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1250 // lack was reported above and the error message is to be converted to an
1251 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001252 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 ? (char_u *)"endfunction" : (char_u *)NULL);
1254
1255 if (trylevel == 0)
1256 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001257 // Just in case did_throw got set but current_exception wasn't.
1258 if (current_exception == NULL)
1259 did_throw = FALSE;
1260
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 /*
1262 * When an exception is being thrown out of the outermost try
1263 * conditional, discard the uncaught exception, disable the conversion
1264 * of interrupts or errors to exceptions, and ensure that no more
1265 * commands are executed.
1266 */
1267 if (did_throw)
1268 {
Bram Moolenaar033135e2021-01-14 21:40:22 +01001269 char *p = NULL;
Bram Moolenaare8c46602021-04-05 22:27:37 +02001270 msglist_T *messages = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271
1272 /*
1273 * If the uncaught exception is a user exception, report it as an
1274 * error. If it is an error exception, display the saved error
1275 * message now. For an interrupt exception, do nothing; the
1276 * interrupt message is given elsewhere.
1277 */
1278 switch (current_exception->type)
1279 {
1280 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001281 vim_snprintf((char *)IObuff, IOSIZE,
1282 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 current_exception->value);
Bram Moolenaar033135e2021-01-14 21:40:22 +01001284 p = (char *)vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 break;
1286 case ET_ERROR:
1287 messages = current_exception->messages;
1288 current_exception->messages = NULL;
1289 break;
1290 case ET_INTERRUPT:
1291 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001294 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1295 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001296 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 current_exception->throw_name = NULL;
1298
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001299 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 suppress_errthrow = TRUE;
1301 force_abort = TRUE;
1302
1303 if (messages != NULL)
1304 {
1305 do
1306 {
Bram Moolenaare8c46602021-04-05 22:27:37 +02001307 msglist_T *next = messages->next;
1308 int save_compiling = estack_compiling;
1309
1310 estack_compiling = messages->msg_compiling;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 emsg(messages->msg);
1312 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001313 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 vim_free(messages);
1315 messages = next;
Bram Moolenaare8c46602021-04-05 22:27:37 +02001316 estack_compiling = save_compiling;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 }
1318 while (messages != NULL);
1319 }
1320 else if (p != NULL)
1321 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001322 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 vim_free(p);
1324 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001325 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001326 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001327 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329
1330 /*
1331 * On an interrupt or an aborting error not converted to an exception,
1332 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001333 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 * when force_abort is set and did_emsg unset in case of an interrupt
1335 * from a finally clause after an error.
1336 */
1337 else if (got_int || (did_emsg && force_abort))
1338 suppress_errthrow = TRUE;
1339 }
1340
1341 /*
1342 * The current cstack will be freed when do_cmdline() returns. An uncaught
1343 * exception will have to be rethrown in the previous cstack. If a function
1344 * has just returned or a script file was just finished and the previous
1345 * cstack belongs to the same function or, respectively, script file, it
1346 * will have to be checked for finally clauses to be executed due to the
1347 * ":return" or ":finish". This is done in do_one_cmd().
1348 */
1349 if (did_throw)
1350 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001351 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001353 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 && ex_nesting_level > func_level(real_cookie) + 1))
1355 {
1356 if (!did_throw)
1357 check_cstack = TRUE;
1358 }
1359 else
1360 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001361 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001362 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 --ex_nesting_level;
1364 /*
1365 * Go to debug mode when returning from a function in which we are
1366 * single-stepping.
1367 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001368 if ((getline_equal(fgetline, cookie, getsourceline)
1369 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001371 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 ? (char_u *)_("End of sourced file")
1373 : (char_u *)_("End of function"));
1374 }
1375
1376 /*
1377 * Restore the exception environment (done after returning from the
1378 * debugger).
1379 */
1380 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001381 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
1383 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001384
1385 // Cleanup if "cs_emsg_silent_list" remains.
1386 if (cstack.cs_emsg_silent_list != NULL)
1387 {
1388 eslist_T *elem, *temp;
1389
1390 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1391 {
1392 temp = elem->next;
1393 vim_free(elem);
1394 }
1395 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001396#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397
1398 /*
1399 * If there was too much output to fit on the command line, ask the user to
1400 * hit return before redrawing the screen. With the ":global" command we do
1401 * this only once after the command is finished.
1402 */
1403 if (did_inc)
1404 {
1405 --RedrawingDisabled;
1406 --no_wait_return;
1407 msg_scroll = FALSE;
1408
1409 /*
1410 * When just finished an ":if"-":else" which was typed, no need to
1411 * wait for hit-return. Also for an error situation.
1412 */
1413 if (retval == FAIL
1414#ifdef FEAT_EVAL
1415 || (did_endif && KeyTyped && !did_emsg)
1416#endif
1417 )
1418 {
1419 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001420 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 }
1422 else if (need_wait_return)
1423 {
1424 /*
1425 * The msg_start() above clears msg_didout. The wait_return we do
1426 * here should not overwrite the command that may be shown before
1427 * doing that.
1428 */
1429 msg_didout |= msg_didout_before_start;
1430 wait_return(FALSE);
1431 }
1432 }
1433
Bram Moolenaar2a942252012-11-28 23:03:07 +01001434#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001435 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001436#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 /*
1438 * Reset if_level, in case a sourced script file contains more ":if" than
1439 * ":endif" (could be ":if x | foo | endif").
1440 */
1441 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001442#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001443
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 --call_depth;
1445 return retval;
1446}
1447
1448#ifdef FEAT_EVAL
1449/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001450 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 */
1452 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001453get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001455 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 wcmd_T *wp;
1457 char_u *line;
1458
1459 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1460 {
1461 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001462 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001464 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001466 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001468 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001469 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 ++cp->current_line;
1471
1472 return line;
1473 }
1474
1475 KeyTyped = FALSE;
1476 ++cp->current_line;
1477 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001478 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 return vim_strsave(wp->line);
1480}
1481
1482/*
1483 * Store a line in "gap" so that a ":while" loop can execute it again.
1484 */
1485 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001486store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488 if (ga_grow(gap, 1) == FAIL)
1489 return FAIL;
1490 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001491 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 return OK;
1494}
1495
1496/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001497 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 */
1499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001500free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 while (gap->ga_len > 0)
1503 {
1504 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1505 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
1507}
1508#endif
1509
1510/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001511 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1512 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001515getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001516 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001517 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001518 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
1520#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001521 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001522 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001524 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1525 // function that's originally used to obtain the lines. This may be
1526 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001527 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001528 cp = (struct loop_cookie *)cookie;
1529 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
1531 gp = cp->getline;
1532 cp = cp->cookie;
1533 }
1534 return gp == func;
1535#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001536 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#endif
1538}
1539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001541 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 * getline function. Otherwise return "cookie".
1543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001545getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001546 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001547 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
Bram Moolenaar13505972019-01-24 15:04:48 +01001549#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001550 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001551 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001553 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1554 // cookie that's originally used to obtain the lines. This may be nested
1555 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001556 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001557 cp = (struct loop_cookie *)cookie;
1558 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 {
1560 gp = cp->getline;
1561 cp = cp->cookie;
1562 }
1563 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001564#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001567}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaard5053d02020-06-28 15:51:16 +02001569#if defined(FEAT_EVAL) || defined(PROT)
1570/*
1571 * Get the next line source line without advancing.
1572 */
1573 char_u *
1574getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001575 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001576 void *cookie) // argument for fgetline()
1577{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001578 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001579 struct loop_cookie *cp;
1580 wcmd_T *wp;
1581
1582 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1583 // cookie that's originally used to obtain the lines. This may be nested
1584 // several levels.
1585 gp = fgetline;
1586 cp = (struct loop_cookie *)cookie;
1587 while (gp == get_loop_line)
1588 {
1589 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1590 {
1591 // executing lines a second time, use the stored copy
1592 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1593 return wp->line;
1594 }
1595 gp = cp->getline;
1596 cp = cp->cookie;
1597 }
1598 if (gp == getsourceline)
1599 return source_nextline(cp);
1600 return NULL;
1601}
1602#endif
1603
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001604
1605/*
1606 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1607 * ":bwipeout", etc.
1608 * Returns the buffer number.
1609 */
1610 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001611compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001612{
1613 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001614 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615 int count = offset;
1616
1617 buf = firstbuf;
1618 while (buf->b_next != NULL && buf->b_fnum < lnum)
1619 buf = buf->b_next;
1620 while (count != 0)
1621 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001622 count += (offset < 0) ? 1 : -1;
1623 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1624 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001625 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001626 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001627 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001628 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001629 while (buf->b_ml.ml_mfp == NULL)
1630 {
1631 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1632 if (nextbuf == NULL)
1633 break;
1634 buf = nextbuf;
1635 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001636 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001637 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001638 if (addr_type == ADDR_LOADED_BUFFERS)
1639 while (buf->b_ml.ml_mfp == NULL)
1640 {
1641 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1642 if (nextbuf == NULL)
1643 break;
1644 buf = nextbuf;
1645 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001646 return buf->b_fnum;
1647}
1648
Bram Moolenaarb7316892019-05-01 18:08:42 +02001649/*
1650 * Return the window number of "win".
1651 * When "win" is NULL return the number of windows.
1652 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001653 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001654current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001655{
1656 win_T *wp;
1657 int nr = 0;
1658
Bram Moolenaar29323592016-07-24 22:04:11 +02001659 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001660 {
1661 ++nr;
1662 if (wp == win)
1663 break;
1664 }
1665 return nr;
1666}
1667
1668 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001669current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001670{
1671 tabpage_T *tp;
1672 int nr = 0;
1673
Bram Moolenaar29323592016-07-24 22:04:11 +02001674 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001675 {
1676 ++nr;
1677 if (tp == tab)
1678 break;
1679 }
1680 return nr;
1681}
1682
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001683 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001684comment_start(char_u *p, int starts_with_colon UNUSED)
1685{
1686#ifdef FEAT_EVAL
1687 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001688 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001689#endif
1690 return *p == '"';
1691}
1692
Bram Moolenaarf240e182014-11-27 18:33:02 +01001693# define CURRENT_WIN_NR current_win_nr(curwin)
1694# define LAST_WIN_NR current_win_nr(NULL)
1695# define CURRENT_TAB_NR current_tab_nr(curtab)
1696# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001697
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698/*
1699 * Execute one Ex command.
1700 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001701 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1702 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 *
1704 * 1. skip comment lines and leading space
1705 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001706 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001707 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001708 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001709 * 6. parse arguments
1710 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001712 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 *
1714 * This function may be called recursively!
1715 */
1716#if (_MSC_VER == 1200)
1717/*
Bram Moolenaared203462004-06-16 11:19:22 +00001718 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001720 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#endif
1722 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001723do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001724 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001725 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001727 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001729 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001730 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001732 char_u *p;
1733 linenr_T lnum;
1734 long n;
1735 char *errormsg = NULL; // error message
1736 char_u *after_modifier = NULL;
1737 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001738 cmdmod_T save_cmdmod;
1739 int save_reg_executing = reg_executing;
1740 int ni; // set when Not Implemented
1741 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001742 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001743#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001744 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001745 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001746 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001747#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001748 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
Bram Moolenaara80faa82020-04-12 19:37:17 +02001750 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 ea.line1 = 1;
1752 ea.line2 = 1;
1753#ifdef FEAT_EVAL
1754 ++ex_nesting_level;
1755#endif
1756
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001757 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 if (quitmore
1759#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001760 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001761 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001762#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001763 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001764 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 --quitmore;
1766
1767 /*
1768 * Reset browse, confirm, etc.. They are restored when returning, for
1769 * recursive calls.
1770 */
1771 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001773 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001774 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1775 goto doend;
1776
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001777/*
1778 * 1. Skip comment lines and leading white space and colons.
1779 * 2. Handle command modifiers.
1780 */
1781 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001783 ea.cmdlinep = cmdlinep;
1784 ea.getline = fgetline;
1785 ea.cookie = cookie;
1786#ifdef FEAT_EVAL
1787 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001788 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001790 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001791 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001792 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001793#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001794 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001795#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001796 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798#ifdef FEAT_EVAL
1799 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1800 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1801#else
1802 ea.skip = (if_level > 0);
1803#endif
1804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001806 * 3. Skip over the range to find the command. Let "p" point to after it.
1807 *
1808 * We need the command to know what kind of range it uses.
1809 */
1810 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001811#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001812 // In Vim9 script a colon is required before the range. This may also be
1813 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001814 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001815 {
1816 may_have_range = FALSE;
1817 for (p = ea.cmd; p >= *cmdlinep; --p)
1818 {
1819 if (*p == ':')
1820 may_have_range = TRUE;
1821 if (p < ea.cmd && !VIM_ISWHITE(*p))
1822 break;
1823 }
1824 }
1825 else
1826 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001827 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001828#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001829 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830
1831#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001832 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001833 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001834 if (ea.cmd == cmd + 1 && *cmd == '$')
1835 // should be "$VAR = val"
1836 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001837 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001838 if (ea.cmdidx == CMD_SIZE)
1839 {
1840 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1841
1842 // If a ':' before the range is missing, give a clearer error
1843 // message.
1844 if (ar > ea.cmd)
1845 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001846 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001847 goto doend;
1848 }
1849 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001850 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 else
1852#endif
1853 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001854
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001855#ifdef FEAT_EVAL
1856# ifdef FEAT_PROFILE
1857 // Count this line for profiling if skip is TRUE.
1858 if (do_profiling == PROF_YES
1859 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1860 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1861 {
1862 int skip = did_emsg || got_int || did_throw;
1863
1864 if (ea.cmdidx == CMD_catch)
1865 skip = !skip && !(cstack->cs_idx >= 0
1866 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1867 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1868 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1869 skip = skip || !(cstack->cs_idx >= 0
1870 && !(cstack->cs_flags[cstack->cs_idx]
1871 & (CSF_ACTIVE | CSF_TRUE)));
1872 else if (ea.cmdidx == CMD_finally)
1873 skip = FALSE;
1874 else if (ea.cmdidx != CMD_endif
1875 && ea.cmdidx != CMD_endfor
1876 && ea.cmdidx != CMD_endtry
1877 && ea.cmdidx != CMD_endwhile)
1878 skip = ea.skip;
1879
1880 if (!skip)
1881 {
1882 if (getline_equal(fgetline, cookie, get_func_line))
1883 func_line_exec(getline_cookie(fgetline, cookie));
1884 else if (getline_equal(fgetline, cookie, getsourceline))
1885 script_line_exec();
1886 }
1887 }
1888# endif
1889
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001890 // May go to debug mode. If this happens and the ">quit" debug command is
1891 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001892 dbg_check_breakpoint(&ea);
1893 if (!ea.skip && got_int)
1894 {
1895 ea.skip = TRUE;
1896 (void)do_intthrow(cstack);
1897 }
1898#endif
1899
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001900/*
1901 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 *
1903 * where 'addr' is:
1904 *
1905 * % (entire file)
1906 * $ [+-NUM]
1907 * 'x [+-NUM] (where x denotes a currently defined mark)
1908 * . [+-NUM]
1909 * [+-NUM]..
1910 * NUM
1911 *
1912 * The ea.cmd pointer is updated to point to the first character following the
1913 * range spec. If an initial address is found, but no second, the upper bound
1914 * is equal to the lower.
1915 */
1916
Bram Moolenaar25190db2019-05-04 15:05:28 +02001917 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001918 if (!IS_USER_CMDIDX(ea.cmdidx))
1919 {
1920 if (ea.cmdidx != CMD_SIZE)
1921 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1922 else
1923 ea.addr_type = ADDR_LINES;
1924
Bram Moolenaar25190db2019-05-04 15:05:28 +02001925 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001926 if (ea.cmdidx == CMD_wincmd && p != NULL)
1927 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001928#ifdef FEAT_QUICKFIX
1929 // :.cc in quickfix window uses line number
1930 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1931 ea.addr_type = ADDR_OTHER;
1932#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001933 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001934
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001935 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001936#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001937 if (!may_have_range)
1938 ea.line1 = ea.line2 = default_address(&ea);
1939 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001940#endif
1941 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1942 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001945 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 */
1947
1948 /*
1949 * Skip ':' and any white space
1950 */
1951 ea.cmd = skipwhite(ea.cmd);
1952 while (*ea.cmd == ':')
1953 ea.cmd = skipwhite(ea.cmd + 1);
1954
1955 /*
1956 * If we got a line, but no command, then go to the line.
1957 * If we find a '|' or '\n' we set ea.nextcmd.
1958 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001959 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1960 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
1962 /*
1963 * strange vi behaviour:
1964 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001965 * ":3|..." prints line 3 (not in Vim9 script)
1966 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001968 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 goto doend;
Bram Moolenaarb8554302021-02-15 21:30:30 +01001970 if ((*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
1971#ifdef FEAT_EVAL
1972 && !vim9script
1973#endif
1974 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 {
1976 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001977 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if ((errormsg = invalid_range(&ea)) == NULL)
1979 {
1980 correct_range(&ea);
1981 ex_print(&ea);
1982 }
1983 }
1984 else if (ea.addr_count != 0)
1985 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001986 if (ea.line2 > curbuf->b_ml.ml_line_count)
1987 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001988 // With '-' in 'cpoptions' a line number past the file is an
1989 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001990 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1991 ea.line2 = -1;
1992 else
1993 ea.line2 = curbuf->b_ml.ml_line_count;
1994 }
1995
1996 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001997 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 else
1999 {
2000 if (ea.line2 == 0)
2001 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 else
2003 curwin->w_cursor.lnum = ea.line2;
2004 beginline(BL_SOL | BL_FIX);
2005 }
2006 }
2007 goto doend;
2008 }
2009
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002010 // If this looks like an undefined user command and there are CmdUndefined
2011 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002012 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2013 && ASCII_ISUPPER(*ea.cmd)
2014 && has_cmdundefined())
2015 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002016 int ret;
2017
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002018 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002019 while (ASCII_ISALNUM(*p))
2020 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002021 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002022 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2023 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002024 // If the autocommands did something and didn't cause an error, try
2025 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002026 p = (ret
2027#ifdef FEAT_EVAL
2028 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002029#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002031 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002032
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 if (p == NULL)
2034 {
2035 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002036 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 goto doend;
2038 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002039 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002040 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2041 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
2043 errormsg = uc_fun_cmd();
2044 goto doend;
2045 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002046
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 if (ea.cmdidx == CMD_SIZE)
2048 {
2049 if (!ea.skip)
2050 {
2051 STRCPY(IObuff, _("E492: Not an editor command"));
2052 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002053 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002054 // If the modifier was parsed OK the error must be in the
2055 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002056 if (after_modifier != NULL)
2057 append_command(after_modifier);
2058 else
2059 append_command(*cmdlinep);
2060 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002061 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002062 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064 goto doend;
2065 }
2066
Bram Moolenaar958636c2014-10-21 20:01:58 +02002067 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2068 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002069#ifdef HAVE_EX_SCRIPT_NI
2070 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2071#endif
2072 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074#ifndef FEAT_EVAL
2075 /*
2076 * When the expression evaluation is disabled, recognize the ":if" and
2077 * ":endif" commands and ignore everything in between it.
2078 */
2079 if (ea.cmdidx == CMD_if)
2080 ++if_level;
2081 if (if_level)
2082 {
2083 if (ea.cmdidx == CMD_endif)
2084 --if_level;
2085 goto doend;
2086 }
2087
2088#endif
2089
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002090 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002091 if (*p == '!' && ea.cmdidx != CMD_substitute
2092 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
2094 ++p;
2095 ea.forceit = TRUE;
2096 }
2097 else
2098 ea.forceit = FALSE;
2099
2100/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002101 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002103 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002104 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
2106 if (!ea.skip)
2107 {
2108#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002109 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002111 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002112 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 goto doend;
2114 }
2115#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002116 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002117 {
2118 errormsg = _("E981: Command not allowed in rvim");
2119 goto doend;
2120 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002121 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002123 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002124 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 goto doend;
2126 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002127
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002128 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002130#ifdef FEAT_CMDWIN
2131 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2132 {
2133 // Command not allowed in the command line window
2134 errormsg = _(e_cmdwin);
2135 goto doend;
2136 }
2137#endif
2138 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2139 {
2140 // Command not allowed when text is locked
2141 errormsg = _(get_text_locked_msg());
2142 goto doend;
2143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002145
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002146 // Disallow editing another buffer when "curbuf_lock" is set.
2147 // Do allow ":checktime" (it is postponed).
2148 // Do allow ":edit" (check for an argument later).
2149 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002150 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002151 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002152 && ea.cmdidx != CMD_edit
2153 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002154 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002155 && curbuf_locked())
2156 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002158 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002160 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002161 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 goto doend;
2163 }
2164 }
2165
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002166 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002168 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 goto doend;
2170 }
2171
2172 /*
2173 * Don't complain about the range if it is not used
2174 * (could happen if line_count is accidentally set to 0).
2175 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002176 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
2178 /*
2179 * If the range is backwards, ask for confirmation and, if given, swap
2180 * ea.line1 & ea.line2 so it's forwards again.
2181 * When global command is busy, don't ask, will fail below.
2182 */
2183 if (!global_busy && ea.line1 > ea.line2)
2184 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002185 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002187 if (sourcing || exmode_active)
2188 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002189 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002190 goto doend;
2191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 if (ask_yesno((char_u *)
2193 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002194 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 }
2196 lnum = ea.line1;
2197 ea.line1 = ea.line2;
2198 ea.line2 = lnum;
2199 }
2200 if ((errormsg = invalid_range(&ea)) != NULL)
2201 goto doend;
2202 }
2203
Bram Moolenaarb7316892019-05-01 18:08:42 +02002204 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2205 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 ea.line2 = 1;
2207
2208 correct_range(&ea);
2209
2210#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002211 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002212 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002214 // Put the first line at the start of a closed fold, put the last line
2215 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 (void)hasFolding(ea.line1, &ea.line1, NULL);
2217 (void)hasFolding(ea.line2, NULL, &ea.line2);
2218 }
2219#endif
2220
2221#ifdef FEAT_QUICKFIX
2222 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002223 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 * option here, so things like % get expanded.
2225 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002226 p = replace_makeprg(&ea, p, cmdlinep);
2227 if (p == NULL)
2228 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229#endif
2230
2231 /*
2232 * Skip to start of argument.
2233 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2234 */
2235 if (ea.cmdidx == CMD_bang)
2236 ea.arg = p;
2237 else
2238 ea.arg = skipwhite(p);
2239
Bram Moolenaar379fb762018-08-30 15:58:28 +02002240 // ":file" cannot be run with an argument when "curbuf_lock" is set
2241 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2242 goto doend;
2243
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 /*
2245 * Check for "++opt=val" argument.
2246 * Must be first, allow ":w ++enc=utf8 !cmd"
2247 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002248 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2250 if (getargopt(&ea) == FAIL && !ni)
2251 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002252 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 goto doend;
2254 }
2255
2256 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2257 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002258 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002260 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002262 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 goto doend;
2264 }
2265 ea.arg = skipwhite(ea.arg + 1);
2266 ea.append = TRUE;
2267 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002268 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
2270 ++ea.arg;
2271 ea.usefilter = TRUE;
2272 }
2273 }
2274
2275 if (ea.cmdidx == CMD_read)
2276 {
2277 if (ea.forceit)
2278 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002279 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 ea.forceit = FALSE;
2281 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002282 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
2284 ++ea.arg;
2285 ea.usefilter = TRUE;
2286 }
2287 }
2288
2289 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2290 {
2291 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
2294 ++ea.arg;
2295 ++ea.amount;
2296 }
2297 ea.arg = skipwhite(ea.arg);
2298 }
2299
2300 /*
2301 * Check for "+command" argument, before checking for next command.
2302 * Don't do this for ":read !cmd" and ":write !cmd".
2303 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002304 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2306
2307 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002308 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 * Don't do this for ":read !cmd" and ":write !cmd".
2310 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002311 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 separate_nextcmd(&ea);
2313
2314 /*
2315 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002316 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2317 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002319 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002320 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002321 || ea.cmdidx == CMD_global
2322 || ea.cmdidx == CMD_vglobal
2323 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
2325 for (p = ea.arg; *p; ++p)
2326 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002327 // Remove one backslash before a newline, so that it's possible to
2328 // pass a newline to the shell and also a newline that is preceded
2329 // with a backslash. This makes it impossible to end a shell
2330 // command in a backslash, but that doesn't appear useful.
2331 // Halving the number of backslashes is incompatible with previous
2332 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002334 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 else if (*p == '\n')
2336 {
2337 ea.nextcmd = p + 1;
2338 *p = NUL;
2339 break;
2340 }
2341 }
2342 }
2343
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002344 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002345 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002347 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002348 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002350 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002351 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002352 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002354#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002355 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002356 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002358 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002359 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
2361#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002362 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2363 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002364 {
2365 ea.regname = *ea.arg++;
2366#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002367 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002368 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2369 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002370 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002371 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002372 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002373 }
2374#endif
2375 ea.arg = skipwhite(ea.arg);
2376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
2378
2379 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002380 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 * count, it's a buffer name.
2382 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002383 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2384 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002385 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
2387 n = getdigits(&ea.arg);
2388 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002389 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002391 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 goto doend;
2393 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002394 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
2396 ea.line2 = n;
2397 if (ea.addr_count == 0)
2398 ea.addr_count = 1;
2399 }
2400 else
2401 {
2402 ea.line1 = ea.line2;
2403 ea.line2 += n - 1;
2404 ++ea.addr_count;
2405 /*
2406 * Be vi compatible: no error message for out of range.
2407 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002408 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 ea.line2 = curbuf->b_ml.ml_line_count;
2410 }
2411 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002412
2413 /*
2414 * Check for flags: 'l', 'p' and '#'.
2415 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002416 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002417 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002418 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2419 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002421 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002422 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 goto doend;
2424 }
2425
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002426 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002428 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 goto doend;
2430 }
2431
2432#ifdef FEAT_EVAL
2433 /*
2434 * Skip the command when it's not going to be executed.
2435 * The commands like :if, :endif, etc. always need to be executed.
2436 * Also make an exception for commands that handle a trailing command
2437 * themselves.
2438 */
2439 if (ea.skip)
2440 {
2441 switch (ea.cmdidx)
2442 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002443 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 case CMD_while:
2445 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002446 case CMD_for:
2447 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 case CMD_if:
2449 case CMD_elseif:
2450 case CMD_else:
2451 case CMD_endif:
2452 case CMD_try:
2453 case CMD_catch:
2454 case CMD_finally:
2455 case CMD_endtry:
2456 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002457 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 break;
2459
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002460 // Commands that handle '|' themselves. Check: A command should
2461 // either have the EX_TRLBAR flag, appear in this list or appear in
2462 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 case CMD_aboveleft:
2464 case CMD_and:
2465 case CMD_belowright:
2466 case CMD_botright:
2467 case CMD_browse:
2468 case CMD_call:
2469 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002470 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 case CMD_delfunction:
2472 case CMD_djump:
2473 case CMD_dlist:
2474 case CMD_dsearch:
2475 case CMD_dsplit:
2476 case CMD_echo:
2477 case CMD_echoerr:
2478 case CMD_echomsg:
2479 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002480 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002482 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002483 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 case CMD_help:
2485 case CMD_hide:
2486 case CMD_ijump:
2487 case CMD_ilist:
2488 case CMD_isearch:
2489 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002490 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 case CMD_keepjumps:
2492 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002493 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 case CMD_leftabove:
2495 case CMD_let:
2496 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002497 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002498 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002500 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002501 case CMD_noautocmd:
2502 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 case CMD_perl:
2504 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002505 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002506 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002507 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 case CMD_return:
2509 case CMD_rightbelow:
2510 case CMD_ruby:
2511 case CMD_silent:
2512 case CMD_smagic:
2513 case CMD_snomagic:
2514 case CMD_substitute:
2515 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002516 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 case CMD_tcl:
2518 case CMD_throw:
2519 case CMD_tilde:
2520 case CMD_topleft:
2521 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002522 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002523 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 case CMD_verbose:
2525 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002526 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 break;
2528
2529 default: goto doend;
2530 }
2531 }
2532#endif
2533
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002534 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
2536 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2537 goto doend;
2538 }
2539
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 /*
2541 * Accept buffer name. Cannot be used at the same time with a buffer
2542 * number. Don't do this for a user command.
2543 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002544 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002545 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
2547 /*
2548 * :bdelete, :bwipeout and :bunload take several arguments, separated
2549 * by spaces: find next space (skipping over escaped characters).
2550 * The others take one argument: ignore trailing spaces.
2551 */
2552 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2553 || ea.cmdidx == CMD_bunload)
2554 p = skiptowhite_esc(ea.arg);
2555 else
2556 {
2557 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002558 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 --p;
2560 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002561 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002562 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002563 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 goto doend;
2565 ea.addr_count = 1;
2566 ea.arg = skipwhite(p);
2567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002569 // The :try command saves the emsg_silent flag, reset it here when
2570 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002571 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002572 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002573 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002574 if (emsg_silent < 0)
2575 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002576 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002577 }
2578
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002580 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
Bram Moolenaar958636c2014-10-21 20:01:58 +02002583 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 {
2585 /*
2586 * Execute a user-defined command.
2587 */
2588 do_ucmd(&ea);
2589 }
2590 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 {
2592 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002593 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 */
2595 ea.errmsg = NULL;
2596 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2597 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002598 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600
2601#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002603 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002604 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002605 && current_sctx.sc_sid > 0
2606 && ea.cmdidx != CMD_endif
2607 && (cstack->cs_idx < 0
2608 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002609 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 /*
2612 * If the command just executed called do_cmdline(), any throw or ":return"
2613 * or ":finish" encountered there must also check the cstack of the still
2614 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2615 * exception, or reanimate a returned function or finished script file and
2616 * return or finish it again.
2617 */
2618 if (need_rethrow)
2619 do_throw(cstack);
2620 else if (check_cstack)
2621 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002622 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002624 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 && current_func_returned())
2626 do_return(&ea, TRUE, FALSE, NULL);
2627 }
2628 need_rethrow = check_cstack = FALSE;
2629#endif
2630
2631doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002632 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002633 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002635 curwin->w_cursor.col = 0;
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2639 {
2640 if (sourcing)
2641 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002642 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 {
2644 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002645 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002647 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649 emsg(errormsg);
2650 }
2651#ifdef FEAT_EVAL
2652 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002653 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2654 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002655
2656 if (did_set_expr_line)
2657 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658#endif
2659
Bram Moolenaare1004402020-10-24 20:49:43 +02002660 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002662 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002664 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 ea.nextcmd = NULL;
2666
2667#ifdef FEAT_EVAL
2668 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002669 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670#endif
2671
2672 return ea.nextcmd;
2673}
2674#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002675 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676#endif
2677
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002678static char ex_error_buf[MSG_BUF_LEN];
2679
2680/*
2681 * Return an error message with argument included.
2682 * Uses a static buffer, only the last error will be kept.
2683 * "msg" will be translated, caller should use N_().
2684 */
2685 char *
2686ex_errmsg(char *msg, char_u *arg)
2687{
2688 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2689 return ex_error_buf;
2690}
2691
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002693 * Check for an Ex command with optional tail.
2694 * If there is a match advance "pp" to the argument and return TRUE.
2695 * If "noparen" is TRUE do not recognize the command followed by "(".
2696 */
2697 static int
2698checkforcmd_opt(
2699 char_u **pp, // start of command
2700 char *cmd, // name of command
2701 int len, // required length
2702 int noparen)
2703{
2704 int i;
2705
2706 for (i = 0; cmd[i] != NUL; ++i)
2707 if (((char_u *)cmd)[i] != (*pp)[i])
2708 break;
2709 if (i >= len && !isalpha((*pp)[i])
2710 && (*pp)[i] != '_' && (!noparen || (*pp)[i] != '('))
2711 {
2712 *pp = skipwhite(*pp + i);
2713 return TRUE;
2714 }
2715 return FALSE;
2716}
2717
2718/*
2719 * Check for an Ex command with optional tail.
2720 * If there is a match advance "pp" to the argument and return TRUE.
2721 */
2722 int
2723checkforcmd(
2724 char_u **pp, // start of command
2725 char *cmd, // name of command
2726 int len) // required length
2727{
2728 return checkforcmd_opt(pp, cmd, len, FALSE);
2729}
2730
2731/*
2732 * Check for an Ex command with optional tail, not followed by "(".
2733 * If there is a match advance "pp" to the argument and return TRUE.
2734 */
2735 static int
2736checkforcmd_noparen(
2737 char_u **pp, // start of command
2738 char *cmd, // name of command
2739 int len) // required length
2740{
2741 return checkforcmd_opt(pp, cmd, len, TRUE);
2742}
2743
2744/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002745 * Parse and skip over command modifiers:
2746 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002747 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002748 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002749 * When "skip_only" is TRUE the global variables are not changed, except for
2750 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002751 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2752 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002753 * Call apply_cmdmod() to get the side effects of the modifiers:
2754 * - Increment "sandbox" for ":sandbox"
2755 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002756 * - set msg_silent for ":silent"
2757 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002758 * Return FAIL when the command is not to be executed.
2759 * May set "errormsg" to an error message.
2760 */
2761 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002762parse_command_modifiers(
2763 exarg_T *eap,
2764 char **errormsg,
2765 cmdmod_T *cmod,
2766 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002767{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002768 char_u *p;
2769 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002770
Bram Moolenaare1004402020-10-24 20:49:43 +02002771 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002772
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002773 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002774 for (;;)
2775 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002776 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002777 {
2778 if (*eap->cmd == ':')
2779 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002780 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002781 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002782
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002783 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002784 if (*eap->cmd == NUL && exmode_active
2785 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2786 || getline_equal(eap->getline, eap->cookie, getexline))
2787 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2788 {
2789 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002790 if (!skip_only)
2791 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002792 }
2793
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002794 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002795 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002796 return FAIL;
2797 if (*eap->cmd == NUL)
2798 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002799 if (!skip_only)
2800 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002801 return FAIL;
2802 }
2803
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002804 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002805
2806 // In Vim9 script a variable can shadow a command modifier:
2807 // verbose = 123
2808 // verbose += 123
2809 // silent! verbose = func()
2810 // verbose.member = 2
2811 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002812 // But not:
2813 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002814 if (in_vim9script())
2815 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002816 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002817
2818 for (s = p; ASCII_ISALPHA(*s); ++s)
2819 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002820 n = skipwhite(s);
2821 if (vim_strchr((char_u *)".=", *n) != NULL
2822 || *s == '['
2823 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002824 break;
2825 }
2826
Bram Moolenaareffed932018-08-14 13:38:17 +02002827 switch (*p)
2828 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002829 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002830 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002831 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002832 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002833 continue;
2834
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002835 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002837 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 continue;
2839 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002840 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 {
2842#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002843 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002844#endif
2845 continue;
2846 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002847 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002848 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002849 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002850 continue;
2851
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002852 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002853 break;
2854#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002855 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002856#endif
2857 continue;
2858
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002859 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002861 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002862 continue;
2863 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002864 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002866 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002867 continue;
2868 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002869 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002871 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002872 continue;
2873 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002874 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002876 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 continue;
2878
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002879 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 {
2881 char_u *reg_pat;
2882
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002883 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 || *p == NUL || ends_excmd(*p))
2885 break;
2886 if (*p == '!')
2887 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002888 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 p = skipwhite(p + 1);
2890 if (*p == NUL || ends_excmd(*p))
2891 break;
2892 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002893#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002894 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002895 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002896 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002897#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002898 if (skip_only)
2899 p = skip_vimgrep_pat(p, NULL, NULL);
2900 else
2901 // NOTE: This puts a NUL after the pattern.
2902 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002903 if (p == NULL || *p == NUL)
2904 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002905 if (!skip_only)
2906 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002907 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002908 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002909 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002910 break;
2911 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002912 eap->cmd = p;
2913 continue;
2914 }
2915
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002916 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002917 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002918 || *p == NUL || ends_excmd(*p))
2919 break;
2920 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002921 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002922 continue;
2923
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002924 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002925 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002926 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002927 continue;
2928 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002929 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2930 {
2931 if (ends_excmd2(p, eap->cmd))
2932 {
2933 *errormsg =
2934 _(e_vim9cmd_must_be_followed_by_command);
2935 return FAIL;
2936 }
2937 cmod->cmod_flags |= CMOD_LEGACY;
2938 continue;
2939 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002940
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002941 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002942 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002943 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002944 continue;
2945
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002946 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002947 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002948 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002949 continue;
2950 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002951 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002952 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002953 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002954 continue;
2955
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002956 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02002957 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002958 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002959 continue;
2960
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002961 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002962 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002963 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002964 continue;
2965 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002966 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002967 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002968 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002969 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2970 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002971 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002972 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002973 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002974 }
2975 continue;
2976
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002977 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002978 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002979 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002980 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002981 long tabnr = get_address(eap, &eap->cmd,
2982 ADDR_TABS, eap->skip,
2983 skip_only, FALSE, 1);
2984 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002985 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002986 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002988 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2989 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002990 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002991 return FAIL;
2992 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002993 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002995 }
2996 eap->cmd = p;
2997 continue;
2998 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002999 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003000 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003001 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 continue;
3003
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003004 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003005 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003006 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 continue;
3008
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003009 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003010 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003011 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003012 continue;
3013 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003014 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003015 {
3016 if (ends_excmd2(p, eap->cmd))
3017 {
3018 *errormsg =
3019 _(e_vim9cmd_must_be_followed_by_command);
3020 return FAIL;
3021 }
3022 cmod->cmod_flags |= CMOD_VIM9CMD;
3023 continue;
3024 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003025 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003026 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003027 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003028 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003029 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003030 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003031 eap->cmd = p;
3032 continue;
3033 }
3034 break;
3035 }
3036
3037 return OK;
3038}
3039
3040/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003041 * Return TRUE if "cmod" has anything set.
3042 */
3043 int
3044has_cmdmod(cmdmod_T *cmod)
3045{
3046 return cmod->cmod_flags != 0
3047 || cmod->cmod_split != 0
3048 || cmod->cmod_verbose != 0
3049 || cmod->cmod_tab != 0
3050 || cmod->cmod_filter_regmatch.regprog != NULL;
3051}
3052
3053/*
3054 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3055 */
3056 int
3057cmdmod_error(void)
3058{
3059 if (in_vim9script() && has_cmdmod(&cmdmod))
3060 {
3061 emsg(_(e_misplaced_command_modifier));
3062 return TRUE;
3063 }
3064 return FALSE;
3065}
3066
3067/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003068 * Apply the command modifiers. Saves current state in "cmdmod", call
3069 * undo_cmdmod() later.
3070 */
3071 void
3072apply_cmdmod(cmdmod_T *cmod)
3073{
3074#ifdef HAVE_SANDBOX
3075 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3076 {
3077 ++sandbox;
3078 cmod->cmod_did_sandbox = TRUE;
3079 }
3080#endif
3081 if (cmod->cmod_verbose > 0)
3082 {
3083 if (cmod->cmod_verbose_save == 0)
3084 cmod->cmod_verbose_save = p_verbose + 1;
3085 p_verbose = cmod->cmod_verbose;
3086 }
3087
3088 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3089 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003090 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003091 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003092 cmod->cmod_save_msg_scroll = msg_scroll;
3093 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003094 if (cmod->cmod_flags & CMOD_SILENT)
3095 ++msg_silent;
3096 if (cmod->cmod_flags & CMOD_UNSILENT)
3097 msg_silent = 0;
3098
3099 if (cmod->cmod_flags & CMOD_ERRSILENT)
3100 {
3101 ++emsg_silent;
3102 ++cmod->cmod_did_esilent;
3103 }
3104
Bram Moolenaare1004402020-10-24 20:49:43 +02003105 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003106 {
3107 // Set 'eventignore' to "all".
3108 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003109 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003110 set_string_option_direct((char_u *)"ei", -1,
3111 (char_u *)"all", OPT_FREE, SID_NONE);
3112 }
3113}
3114
3115/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003116 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003117 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003118 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003119undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003120{
Bram Moolenaare1004402020-10-24 20:49:43 +02003121 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003122 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003123 p_verbose = cmod->cmod_verbose_save - 1;
3124 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003125 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003126
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003127#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003128 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003129 {
3130 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003131 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003132 }
3133#endif
3134
Bram Moolenaare1004402020-10-24 20:49:43 +02003135 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003136 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003137 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003138 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3139 OPT_FREE, SID_NONE);
3140 free_string_option(cmod->cmod_save_ei);
3141 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003142 }
3143
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003144 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003145
Bram Moolenaare1004402020-10-24 20:49:43 +02003146 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003147 {
3148 // messages could be enabled for a serious error, need to check if the
3149 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003150 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3151 msg_silent = cmod->cmod_save_msg_silent - 1;
3152 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003153 if (emsg_silent < 0)
3154 emsg_silent = 0;
3155 // Restore msg_scroll, it's set by file I/O commands, even when no
3156 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003157 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003158
3159 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3160 // somewhere in the line. Put it back in the first column.
3161 if (redirecting())
3162 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003163
Bram Moolenaare1004402020-10-24 20:49:43 +02003164 cmod->cmod_save_msg_silent = 0;
3165 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003166 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003167}
3168
3169/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003170 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003171 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003172 * Return FAIL and set "errormsg" or return OK.
3173 */
3174 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003175parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003176{
3177 int address_count = 1;
3178 linenr_T lnum;
3179
3180 // Repeat for all ',' or ';' separated addresses.
3181 for (;;)
3182 {
3183 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003184 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003185 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003186 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003187 eap->addr_count == 0, address_count++);
3188 if (eap->cmd == NULL) // error detected
3189 return FAIL;
3190 if (lnum == MAXLNUM)
3191 {
3192 if (*eap->cmd == '%') // '%' - all lines
3193 {
3194 ++eap->cmd;
3195 switch (eap->addr_type)
3196 {
3197 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003198 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003199 eap->line1 = 1;
3200 eap->line2 = curbuf->b_ml.ml_line_count;
3201 break;
3202 case ADDR_LOADED_BUFFERS:
3203 {
3204 buf_T *buf = firstbuf;
3205
3206 while (buf->b_next != NULL
3207 && buf->b_ml.ml_mfp == NULL)
3208 buf = buf->b_next;
3209 eap->line1 = buf->b_fnum;
3210 buf = lastbuf;
3211 while (buf->b_prev != NULL
3212 && buf->b_ml.ml_mfp == NULL)
3213 buf = buf->b_prev;
3214 eap->line2 = buf->b_fnum;
3215 break;
3216 }
3217 case ADDR_BUFFERS:
3218 eap->line1 = firstbuf->b_fnum;
3219 eap->line2 = lastbuf->b_fnum;
3220 break;
3221 case ADDR_WINDOWS:
3222 case ADDR_TABS:
3223 if (IS_USER_CMDIDX(eap->cmdidx))
3224 {
3225 eap->line1 = 1;
3226 eap->line2 = eap->addr_type == ADDR_WINDOWS
3227 ? LAST_WIN_NR : LAST_TAB_NR;
3228 }
3229 else
3230 {
3231 // there is no Vim command which uses '%' and
3232 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003233 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003234 return FAIL;
3235 }
3236 break;
3237 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003238 case ADDR_UNSIGNED:
3239 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003240 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003241 return FAIL;
3242 case ADDR_ARGUMENTS:
3243 if (ARGCOUNT == 0)
3244 eap->line1 = eap->line2 = 0;
3245 else
3246 {
3247 eap->line1 = 1;
3248 eap->line2 = ARGCOUNT;
3249 }
3250 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003251 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003252#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003253 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003254 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003255 if (eap->line2 == 0)
3256 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003257#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003258 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003259 case ADDR_NONE:
3260 // Will give an error later if a range is found.
3261 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003262 }
3263 ++eap->addr_count;
3264 }
3265 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3266 {
3267 pos_T *fp;
3268
3269 // '*' - visual area
3270 if (eap->addr_type != ADDR_LINES)
3271 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003272 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003273 return FAIL;
3274 }
3275
3276 ++eap->cmd;
3277 if (!eap->skip)
3278 {
3279 fp = getmark('<', FALSE);
3280 if (check_mark(fp) == FAIL)
3281 return FAIL;
3282 eap->line1 = fp->lnum;
3283 fp = getmark('>', FALSE);
3284 if (check_mark(fp) == FAIL)
3285 return FAIL;
3286 eap->line2 = fp->lnum;
3287 ++eap->addr_count;
3288 }
3289 }
3290 }
3291 else
3292 eap->line2 = lnum;
3293 eap->addr_count++;
3294
3295 if (*eap->cmd == ';')
3296 {
3297 if (!eap->skip)
3298 {
3299 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003300 // Don't leave the cursor on an illegal line or column, but do
3301 // accept zero as address, so 0;/PATTERN/ works correctly.
3302 if (eap->line2 > 0)
3303 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003304 }
3305 }
3306 else if (*eap->cmd != ',')
3307 break;
3308 ++eap->cmd;
3309 }
3310
3311 // One address given: set start and end lines.
3312 if (eap->addr_count == 1)
3313 {
3314 eap->line1 = eap->line2;
3315 // ... but only implicit: really no address given
3316 if (lnum == MAXLNUM)
3317 eap->addr_count = 0;
3318 }
3319 return OK;
3320}
3321
3322/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003323 * Append "cmd" to the error message in IObuff.
3324 * Takes care of limiting the length and handling 0xa0, which would be
3325 * invisible otherwise.
3326 */
3327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003328append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003329{
3330 char_u *s = cmd;
3331 char_u *d;
3332
3333 STRCAT(IObuff, ": ");
3334 d = IObuff + STRLEN(IObuff);
3335 while (*s != NUL && d - IObuff < IOSIZE - 7)
3336 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003337 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003338 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003339 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003340 STRCPY(d, "<a0>");
3341 d += 4;
3342 }
3343 else
3344 MB_COPY_CHAR(s, d);
3345 }
3346 *d = NUL;
3347}
3348
3349/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003350 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3351 * the name. Otherwise just return "start".
3352 */
3353 char_u *
3354skip_option_env_lead(char_u *start)
3355{
3356 char_u *name = start;
3357
3358 if (*start == '&')
3359 {
3360 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3361 name += 3;
3362 else
3363 name += 1;
3364 }
3365 else if (*start == '$')
3366 name += 1;
3367 return name;
3368}
3369
3370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003372 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003373 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003374 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 *
3376 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3377 * assignment without "let". Sets eap->cmdidx to the command while returning
3378 * "eap->cmd".
3379 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 * Returns NULL for an ambiguous user command.
3381 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 char_u *
3383find_ex_command(
3384 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003385 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003386 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003387 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
3389 int len;
3390 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003391 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393#ifdef FEAT_EVAL
3394 /*
3395 * Recognize a Vim9 script function/method call and assignment:
3396 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3397 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003398 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003399 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003401 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402
Bram Moolenaar83144542020-08-02 20:40:43 +02003403 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003404 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003406 int oplen;
3407 int heredoc;
3408 char_u *swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003410 if (
3411 // "(..." is an expression.
3412 // "funcname(" is always a function call.
3413 *p == '('
3414 || (p == eap->cmd
3415 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003416 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003417 *eap->cmd == '{'
3418 // "'string'->func()" is an expression.
3419 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003420 // '"string"->func()' is an expression.
3421 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003422 // "g:varname" is an expression.
3423 || eap->cmd[1] == ':'
3424 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003425 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003426 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003427 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003428 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3429 {
3430 // "{" by itself is the start of a block.
3431 eap->cmdidx = CMD_block;
3432 return eap->cmd + 1;
3433 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003434 eap->cmdidx = CMD_eval;
3435 return eap->cmd;
3436 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003437
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003438 if (p != eap->cmd && (
3439 // "varname[]" is an expression.
3440 *p == '['
3441 // "varname.key" is an expression.
3442 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3443 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003444 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003445
3446 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003447 // Skip over the whole thing, it can be:
3448 // name.member = val
3449 // name[a : b] = val
3450 // name[idx] = val
3451 // name[idx].member = val
3452 // etc.
3453 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003454 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003455 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003456 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003457 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003458 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003459 || (after[0] == '.' && after[1] == '.'
3460 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003461 eap->cmdidx = CMD_var;
3462 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003463 --emsg_silent;
3464 return eap->cmd;
3465 }
3466
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003467 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3468 // an assignment.
3469 // If there is no line break inside the "[...]" then "p" is
3470 // advanced to after the "]" by to_name_const_end(): check if a "="
3471 // follows.
3472 // If "[...]" has a line break "p" still points at the "[" and it
3473 // can't be an assignment.
3474 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003475 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003476 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003477 if (p == eap->cmd && *p == '[')
3478 {
3479 int count = 0;
3480 int semicolon = FALSE;
3481
3482 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3483 }
3484 if (p == NULL || p == eap->cmd || *skipwhite(p) != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003485 {
3486 eap->cmdidx = CMD_eval;
3487 return eap->cmd;
3488 }
3489 if (p > eap->cmd && *skipwhite(p) == '=')
3490 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003491 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003492 return eap->cmd;
3493 }
3494 }
3495
3496 // Recognize an assignment if we recognize the variable name:
3497 // "g:var = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003498 // "var = expr" where "var" is a variable name or we are skipping
3499 // (variable declaration might have been skipped).
Bram Moolenaar83144542020-08-02 20:40:43 +02003500 if (*eap->cmd == '@')
3501 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003502 oplen = assignment_len(skipwhite(p), &heredoc);
3503 if (oplen > 0)
3504 {
3505 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3506 || *eap->cmd == '&'
3507 || *eap->cmd == '$'
3508 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003509 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003510 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003511 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003512 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003513 return eap->cmd;
3514 }
3515 }
3516
3517 // Recognize using a type for a w:, b:, t: or g: variable:
3518 // "w:varname: number = 123".
3519 if (eap->cmd[1] == ':' && *p == ':')
3520 {
3521 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003522 return eap->cmd;
3523 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003524 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003525
3526 // If it is an ID it might be a variable with an operator on the next
3527 // line, if the variable exists it can't be an Ex command.
3528 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003529 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003530 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3531 {
3532 eap->cmdidx = CMD_eval;
3533 return eap->cmd;
3534 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003535
3536 // Check for "++nr" and "--nr".
3537 if (p == eap->cmd && p[0] == p[1] && (*p == '+' || *p == '-'))
3538 {
3539 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3540 return eap->cmd + 2;
3541 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 }
3543#endif
3544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /*
3546 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003547 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003548 * - The 'k' command can directly be followed by any character.
3549 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003551 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003553 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 */
3555 p = eap->cmd;
3556 if (*p == 'k')
3557 {
3558 eap->cmdidx = CMD_k;
3559 ++p;
3560 }
3561 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003562 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3563 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 || p[1] == 'g'
3565 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3566 || p[1] == 'I'
3567 || (p[1] == 'r' && p[2] != 'e')))
3568 {
3569 eap->cmdidx = CMD_substitute;
3570 ++p;
3571 }
3572 else
3573 {
3574 while (ASCII_ISALPHA(*p))
3575 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003576 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003577 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003579 while (ASCII_ISALNUM(*p))
3580 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 }
3582 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3583 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003584 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 ++p;
3586 while (ASCII_ISALPHA(*p))
3587 ++p;
3588 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003590 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003591 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 ++p;
3593 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003594 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3595 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003596 // Check for ":dl", ":dell", etc. to ":deletel": that's
3597 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003598 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003599 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003600 break;
3601 if (i == len - 1)
3602 {
3603 --len;
3604 if (p[-1] == 'l')
3605 eap->flags |= EXFLAG_LIST;
3606 else
3607 eap->flags |= EXFLAG_PRINT;
3608 }
3609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003611 if (ASCII_ISLOWER(eap->cmd[0]))
3612 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003613 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003614 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003615
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003616 if (command_count != (int)CMD_SIZE)
3617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003618 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003619 getout(1);
3620 }
3621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003622 // Use a precomputed index for fast look-up in cmdnames[]
3623 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003624 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3625 if (ASCII_ISLOWER(c2))
3626 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3627 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003628 else if (ASCII_ISUPPER(eap->cmd[0]))
3629 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003631 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
3633 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3634 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3635 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3636 (size_t)len) == 0)
3637 {
3638#ifdef FEAT_EVAL
3639 if (full != NULL
3640 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3641 *full = TRUE;
3642#endif
3643 break;
3644 }
3645
Bram Moolenaar95388e32020-11-20 21:07:00 +01003646 // Not not recognize ":*" as the star command unless '*' is in
3647 // 'cpoptions'.
3648 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3649 p = eap->cmd;
3650
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003651 // Look for a user defined command as a last resort. Let ":Print" be
3652 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003653 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3654 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003656 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 while (ASCII_ISALNUM(*p))
3658 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003659 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003661 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 eap->cmdidx = CMD_SIZE;
3663 }
3664
Bram Moolenaar373863e2020-09-26 17:20:53 +02003665 // ":fina" means ":finally" for backwards compatibility.
3666 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3667 eap->cmdidx = CMD_finally;
3668
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003669#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003670 if (eap->cmdidx < CMD_SIZE
3671 && in_vim9script()
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003672 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003673 && (eap->cmdidx < 0 ||
3674 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003675 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003676 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3677
3678 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003679 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003680 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003681 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003682#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003683
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 return p;
3685}
3686
3687#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003688static struct cmdmod
3689{
3690 char *name;
3691 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003692 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003693} cmdmods[] = {
3694 {"aboveleft", 3, FALSE},
3695 {"belowright", 3, FALSE},
3696 {"botright", 2, FALSE},
3697 {"browse", 3, FALSE},
3698 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003699 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003700 {"hide", 3, FALSE},
3701 {"keepalt", 5, FALSE},
3702 {"keepjumps", 5, FALSE},
3703 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003704 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003705 {"leftabove", 5, FALSE},
3706 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003707 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003708 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003709 {"rightbelow", 6, FALSE},
3710 {"sandbox", 3, FALSE},
3711 {"silent", 3, FALSE},
3712 {"tab", 3, TRUE},
3713 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003714 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003715 {"verbose", 4, TRUE},
3716 {"vertical", 4, FALSE},
3717};
3718
3719/*
3720 * Return length of a command modifier (including optional count).
3721 * Return zero when it's not a modifier.
3722 */
3723 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003724modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003725{
3726 int i, j;
3727 char_u *p = cmd;
3728
3729 if (VIM_ISDIGIT(*cmd))
3730 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003731 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003732 {
3733 for (j = 0; p[j] != NUL; ++j)
3734 if (p[j] != cmdmods[i].name[j])
3735 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003736 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003737 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003738 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003739 }
3740 return 0;
3741}
3742
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743/*
3744 * Return > 0 if an Ex command "name" exists.
3745 * Return 2 if there is an exact match.
3746 * Return 3 if there is an ambiguous match.
3747 */
3748 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003749cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
3751 exarg_T ea;
3752 int full = FALSE;
3753 int i;
3754 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003755 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003757 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003758 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
3760 for (j = 0; name[j] != NUL; ++j)
3761 if (name[j] != cmdmods[i].name[j])
3762 break;
3763 if (name[j] == NUL && j >= cmdmods[i].minlen)
3764 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3765 }
3766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003767 // Check built-in commands and user defined commands.
3768 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003769 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003772 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003774 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3775 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003776 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003777 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3779}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003780
3781/*
3782 * "fullcommand" function
3783 */
3784 void
3785f_fullcommand(typval_T *argvars, typval_T *rettv)
3786{
3787 exarg_T ea;
3788 char_u *name = argvars[0].vval.v_string;
3789 char_u *p;
3790
3791 while (name[0] != NUL && name[0] == ':')
3792 name++;
3793 name = skip_range(name, TRUE, NULL);
3794
3795 rettv->v_type = VAR_STRING;
3796
3797 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3798 ea.cmdidx = (cmdidx_T)0;
3799 p = find_ex_command(&ea, NULL, NULL, NULL);
3800 if (p == NULL || ea.cmdidx == CMD_SIZE)
3801 return;
3802
3803 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
3804 ? get_user_commands(NULL, ea.useridx)
3805 : cmdnames[ea.cmdidx].cmd_name);
3806}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807#endif
3808
Bram Moolenaard0190392019-08-23 21:17:35 +02003809 cmdidx_T
3810excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
Bram Moolenaard0190392019-08-23 21:17:35 +02003812 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
Bram Moolenaard0190392019-08-23 21:17:35 +02003814 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3815 idx = (cmdidx_T)((int)idx + 1))
3816 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 break;
3818
Bram Moolenaard0190392019-08-23 21:17:35 +02003819 return idx;
3820}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
Bram Moolenaard0190392019-08-23 21:17:35 +02003822 long
3823excmd_get_argt(cmdidx_T idx)
3824{
3825 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826}
3827
3828/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003829 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 *
3831 * Backslashed delimiters after / or ? will be skipped, and commands will
3832 * not be expanded between /'s and ?'s or after "'".
3833 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003834 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 * Returns the "cmd" pointer advanced to beyond the range.
3836 */
3837 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003838skip_range(
3839 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003840 int skip_star, // skip "*" used for Visual range
3841 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003843 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003845 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003847 if (*cmd == '\\')
3848 {
3849 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3850 ++cmd;
3851 else
3852 break;
3853 }
3854 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
3856 if (*++cmd == NUL && ctx != NULL)
3857 *ctx = EXPAND_NOTHING;
3858 }
3859 else if (*cmd == '/' || *cmd == '?')
3860 {
3861 delim = *cmd++;
3862 while (*cmd != NUL && *cmd != delim)
3863 if (*cmd++ == '\\' && *cmd != NUL)
3864 ++cmd;
3865 if (*cmd == NUL && ctx != NULL)
3866 *ctx = EXPAND_NOTHING;
3867 }
3868 if (*cmd != NUL)
3869 ++cmd;
3870 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003871
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003872 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003873 while (*cmd == ':')
3874 cmd = skipwhite(cmd + 1);
3875
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003876 // Skip "*" used for Visual range.
3877 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3878 cmd = skipwhite(cmd + 1);
3879
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 return cmd;
3881}
3882
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003883 static void
3884addr_error(cmd_addr_T addr_type)
3885{
3886 if (addr_type == ADDR_NONE)
3887 emsg(_(e_norange));
3888 else
3889 emsg(_(e_invrange));
3890}
3891
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003893 * Return the default address for an address type.
3894 */
3895 static linenr_T
3896default_address(exarg_T *eap)
3897{
3898 linenr_T lnum = 0;
3899
3900 switch (eap->addr_type)
3901 {
3902 case ADDR_LINES:
3903 case ADDR_OTHER:
3904 // Default is the cursor line number. Avoid using an invalid
3905 // line number though.
3906 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3907 lnum = curbuf->b_ml.ml_line_count;
3908 else
3909 lnum = curwin->w_cursor.lnum;
3910 break;
3911 case ADDR_WINDOWS:
3912 lnum = CURRENT_WIN_NR;
3913 break;
3914 case ADDR_ARGUMENTS:
3915 lnum = curwin->w_arg_idx + 1;
3916 if (lnum > ARGCOUNT)
3917 lnum = ARGCOUNT;
3918 break;
3919 case ADDR_LOADED_BUFFERS:
3920 case ADDR_BUFFERS:
3921 lnum = curbuf->b_fnum;
3922 break;
3923 case ADDR_TABS:
3924 lnum = CURRENT_TAB_NR;
3925 break;
3926 case ADDR_TABS_RELATIVE:
3927 case ADDR_UNSIGNED:
3928 lnum = 1;
3929 break;
3930 case ADDR_QUICKFIX:
3931#ifdef FEAT_QUICKFIX
3932 lnum = qf_get_cur_idx(eap);
3933#endif
3934 break;
3935 case ADDR_QUICKFIX_VALID:
3936#ifdef FEAT_QUICKFIX
3937 lnum = qf_get_cur_valid_idx(eap);
3938#endif
3939 break;
3940 case ADDR_NONE:
3941 // Will give an error later if a range is found.
3942 break;
3943 }
3944 return lnum;
3945}
3946
3947/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003948 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 *
3950 * Set ptr to the next character after the part that was interpreted.
3951 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003952 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 *
3954 * Return MAXLNUM when no Ex address was found.
3955 */
3956 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003957get_address(
3958 exarg_T *eap UNUSED,
3959 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003960 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003961 int skip, // only skip the address, don't use it
3962 int silent, // no errors or side effects
3963 int to_other_file, // flag: may jump to other file
3964 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
3966 int c;
3967 int i;
3968 long n;
3969 char_u *cmd;
3970 pos_T pos;
3971 pos_T *fp;
3972 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003973 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975 cmd = skipwhite(*ptr);
3976 lnum = MAXLNUM;
3977 do
3978 {
3979 switch (*cmd)
3980 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003981 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003982 ++cmd;
3983 switch (addr_type)
3984 {
3985 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003986 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 lnum = curwin->w_cursor.lnum;
3988 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003989 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003990 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003991 break;
3992 case ADDR_ARGUMENTS:
3993 lnum = curwin->w_arg_idx + 1;
3994 break;
3995 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003996 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003997 lnum = curbuf->b_fnum;
3998 break;
3999 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004000 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004001 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004002 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004003 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004004 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004005 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004006 cmd = NULL;
4007 goto error;
4008 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004009 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004010#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004011 lnum = qf_get_cur_idx(eap);
4012#endif
4013 break;
4014 case ADDR_QUICKFIX_VALID:
4015#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004016 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004017#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004018 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004019 }
4020 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004022 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004023 ++cmd;
4024 switch (addr_type)
4025 {
4026 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004027 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 lnum = curbuf->b_ml.ml_line_count;
4029 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004030 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004031 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004032 break;
4033 case ADDR_ARGUMENTS:
4034 lnum = ARGCOUNT;
4035 break;
4036 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004037 buf = lastbuf;
4038 while (buf->b_ml.ml_mfp == NULL)
4039 {
4040 if (buf->b_prev == NULL)
4041 break;
4042 buf = buf->b_prev;
4043 }
4044 lnum = buf->b_fnum;
4045 break;
4046 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004047 lnum = lastbuf->b_fnum;
4048 break;
4049 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004050 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004051 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004052 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004053 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004054 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004055 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004056 cmd = NULL;
4057 goto error;
4058 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004059 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004060#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004061 lnum = qf_get_size(eap);
4062 if (lnum == 0)
4063 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004064#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004065 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004066 case ADDR_QUICKFIX_VALID:
4067#ifdef FEAT_QUICKFIX
4068 lnum = qf_get_valid_size(eap);
4069 if (lnum == 0)
4070 lnum = 1;
4071#endif
4072 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004073 }
4074 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004076 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004077 if (*++cmd == NUL)
4078 {
4079 cmd = NULL;
4080 goto error;
4081 }
4082 if (addr_type != ADDR_LINES)
4083 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004084 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004085 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004086 goto error;
4087 }
4088 if (skip)
4089 ++cmd;
4090 else
4091 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004092 // Only accept a mark in another file when it is
4093 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004094 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4095 ++cmd;
4096 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004097 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004098 lnum = curwin->w_cursor.lnum;
4099 else
4100 {
4101 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
4103 cmd = NULL;
4104 goto error;
4105 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004106 lnum = fp->lnum;
4107 }
4108 }
4109 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110
4111 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004112 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004113 c = *cmd++;
4114 if (addr_type != ADDR_LINES)
4115 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004116 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004117 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004118 goto error;
4119 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004120 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004121 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004122 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004123 if (*cmd == c)
4124 ++cmd;
4125 }
4126 else
4127 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004128 int flags;
4129
4130 pos = curwin->w_cursor; // save curwin->w_cursor
4131
4132 // When '/' or '?' follows another address, start from
4133 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004134 if (lnum != MAXLNUM)
4135 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004136
4137 // Start a forward search at the end of the line (unless
4138 // before the first line).
4139 // Start a backward search at the start of the line.
4140 // This makes sure we never match in the current
4141 // line, and can match anywhere in the
4142 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004143 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004144 curwin->w_cursor.col = MAXCOL;
4145 else
4146 curwin->w_cursor.col = 0;
4147 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004148 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004149 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004150 {
4151 curwin->w_cursor = pos;
4152 cmd = NULL;
4153 goto error;
4154 }
4155 lnum = curwin->w_cursor.lnum;
4156 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004157 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004158 cmd += searchcmdlen;
4159 }
4160 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004162 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004163 ++cmd;
4164 if (addr_type != ADDR_LINES)
4165 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004166 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004167 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004168 goto error;
4169 }
4170 if (*cmd == '&')
4171 i = RE_SUBST;
4172 else if (*cmd == '?' || *cmd == '/')
4173 i = RE_SEARCH;
4174 else
4175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004176 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004177 cmd = NULL;
4178 goto error;
4179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004181 if (!skip)
4182 {
4183 /*
4184 * When search follows another address, start from
4185 * there.
4186 */
4187 if (lnum != MAXLNUM)
4188 pos.lnum = lnum;
4189 else
4190 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004192 /*
4193 * Start the search just like for the above
4194 * do_search().
4195 */
4196 if (*cmd != '?')
4197 pos.col = MAXCOL;
4198 else
4199 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004200 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004201 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004202 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004203 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004204 lnum = pos.lnum;
4205 else
4206 {
4207 cmd = NULL;
4208 goto error;
4209 }
4210 }
4211 ++cmd;
4212 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
4214 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004215 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004216 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218
4219 for (;;)
4220 {
4221 cmd = skipwhite(cmd);
4222 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4223 break;
4224
4225 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004226 {
4227 switch (addr_type)
4228 {
4229 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004230 case ADDR_OTHER:
4231 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004232 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004233 break;
4234 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004235 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004236 break;
4237 case ADDR_ARGUMENTS:
4238 lnum = curwin->w_arg_idx + 1;
4239 break;
4240 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004241 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004242 lnum = curbuf->b_fnum;
4243 break;
4244 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004245 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004246 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004247 case ADDR_TABS_RELATIVE:
4248 lnum = 1;
4249 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004250 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004251#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004252 lnum = qf_get_cur_idx(eap);
4253#endif
4254 break;
4255 case ADDR_QUICKFIX_VALID:
4256#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004257 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004258#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004259 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004260 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004261 case ADDR_UNSIGNED:
4262 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004263 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004264 }
4265 }
4266
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004268 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 else
4270 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004271 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 n = 1;
4273 else
4274 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004275
4276 if (addr_type == ADDR_TABS_RELATIVE)
4277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004278 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004279 cmd = NULL;
4280 goto error;
4281 }
4282 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004283 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004284 lnum = compute_buffer_local_count(
4285 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004287 {
4288#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004289 // Relative line addressing, need to adjust for folded lines
4290 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004291 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4292 && address_count >= 2)
4293 (void)hasFolding(lnum, NULL, &lnum);
4294#endif
4295 if (i == '-')
4296 lnum -= n;
4297 else
4298 lnum += n;
4299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 } while (*cmd == '/' || *cmd == '?');
4302
4303error:
4304 *ptr = cmd;
4305 return lnum;
4306}
4307
4308/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004309 * Set eap->line1 and eap->line2 to the whole range.
4310 * Used for commands with the EX_DFLALL flag and no range given.
4311 */
4312 static void
4313address_default_all(exarg_T *eap)
4314{
4315 eap->line1 = 1;
4316 switch (eap->addr_type)
4317 {
4318 case ADDR_LINES:
4319 case ADDR_OTHER:
4320 eap->line2 = curbuf->b_ml.ml_line_count;
4321 break;
4322 case ADDR_LOADED_BUFFERS:
4323 {
4324 buf_T *buf = firstbuf;
4325
4326 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4327 buf = buf->b_next;
4328 eap->line1 = buf->b_fnum;
4329 buf = lastbuf;
4330 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4331 buf = buf->b_prev;
4332 eap->line2 = buf->b_fnum;
4333 }
4334 break;
4335 case ADDR_BUFFERS:
4336 eap->line1 = firstbuf->b_fnum;
4337 eap->line2 = lastbuf->b_fnum;
4338 break;
4339 case ADDR_WINDOWS:
4340 eap->line2 = LAST_WIN_NR;
4341 break;
4342 case ADDR_TABS:
4343 eap->line2 = LAST_TAB_NR;
4344 break;
4345 case ADDR_TABS_RELATIVE:
4346 eap->line2 = 1;
4347 break;
4348 case ADDR_ARGUMENTS:
4349 if (ARGCOUNT == 0)
4350 eap->line1 = eap->line2 = 0;
4351 else
4352 eap->line2 = ARGCOUNT;
4353 break;
4354 case ADDR_QUICKFIX_VALID:
4355#ifdef FEAT_QUICKFIX
4356 eap->line2 = qf_get_valid_size(eap);
4357 if (eap->line2 == 0)
4358 eap->line2 = 1;
4359#endif
4360 break;
4361 case ADDR_NONE:
4362 case ADDR_UNSIGNED:
4363 case ADDR_QUICKFIX:
4364 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4365 break;
4366 }
4367}
4368
4369
4370/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004371 * Get flags from an Ex command argument.
4372 */
4373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004374get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004375{
4376 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4377 {
4378 if (*eap->arg == 'l')
4379 eap->flags |= EXFLAG_LIST;
4380 else if (*eap->arg == 'p')
4381 eap->flags |= EXFLAG_PRINT;
4382 else
4383 eap->flags |= EXFLAG_NR;
4384 eap->arg = skipwhite(eap->arg + 1);
4385 }
4386}
4387
4388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 * Function called for command which is Not Implemented. NI!
4390 */
4391 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004392ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393{
4394 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004395 eap->errmsg =
4396 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397}
4398
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004399#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400/*
4401 * Function called for script command which is Not Implemented. NI!
4402 * Skips over ":perl <<EOF" constructs.
4403 */
4404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004405ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406{
4407 if (!eap->skip)
4408 ex_ni(eap);
4409 else
4410 vim_free(script_get(eap, eap->arg));
4411}
4412#endif
4413
4414/*
4415 * Check range in Ex command for validity.
4416 * Return NULL when valid, error message when invalid.
4417 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004418 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004419invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004421 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004422
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if ( eap->line1 < 0
4424 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004425 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004426 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004427
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004428 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004429 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004430 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004431 {
4432 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004433 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004434#ifdef FEAT_DIFF
4435 + (eap->cmdidx == CMD_diffget)
4436#endif
4437 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004438 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004439 break;
4440 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004441 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004442 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004443 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004444 break;
4445 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004446 // Only a boundary check, not whether the buffers actually
4447 // exist.
4448 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004449 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004450 break;
4451 case ADDR_LOADED_BUFFERS:
4452 buf = firstbuf;
4453 while (buf->b_ml.ml_mfp == NULL)
4454 {
4455 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004456 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004457 buf = buf->b_next;
4458 }
4459 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004460 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004461 buf = lastbuf;
4462 while (buf->b_ml.ml_mfp == NULL)
4463 {
4464 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004465 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004466 buf = buf->b_prev;
4467 }
4468 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004469 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004470 break;
4471 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004472 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004473 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004474 break;
4475 case ADDR_TABS:
4476 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004477 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004478 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004479 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004480 case ADDR_OTHER:
4481 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004482 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004483 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004484#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004485 // No error for value that is too big, will use the last entry.
4486 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004487 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004488#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004489 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004490 case ADDR_QUICKFIX_VALID:
4491#ifdef FEAT_QUICKFIX
4492 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4493 || eap->line2 < 0)
4494 return _(e_invrange);
4495#endif
4496 break;
4497 case ADDR_UNSIGNED:
4498 if (eap->line2 < 0)
4499 return _(e_invrange);
4500 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004501 case ADDR_NONE:
4502 // Will give an error elsewhere.
4503 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004504 }
4505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 return NULL;
4507}
4508
4509/*
4510 * Correct the range for zero line number, if required.
4511 */
4512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004513correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004515 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
4517 if (eap->line1 == 0)
4518 eap->line1 = 1;
4519 if (eap->line2 == 0)
4520 eap->line2 = 1;
4521 }
4522}
4523
Bram Moolenaar748bf032005-02-02 23:04:36 +00004524#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004525/*
4526 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4527 * pattern. Otherwise return eap->arg.
4528 */
4529 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004530skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004531{
4532 char_u *p = eap->arg;
4533
Bram Moolenaara37420f2006-02-04 22:37:47 +00004534 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4535 || eap->cmdidx == CMD_vimgrepadd
4536 || eap->cmdidx == CMD_lvimgrepadd
4537 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004538 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004539 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004540 if (p == NULL)
4541 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004542 }
4543 return p;
4544}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004545
4546/*
4547 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4548 * in the command line, so that things like % get expanded.
4549 */
4550 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004551replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004552{
4553 char_u *new_cmdline;
4554 char_u *program;
4555 char_u *pos;
4556 char_u *ptr;
4557 int len;
4558 int i;
4559
4560 /*
4561 * Don't do it when ":vimgrep" is used for ":grep".
4562 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004563 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4564 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4565 || eap->cmdidx == CMD_grepadd
4566 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004567 && !grep_internal(eap->cmdidx))
4568 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004569 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4570 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004571 {
4572 if (*curbuf->b_p_gp == NUL)
4573 program = p_gp;
4574 else
4575 program = curbuf->b_p_gp;
4576 }
4577 else
4578 {
4579 if (*curbuf->b_p_mp == NUL)
4580 program = p_mp;
4581 else
4582 program = curbuf->b_p_mp;
4583 }
4584
4585 p = skipwhite(p);
4586
4587 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4588 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004589 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004590 i = 1;
4591 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4592 ++i;
4593 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004594 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004595 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004596 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004597 ptr = new_cmdline;
4598 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4599 {
4600 i = (int)(pos - program);
4601 STRNCPY(ptr, program, i);
4602 STRCPY(ptr += i, p);
4603 ptr += len;
4604 program = pos + 2;
4605 }
4606 STRCPY(ptr, program);
4607 }
4608 else
4609 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004610 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004611 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004612 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004613 STRCPY(new_cmdline, program);
4614 STRCAT(new_cmdline, " ");
4615 STRCAT(new_cmdline, p);
4616 }
4617 msg_make(p);
4618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004619 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004620 vim_free(*cmdlinep);
4621 *cmdlinep = new_cmdline;
4622 p = new_cmdline;
4623 }
4624 return p;
4625}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004626#endif
4627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628/*
4629 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004630 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 * Return FAIL for failure, OK otherwise.
4632 */
4633 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004634expand_filename(
4635 exarg_T *eap,
4636 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004637 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004639 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 char_u *repl;
4641 int srclen;
4642 char_u *p;
4643 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004644 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
Bram Moolenaar748bf032005-02-02 23:04:36 +00004646#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004647 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004648 p = skip_grep_pat(eap);
4649#else
4650 p = eap->arg;
4651#endif
4652
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 /*
4654 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4655 * the file name contains a wildcard it should not cause expanding.
4656 * (it will be expanded anyway if there is a wildcard before replacing).
4657 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004658 has_wildcards = mch_has_wildcard(p);
4659 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004661#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004662 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004663 if (p[0] == '`' && p[1] == '=')
4664 {
4665 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004666 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004667 if (*p == '`')
4668 ++p;
4669 continue;
4670 }
4671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 /*
4673 * Quick check if this cannot be the start of a special string.
4674 * Also removes backslash before '%', '#' and '<'.
4675 */
4676 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4677 {
4678 ++p;
4679 continue;
4680 }
4681
4682 /*
4683 * Try to find a match at this position.
4684 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004685 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4686 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004687 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004689 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
4691 p += srclen;
4692 continue;
4693 }
4694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004695 // Wildcards won't be expanded below, the replacement is taken
4696 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004697 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4698 {
4699 char_u *l = repl;
4700
4701 repl = expand_env_save(repl);
4702 vim_free(l);
4703 }
4704
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004705 // Need to escape white space et al. with a backslash.
4706 // Don't do this for:
4707 // - replacement that already has been escaped: "##"
4708 // - shell commands (may have to use quotes instead).
4709 // - non-unix systems when there is a single argument (spaces don't
4710 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004712 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 && eap->cmdidx != CMD_grep
4715 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004716 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004717 && eap->cmdidx != CMD_lgrep
4718 && eap->cmdidx != CMD_lgrepadd
4719 && eap->cmdidx != CMD_lmake
4720 && eap->cmdidx != CMD_make
4721 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004723 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724#endif
4725 )
4726 {
4727 char_u *l;
4728#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004729 // Don't escape a backslash here, because rem_backslash() doesn't
4730 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 static char_u *nobslash = (char_u *)" \t\"|";
4732# define ESCAPE_CHARS nobslash
4733#else
4734# define ESCAPE_CHARS escape_chars
4735#endif
4736
4737 for (l = repl; *l; ++l)
4738 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4739 {
4740 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4741 if (l != NULL)
4742 {
4743 vim_free(repl);
4744 repl = l;
4745 }
4746 break;
4747 }
4748 }
4749
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004750 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004751 if ((eap->usefilter || eap->cmdidx == CMD_bang
4752 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004753 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
4755 char_u *l;
4756
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004757 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 if (l != NULL)
4759 {
4760 vim_free(repl);
4761 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
4763 }
4764
4765 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4766 vim_free(repl);
4767 if (p == NULL)
4768 return FAIL;
4769 }
4770
4771 /*
4772 * One file argument: Expand wildcards.
4773 * Don't do this with ":r !command" or ":w !command".
4774 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004775 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
4777 /*
4778 * May do this twice:
4779 * 1. Replace environment variables.
4780 * 2. Replace any other wildcards, remove backslashes.
4781 */
4782 for (n = 1; n <= 2; ++n)
4783 {
4784 if (n == 2)
4785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 /*
4787 * Halve the number of backslashes (this is Vi compatible).
4788 * For Unix and OS/2, when wildcards are expanded, this is
4789 * done by ExpandOne() below.
4790 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004791#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 if (!has_wildcards)
4793#endif
4794 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
4796
4797 if (has_wildcards)
4798 {
4799 if (n == 1)
4800 {
4801 /*
4802 * First loop: May expand environment variables. This
4803 * can be done much faster with expand_env() than with
4804 * something else (e.g., calling a shell).
4805 * After expanding environment variables, check again
4806 * if there are still wildcards present.
4807 */
4808 if (vim_strchr(eap->arg, '$') != NULL
4809 || vim_strchr(eap->arg, '~') != NULL)
4810 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004811 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004812 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 has_wildcards = mch_has_wildcard(NameBuff);
4814 p = NameBuff;
4815 }
4816 else
4817 p = NULL;
4818 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004819 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004822 int options = WILD_LIST_NOTFOUND
4823 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
4825 ExpandInit(&xpc);
4826 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004827 if (p_wic)
4828 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004830 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 if (p == NULL)
4832 return FAIL;
4833 }
4834 if (p != NULL)
4835 {
4836 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4837 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004838 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 vim_free(p);
4840 }
4841 }
4842 }
4843 }
4844 return OK;
4845}
4846
4847/*
4848 * Replace part of the command line, keeping eap->cmd, eap->arg and
4849 * eap->nextcmd correct.
4850 * "src" points to the part that is to be replaced, of length "srclen".
4851 * "repl" is the replacement string.
4852 * Returns a pointer to the character after the replaced string.
4853 * Returns NULL for failure.
4854 */
4855 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004856repl_cmdline(
4857 exarg_T *eap,
4858 char_u *src,
4859 int srclen,
4860 char_u *repl,
4861 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 int len;
4864 int i;
4865 char_u *new_cmdline;
4866
4867 /*
4868 * The new command line is build in new_cmdline[].
4869 * First allocate it.
4870 * Careful: a "+cmd" argument may have been NUL terminated.
4871 */
4872 len = (int)STRLEN(repl);
4873 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004874 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004875 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004876 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004877 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
4879 /*
4880 * Copy the stuff before the expanded part.
4881 * Copy the expanded stuff.
4882 * Copy what came after the expanded part.
4883 * Copy the next commands, if there are any.
4884 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004885 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004887
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004889 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004891 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004893 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 {
4895 i = (int)STRLEN(new_cmdline) + 1;
4896 STRCPY(new_cmdline + i, eap->nextcmd);
4897 eap->nextcmd = new_cmdline + i;
4898 }
4899 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4900 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4901 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4902 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4903 vim_free(*cmdlinep);
4904 *cmdlinep = new_cmdline;
4905
4906 return src;
4907}
4908
4909/*
4910 * Check for '|' to separate commands and '"' to start comments.
4911 */
4912 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004913separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
4915 char_u *p;
4916
Bram Moolenaar86b68352004-12-27 21:59:20 +00004917#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004918 p = skip_grep_pat(eap);
4919#else
4920 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004921#endif
4922
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004923 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
4925 if (*p == Ctrl_V)
4926 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004927 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004928 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004930 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004931 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004932 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 break;
4934 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004935
4936#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004937 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004938 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004939 {
4940 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004941 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004942 if (*p == NUL) // stop at NUL after CTRL-V
4943 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004944 }
4945#endif
4946
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004947 // Check for '"': start of comment or '|': next command
4948 // :@" and :*" do not start a comment!
4949 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004950 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004951#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004952 && !in_vim9script()
4953#endif
4954 && !(eap->argt & EX_NOTRLCOM)
4955 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4956 || p != eap->arg)
4957 && (eap->cmdidx != CMD_redir
4958 || p != eap->arg + 1 || p[-1] != '@'))
4959#ifdef FEAT_EVAL
4960 || (*p == '#'
4961 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004962 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004963 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 || *p == '|' || *p == '\n')
4966 {
4967 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004968 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 * AND 'b' is present in 'cpoptions'.
4970 */
4971 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004972 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004974 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 --p;
4976 }
4977 else
4978 {
4979 eap->nextcmd = check_nextcmd(p);
4980 *p = NUL;
4981 break;
4982 }
4983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004985
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004986 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 del_trailing_spaces(eap->arg);
4988}
4989
4990/*
4991 * get + command from ex argument
4992 */
4993 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004994getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
4996 char_u *arg = *argp;
4997 char_u *command = NULL;
4998
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004999 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
5001 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005002 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 command = dollar_command;
5004 else
5005 {
5006 command = arg;
5007 arg = skip_cmd_arg(command, TRUE);
5008 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005009 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005012 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 *argp = arg;
5014 }
5015 return command;
5016}
5017
5018/*
5019 * Find end of "+command" argument. Skip over "\ " and "\\".
5020 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005021 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005022skip_cmd_arg(
5023 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005024 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025{
5026 while (*p && !vim_isspace(*p))
5027 {
5028 if (*p == '\\' && p[1] != NUL)
5029 {
5030 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005031 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 else
5033 ++p;
5034 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005035 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037 return p;
5038}
5039
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005040 int
5041get_bad_opt(char_u *p, exarg_T *eap)
5042{
5043 if (STRICMP(p, "keep") == 0)
5044 eap->bad_char = BAD_KEEP;
5045 else if (STRICMP(p, "drop") == 0)
5046 eap->bad_char = BAD_DROP;
5047 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5048 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005049 else
5050 return FAIL;
5051 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005052}
5053
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054/*
5055 * Get "++opt=arg" argument.
5056 * Return FAIL or OK.
5057 */
5058 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005059getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
5061 char_u *arg = eap->arg + 2;
5062 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005063 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005066 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5068 {
5069 if (*arg == 'n')
5070 {
5071 arg += 2;
5072 eap->force_bin = FORCE_NOBIN;
5073 }
5074 else
5075 eap->force_bin = FORCE_BIN;
5076 if (!checkforcmd(&arg, "binary", 3))
5077 return FAIL;
5078 eap->arg = skipwhite(arg);
5079 return OK;
5080 }
5081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005082 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005083 if (STRNCMP(arg, "edit", 4) == 0)
5084 {
5085 eap->read_edit = TRUE;
5086 eap->arg = skipwhite(arg + 4);
5087 return OK;
5088 }
5089
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if (STRNCMP(arg, "ff", 2) == 0)
5091 {
5092 arg += 2;
5093 pp = &eap->force_ff;
5094 }
5095 else if (STRNCMP(arg, "fileformat", 10) == 0)
5096 {
5097 arg += 10;
5098 pp = &eap->force_ff;
5099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 else if (STRNCMP(arg, "enc", 3) == 0)
5101 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005102 if (STRNCMP(arg, "encoding", 8) == 0)
5103 arg += 8;
5104 else
5105 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 pp = &eap->force_enc;
5107 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005108 else if (STRNCMP(arg, "bad", 3) == 0)
5109 {
5110 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005111 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
5114 if (pp == NULL || *arg != '=')
5115 return FAIL;
5116
5117 ++arg;
5118 *pp = (int)(arg - eap->cmd);
5119 arg = skip_cmd_arg(arg, FALSE);
5120 eap->arg = skipwhite(arg);
5121 *arg = NUL;
5122
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 if (pp == &eap->force_ff)
5124 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5126 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005127 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005129 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005131 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5133 *p = TOLOWER_ASC(*p);
5134 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005135 else
5136 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005137 // Check ++bad= argument. Must be a single-byte character, "keep" or
5138 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005139 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005140 return FAIL;
5141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142
5143 return OK;
5144}
5145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005147ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148{
5149 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005150 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 * directory for security reasons.
5152 */
5153 if (secure)
5154 {
5155 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005156 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158 else if (eap->cmdidx == CMD_autocmd)
5159 do_autocmd(eap->arg, eap->forceit);
5160 else
5161 do_augroup(eap->arg, eap->forceit);
5162}
5163
5164/*
5165 * ":doautocmd": Apply the automatic commands to the current buffer.
5166 */
5167 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005168ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005170 char_u *arg = eap->arg;
5171 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005172 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005173
Bram Moolenaar1610d052016-06-09 22:53:01 +02005174 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005175 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005176 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005177 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180/*
5181 * :[N]bunload[!] [N] [bufname] unload buffer
5182 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5183 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5184 */
5185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005186ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005188 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005189 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 eap->errmsg = do_bufdel(
5191 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5192 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5193 : DOBUF_UNLOAD, eap->arg,
5194 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5195}
5196
5197/*
5198 * :[N]buffer [N] to buffer N
5199 * :[N]sbuffer [N] to buffer N
5200 */
5201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005202ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005204 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005205 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005207 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 else
5209 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005210 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5212 else
5213 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005214 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005215 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217}
5218
5219/*
5220 * :[N]bmodified [N] to next mod. buffer
5221 * :[N]sbmodified [N] to next mod. buffer
5222 */
5223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005224ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225{
5226 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005227 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005228 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229}
5230
5231/*
5232 * :[N]bnext [N] to next buffer
5233 * :[N]sbnext [N] split and to next buffer
5234 */
5235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005236ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005238 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005239 return;
5240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005242 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005243 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244}
5245
5246/*
5247 * :[N]bNext [N] to previous buffer
5248 * :[N]bprevious [N] to previous buffer
5249 * :[N]sbNext [N] split and to previous buffer
5250 * :[N]sbprevious [N] split and to previous buffer
5251 */
5252 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005253ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005255 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005256 return;
5257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005259 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005260 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261}
5262
5263/*
5264 * :brewind to first buffer
5265 * :bfirst to first buffer
5266 * :sbrewind split and to first buffer
5267 * :sbfirst split and to first buffer
5268 */
5269 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005270ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005272 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005273 return;
5274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005276 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005277 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278}
5279
5280/*
5281 * :blast to last buffer
5282 * :sblast split and to last buffer
5283 */
5284 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005285ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005287 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005288 return;
5289
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005291 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005292 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
Bram Moolenaar7a092242020-04-16 22:10:49 +02005295/*
5296 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005297 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005300ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005302 int comment_char = '"';
5303
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005304#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005305 if (in_vim9script())
5306 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005307#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005308 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005309}
5310
5311/*
5312 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5313 * to "cmd_start" or has a white space character before it.
5314 */
5315 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005316ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005317{
5318 int c = *cmd;
5319
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005320 if (c == NUL || c == '|' || c == '\n')
5321 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005322#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005323 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005324 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5325 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005326 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005327#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005328 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329}
5330
5331#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5332 || defined(PROTO)
5333/*
5334 * Return the next command, after the first '|' or '\n'.
5335 * Return NULL if not found.
5336 */
5337 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005338find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
5340 while (*p != '|' && *p != '\n')
5341 {
5342 if (*p == NUL)
5343 return NULL;
5344 ++p;
5345 }
5346 return (p + 1);
5347}
5348#endif
5349
5350/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005351 * Check if *p is a separator between Ex commands, skipping over white space.
5352 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 */
5354 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005355check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005357 char_u *s = skipwhite(p);
5358
5359 if (*s == '|' || *s == '\n')
5360 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 else
5362 return NULL;
5363}
5364
5365/*
5366 * - if there are more files to edit
5367 * - and this is the last window
5368 * - and forceit not used
5369 * - and not repeated twice on a row
5370 * return FAIL and give error message if 'message' TRUE
5371 * return OK otherwise
5372 */
5373 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005374check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005375 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005376 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
5378 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5379
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005380 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005381 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
5383 if (message)
5384 {
5385#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005386 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5387 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005389 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005391 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5392 NGETTEXT("%d more file to edit. Quit anyway?",
5393 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5395 return OK;
5396 return FAIL;
5397 }
5398#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005399 semsg(NGETTEXT("E173: %d more file to edit",
5400 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005401 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 }
5403 return FAIL;
5404 }
5405 return OK;
5406}
5407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408/*
5409 * Function given to ExpandGeneric() to obtain the list of command names.
5410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005412get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413{
5414 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 return cmdnames[idx].cmd_name;
5417}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005420ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421{
Bram Moolenaare6850792010-05-14 15:28:44 +02005422 if (*eap->arg == NUL)
5423 {
5424#ifdef FEAT_EVAL
5425 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5426 char_u *p = NULL;
5427
5428 if (expr != NULL)
5429 {
5430 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005431 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005432 --emsg_off;
5433 vim_free(expr);
5434 }
5435 if (p != NULL)
5436 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005437 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005438 vim_free(p);
5439 }
5440 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005441 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005442#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005443 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005444#endif
5445 }
5446 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005447 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005448
5449#ifdef FEAT_VTP
5450 else if (has_vtp_working())
5451 {
5452 // background color change requires clear + redraw
5453 update_screen(CLEAR);
5454 redrawcmd();
5455 }
5456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457}
5458
5459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005460ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461{
5462 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005463 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 do_highlight(eap->arg, eap->forceit, FALSE);
5465}
5466
5467
5468/*
5469 * Call this function if we thought we were going to exit, but we won't
5470 * (because of an error). May need to restore the terminal mode.
5471 */
5472 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005473not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474{
5475 exiting = FALSE;
5476 settmode(TMODE_RAW);
5477}
5478
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005479 static int
5480before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5481{
5482 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5483
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005484 // Bail out when autocommands closed the window.
5485 // Refuse to quit when the buffer in the last window is being closed (can
5486 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005487 if (!win_valid(wp)
5488 || curbuf_locked()
5489 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5490 return TRUE;
5491
5492 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5493 {
5494 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005495 // Refuse to quit when locked or when the window was closed or the
5496 // buffer in the last window is being closed (can only happen in
5497 // autocommands).
5498 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005499 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5500 return TRUE;
5501 }
5502
5503 return FALSE;
5504}
5505
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005507 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005508 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005509 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005511 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005514 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005515
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516#ifdef FEAT_CMDWIN
5517 if (cmdwin_type != 0)
5518 {
5519 cmdwin_result = Ctrl_C;
5520 return;
5521 }
5522#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005523 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005524 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005525 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005526 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005527 return;
5528 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005529 if (eap->addr_count > 0)
5530 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005531 int wnr = eap->line2;
5532
5533 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5534 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005535 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005536 }
5537 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005538 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005539
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005540 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005541 if (curbuf_locked())
5542 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005544 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005545 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005546 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548#ifdef FEAT_NETBEANS_INTG
5549 netbeansForcedQuit = eap->forceit;
5550#endif
5551
5552 /*
5553 * If there are more files or windows we won't exit.
5554 */
5555 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5556 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005557 if ((!buf_hide(wp->w_buffer)
5558 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005559 | (eap->forceit ? CCGD_FORCEIT : 0)
5560 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005562 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
5564 not_exiting();
5565 }
5566 else
5567 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005568 // quit last window
5569 // Note: only_one_window() returns true, even so a help window is
5570 // still open. In that case only quit, if no address has been
5571 // specified. Example:
5572 // :h|wincmd w|1q - don't quit
5573 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005574 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005576 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005577#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005579#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005580 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005581 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583}
5584
5585/*
5586 * ":cquit".
5587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005589ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005591 // this does not always pass on the exit code to the Manx compiler. why?
5592 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593}
5594
5595/*
5596 * ":qall": try to quit all windows
5597 */
5598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005599ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
5601# ifdef FEAT_CMDWIN
5602 if (cmdwin_type != 0)
5603 {
5604 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005605 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 else
5607 cmdwin_result = K_XF2;
5608 return;
5609 }
5610# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005611
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005612 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005613 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005614 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005615 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005616 return;
5617 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005618
5619 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005620 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005621
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005623 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 getout(0);
5625 not_exiting();
5626}
5627
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628/*
5629 * ":close": close current window, unless it is the last one
5630 */
5631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005632ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005634 win_T *win;
5635 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005636#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005638 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005640#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005641 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005642 {
5643 if (eap->addr_count == 0)
5644 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005645 else
5646 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005647 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005648 {
5649 winnr++;
5650 if (winnr == eap->line2)
5651 break;
5652 }
5653 if (win == NULL)
5654 win = lastwin;
5655 ex_win_close(eap->forceit, win, NULL);
5656 }
5657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658}
5659
Bram Moolenaar4033c552017-09-16 20:54:51 +02005660#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005661/*
5662 * ":pclose": Close any preview window.
5663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005665ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005666{
5667 win_T *win;
5668
Bram Moolenaar79648732019-07-18 21:43:07 +02005669 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005670 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005671 if (win->w_p_pvw)
5672 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005673 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005674 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005675 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005676# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005677 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005678 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005679# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005680}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005681#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005682
Bram Moolenaarf740b292006-02-16 22:11:02 +00005683/*
5684 * Close window "win" and take care of handling closing the last window for a
5685 * modified buffer.
5686 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005688ex_win_close(
5689 int forceit,
5690 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005691 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
5693 int need_hide;
5694 buf_T *buf = win->w_buffer;
5695
Bram Moolenaarcf844172020-06-26 19:44:06 +02005696 // Never close the autocommand window.
5697 if (win == aucmd_win)
5698 {
5699 emsg(_(e_autocmd_close));
5700 return;
5701 }
5702
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005704 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005706#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005707 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005709 bufref_T bufref;
5710
5711 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005713 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 return;
5715 need_hide = FALSE;
5716 }
5717 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005720 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 return;
5722 }
5723 }
5724
Bram Moolenaar4033c552017-09-16 20:54:51 +02005725#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005727#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005728
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005729 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005730 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005731 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005732 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005733 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734}
5735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005737 * Handle the argument for a tabpage related ex command.
5738 * Returns a tabpage number.
5739 * When an error is encountered then eap->errmsg is set.
5740 */
5741 static int
5742get_tabpage_arg(exarg_T *eap)
5743{
5744 int tab_number;
5745 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5746
5747 if (eap->arg && *eap->arg != NUL)
5748 {
5749 char_u *p = eap->arg;
5750 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005751 int relative = 0; // argument +N/-N means: go to N places to the
5752 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005753
5754 if (*p == '-')
5755 {
5756 relative = -1;
5757 p++;
5758 }
5759 else if (*p == '+')
5760 {
5761 relative = 1;
5762 p++;
5763 }
5764
5765 p_save = p;
5766 tab_number = getdigits(&p);
5767
5768 if (relative == 0)
5769 {
5770 if (STRCMP(p, "$") == 0)
5771 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005772 else if (STRCMP(p, "#") == 0)
5773 if (valid_tabpage(lastused_tabpage))
5774 tab_number = tabpage_index(lastused_tabpage);
5775 else
5776 {
5777 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5778 tab_number = 0;
5779 goto theend;
5780 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005781 else if (p == p_save || *p_save == '-' || *p != NUL
5782 || tab_number > LAST_TAB_NR)
5783 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005784 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005785 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005786 goto theend;
5787 }
5788 }
5789 else
5790 {
5791 if (*p_save == NUL)
5792 tab_number = 1;
5793 else if (p == p_save || *p_save == '-' || *p != NUL
5794 || tab_number == 0)
5795 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005796 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005797 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005798 goto theend;
5799 }
5800 tab_number = tab_number * relative + tabpage_index(curtab);
5801 if (!unaccept_arg0 && relative == -1)
5802 --tab_number;
5803 }
5804 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005805 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005806 }
5807 else if (eap->addr_count > 0)
5808 {
5809 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005810 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005811 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005812 tab_number = 0;
5813 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005814 else
5815 {
5816 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005817 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005818 {
5819 --tab_number;
5820 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005821 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005822 }
5823 }
5824 }
5825 else
5826 {
5827 switch (eap->cmdidx)
5828 {
5829 case CMD_tabnext:
5830 tab_number = tabpage_index(curtab) + 1;
5831 if (tab_number > LAST_TAB_NR)
5832 tab_number = 1;
5833 break;
5834 case CMD_tabmove:
5835 tab_number = LAST_TAB_NR;
5836 break;
5837 default:
5838 tab_number = tabpage_index(curtab);
5839 }
5840 }
5841
5842theend:
5843 return tab_number;
5844}
5845
5846/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005847 * ":tabclose": close current tab page, unless it is the last one.
5848 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 */
5850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005851ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005853 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005854 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005855
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005856# ifdef FEAT_CMDWIN
5857 if (cmdwin_type != 0)
5858 cmdwin_result = K_IGNORE;
5859 else
5860# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005861 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005862 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005863 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005865 tab_number = get_tabpage_arg(eap);
5866 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005867 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005868 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005869 if (tp == NULL)
5870 {
5871 beep_flush();
5872 return;
5873 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005874 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005875 {
5876 tabpage_close_other(tp, eap->forceit);
5877 return;
5878 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005879 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005880 tabpage_close(eap->forceit);
5881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005883}
5884
5885/*
5886 * ":tabonly": close all tab pages except the current one
5887 */
5888 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005889ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005890{
5891 tabpage_T *tp;
5892 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005893 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005894
5895# ifdef FEAT_CMDWIN
5896 if (cmdwin_type != 0)
5897 cmdwin_result = K_IGNORE;
5898 else
5899# endif
5900 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005901 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005902 else
5903 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005904 tab_number = get_tabpage_arg(eap);
5905 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005906 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005907 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005908 // Repeat this up to a 1000 times, because autocommands may
5909 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005910 for (done = 0; done < 1000; ++done)
5911 {
5912 FOR_ALL_TABPAGES(tp)
5913 if (tp->tp_topframe != topframe)
5914 {
5915 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005916 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005917 if (valid_tabpage(tp))
5918 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005919 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005920 break;
5921 }
5922 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005923 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005924 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005925 }
5926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928
5929/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005930 * Close the current tab page.
5931 */
5932 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005933tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005934{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005935 // First close all the windows but the current one. If that worked then
5936 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005937 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005938 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005939 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005940 ex_win_close(forceit, curwin, NULL);
5941# ifdef FEAT_GUI
5942 need_mouse_correct = TRUE;
5943# endif
5944}
5945
5946/*
5947 * Close tab page "tp", which is not the current tab page.
5948 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005949 * Also takes care of the tab pages line disappearing when closing the
5950 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005951 */
5952 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005953tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005954{
5955 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005956 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005957 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005958
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005959 // Limit to 1000 windows, autocommands may add a window while we close
5960 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005961 while (++done < 1000)
5962 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005963 wp = tp->tp_firstwin;
5964 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005965
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005966 // Autocommands may delete the tab page under our fingers and we may
5967 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005968 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005969 break;
5970 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005971
Bram Moolenaar29323592016-07-24 22:04:11 +02005972 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005973
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005974 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005975 if (h != tabline_height())
5976 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005977}
5978
5979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 * ":only".
5981 */
5982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005983ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005985 win_T *wp;
5986 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987# ifdef FEAT_GUI
5988 need_mouse_correct = TRUE;
5989# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005990 if (eap->addr_count > 0)
5991 {
5992 wnr = eap->line2;
5993 for (wp = firstwin; --wnr > 0; )
5994 {
5995 if (wp->w_next == NULL)
5996 break;
5997 else
5998 wp = wp->w_next;
5999 }
6000 win_goto(wp);
6001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 close_others(TRUE, eap->forceit);
6003}
6004
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006006ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006008 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006009 if (!eap->skip)
6010 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006011#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006012 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006013#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006014 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006015 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006016 else
6017 {
6018 int winnr = 0;
6019 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006020
Bram Moolenaar2256c992016-11-15 21:17:07 +01006021 FOR_ALL_WINDOWS(win)
6022 {
6023 winnr++;
6024 if (winnr == eap->line2)
6025 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006026 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006027 if (win == NULL)
6028 win = lastwin;
6029 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 }
6032}
6033
6034/*
6035 * ":stop" and ":suspend": Suspend Vim.
6036 */
6037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006038ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
6040 /*
6041 * Disallow suspending for "rvim".
6042 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006043 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 {
6045 if (!eap->forceit)
6046 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006047 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 windgoto((int)Rows - 1, 0);
6049 out_char('\n');
6050 out_flush();
6051 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006052 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006054 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006056 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057#ifdef FEAT_TITLE
6058 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006059 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060#endif
6061 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006062 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006064 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006065 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 }
6067}
6068
6069/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006070 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 */
6072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006073ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006075#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006076 if (not_in_vim9(eap) == FAIL)
6077 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079#ifdef FEAT_CMDWIN
6080 if (cmdwin_type != 0)
6081 {
6082 cmdwin_result = Ctrl_C;
6083 return;
6084 }
6085#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006086 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006087 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006088 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006089 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006090 return;
6091 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006092
6093 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006094 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
6096 /*
6097 * if more files or windows we won't exit
6098 */
6099 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6100 exiting = TRUE;
6101 if ( ((eap->cmdidx == CMD_wq
6102 || curbufIsChanged())
6103 && do_write(eap) == FAIL)
6104 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006105 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {
6107 not_exiting();
6108 }
6109 else
6110 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006111 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006113 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114# ifdef FEAT_GUI
6115 need_mouse_correct = TRUE;
6116# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006117 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006118 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 }
6120}
6121
6122/*
6123 * ":print", ":list", ":number".
6124 */
6125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006126ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006128 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006129 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006130 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006132 for ( ;!got_int; ui_breakcheck())
6133 {
6134 print_line(eap->line1,
6135 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6136 || (eap->flags & EXFLAG_NR)),
6137 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6138 if (++eap->line1 > eap->line2)
6139 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006140 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006141 }
6142 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006143 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006144 curwin->w_cursor.lnum = eap->line2;
6145 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 }
6147
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149}
6150
6151#ifdef FEAT_BYTEOFF
6152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006153ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
6155 goto_byte(eap->line2);
6156}
6157#endif
6158
6159/*
6160 * ":shell".
6161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006163ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164{
6165 do_shell(NULL, 0);
6166}
6167
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006168#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006170static int drop_busy = FALSE;
6171static int drop_filec;
6172static char_u **drop_filev = NULL;
6173static int drop_split;
6174static void (*drop_callback)(void *);
6175static void *drop_cookie;
6176
6177 static void
6178handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179{
6180 exarg_T ea;
6181 int save_msg_scroll = msg_scroll;
6182
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006183 // Setting the argument list may cause screen updates and being called
6184 // recursively. Avoid that by setting drop_busy.
6185 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006187 // Check whether the current buffer is changed. If so, we will need
6188 // to split the current window or data could be lost.
6189 // We don't need to check if the 'hidden' option is set, as in this
6190 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006191 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 {
6193 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006194 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 --emsg_off;
6196 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006197 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 if (win_split(0, 0) == FAIL)
6200 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006201 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006203 // When splitting the window, create a new alist. Otherwise the
6204 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 alist_unlink(curwin->w_alist);
6206 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 }
6208
6209 /*
6210 * Set up the new argument list.
6211 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006212 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
6214 /*
6215 * Move to the first file.
6216 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006217 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006218 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 ea.cmd = (char_u *)"next";
6220 do_argfile(&ea, 0);
6221
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006222 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6223 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 need_start_insertmode = FALSE;
6225
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006226 // Restore msg_scroll, otherwise a following command may cause scrolling
6227 // unexpectedly. The screen will be redrawn by the caller, thus
6228 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006230
6231 if (drop_callback != NULL)
6232 drop_callback(drop_cookie);
6233
6234 drop_filev = NULL;
6235 drop_busy = FALSE;
6236}
6237
6238/*
6239 * Handle a file drop. The code is here because a drop is *nearly* like an
6240 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006241 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006242 * code is very similar to :args and hence needs access to a lot of the static
6243 * functions in this file.
6244 *
6245 * The "filev" list must have been allocated using alloc(), as should each item
6246 * in the list. This function takes over responsibility for freeing the "filev"
6247 * list.
6248 */
6249 void
6250handle_drop(
6251 int filec, // the number of files dropped
6252 char_u **filev, // the list of files dropped
6253 int split, // force splitting the window
6254 void (*callback)(void *), // to be called after setting the argument
6255 // list
6256 void *cookie) // argument for "callback" (allocated)
6257{
6258 // Cannot handle recursive drops, finish the pending one.
6259 if (drop_busy)
6260 {
6261 FreeWild(filec, filev);
6262 vim_free(cookie);
6263 return;
6264 }
6265
6266 // When calling handle_drop() more than once in a row we only use the last
6267 // one.
6268 if (drop_filev != NULL)
6269 {
6270 FreeWild(drop_filec, drop_filev);
6271 vim_free(drop_cookie);
6272 }
6273
6274 drop_filec = filec;
6275 drop_filev = filev;
6276 drop_split = split;
6277 drop_callback = callback;
6278 drop_cookie = cookie;
6279
6280 // Postpone this when:
6281 // - editing the command line
6282 // - not possible to change the current buffer
6283 // - updating the screen
6284 // As it may change buffers and window structures that are in use and cause
6285 // freed memory to be used.
6286 if (text_locked() || curbuf_locked() || updating_screen)
6287 return;
6288
6289 handle_drop_internal();
6290}
6291
6292/*
6293 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6294 * reset: Handle a postponed drop.
6295 */
6296 void
6297handle_any_postponed_drop(void)
6298{
6299 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006300 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006301 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302}
6303#endif
6304
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 * ":preserve".
6307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006309ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006311 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 ml_preserve(curbuf, TRUE);
6313}
6314
6315/*
6316 * ":recover".
6317 */
6318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006319ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006321 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006323 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6324 | CCGD_MULTWIN
6325 | (eap->forceit ? CCGD_FORCEIT : 0)
6326 | CCGD_EXCMD)
6327
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 && (*eap->arg == NUL
6329 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006330 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 recoverymode = FALSE;
6332}
6333
6334/*
6335 * Command modifier used in a wrong way.
6336 */
6337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006338ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006340 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341}
6342
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343/*
6344 * :sview [+command] file split window with new file, read-only
6345 * :split [[+command] file] split window with current or new file
6346 * :vsplit [[+command] file] split window vertically with current or new file
6347 * :new [[+command] file] split window with no or new file
6348 * :vnew [[+command] file] split vertically window with no or new file
6349 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006350 *
6351 * :tabedit open new Tab page with empty window
6352 * :tabedit [+command] file open new Tab page and edit "file"
6353 * :tabnew [[+command] file] just like :tabedit
6354 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 */
6356 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006357ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006359 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006360#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006362#endif
6363#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006364 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006365 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006366#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006367 int use_tab = eap->cmdidx == CMD_tabedit
6368 || eap->cmdidx == CMD_tabfind
6369 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006371 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006372 return;
6373
Bram Moolenaar4033c552017-09-16 20:54:51 +02006374#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
Bram Moolenaar4033c552017-09-16 20:54:51 +02006378#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006379 // A ":split" in the quickfix window works like ":new". Don't want two
6380 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006381 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 {
6383 if (eap->cmdidx == CMD_split)
6384 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 if (eap->cmdidx == CMD_vsplit)
6386 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
Bram Moolenaar4033c552017-09-16 20:54:51 +02006390#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006391 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 {
6393 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6394 FNAME_MESS, TRUE, curbuf->b_ffname);
6395 if (fname == NULL)
6396 goto theend;
6397 eap->arg = fname;
6398 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006399# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006400 else
6401# endif
6402#endif
6403#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006404 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 && eap->cmdidx != CMD_new)
6407 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006408 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006409# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006410 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006411# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006412 au_has_group((char_u *)"FileExplorer"))
6413 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006414 // No browsing supported but we do have the file explorer:
6415 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006416 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006417 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006418 }
6419 else
6420 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006421 fname = do_browse(0, (char_u *)(use_tab
6422 ? _("Edit File in new tab page")
6423 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006425 if (fname == NULL)
6426 goto theend;
6427 eap->arg = fname;
6428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006430 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006433 /*
6434 * Either open new tab page or split the window.
6435 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006436 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006437 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006438 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006439 : eap->addr_count == 0 ? 0
6440 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006441 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006442 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006443
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006444 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006445 if (curwin != old_curwin
6446 && win_valid(old_curwin)
6447 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006448 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006449 old_curwin->w_alt_fnum = curbuf->b_fnum;
6450 }
6451 }
6452 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6454 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006455 // Reset 'scrollbind' when editing another file, but keep it when
6456 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006457 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006458 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 else
6460 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 do_exedit(eap, old_curwin);
6462 }
6463
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006464# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006465 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006466# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006468# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469theend:
6470 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006473
6474/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006475 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006476 */
6477 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006478tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006479{
6480 exarg_T ea;
6481
Bram Moolenaara80faa82020-04-12 19:37:17 +02006482 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006483 ea.cmdidx = CMD_tabnew;
6484 ea.cmd = (char_u *)"tabn";
6485 ea.arg = (char_u *)"";
6486 ex_splitview(&ea);
6487}
6488
6489/*
6490 * :tabnext command
6491 */
6492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006493ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006494{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006495 int tab_number;
6496
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006497 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006498 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006499 switch (eap->cmdidx)
6500 {
6501 case CMD_tabfirst:
6502 case CMD_tabrewind:
6503 goto_tabpage(1);
6504 break;
6505 case CMD_tablast:
6506 goto_tabpage(9999);
6507 break;
6508 case CMD_tabprevious:
6509 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006510 if (eap->arg && *eap->arg != NUL)
6511 {
6512 char_u *p = eap->arg;
6513 char_u *p_save = p;
6514
6515 tab_number = getdigits(&p);
6516 if (p == p_save || *p_save == '-' || *p != NUL
6517 || tab_number == 0)
6518 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006519 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006520 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006521 return;
6522 }
6523 }
6524 else
6525 {
6526 if (eap->addr_count == 0)
6527 tab_number = 1;
6528 else
6529 {
6530 tab_number = eap->line2;
6531 if (tab_number < 1)
6532 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006533 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006534 return;
6535 }
6536 }
6537 }
6538 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006539 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006540 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006541 tab_number = get_tabpage_arg(eap);
6542 if (eap->errmsg == NULL)
6543 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006544 break;
6545 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006546}
6547
6548/*
6549 * :tabmove command
6550 */
6551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006552ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006553{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006554 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006555
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006556 tab_number = get_tabpage_arg(eap);
6557 if (eap->errmsg == NULL)
6558 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006559}
6560
6561/*
6562 * :tabs command: List tabs and their contents.
6563 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006565ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006566{
6567 tabpage_T *tp;
6568 win_T *wp;
6569 int tabcount = 1;
6570
6571 msg_start();
6572 msg_scroll = TRUE;
6573 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6574 {
6575 msg_putchar('\n');
6576 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006577 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006578 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006579 ui_breakcheck();
6580
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006581 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006582 wp = firstwin;
6583 else
6584 wp = tp->tp_firstwin;
6585 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6586 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006587 msg_putchar('\n');
6588 msg_putchar(wp == curwin ? '>' : ' ');
6589 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006590 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6591 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006592 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006593 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006594 else
6595 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6596 IObuff, IOSIZE, TRUE);
6597 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006598 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006599 ui_breakcheck();
6600 }
6601 }
6602}
6603
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604/*
6605 * ":mode": Set screen mode.
6606 * If no argument given, just get the screen size and redraw.
6607 */
6608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006609ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
6611 if (*eap->arg == NUL)
6612 shell_resized();
6613 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006614 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615}
6616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617/*
6618 * ":resize".
6619 * set, increment or decrement current window height
6620 */
6621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006622ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
6624 int n;
6625 win_T *wp = curwin;
6626
6627 if (eap->addr_count > 0)
6628 {
6629 n = eap->line2;
6630 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6631 ;
6632 }
6633
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006634# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006638 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
6640 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006641 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006642 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 n = 9999;
6644 win_setwidth_win((int)n, wp);
6645 }
6646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 {
6648 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006649 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006650 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 n = 9999;
6652 win_setheight_win((int)n, wp);
6653 }
6654}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655
6656/*
6657 * ":find [+command] <file>" command.
6658 */
6659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006660ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
6662#ifdef FEAT_SEARCHPATH
6663 char_u *fname;
6664 int count;
6665
6666 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6667 TRUE, curbuf->b_ffname);
6668 if (eap->addr_count > 0)
6669 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006670 // Repeat finding the file "count" times. This matters when it
6671 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 count = eap->line2;
6673 while (fname != NULL && --count > 0)
6674 {
6675 vim_free(fname);
6676 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6677 FALSE, curbuf->b_ffname);
6678 }
6679 }
6680
6681 if (fname != NULL)
6682 {
6683 eap->arg = fname;
6684#endif
6685 do_exedit(eap, NULL);
6686#ifdef FEAT_SEARCHPATH
6687 vim_free(fname);
6688 }
6689#endif
6690}
6691
6692/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006693 * ":open" simulation: for now just work like ":visual".
6694 */
6695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006696ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006697{
6698 regmatch_T regmatch;
6699 char_u *p;
6700
Bram Moolenaar65088802021-03-13 21:07:21 +01006701#ifdef FEAT_EVAL
6702 if (not_in_vim9(eap) == FAIL)
6703 return;
6704#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006705 curwin->w_cursor.lnum = eap->line2;
6706 beginline(BL_SOL | BL_FIX);
6707 if (*eap->arg == '/')
6708 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006709 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006710 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006711 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006712 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006713 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006714 if (regmatch.regprog != NULL)
6715 {
6716 regmatch.rm_ic = p_ic;
6717 p = ml_get_curline();
6718 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006719 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006720 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006721 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006722 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006723 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006724 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006725 eap->arg += STRLEN(eap->arg);
6726 }
6727 check_cursor();
6728
6729 eap->cmdidx = CMD_visual;
6730 do_exedit(eap, NULL);
6731}
6732
6733/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006734 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 */
6736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006737ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 do_exedit(eap, NULL);
6740}
6741
6742/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006743 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006746do_exedit(
6747 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006748 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
6750 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006752 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006754 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6755 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006756 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 /*
6758 * ":vi" command ends Ex mode.
6759 */
6760 if (exmode_active && (eap->cmdidx == CMD_visual
6761 || eap->cmdidx == CMD_view))
6762 {
6763 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006764 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006766 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006767 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006768 if (global_busy)
6769 {
6770 int rd = RedrawingDisabled;
6771 int nwr = no_wait_return;
6772 int ms = msg_scroll;
6773#ifdef FEAT_GUI
6774 int he = hold_gui_events;
6775#endif
6776
6777 if (eap->nextcmd != NULL)
6778 {
6779 stuffReadbuff(eap->nextcmd);
6780 eap->nextcmd = NULL;
6781 }
6782
6783 if (exmode_was != EXMODE_VIM)
6784 settmode(TMODE_RAW);
6785 RedrawingDisabled = 0;
6786 no_wait_return = 0;
6787 need_wait_return = FALSE;
6788 msg_scroll = 0;
6789#ifdef FEAT_GUI
6790 hold_gui_events = 0;
6791#endif
6792 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006793 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006794
6795 main_loop(FALSE, TRUE);
6796
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006797 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006798 RedrawingDisabled = rd;
6799 no_wait_return = nwr;
6800 msg_scroll = ms;
6801#ifdef FEAT_GUI
6802 hold_gui_events = he;
6803#endif
6804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808
6809 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006810 || eap->cmdidx == CMD_tabnew
6811 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006812 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006814 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 setpcmark();
6816 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006817 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6818 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006820 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 || *eap->arg != NUL
6822#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006823 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824#endif
6825 )
6826 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006827 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6828 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006829 if (*eap->arg != NUL && curbuf_locked())
6830 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006831
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 n = readonlymode;
6833 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6834 readonlymode = TRUE;
6835 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006836 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6837 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006838 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6839 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6841 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006842 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6844 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6845 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006846 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006848 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006849 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006850 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6851 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006852 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006854 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 if (old_curwin != NULL)
6856 {
6857 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006858 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006860#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006861 cleanup_T cs;
6862
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006863 // Reset the error/interrupt/exception state here so that
6864 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006865 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006866#endif
6867#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006869#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006870 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006871
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006872#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006873 // Restore the error/interrupt/exception state if not
6874 // discarded by a new aborting error, interrupt, or
6875 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006876 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 }
6879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 }
6881 else if (readonlymode && curbuf->b_nwindows == 1)
6882 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006883 // When editing an already visited buffer, 'readonly' won't be set
6884 // but the previous value is kept. With ":view" and ":sview" we
6885 // want the file to be readonly, except when another window is
6886 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 curbuf->b_p_ro = TRUE;
6888 }
6889 readonlymode = n;
6890 }
6891 else
6892 {
6893 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01006894 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895#ifdef FEAT_TITLE
6896 n = curwin->w_arg_idx_invalid;
6897#endif
6898 check_arg_idx(curwin);
6899#ifdef FEAT_TITLE
6900 if (n != curwin->w_arg_idx_invalid)
6901 maketitle();
6902#endif
6903 }
6904
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 /*
6906 * if ":split file" worked, set alternate file name in old window to new
6907 * file
6908 */
6909 if (old_curwin != NULL
6910 && *eap->arg != NUL
6911 && curwin != old_curwin
6912 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006913 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006914 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916
6917 ex_no_reprint = TRUE;
6918}
6919
6920#ifndef FEAT_GUI
6921/*
6922 * ":gui" and ":gvim" when there is no GUI.
6923 */
6924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006925ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006927 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928}
6929#endif
6930
Bram Moolenaar4f974752019-02-17 17:44:42 +01006931#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006933ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934{
6935 gui_make_tearoff(eap->arg);
6936}
6937#endif
6938
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006939#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6940 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006942ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006944# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6945 if (gui.in_use)
6946 gui_make_popup(eap->arg, eap->forceit);
6947# ifdef FEAT_TERM_POPUP_MENU
6948 else
6949# endif
6950# endif
6951# ifdef FEAT_TERM_POPUP_MENU
6952 pum_make_popup(eap->arg, eap->forceit);
6953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954}
6955#endif
6956
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006958ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
6960 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006961 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006963 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964}
6965
6966/*
6967 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6968 * offset.
6969 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006972ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006975 win_T *save_curwin = curwin;
6976 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 long topline;
6978 long y;
6979 linenr_T old_linenr = curwin->w_cursor.lnum;
6980
6981 setpcmark();
6982
6983 /*
6984 * determine max topline
6985 */
6986 if (curwin->w_p_scb)
6987 {
6988 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006989 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {
6991 if (wp->w_p_scb && wp->w_buffer)
6992 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006993 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 if (topline > y)
6995 topline = y;
6996 }
6997 }
6998 if (topline < 1)
6999 topline = 1;
7000 }
7001 else
7002 {
7003 topline = 1;
7004 }
7005
7006
7007 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007008 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007010 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
7012 if (curwin->w_p_scb)
7013 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007014 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 y = topline - curwin->w_topline;
7016 if (y > 0)
7017 scrollup(y, TRUE);
7018 else
7019 scrolldown(-y, TRUE);
7020 curwin->w_scbind_pos = topline;
7021 redraw_later(VALID);
7022 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 }
7025 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007026 curwin = save_curwin;
7027 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 if (curwin->w_p_scb)
7029 {
7030 did_syncbind = TRUE;
7031 checkpcmark();
7032 if (old_linenr != curwin->w_cursor.lnum)
7033 {
7034 char_u ctrl_o[2];
7035
7036 ctrl_o[0] = Ctrl_O;
7037 ctrl_o[1] = 0;
7038 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7039 }
7040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041}
7042
7043
7044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007045ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007047 int i;
7048 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7049 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007051 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 do_bang(1, eap, FALSE, FALSE, TRUE);
7053 else
7054 {
7055 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7056 return;
7057
7058#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007059 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {
7061 char_u *browseFile;
7062
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007063 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 NULL, NULL, NULL, curbuf);
7065 if (browseFile != NULL)
7066 {
7067 i = readfile(browseFile, NULL,
7068 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7069 vim_free(browseFile);
7070 }
7071 else
7072 i = OK;
7073 }
7074 else
7075#endif
7076 if (*eap->arg == NUL)
7077 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007078 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 return;
7080 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7081 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7082 }
7083 else
7084 {
7085 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7086 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7087 i = readfile(eap->arg, NULL,
7088 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7089
7090 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007091 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007093#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 if (!aborting())
7095#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007096 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 }
7098 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007099 {
7100 if (empty && exmode_active)
7101 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007102 // Delete the empty line that remains. Historically ex does
7103 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007104 if (eap->line2 == 0)
7105 lnum = curbuf->b_ml.ml_line_count;
7106 else
7107 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007108 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007109 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007110 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007111 if (curwin->w_cursor.lnum > 1
7112 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007113 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007114 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007115 }
7116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 }
7120}
7121
Bram Moolenaarea408852005-06-25 22:49:46 +00007122static char_u *prev_dir = NULL;
7123
7124#if defined(EXITFREE) || defined(PROTO)
7125 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007126free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007127{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007128 VIM_CLEAR(prev_dir);
7129 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007130}
7131#endif
7132
Bram Moolenaarf4258302013-06-02 18:20:17 +02007133/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007134 * Get the previous directory for the given chdir scope.
7135 */
7136 static char_u *
7137get_prevdir(cdscope_T scope)
7138{
7139 if (scope == CDSCOPE_WINDOW)
7140 return curwin->w_prevdir;
7141 else if (scope == CDSCOPE_TABPAGE)
7142 return curtab->tp_prevdir;
7143 return prev_dir;
7144}
7145
7146/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007147 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007148 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7149 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007150 */
7151 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007152post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007153{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007154 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007155 // Clear tab local directory for both :cd and :tcd
7156 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007157 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007158 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007159 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007160 char_u *pdir = get_prevdir(scope);
7161
7162 // If still in the global directory, need to remember current
7163 // directory as the global directory.
7164 if (globaldir == NULL && pdir != NULL)
7165 globaldir = vim_strsave(pdir);
7166
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007167 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007168 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007169 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007170 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007171 curtab->tp_localdir = vim_strsave(NameBuff);
7172 else
7173 curwin->w_localdir = vim_strsave(NameBuff);
7174 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007175 }
7176 else
7177 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007178 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007179 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007180 }
7181
7182 shorten_fnames(TRUE);
7183}
7184
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007185/*
7186 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007187 * chdir() function.
7188 * scope == CDSCOPE_WINDOW: changes the window-local directory
7189 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7190 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007191 * Returns TRUE if the directory is successfully changed.
7192 */
7193 int
7194changedir_func(
7195 char_u *new_dir,
7196 int forceit,
7197 cdscope_T scope)
7198{
7199 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007200 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007201 int dir_differs;
7202 int retval = FALSE;
7203
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007204 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007205 return FALSE;
7206
7207 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7208 {
7209 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7210 return FALSE;
7211 }
7212
7213 // ":cd -": Change to previous directory
7214 if (STRCMP(new_dir, "-") == 0)
7215 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007216 pdir = get_prevdir(scope);
7217 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007218 {
7219 emsg(_("E186: No previous directory"));
7220 return FALSE;
7221 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007222 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007223 }
7224
Bram Moolenaar002bc792020-06-05 22:33:42 +02007225 // Free the previous directory
7226 tofree = get_prevdir(scope);
7227
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007228 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007229 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007230 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007231 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007232 pdir = NULL;
7233 if (scope == CDSCOPE_WINDOW)
7234 curwin->w_prevdir = pdir;
7235 else if (scope == CDSCOPE_TABPAGE)
7236 curtab->tp_prevdir = pdir;
7237 else
7238 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007239
7240#if defined(UNIX) || defined(VMS)
7241 // for UNIX ":cd" means: go to home directory
7242 if (*new_dir == NUL)
7243 {
7244 // use NameBuff for home directory name
7245# ifdef VMS
7246 char_u *p;
7247
7248 p = mch_getenv((char_u *)"SYS$LOGIN");
7249 if (p == NULL || *p == NUL) // empty is the same as not set
7250 NameBuff[0] = NUL;
7251 else
7252 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7253# else
7254 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7255# endif
7256 new_dir = NameBuff;
7257 }
7258#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007259 dir_differs = new_dir == NULL || pdir == NULL
7260 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007261 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7262 emsg(_(e_failed));
7263 else
7264 {
7265 char_u *acmd_fname;
7266
7267 post_chdir(scope);
7268
7269 if (dir_differs)
7270 {
7271 if (scope == CDSCOPE_WINDOW)
7272 acmd_fname = (char_u *)"window";
7273 else if (scope == CDSCOPE_TABPAGE)
7274 acmd_fname = (char_u *)"tabpage";
7275 else
7276 acmd_fname = (char_u *)"global";
7277 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7278 curbuf);
7279 }
7280 retval = TRUE;
7281 }
7282 vim_free(tofree);
7283
7284 return retval;
7285}
Bram Moolenaarea408852005-06-25 22:49:46 +00007286
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007288 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007290 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007291ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007293 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
7295 new_dir = eap->arg;
7296#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007297 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 if (*new_dir == NUL)
7299 ex_pwd(NULL);
7300 else
7301#endif
7302 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007303 cdscope_T scope = CDSCOPE_GLOBAL;
7304
7305 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7306 scope = CDSCOPE_WINDOW;
7307 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7308 scope = CDSCOPE_TABPAGE;
7309
7310 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007311 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007312 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007313 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 ex_pwd(eap);
7315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 }
7317}
7318
7319/*
7320 * ":pwd".
7321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007323ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
7325 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7326 {
7327#ifdef BACKSLASH_IN_FILENAME
7328 slash_adjust(NameBuff);
7329#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007330 if (p_verbose > 0)
7331 {
7332 char *context = "global";
7333
7334 if (curwin->w_localdir != NULL)
7335 context = "window";
7336 else if (curtab->tp_localdir != NULL)
7337 context = "tabpage";
7338 smsg("[%s] %s", context, (char *)NameBuff);
7339 }
7340 else
7341 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 }
7343 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007344 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345}
7346
7347/*
7348 * ":=".
7349 */
7350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007351ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007353 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007354 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355}
7356
7357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007358ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007360 int n;
7361 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362
7363 if (cursor_valid())
7364 {
7365 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7366 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007367 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007369
7370 len = eap->line2;
7371 switch (*eap->arg)
7372 {
7373 case 'm': break;
7374 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007375 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007376 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007377
7378 // Hide the cursor if invoked with !
7379 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380}
7381
7382/*
7383 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007384 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 */
7386 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007387do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388{
Bram Moolenaar52953192019-08-16 10:27:13 +02007389 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007390 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007391# ifdef ELAPSED_FUNC
7392 elapsed_T start_tv;
7393
7394 // Remember at what time we started, so that we know how much longer we
7395 // should wait after waiting for a bit.
7396 ELAPSED_INIT(start_tv);
7397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007399 if (hide_cursor)
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007400 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007401 else
7402 cursor_on();
7403
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007404 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007405 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007407 wait_now = msec - done > 1000L ? 1000L : msec - done;
7408#ifdef FEAT_TIMERS
7409 {
7410 long due_time = check_due_timer();
7411
7412 if (due_time > 0 && due_time < wait_now)
7413 wait_now = due_time;
7414 }
7415#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007416#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007417 if (has_any_channel() && wait_now > 20L)
7418 wait_now = 20L;
7419#endif
7420#ifdef FEAT_SOUND
7421 if (has_any_sound_callback() && wait_now > 20L)
7422 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007423#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007424 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007425
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007426#ifdef FEAT_JOB_CHANNEL
7427 if (has_any_channel())
7428 ui_breakcheck_force(TRUE);
7429 else
7430#endif
7431 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007432#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007433 // Process the netbeans and clientserver messages that may have been
7434 // received in the call to ui_breakcheck() when the GUI is in use. This
7435 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007436 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007437#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007438
7439# ifdef ELAPSED_FUNC
7440 // actual time passed
7441 done = ELAPSED_FUNC(start_tv);
7442# else
7443 // guestimate time passed (will actually be more)
7444 done += wait_now;
7445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007447
7448 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7449 // input buffer, otherwise a following call to input() fails.
7450 if (got_int)
7451 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007452
7453 if (hide_cursor)
7454 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455}
7456
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457/*
7458 * ":winsize" command (obsolete).
7459 */
7460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007461ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462{
7463 int w, h;
7464 char_u *arg = eap->arg;
7465 char_u *p;
7466
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007467 if (!isdigit(*arg))
7468 {
7469 semsg(_(e_invarg2), arg);
7470 return;
7471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 w = getdigits(&arg);
7473 arg = skipwhite(arg);
7474 p = arg;
7475 h = getdigits(&arg);
7476 if (*p != NUL && *arg == NUL)
7477 set_shellsize(w, h, TRUE);
7478 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007479 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480}
7481
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007483ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485 int xchar = NUL;
7486 char_u *p;
7487
7488 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7489 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007490 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 if (eap->arg[1] == NUL)
7492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007493 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 return;
7495 }
7496 xchar = eap->arg[1];
7497 p = eap->arg + 2;
7498 }
7499 else
7500 p = eap->arg + 1;
7501
7502 eap->nextcmd = check_nextcmd(p);
7503 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007504 if (*p != NUL && *p != (
7505#ifdef FEAT_EVAL
7506 in_vim9script() ? '#' :
7507#endif
7508 '"')
7509 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007510 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007511 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007513 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007514 postponed_split_flags = cmdmod.cmod_split;
7515 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7517 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007518 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 }
7520}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521
Bram Moolenaar843ee412004-06-30 16:16:41 +00007522#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523/*
7524 * ":winpos".
7525 */
7526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007527ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528{
7529 int x, y;
7530 char_u *arg = eap->arg;
7531 char_u *p;
7532
7533 if (*arg == NUL)
7534 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007535# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007536# ifdef VIMDLL
7537 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7538 mch_get_winpos(&x, &y) != FAIL)
7539# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007541# else
7542 if (mch_get_winpos(&x, &y) != FAIL)
7543# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 {
7545 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007546 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 }
7548 else
7549# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007550 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 }
7552 else
7553 {
7554 x = getdigits(&arg);
7555 arg = skipwhite(arg);
7556 p = arg;
7557 y = getdigits(&arg);
7558 if (*p == NUL || *arg != NUL)
7559 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007560 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 return;
7562 }
7563# ifdef FEAT_GUI
7564 if (gui.in_use)
7565 gui_mch_set_winpos(x, y);
7566 else if (gui.starting)
7567 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007568 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 gui_win_x = x;
7570 gui_win_y = y;
7571 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007572# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 else
7574# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007575# endif
7576# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007577 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578# endif
7579# ifdef HAVE_TGETENT
7580 if (*T_CWP)
7581 term_set_winpos(x, y);
7582# endif
7583 }
7584}
7585#endif
7586
7587/*
7588 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7589 */
7590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007591ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
7593 oparg_T oa;
7594
7595 clear_oparg(&oa);
7596 oa.regname = eap->regname;
7597 oa.start.lnum = eap->line1;
7598 oa.end.lnum = eap->line2;
7599 oa.line_count = eap->line2 - eap->line1 + 1;
7600 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007602 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 {
7604 setpcmark();
7605 curwin->w_cursor.lnum = eap->line1;
7606 beginline(BL_SOL | BL_FIX);
7607 }
7608
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007609 if (VIsual_active)
7610 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007611
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 switch (eap->cmdidx)
7613 {
7614 case CMD_delete:
7615 oa.op_type = OP_DELETE;
7616 op_delete(&oa);
7617 break;
7618
7619 case CMD_yank:
7620 oa.op_type = OP_YANK;
7621 (void)op_yank(&oa, FALSE, TRUE);
7622 break;
7623
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007624 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007625 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007627 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7628#else
7629 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007631 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 oa.op_type = OP_RSHIFT;
7633 else
7634 oa.op_type = OP_LSHIFT;
7635 op_shift(&oa, FALSE, eap->amount);
7636 break;
7637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007639 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640}
7641
7642/*
7643 * ":put".
7644 */
7645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007646ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007648 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 if (eap->line2 == 0)
7650 {
7651 eap->line2 = 1;
7652 eap->forceit = TRUE;
7653 }
7654 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007655 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007656 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657}
7658
7659/*
7660 * Handle ":copy" and ":move".
7661 */
7662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007663ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664{
7665 long n;
7666
Bram Moolenaar491799b2020-08-01 19:23:43 +02007667#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007668 if (not_in_vim9(eap) == FAIL)
7669 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007670#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007671 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007672 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 {
7674 eap->nextcmd = NULL;
7675 return;
7676 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007677 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678
7679 /*
7680 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7681 */
7682 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7683 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007684 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 return;
7686 }
7687
7688 if (eap->cmdidx == CMD_move)
7689 {
7690 if (do_move(eap->line1, eap->line2, n) == FAIL)
7691 return;
7692 }
7693 else
7694 ex_copy(eap->line1, eap->line2, n);
7695 u_clearline();
7696 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007697 ex_may_print(eap);
7698}
7699
7700/*
7701 * Print the current line if flags were given to the Ex command.
7702 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007703 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007704ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007705{
7706 if (eap->flags != 0)
7707 {
7708 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7709 (eap->flags & EXFLAG_LIST));
7710 ex_no_reprint = TRUE;
7711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712}
7713
7714/*
7715 * ":smagic" and ":snomagic".
7716 */
7717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007718ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007720 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007722 magic_overruled = eap->cmdidx == CMD_smagic
7723 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007724 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007725 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726}
7727
7728/*
7729 * ":join".
7730 */
7731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007732ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734 curwin->w_cursor.lnum = eap->line1;
7735 if (eap->line1 == eap->line2)
7736 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007737 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 return;
7739 if (eap->line2 == curbuf->b_ml.ml_line_count)
7740 {
7741 beep_flush();
7742 return;
7743 }
7744 ++eap->line2;
7745 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007746 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007748 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749}
7750
7751/*
7752 * ":[addr]@r" or ":[addr]*r": execute register
7753 */
7754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007755ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
7757 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007758 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759
7760 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007761 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
7763#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007764 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765#endif
7766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007767 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 c = *eap->arg;
7769 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7770 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007771 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007772 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7773 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007774 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 else
7778 {
7779 int save_efr = exec_from_reg;
7780
7781 exec_from_reg = TRUE;
7782
7783 /*
7784 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007785 * Continue until the stuff buffer is empty and all added characters
7786 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007788 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7790
7791 exec_from_reg = save_efr;
7792 }
7793}
7794
7795/*
7796 * ":!".
7797 */
7798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007799ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
7801 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7802}
7803
7804/*
7805 * ":undo".
7806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007808ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007810 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007811 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007812 else
7813 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814}
7815
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007816#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007817 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007818ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007819{
7820 char_u hash[UNDO_HASH_SIZE];
7821
7822 u_compute_hash(hash);
7823 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7824}
7825
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007827ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007828{
7829 char_u hash[UNDO_HASH_SIZE];
7830
7831 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007832 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007833}
7834#endif
7835
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836/*
7837 * ":redo".
7838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007840ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
7842 u_redo(1);
7843}
7844
7845/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007846 * ":earlier" and ":later".
7847 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007849ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007850{
7851 long count = 0;
7852 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007853 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007854 char_u *p = eap->arg;
7855
7856 if (*p == NUL)
7857 count = 1;
7858 else if (isdigit(*p))
7859 {
7860 count = getdigits(&p);
7861 switch (*p)
7862 {
7863 case 's': ++p; sec = TRUE; break;
7864 case 'm': ++p; sec = TRUE; count *= 60; break;
7865 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007866 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7867 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007868 }
7869 }
7870
7871 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007872 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007873 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007874 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7875 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007876}
7877
7878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 * ":redir": start/stop redirection.
7880 */
7881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007882ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
7884 char *mode;
7885 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007886 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887
Bram Moolenaarba768492016-07-08 15:32:54 +02007888#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007889 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007891 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007892 return;
7893 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007894#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007895
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 if (STRICMP(eap->arg, "END") == 0)
7897 close_redir();
7898 else
7899 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007900 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007902 ++arg;
7903 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007905 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 mode = "a";
7907 }
7908 else
7909 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007910 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911
7912 close_redir();
7913
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007914 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007915 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 if (fname == NULL)
7917 return;
7918#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007919 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {
7921 char_u *browseFile;
7922
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007923 browseFile = do_browse(BROWSE_SAVE,
7924 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007925 fname, NULL, NULL,
7926 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007928 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 vim_free(fname);
7930 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007931 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 }
7933#endif
7934
7935 redir_fd = open_exfile(fname, eap->forceit, mode);
7936 vim_free(fname);
7937 }
7938#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007939 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007941 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007943 ++arg;
7944 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007946 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007947 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007949 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007951 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007952 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007953 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007954 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007956 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007957 if (*arg == '>')
7958 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007959 // Make register empty when not using @A-@Z and the
7960 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007961 if (*arg == NUL && !isupper(redir_reg))
7962 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007964 }
7965 if (*arg != NUL)
7966 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007967 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007968 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007969 }
7970 }
7971 else if (*arg == '=' && arg[1] == '>')
7972 {
7973 int append;
7974
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007975 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007976 close_redir();
7977 arg += 2;
7978
7979 if (*arg == '>')
7980 {
7981 ++arg;
7982 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 }
7984 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007985 append = FALSE;
7986
7987 if (var_redir_start(skipwhite(arg), append) == OK)
7988 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 }
7990#endif
7991
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007992 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007995 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007997
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007998 // Make sure redirection is not off. Can happen for cmdline completion
7999 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008000 if (redir_fd != NULL
8001#ifdef FEAT_EVAL
8002 || redir_reg || redir_vname
8003#endif
8004 )
8005 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006}
8007
8008/*
8009 * ":redraw": force redraw
8010 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008011 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008012ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013{
8014 int r = RedrawingDisabled;
8015 int p = p_lz;
8016
8017 RedrawingDisabled = 0;
8018 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008019 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008021 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022#ifdef FEAT_TITLE
8023 if (need_maketitle)
8024 maketitle();
8025#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008026#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8027# ifdef VIMDLL
8028 if (!gui.in_use)
8029# endif
8030 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 RedrawingDisabled = r;
8033 p_lz = p;
8034
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008035 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 msg_didout = FALSE;
8037 msg_col = 0;
8038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008039 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008040 need_wait_return = FALSE;
8041
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 out_flush();
8043}
8044
8045/*
8046 * ":redrawstatus": force redraw of status line(s)
8047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008049ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 int r = RedrawingDisabled;
8052 int p = p_lz;
8053
8054 RedrawingDisabled = 0;
8055 p_lz = FALSE;
8056 if (eap->forceit)
8057 status_redraw_all();
8058 else
8059 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008060 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 RedrawingDisabled = r;
8062 p_lz = p;
8063 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064}
8065
Bram Moolenaare12bab32019-01-08 22:02:56 +01008066/*
8067 * ":redrawtabline": force redraw of the tabline
8068 */
8069 static void
8070ex_redrawtabline(exarg_T *eap UNUSED)
8071{
8072 int r = RedrawingDisabled;
8073 int p = p_lz;
8074
8075 RedrawingDisabled = 0;
8076 p_lz = FALSE;
8077
8078 draw_tabline();
8079
8080 RedrawingDisabled = r;
8081 p_lz = p;
8082 out_flush();
8083}
8084
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008086close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
8088 if (redir_fd != NULL)
8089 {
8090 fclose(redir_fd);
8091 redir_fd = NULL;
8092 }
8093#ifdef FEAT_EVAL
8094 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008095 if (redir_vname)
8096 {
8097 var_redir_stop();
8098 redir_vname = 0;
8099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100#endif
8101}
8102
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008103#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008104 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008105vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008106{
8107 if (vim_mkdir(name, prot) != 0)
8108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008109 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008110 return FAIL;
8111 }
8112 return OK;
8113}
8114#endif
8115
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116/*
8117 * Open a file for writing for an Ex command, with some checks.
8118 * Return file descriptor, or NULL on failure.
8119 */
8120 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008121open_exfile(
8122 char_u *fname,
8123 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008124 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125{
8126 FILE *fd;
8127
8128#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008129 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 if (mch_isdir(fname))
8131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008132 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 return NULL;
8134 }
8135#endif
8136 if (!forceit && *mode != 'a' && vim_fexists(fname))
8137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008138 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 return NULL;
8140 }
8141
8142 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008143 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144
8145 return fd;
8146}
8147
8148/*
8149 * ":mark" and ":k".
8150 */
8151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008152ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153{
8154 pos_T pos;
8155
Bram Moolenaar10b94212021-02-19 21:42:57 +01008156#ifdef FEAT_EVAL
8157 if (not_in_vim9(eap) == FAIL)
8158 return;
8159#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008160 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008161 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008162 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008163 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 else
8165 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008166 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 curwin->w_cursor.lnum = eap->line2;
8168 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008169 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008170 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008171 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 }
8173}
8174
8175/*
8176 * Update w_topline, w_leftcol and the cursor position.
8177 */
8178 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008179update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008181 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 update_topline();
8183 if (!curwin->w_p_wrap)
8184 validate_cursor();
8185 update_curswant();
8186}
8187
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008189 * Save the current State and go to Normal mode.
8190 * Return TRUE if the typeahead could be saved.
8191 */
8192 int
8193save_current_state(save_state_T *sst)
8194{
8195 sst->save_msg_scroll = msg_scroll;
8196 sst->save_restart_edit = restart_edit;
8197 sst->save_msg_didout = msg_didout;
8198 sst->save_State = State;
8199 sst->save_insertmode = p_im;
8200 sst->save_finish_op = finish_op;
8201 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008202 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008203
Bram Moolenaar4324d872020-12-01 20:12:24 +01008204 msg_scroll = FALSE; // no msg scrolling in Normal mode
8205 restart_edit = 0; // don't go to Insert mode
8206 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008207
Bram Moolenaara452b802020-12-01 21:47:59 +01008208 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008209 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008210
8211 /*
8212 * Save the current typeahead. This is required to allow using ":normal"
8213 * from an event handler and makes sure we don't hang when the argument
8214 * ends with half a command.
8215 */
8216 save_typeahead(&sst->tabuf);
8217 return sst->tabuf.typebuf_valid;
8218}
8219
8220 void
8221restore_current_state(save_state_T *sst)
8222{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008223 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008224 restore_typeahead(&sst->tabuf);
8225
8226 msg_scroll = sst->save_msg_scroll;
8227 restart_edit = sst->save_restart_edit;
8228 p_im = sst->save_insertmode;
8229 finish_op = sst->save_finish_op;
8230 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008231 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008232 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008233 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008234
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008235 // Restore the state (needed when called from a function executed for
8236 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008237 State = sst->save_State;
8238#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008239 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008240#endif
8241}
8242
8243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 * ":normal[!] {commands}": Execute normal mode commands.
8245 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008246 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008247ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008249 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 char_u *arg = NULL;
8251 int l;
8252 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008254 if (ex_normal_lock > 0)
8255 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008256 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008257 return;
8258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 if (ex_normal_busy >= p_mmd)
8260 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008261 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 return;
8263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 /*
8266 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8267 * this for the K_SPECIAL leading byte, otherwise special keys will not
8268 * work.
8269 */
8270 if (has_mbyte)
8271 {
8272 int len = 0;
8273
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008274 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 for (p = eap->arg; *p != NUL; ++p)
8276 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008277#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008278 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008280#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008281 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008282 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008283#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 )
8287 len += 2;
8288 }
8289 if (len > 0)
8290 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008291 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 if (arg != NULL)
8293 {
8294 len = 0;
8295 for (p = eap->arg; *p != NUL; ++p)
8296 {
8297 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008298#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 if (*p == CSI)
8300 {
8301 arg[len++] = KS_EXTRA;
8302 arg[len++] = (int)KE_CSI;
8303 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008304#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008305 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {
8307 arg[len++] = *++p;
8308 if (*p == K_SPECIAL)
8309 {
8310 arg[len++] = KS_SPECIAL;
8311 arg[len++] = KE_FILLER;
8312 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008313#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 else if (*p == CSI)
8315 {
8316 arg[len++] = KS_EXTRA;
8317 arg[len++] = (int)KE_CSI;
8318 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 }
8321 arg[len] = NUL;
8322 }
8323 }
8324 }
8325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008327 ++ex_normal_busy;
8328 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {
8330 /*
8331 * Repeat the :normal command for each line in the range. When no
8332 * range given, execute it just once, without positioning the cursor
8333 * first.
8334 */
8335 do
8336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 if (eap->addr_count != 0)
8338 {
8339 curwin->w_cursor.lnum = eap->line1++;
8340 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008341 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 }
8343
Bram Moolenaar13505972019-01-24 15:04:48 +01008344 exec_normal_cmd(arg != NULL
8345 ? arg
8346 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 }
8348 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8349 }
8350
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008351 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 update_topline_cursor();
8353
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008354 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008356 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008357#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008358 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008359#endif
8360
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362}
8363
8364/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008365 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 */
8367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008368ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008370 if (eap->forceit)
8371 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008372 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008373 if (!curwin->w_cursor.lnum)
8374 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008375 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008376 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008377#ifdef FEAT_TERMINAL
8378 // Ignore this when running in an active terminal.
8379 if (term_job_running(curbuf->b_term))
8380 return;
8381#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008382
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008383 // Ignore the command when already in Insert mode. Inserting an
8384 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008385 if (State & INSERT)
8386 return;
8387
Bram Moolenaar64969662005-12-14 21:59:55 +00008388 if (eap->cmdidx == CMD_startinsert)
8389 restart_edit = 'a';
8390 else if (eap->cmdidx == CMD_startreplace)
8391 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008393 restart_edit = 'V';
8394
8395 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008397 if (eap->cmdidx == CMD_startinsert)
8398 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008399 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008401
8402 if (VIsual_active)
8403 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404}
8405
8406/*
8407 * ":stopinsert"
8408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008410ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
8412 restart_edit = 0;
8413 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008414 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008417/*
8418 * Execute normal mode command "cmd".
8419 * "remap" can be REMAP_NONE or REMAP_YES.
8420 */
8421 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008422exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008423{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008424 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008425 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008426 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008427}
Bram Moolenaar25281632016-01-21 23:32:32 +01008428
Bram Moolenaar25281632016-01-21 23:32:32 +01008429/*
8430 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008431 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008432 */
8433 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008434exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008435{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008436 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008437 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008438
Bram Moolenaar905dd902019-04-07 14:21:47 +02008439 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8440 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008441 clear_oparg(&oa);
8442 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008443 while ((!stuff_empty()
8444 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008445 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008446 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008447 {
8448 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008449#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008450 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008451 && oa.op_type == OP_NOP && oa.regname == NUL
8452 && !VIsual_active)
8453 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008454 // If terminal_loop() returns OK we got a key that is handled
8455 // in Normal model. With FAIL we first need to position the
8456 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008457 if (terminal_loop(TRUE) == OK)
8458 normal_cmd(&oa, TRUE);
8459 }
8460 else
8461#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008462 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008463 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008464 }
8465}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008466
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467#ifdef FEAT_FIND_ID
8468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008469ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
8471 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8472 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8473 (linenr_T)1, (linenr_T)MAXLNUM);
8474}
8475
Bram Moolenaar4033c552017-09-16 20:54:51 +02008476#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477/*
8478 * ":psearch"
8479 */
8480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008481ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
8483 g_do_tagpreview = p_pvh;
8484 ex_findpat(eap);
8485 g_do_tagpreview = 0;
8486}
8487#endif
8488
8489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008490ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491{
8492 int whole = TRUE;
8493 long n;
8494 char_u *p;
8495 int action;
8496
8497 switch (cmdnames[eap->cmdidx].cmd_name[2])
8498 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008499 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8501 action = ACTION_GOTO;
8502 else
8503 action = ACTION_SHOW;
8504 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008505 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 action = ACTION_SHOW_ALL;
8507 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008508 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 action = ACTION_GOTO;
8510 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008511 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 action = ACTION_SPLIT;
8513 break;
8514 }
8515
8516 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008517 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 {
8519 n = getdigits(&eap->arg);
8520 eap->arg = skipwhite(eap->arg);
8521 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008522 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {
8524 whole = FALSE;
8525 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008526 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 if (*p)
8528 {
8529 *p++ = NUL;
8530 p = skipwhite(p);
8531
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008532 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008533 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008534 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 else
8536 eap->nextcmd = check_nextcmd(p);
8537 }
8538 }
8539 if (!eap->skip)
8540 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8541 whole, !eap->forceit,
8542 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8543 n, action, eap->line1, eap->line2);
8544}
8545#endif
8546
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547
Bram Moolenaar4033c552017-09-16 20:54:51 +02008548#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549/*
8550 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8551 */
8552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008553ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008555 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8557}
8558
8559/*
8560 * ":pedit"
8561 */
8562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008563ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 win_T *curwin_save = curwin;
8566
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008567 if (ERROR_IF_ANY_POPUP_WINDOW)
8568 return;
8569
Bram Moolenaar026587b2019-08-17 15:08:00 +02008570 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008572 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008573
Bram Moolenaar026587b2019-08-17 15:08:00 +02008574 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008576
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 if (curwin != curwin_save && win_valid(curwin_save))
8578 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008579 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 validate_cursor();
8581 redraw_later(VALID);
8582 win_enter(curwin_save, TRUE);
8583 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008584# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008585 else if (WIN_IS_POPUP(curwin))
8586 {
8587 // can't keep focus in popup window
8588 win_enter(firstwin, TRUE);
8589 }
8590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 g_do_tagpreview = 0;
8592}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594
8595/*
8596 * ":stag", ":stselect" and ":stjump".
8597 */
8598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008599ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600{
8601 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008602 postponed_split_flags = cmdmod.cmod_split;
8603 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8605 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008606 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
8609/*
8610 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8611 */
8612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008613ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
8615 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8616}
8617
8618 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008619ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
8621 int cmd;
8622
8623 switch (name[1])
8624 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008625 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008627 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008629 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008631 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008633 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008635 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008637 case 'f': // ":tfirst"
8638 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008640 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008642 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008644 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008646 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 return;
8648 }
8649#endif
8650 cmd = DT_TAG;
8651 break;
8652 }
8653
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008654 if (name[0] == 'l')
8655 {
8656#ifndef FEAT_QUICKFIX
8657 ex_ni(eap);
8658 return;
8659#else
8660 cmd = DT_LTAG;
8661#endif
8662 }
8663
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8665 eap->forceit, TRUE);
8666}
8667
8668/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008669 * Check "str" for starting with a special cmdline variable.
8670 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8671 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8672 */
8673 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008674find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008675{
8676 int len;
8677 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008678 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008679 "%",
8680#define SPEC_PERC 0
8681 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008682#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008683 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008684#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008685 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008686#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008687 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008688#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008689 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008690#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008691 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008692#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008693 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008694#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008695 "<stack>", // call stack
8696#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008697 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008698#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008699 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008700#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008701 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008702#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008703 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008704#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008705 "<SID>", // script ID: <SNR>123_
8706#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008707#ifdef FEAT_CLIENTSERVER
8708 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008709# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008710#endif
8711 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008712
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008713 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008714 {
8715 len = (int)STRLEN(spec_str[i]);
8716 if (STRNCMP(src, spec_str[i], len) == 0)
8717 {
8718 *usedlen = len;
8719 return i;
8720 }
8721 }
8722 return -1;
8723}
8724
8725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 * Evaluate cmdline variables.
8727 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008728 * change "%" to curbuf->b_ffname
8729 * "#" to curwin->w_alt_fnum
8730 * "%%" to curwin->w_alt_fnum in Vim9 script
8731 * "<cword>" to word under the cursor
8732 * "<cWORD>" to WORD under the cursor
8733 * "<cexpr>" to C-expression under the cursor
8734 * "<cfile>" to path name under the cursor
8735 * "<sfile>" to sourced file name
8736 * "<stack>" to call stack
8737 * "<slnum>" to sourced file line number
8738 * "<afile>" to file name for autocommand
8739 * "<abuf>" to buffer number for autocommand
8740 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 *
8742 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8743 * "" for error without a message) and NULL is returned.
8744 * Returns an allocated string if a valid match was found.
8745 * Returns NULL if no match was found. "usedlen" then still contains the
8746 * number of characters to skip.
8747 */
8748 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008749eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008750 char_u *src, // pointer into commandline
8751 char_u *srcstart, // beginning of valid memory for src
8752 int *usedlen, // characters after src that are used
8753 linenr_T *lnump, // line number for :e command, or NULL
8754 char **errormsg, // pointer to error message
8755 int *escaped) // return value has escaped white space (can
8756 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757{
8758 int i;
8759 char_u *s;
8760 char_u *result;
8761 char_u *resultbuf = NULL;
8762 int resultlen;
8763 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008764 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008766 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769
8770 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008771 if (escaped != NULL)
8772 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773
8774 /*
8775 * Check if there is something to do.
8776 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008777 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008778 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 {
8780 *usedlen = 1;
8781 return NULL;
8782 }
8783
8784 /*
8785 * Skip when preceded with a backslash "\%" and "\#".
8786 * Note: In "\\%" the % is also not recognized!
8787 */
8788 if (src > srcstart && src[-1] == '\\')
8789 {
8790 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008791 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 return NULL;
8793 }
8794
8795 /*
8796 * word or WORD under cursor
8797 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008798 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8799 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008801 resultlen = find_ident_under_cursor(&result,
8802 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8803 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8804 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 if (resultlen == 0)
8806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008807 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 return NULL;
8809 }
8810 }
8811
8812 /*
8813 * '#': Alternate file name
8814 * '%': Current file name
8815 * File name under the cursor
8816 * File name for autocommand
8817 * and following modifiers
8818 */
8819 else
8820 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008821 int off = 0;
8822
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 switch (spec_idx)
8824 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008825 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008826#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008827 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008830 // '%': current file
8831 if (curbuf->b_fname == NULL)
8832 {
8833 result = (char_u *)"";
8834 valid = 0; // Must have ":p:h" to be valid
8835 }
8836 else
8837 {
8838 result = curbuf->b_fname;
8839 tilde_file = STRCMP(result, "~") == 0;
8840 }
8841 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008843#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008844 // "%%" alternate file
8845 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008846#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008847 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008848 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008849 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008851 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 result = arg_all();
8853 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008854 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008855 if (escaped != NULL)
8856 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 break;
8859 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008860 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008861 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008862 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008864 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008865 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008866 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008867 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008869 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008871 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00008872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008873 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008874 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008875 return NULL;
8876 }
8877#ifdef FEAT_EVAL
8878 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8879 (long)i);
8880 if (result == NULL)
8881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008882 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008883 return NULL;
8884 }
8885#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008886 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 }
8890 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008891 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008892 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
8893 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008894 buf = buflist_findnr(i);
8895 if (buf == NULL)
8896 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008897 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008898 return NULL;
8899 }
8900 if (lnump != NULL)
8901 *lnump = ECMD_LAST;
8902 if (buf->b_fname == NULL)
8903 {
8904 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008905 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008906 }
8907 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008908 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008909 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008910 tilde_file = STRCMP(result, "~") == 0;
8911 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 break;
8914
8915#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008916 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008917 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 if (result == NULL)
8919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008920 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 return NULL;
8922 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008923 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 break;
8925#endif
8926
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008927 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008929 if (result != NULL && !autocmd_fname_full)
8930 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008931 // Still need to turn the fname into a full path. It is
8932 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008933 autocmd_fname_full = TRUE;
8934 result = FullName_save(autocmd_fname, FALSE);
8935 vim_free(autocmd_fname);
8936 autocmd_fname = result;
8937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 if (result == NULL)
8939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008940 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 return NULL;
8942 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008943 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 break;
8945
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008946 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 if (autocmd_bufnr <= 0)
8948 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008949 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 return NULL;
8951 }
8952 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8953 result = strbuf;
8954 break;
8955
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008956 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 result = autocmd_match;
8958 if (result == NULL)
8959 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008960 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 return NULL;
8962 }
8963 break;
8964
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008965 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008966 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008967 result = estack_sfile(spec_idx == SPEC_SFILE
8968 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 if (result == NULL)
8970 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008971 *errormsg = spec_idx == SPEC_SFILE
8972 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8973 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 return NULL;
8975 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008976 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008978
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008979 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008980 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008981 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008982 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008983 return NULL;
8984 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008985 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008986 result = strbuf;
8987 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008988
8989#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008990 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008991 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008993 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008994 return NULL;
8995 }
8996 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008997 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008998 result = strbuf;
8999 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009000
Bram Moolenaar90944302020-08-01 20:45:11 +02009001 case SPEC_SID:
9002 if (current_sctx.sc_sid <= 0)
9003 {
9004 *errormsg = _(e_usingsid);
9005 return NULL;
9006 }
9007 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9008 result = strbuf;
9009 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009010#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009011
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009012#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009013 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009014 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9015 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 result = strbuf;
9017 break;
9018#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009019
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009020 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009021 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009022 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 }
9024
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009025 resultlen = (int)STRLEN(result); // length of new string
9026 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 {
9028 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 resultlen = (int)(s - result);
9031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 else if (!skip_mod)
9033 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009034 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 &resultlen);
9036 if (result == NULL)
9037 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009038 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 return NULL;
9040 }
9041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 }
9043
9044 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9045 {
9046 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009047 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009048 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009050 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 result = NULL;
9052 }
9053 else
9054 result = vim_strnsave(result, resultlen);
9055 vim_free(resultbuf);
9056 return result;
9057}
9058
9059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 * Expand the <sfile> string in "arg".
9061 *
9062 * Returns an allocated string, or NULL for any error.
9063 */
9064 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009065expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009067 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 int len;
9069 char_u *result;
9070 char_u *newres;
9071 char_u *repl;
9072 int srclen;
9073 char_u *p;
9074
9075 result = vim_strsave(arg);
9076 if (result == NULL)
9077 return NULL;
9078
9079 for (p = result; *p; )
9080 {
9081 if (STRNCMP(p, "<sfile>", 7) != 0)
9082 ++p;
9083 else
9084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009085 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009086 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 if (errormsg != NULL)
9088 {
9089 if (*errormsg)
9090 emsg(errormsg);
9091 vim_free(result);
9092 return NULL;
9093 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009094 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 {
9096 p += srclen;
9097 continue;
9098 }
9099 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9100 newres = alloc(len);
9101 if (newres == NULL)
9102 {
9103 vim_free(repl);
9104 vim_free(result);
9105 return NULL;
9106 }
9107 mch_memmove(newres, result, (size_t)(p - result));
9108 STRCPY(newres + (p - result), repl);
9109 len = (int)STRLEN(newres);
9110 STRCAT(newres, p + srclen);
9111 vim_free(repl);
9112 vim_free(result);
9113 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009114 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 }
9116 }
9117
9118 return result;
9119}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009122/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009123 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009124 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009127dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 if (fname == NULL)
9130 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009131 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132}
9133#endif
9134
9135/*
9136 * ":behave {mswin,xterm}"
9137 */
9138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009139ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140{
9141 if (STRCMP(eap->arg, "mswin") == 0)
9142 {
9143 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9144 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9145 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9146 set_option_value((char_u *)"keymodel", 0L,
9147 (char_u *)"startsel,stopsel", 0);
9148 }
9149 else if (STRCMP(eap->arg, "xterm") == 0)
9150 {
9151 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9152 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9153 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9154 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9155 }
9156 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009157 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158}
9159
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160static int filetype_detect = FALSE;
9161static int filetype_plugin = FALSE;
9162static int filetype_indent = FALSE;
9163
9164/*
9165 * ":filetype [plugin] [indent] {on,off,detect}"
9166 * on: Load the filetype.vim file to install autocommands for file types.
9167 * off: Load the ftoff.vim file to remove all autocommands for file types.
9168 * plugin on: load filetype.vim and ftplugin.vim
9169 * plugin off: load ftplugof.vim
9170 * indent on: load filetype.vim and indent.vim
9171 * indent off: load indoff.vim
9172 */
9173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009174ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
9176 char_u *arg = eap->arg;
9177 int plugin = FALSE;
9178 int indent = FALSE;
9179
9180 if (*eap->arg == NUL)
9181 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009182 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009183 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 filetype_detect ? "ON" : "OFF",
9185 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9186 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9187 return;
9188 }
9189
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009190 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 for (;;)
9192 {
9193 if (STRNCMP(arg, "plugin", 6) == 0)
9194 {
9195 plugin = TRUE;
9196 arg = skipwhite(arg + 6);
9197 continue;
9198 }
9199 if (STRNCMP(arg, "indent", 6) == 0)
9200 {
9201 indent = TRUE;
9202 arg = skipwhite(arg + 6);
9203 continue;
9204 }
9205 break;
9206 }
9207 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9208 {
9209 if (*arg == 'o' || !filetype_detect)
9210 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009211 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 filetype_detect = TRUE;
9213 if (plugin)
9214 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009215 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 filetype_plugin = TRUE;
9217 }
9218 if (indent)
9219 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009220 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 filetype_indent = TRUE;
9222 }
9223 }
9224 if (*arg == 'd')
9225 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009226 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009227 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 }
9229 }
9230 else if (STRCMP(arg, "off") == 0)
9231 {
9232 if (plugin || indent)
9233 {
9234 if (plugin)
9235 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009236 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 filetype_plugin = FALSE;
9238 }
9239 if (indent)
9240 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009241 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 filetype_indent = FALSE;
9243 }
9244 }
9245 else
9246 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009247 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 filetype_detect = FALSE;
9249 }
9250 }
9251 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009252 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253}
9254
9255/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009256 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 */
9258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009259ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
9261 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009262 {
9263 char_u *arg = eap->arg;
9264
9265 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9266 arg += 9;
9267
9268 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9269 if (arg != eap->arg)
9270 did_filetype = FALSE;
9271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009275ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276{
9277#ifdef FEAT_DIGRAPHS
9278 if (*eap->arg != NUL)
9279 putdigraph(eap->arg);
9280 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009281 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009283 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284#endif
9285}
9286
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009287#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9288 void
9289set_no_hlsearch(int flag)
9290{
9291 no_hlsearch = flag;
9292# ifdef FEAT_EVAL
9293 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9294# endif
9295}
9296
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297/*
9298 * ":nohlsearch"
9299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009301ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009303 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009304 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306#endif
9307
9308#ifdef FEAT_CRYPT
9309/*
9310 * ":X": Get crypt key
9311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009313ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009315 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009316 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317}
9318#endif
9319
9320#ifdef FEAT_FOLDING
9321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009322ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323{
9324 if (foldManualAllowed(TRUE))
9325 foldCreate(eap->line1, eap->line2);
9326}
9327
9328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009329ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330{
9331 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9332 eap->forceit, FALSE);
9333}
9334
9335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009336ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337{
9338 linenr_T lnum;
9339
Bram Moolenaar4facea32019-10-12 20:17:40 +02009340# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009341 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009342# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009343
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009344 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9346 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9347 ml_setmarked(lnum);
9348
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009349 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009351 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009352# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009353 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355}
9356#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009358#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009359/*
9360 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9361 * instead of a quickfix command.
9362 */
9363 int
9364is_loclist_cmd(int cmdidx)
9365{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009366 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009367 return FALSE;
9368 return cmdnames[cmdidx].cmd_name[0] == 'l';
9369}
9370#endif
9371
Bram Moolenaar4facea32019-10-12 20:17:40 +02009372#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009373 int
9374get_pressedreturn(void)
9375{
9376 return ex_pressedreturn;
9377}
9378
9379 void
9380set_pressedreturn(int val)
9381{
9382 ex_pressedreturn = val;
9383}
9384#endif