blob: d4863115d85e27c8b811c1be71ab9597f9a521cc [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;
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +0200488 trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000489
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100490 // When using ":global /pat/ visual" and then "Q" we return to continue
491 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000492 if (global_busy)
493 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
495 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 ++RedrawingDisabled; // don't redisplay the window
497 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100499 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 ++hold_gui_events;
501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502
Bram Moolenaar32526b32019-01-19 17:43:09 +0100503 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 while (exmode_active)
505 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100506 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000507 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
508 {
509 exmode_active = FALSE;
510 break;
511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 msg_scroll = TRUE;
513 need_wait_return = FALSE;
514 ex_pressedreturn = FALSE;
515 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100516 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 prev_msg_row = msg_row;
518 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 if (improved)
520 {
521 cmdline_row = msg_row;
522 do_cmdline(NULL, getexline, NULL, 0);
523 }
524 else
525 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
526 lines_left = Rows - 1;
527
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100529 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100532 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000535 if (ex_pressedreturn)
536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100537 // go up one line, to overwrite the ":<CR>" line, so the
538 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000539 msg_row = prev_msg_row;
540 if (prev_msg_row == Rows - 1)
541 msg_row--;
542 }
543 msg_col = 0;
544 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
545 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100548 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 {
550 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100551 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100553 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 }
556
557#ifdef FEAT_GUI
558 --hold_gui_events;
559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 --RedrawingDisabled;
561 --no_wait_return;
562 update_screen(CLEAR);
563 need_wait_return = FALSE;
564 msg_scroll = save_msg_scroll;
565}
566
567/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200568 * Print the executed command for when 'verbose' is set.
569 * When "lnum" is 0 only print the command.
570 */
571 static void
572msg_verbose_cmd(linenr_T lnum, char_u *cmd)
573{
574 ++no_wait_return;
575 verbose_enter_scroll();
576
577 if (lnum == 0)
578 smsg(_("Executing: %s"), cmd);
579 else
580 smsg(_("line %ld: %s"), (long)lnum, cmd);
581 if (msg_silent == 0)
582 msg_puts("\n"); // don't overwrite this
583
584 verbose_leave_scroll();
585 --no_wait_return;
586}
587
588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 * Execute a simple command line. Used for translated commands like "*".
590 */
591 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100592do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 return do_cmdline(cmd, NULL, NULL,
595 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
596}
597
598/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100599 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
600 * This allows for using a range without ":" in Vim9 script.
601 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200602 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100603do_cmd_argument(char_u *cmd)
604{
605 return do_cmdline(cmd, NULL, NULL,
606 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
607}
608
609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 * do_cmdline(): execute one Ex command line
611 *
612 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100613 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 * 2. Split up in parts separated with '|'.
615 *
616 * This function can be called recursively!
617 *
618 * flags:
619 * DOCMD_VERBOSE - The command will be included in the error message.
620 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100621 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 * DOCMD_KEYTYPED - Don't reset KeyTyped.
623 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
624 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
625 *
626 * return FAIL if cmdline could not be executed, OK otherwise
627 */
628 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100629do_cmdline(
630 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200631 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100633 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100635 char_u *next_cmdline; // next cmd to execute
636 char_u *cmdline_copy = NULL; // copy of cmd line
637 int used_getline = FALSE; // used "fgetline" to obtain command
638 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 int count = 0; // line number count
641 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 int retval = OK;
643#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100644 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100645 garray_T lines_ga; // keep lines for ":while"/":for"
646 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200647 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100648 char_u *fname = NULL; // function or script name
649 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
650 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
651 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200653 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200654 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100656 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200657 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000659 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000661 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100663# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664# define cmd_cookie cookie
665#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100666 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200667#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
669 // location for storing error messages to be converted to an exception.
670 // This ensures that the do_errthrow() call in do_one_cmd() does not
671 // combine the messages stored by an earlier invocation of do_one_cmd()
672 // with the command name of the later one. This would happen when
673 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 saved_msg_list = msg_list;
675 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#endif
677
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100678 // It's possible to create an endless loop with ":execute", catch that
679 // here. The value of 200 allows nested function calls, ":source", etc.
680 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100681 if (call_depth >= 200
682#ifdef FEAT_EVAL
683 && call_depth >= p_mfd
684#endif
685 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100687 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100689 // When converting to an exception, we do not include the command name
690 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100691 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 msg_list = saved_msg_list;
693#endif
694 return FAIL;
695 }
696 ++call_depth;
697
698#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100699 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 cstack.cs_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
702
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100703 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100705 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100706 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000707 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 ++ex_nesting_level;
709
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100710 // Get the function or script name and the address where the next breakpoint
711 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000712 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
714 fname = func_name(real_cookie);
715 breakpoint = func_breakpoint(real_cookie);
716 dbg_tick = func_dbg_tick(real_cookie);
717 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100718 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100720 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 breakpoint = source_breakpoint(real_cookie);
722 dbg_tick = source_dbg_tick(real_cookie);
723 }
724
725 /*
726 * Initialize "force_abort" and "suppress_errthrow" at the top level.
727 */
728 if (!recursive)
729 {
730 force_abort = FALSE;
731 suppress_errthrow = FALSE;
732 }
733
734 /*
735 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000736 * exception handling (used when debugging). Otherwise clear it to avoid
737 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000739 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000740 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000741 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200742 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 initial_trylevel = trylevel;
745
746 /*
747 * "did_throw" will be set to TRUE when an exception is being thrown.
748 */
749 did_throw = FALSE;
750#endif
751 /*
752 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000753 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 * If force_abort is set, we cancel everything.
755 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100756#ifdef FEAT_EVAL
757 did_emsg_cumul += did_emsg;
758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 did_emsg = FALSE;
760
761 /*
762 * KeyTyped is only set when calling vgetc(). Reset it here when not
763 * calling vgetc() (sourced command lines).
764 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100765 if (!(flags & DOCMD_KEYTYPED)
766 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 KeyTyped = FALSE;
768
769 /*
770 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000771 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 * - for multiple commands on one line, separated with '|'
773 * - when repeating until there are no more lines (for ":source")
774 */
775 next_cmdline = cmdline;
776 do
777 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000778#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100779 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000780#endif
781
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100782 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 if (next_cmdline == NULL
784#ifdef FEAT_EVAL
785 && !force_abort
786 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000787 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#endif
789 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100790 {
791#ifdef FEAT_EVAL
792 did_emsg_cumul += did_emsg;
793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
797 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000798 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100799 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 * 3. If a line is given: Make a copy, so we can mess with it.
801 */
802
803#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100804 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000805 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100807 // Each '|' separated command is stored separately in lines_ga, to
808 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100809 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100811 // Check if a function has returned or, unless it has an unclosed
812 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000816 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817 func_line_end(real_cookie);
818# endif
819 if (func_has_ended(real_cookie))
820 {
821 retval = FAIL;
822 break;
823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000826 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 script_line_end();
829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100831 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 {
834 retval = FAIL;
835 break;
836 }
837
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (breakpoint != NULL && dbg_tick != NULL
840 && *dbg_tick != debug_tick)
841 {
842 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100843 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100844 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 *dbg_tick = debug_tick;
846 }
847
848 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100849 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100851 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100853 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100855 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100858 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100859 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 *dbg_tick = debug_tick;
861 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000863 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000864 {
865 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100866 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868 script_line_start();
869 }
870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872#endif
873
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100874 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (next_cmdline == NULL)
876 {
877 /*
878 * Need to set msg_didout for the first line after an ":if",
879 * otherwise the ":if" will be overwritten.
880 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_EVAL
885 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
886#else
887 0
888#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100889 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
890 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100892 // Don't call wait_return for aborted command line. The NULL
893 // returned for the end of a sourced file or executed function
894 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (KeyTyped && !(flags & DOCMD_REPEAT))
896 need_wait_return = FALSE;
897 retval = FAIL;
898 break;
899 }
900 used_getline = TRUE;
901
902 /*
903 * Keep the first typed line. Clear it when more lines are typed.
904 */
905 if (flags & DOCMD_KEEPLINE)
906 {
907 vim_free(repeat_cmdline);
908 if (count == 0)
909 repeat_cmdline = vim_strsave(next_cmdline);
910 else
911 repeat_cmdline = NULL;
912 }
913 }
914
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100915 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 else if (cmdline_copy == NULL)
917 {
918 next_cmdline = vim_strsave(next_cmdline);
919 if (next_cmdline == NULL)
920 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200921 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 retval = FAIL;
923 break;
924 }
925 }
926 cmdline_copy = next_cmdline;
927
928#ifdef FEAT_EVAL
929 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200930 * Inside a while/for loop, and when the command looks like a ":while"
931 * or ":for", the line is stored, because we may need it later when
932 * looping.
933 *
934 * When there is a '|' and another command, it is stored separately,
935 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000936 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200937 *
938 * Pass a different "fgetline" function to do_one_cmd() below,
939 * that it stores lines in or reads them from "lines_ga". Makes it
940 * possible to define a function inside a while/for loop and handles
941 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200945 cmd_getline = get_loop_line;
946 cmd_cookie = (void *)&cmd_loop_cookie;
947 cmd_loop_cookie.lines_gap = &lines_ga;
948 cmd_loop_cookie.current_line = current_line;
949 cmd_loop_cookie.getline = fgetline;
950 cmd_loop_cookie.cookie = cookie;
951 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
952
953 // Save the current line when encountering it the first time.
954 if (current_line == lines_ga.ga_len
955 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
957 retval = FAIL;
958 break;
959 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200960 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200962 else
963 {
964 cmd_getline = fgetline;
965 cmd_cookie = cookie;
966 }
967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 did_endif = FALSE;
969#endif
970
971 if (count++ == 0)
972 {
973 /*
974 * All output from the commands is put below each other, without
975 * waiting for a return. Don't do this when executing commands
976 * from a script or when being called recursive (e.g. for ":e
977 * +command file").
978 */
979 if (!(flags & DOCMD_NOWAIT) && !recursive)
980 {
981 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100982 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100984 msg_scroll = TRUE; // put messages below each other
985 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 ++RedrawingDisabled;
987 did_inc = TRUE;
988 }
989 }
990
Bram Moolenaar823654b2020-05-29 23:03:09 +0200991 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100992 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 /*
995 * 2. Execute one '|' separated command.
996 * do_one_cmd() will return NULL if there is no trailing '|'.
997 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
998 */
999 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001000 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#ifdef FEAT_EVAL
1002 &cstack,
1003#endif
1004 cmd_getline, cmd_cookie);
1005 --recursive;
1006
1007#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001008 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001009 // Use "current_line" from "cmd_loop_cookie", it may have been
1010 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001011 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#endif
1013
1014 if (next_cmdline == NULL)
1015 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001016 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 /*
1019 * If the command was typed, remember it for the ':' register.
1020 * Do this AFTER executing the command to make :@: work.
1021 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001022 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 && new_last_cmdline != NULL)
1024 {
1025 vim_free(last_cmdline);
1026 last_cmdline = new_last_cmdline;
1027 new_last_cmdline = NULL;
1028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030 else
1031 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001032 // need to copy the command after the '|' to cmdline_copy, for the
1033 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001034 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 next_cmdline = cmdline_copy;
1036 }
1037
1038
1039#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001040 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001042 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001044 {
1045 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
1051 ++current_line;
1052
1053 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 * An ":endwhile", ":endfor" and ":continue" is handled here.
1055 * If we were executing commands, jump back to the ":while" or
1056 * ":for".
1057 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001063 // Jump back to the matching ":while" or ":for". Be careful
1064 // not to use a cs_line[] from an entry that isn't a ":while"
1065 // or ":for": It would make "current_line" invalid and can
1066 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (!did_emsg && !got_int && !did_throw
1068 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 && (cstack.cs_flags[cstack.cs_idx]
1070 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 && cstack.cs_line[cstack.cs_idx] >= 0
1072 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1073 {
1074 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001075 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001076 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001077 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001079 // Check for the next breakpoint at or after the ":while"
1080 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (breakpoint != NULL)
1082 {
1083 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001084 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 fname,
1086 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1087 *dbg_tick = debug_tick;
1088 }
1089 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001092 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001094 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1095 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
1097 }
1098
1099 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001105 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107 }
1108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001109 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001110 if (breakpoint != NULL && has_watchexpr())
1111 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001112 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001113 *dbg_tick = debug_tick;
1114 }
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 /*
1117 * When not inside any ":while" loop, clear remembered lines.
1118 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121 if (lines_ga.ga_len > 0)
1122 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001123 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1125 free_cmdlines(&lines_ga);
1126 }
1127 current_line = 0;
1128 }
1129
1130 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1132 * being restored at the ":endtry". Reset them here and set the
1133 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1134 * This includes the case where a missing ":endif", ":endwhile" or
1135 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 cstack.cs_lflags &= ~CSL_HAD_FINA;
1140 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1141 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 did_throw ? (void *)current_exception : NULL);
1143 did_emsg = got_int = did_throw = FALSE;
1144 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1145 }
1146
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001147 // Update global "trylevel" for recursive calls to do_cmdline() from
1148 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 trylevel = initial_trylevel + cstack.cs_trylevel;
1150
1151 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001152 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * files) is aborted because of an error, an interrupt, or an uncaught
1154 * exception, cancel everything. If it is left normally, reset
1155 * force_abort to get the non-EH compatible abortion behavior for
1156 * the rest of the script.
1157 */
1158 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1159 force_abort = FALSE;
1160
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001161 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001163#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
1165 }
1166 /*
1167 * Continue executing command lines when:
1168 * - no CTRL-C typed, no aborting error, no exception thrown or try
1169 * conditionals need to be checked for executing finally clauses or
1170 * catching an interrupt exception
1171 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 * looping for ":source" command or function call.
1174 */
1175 while (!((got_int
1176#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001177 || (did_emsg && (force_abort || in_vim9script()))
1178 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#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.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001212 * In Vim9 script do not give a second error, executing aborts after
1213 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 */
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001215 if (!got_int && !did_throw && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001216 && ((getline_equal(fgetline, cookie, getsourceline)
1217 && !source_finished(fgetline, cookie))
1218 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 && !func_has_ended(real_cookie))))
1220 {
1221 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001222 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001224 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001228 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230
1231 /*
1232 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1233 * ":endtry" in a sourced file or executed function. If the try
1234 * conditional is in its finally clause, ignore anything pending.
1235 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001236 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 */
1238 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001239 {
1240 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1241
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001242 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001243 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001244 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1245 &cstack.cs_looplevel);
1246 }
1247 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 trylevel = initial_trylevel;
1249 }
1250
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001251 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1252 // lack was reported above and the error message is to be converted to an
1253 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001254 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 ? (char_u *)"endfunction" : (char_u *)NULL);
1256
1257 if (trylevel == 0)
1258 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001259 // Just in case did_throw got set but current_exception wasn't.
1260 if (current_exception == NULL)
1261 did_throw = FALSE;
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /*
1264 * When an exception is being thrown out of the outermost try
1265 * conditional, discard the uncaught exception, disable the conversion
1266 * of interrupts or errors to exceptions, and ensure that no more
1267 * commands are executed.
1268 */
1269 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001270 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271
1272 /*
1273 * On an interrupt or an aborting error not converted to an exception,
1274 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001275 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 * when force_abort is set and did_emsg unset in case of an interrupt
1277 * from a finally clause after an error.
1278 */
1279 else if (got_int || (did_emsg && force_abort))
1280 suppress_errthrow = TRUE;
1281 }
1282
1283 /*
1284 * The current cstack will be freed when do_cmdline() returns. An uncaught
1285 * exception will have to be rethrown in the previous cstack. If a function
1286 * has just returned or a script file was just finished and the previous
1287 * cstack belongs to the same function or, respectively, script file, it
1288 * will have to be checked for finally clauses to be executed due to the
1289 * ":return" or ":finish". This is done in do_one_cmd().
1290 */
1291 if (did_throw)
1292 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001293 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001295 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 && ex_nesting_level > func_level(real_cookie) + 1))
1297 {
1298 if (!did_throw)
1299 check_cstack = TRUE;
1300 }
1301 else
1302 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001303 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001304 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 --ex_nesting_level;
1306 /*
1307 * Go to debug mode when returning from a function in which we are
1308 * single-stepping.
1309 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001310 if ((getline_equal(fgetline, cookie, getsourceline)
1311 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001313 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 ? (char_u *)_("End of sourced file")
1315 : (char_u *)_("End of function"));
1316 }
1317
1318 /*
1319 * Restore the exception environment (done after returning from the
1320 * debugger).
1321 */
1322 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001323 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324
1325 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001326
1327 // Cleanup if "cs_emsg_silent_list" remains.
1328 if (cstack.cs_emsg_silent_list != NULL)
1329 {
1330 eslist_T *elem, *temp;
1331
1332 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1333 {
1334 temp = elem->next;
1335 vim_free(elem);
1336 }
1337 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001338#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
1340 /*
1341 * If there was too much output to fit on the command line, ask the user to
1342 * hit return before redrawing the screen. With the ":global" command we do
1343 * this only once after the command is finished.
1344 */
1345 if (did_inc)
1346 {
1347 --RedrawingDisabled;
1348 --no_wait_return;
1349 msg_scroll = FALSE;
1350
1351 /*
1352 * When just finished an ":if"-":else" which was typed, no need to
1353 * wait for hit-return. Also for an error situation.
1354 */
1355 if (retval == FAIL
1356#ifdef FEAT_EVAL
1357 || (did_endif && KeyTyped && !did_emsg)
1358#endif
1359 )
1360 {
1361 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001362 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364 else if (need_wait_return)
1365 {
1366 /*
1367 * The msg_start() above clears msg_didout. The wait_return we do
1368 * here should not overwrite the command that may be shown before
1369 * doing that.
1370 */
1371 msg_didout |= msg_didout_before_start;
1372 wait_return(FALSE);
1373 }
1374 }
1375
Bram Moolenaar2a942252012-11-28 23:03:07 +01001376#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001377 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001378#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 /*
1380 * Reset if_level, in case a sourced script file contains more ":if" than
1381 * ":endif" (could be ":if x | foo | endif").
1382 */
1383 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001384#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 --call_depth;
1387 return retval;
1388}
1389
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001390#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001391/*
1392 * Handle when "did_throw" is set after executing commands.
1393 */
1394 void
1395handle_did_throw()
1396{
1397 char *p = NULL;
1398 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001399 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001400
1401 /*
1402 * If the uncaught exception is a user exception, report it as an
1403 * error. If it is an error exception, display the saved error
1404 * message now. For an interrupt exception, do nothing; the
1405 * interrupt message is given elsewhere.
1406 */
1407 switch (current_exception->type)
1408 {
1409 case ET_USER:
1410 vim_snprintf((char *)IObuff, IOSIZE,
1411 _("E605: Exception not caught: %s"),
1412 current_exception->value);
1413 p = (char *)vim_strsave(IObuff);
1414 break;
1415 case ET_ERROR:
1416 messages = current_exception->messages;
1417 current_exception->messages = NULL;
1418 break;
1419 case ET_INTERRUPT:
1420 break;
1421 }
1422
1423 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1424 current_exception->throw_lnum);
1425 ESTACK_CHECK_SETUP
1426 current_exception->throw_name = NULL;
1427
1428 discard_current_exception(); // uses IObuff if 'verbose'
1429 suppress_errthrow = TRUE;
1430 force_abort = TRUE;
1431
1432 if (messages != NULL)
1433 {
1434 do
1435 {
1436 msglist_T *next = messages->next;
1437 int save_compiling = estack_compiling;
1438
1439 estack_compiling = messages->msg_compiling;
1440 emsg(messages->msg);
1441 vim_free(messages->msg);
1442 vim_free(messages->sfile);
1443 vim_free(messages);
1444 messages = next;
1445 estack_compiling = save_compiling;
1446 }
1447 while (messages != NULL);
1448 }
1449 else if (p != NULL)
1450 {
1451 emsg(p);
1452 vim_free(p);
1453 }
1454 vim_free(SOURCING_NAME);
1455 ESTACK_CHECK_NOW
1456 estack_pop();
1457}
1458
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001460 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 */
1462 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001463get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001465 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 wcmd_T *wp;
1467 char_u *line;
1468
1469 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1470 {
1471 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001472 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001474 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001476 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001478 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001479 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 ++cp->current_line;
1481
1482 return line;
1483 }
1484
1485 KeyTyped = FALSE;
1486 ++cp->current_line;
1487 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001488 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 return vim_strsave(wp->line);
1490}
1491
1492/*
1493 * Store a line in "gap" so that a ":while" loop can execute it again.
1494 */
1495 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001496store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 if (ga_grow(gap, 1) == FAIL)
1499 return FAIL;
1500 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001501 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 return OK;
1504}
1505
1506/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001507 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 */
1509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001510free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512 while (gap->ga_len > 0)
1513 {
1514 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1515 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
1517}
1518#endif
1519
1520/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001521 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1522 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001525getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001526 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001527 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001528 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
1530#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001531 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001532 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001534 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1535 // function that's originally used to obtain the lines. This may be
1536 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001537 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 cp = (struct loop_cookie *)cookie;
1539 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 {
1541 gp = cp->getline;
1542 cp = cp->cookie;
1543 }
1544 return gp == func;
1545#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001546 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547#endif
1548}
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001551 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 * getline function. Otherwise return "cookie".
1553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001555getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001556 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001557 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
Bram Moolenaar13505972019-01-24 15:04:48 +01001559#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001560 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001561 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001563 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1564 // cookie that's originally used to obtain the lines. This may be nested
1565 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001566 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 cp = (struct loop_cookie *)cookie;
1568 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570 gp = cp->getline;
1571 cp = cp->cookie;
1572 }
1573 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001574#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001577}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaard5053d02020-06-28 15:51:16 +02001579#if defined(FEAT_EVAL) || defined(PROT)
1580/*
1581 * Get the next line source line without advancing.
1582 */
1583 char_u *
1584getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001585 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001586 void *cookie) // argument for fgetline()
1587{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001588 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001589 struct loop_cookie *cp;
1590 wcmd_T *wp;
1591
1592 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1593 // cookie that's originally used to obtain the lines. This may be nested
1594 // several levels.
1595 gp = fgetline;
1596 cp = (struct loop_cookie *)cookie;
1597 while (gp == get_loop_line)
1598 {
1599 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1600 {
1601 // executing lines a second time, use the stored copy
1602 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1603 return wp->line;
1604 }
1605 gp = cp->getline;
1606 cp = cp->cookie;
1607 }
1608 if (gp == getsourceline)
1609 return source_nextline(cp);
1610 return NULL;
1611}
1612#endif
1613
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614
1615/*
1616 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1617 * ":bwipeout", etc.
1618 * Returns the buffer number.
1619 */
1620 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001621compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001622{
1623 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001624 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001625 int count = offset;
1626
1627 buf = firstbuf;
1628 while (buf->b_next != NULL && buf->b_fnum < lnum)
1629 buf = buf->b_next;
1630 while (count != 0)
1631 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001632 count += (offset < 0) ? 1 : -1;
1633 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1634 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001635 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001636 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001637 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001638 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001639 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 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001647 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001648 if (addr_type == ADDR_LOADED_BUFFERS)
1649 while (buf->b_ml.ml_mfp == NULL)
1650 {
1651 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1652 if (nextbuf == NULL)
1653 break;
1654 buf = nextbuf;
1655 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001656 return buf->b_fnum;
1657}
1658
Bram Moolenaarb7316892019-05-01 18:08:42 +02001659/*
1660 * Return the window number of "win".
1661 * When "win" is NULL return the number of windows.
1662 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001663 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001664current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001665{
1666 win_T *wp;
1667 int nr = 0;
1668
Bram Moolenaar29323592016-07-24 22:04:11 +02001669 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001670 {
1671 ++nr;
1672 if (wp == win)
1673 break;
1674 }
1675 return nr;
1676}
1677
1678 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001679current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001680{
1681 tabpage_T *tp;
1682 int nr = 0;
1683
Bram Moolenaar29323592016-07-24 22:04:11 +02001684 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001685 {
1686 ++nr;
1687 if (tp == tab)
1688 break;
1689 }
1690 return nr;
1691}
1692
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001693 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001694comment_start(char_u *p, int starts_with_colon UNUSED)
1695{
1696#ifdef FEAT_EVAL
1697 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001698 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001699#endif
1700 return *p == '"';
1701}
1702
Bram Moolenaarf240e182014-11-27 18:33:02 +01001703# define CURRENT_WIN_NR current_win_nr(curwin)
1704# define LAST_WIN_NR current_win_nr(NULL)
1705# define CURRENT_TAB_NR current_tab_nr(curtab)
1706# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001707
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708/*
1709 * Execute one Ex command.
1710 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001711 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1712 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 *
1714 * 1. skip comment lines and leading space
1715 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001716 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001717 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001718 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001719 * 6. parse arguments
1720 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001722 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 *
1724 * This function may be called recursively!
1725 */
1726#if (_MSC_VER == 1200)
1727/*
Bram Moolenaared203462004-06-16 11:19:22 +00001728 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001730 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731#endif
1732 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001733do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001734 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001735 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001737 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001739 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001740 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001742 char_u *p;
1743 linenr_T lnum;
1744 long n;
1745 char *errormsg = NULL; // error message
1746 char_u *after_modifier = NULL;
1747 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001748 cmdmod_T save_cmdmod;
1749 int save_reg_executing = reg_executing;
1750 int ni; // set when Not Implemented
1751 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001752 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001753#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001754 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001755 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001756 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001757#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001758 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
Bram Moolenaara80faa82020-04-12 19:37:17 +02001760 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 ea.line1 = 1;
1762 ea.line2 = 1;
1763#ifdef FEAT_EVAL
1764 ++ex_nesting_level;
1765#endif
1766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001767 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 if (quitmore
1769#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001770 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001771 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001772#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001773 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001774 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 --quitmore;
1776
1777 /*
1778 * Reset browse, confirm, etc.. They are restored when returning, for
1779 * recursive calls.
1780 */
1781 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001783 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001784 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1785 goto doend;
1786
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001787/*
1788 * 1. Skip comment lines and leading white space and colons.
1789 * 2. Handle command modifiers.
1790 */
1791 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001793 ea.cmdlinep = cmdlinep;
1794 ea.getline = fgetline;
1795 ea.cookie = cookie;
1796#ifdef FEAT_EVAL
1797 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001798 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001800 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001801 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001802 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001803#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001804 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001805#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001806 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
1808#ifdef FEAT_EVAL
1809 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1810 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1811#else
1812 ea.skip = (if_level > 0);
1813#endif
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001816 * 3. Skip over the range to find the command. Let "p" point to after it.
1817 *
1818 * We need the command to know what kind of range it uses.
1819 */
1820 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001821#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001822 // In Vim9 script a colon is required before the range. This may also be
1823 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001824 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001825 {
1826 may_have_range = FALSE;
1827 for (p = ea.cmd; p >= *cmdlinep; --p)
1828 {
1829 if (*p == ':')
1830 may_have_range = TRUE;
1831 if (p < ea.cmd && !VIM_ISWHITE(*p))
1832 break;
1833 }
1834 }
1835 else
1836 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001837 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001838#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001839 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840
1841#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001842 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001843 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001844 if (ea.cmd == cmd + 1 && *cmd == '$')
1845 // should be "$VAR = val"
1846 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001847 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001848 if (ea.cmdidx == CMD_SIZE)
1849 {
1850 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1851
1852 // If a ':' before the range is missing, give a clearer error
1853 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001854 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001855 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001856 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001857 goto doend;
1858 }
1859 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001860 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 else
1862#endif
1863 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001864
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001865#ifdef FEAT_EVAL
1866# ifdef FEAT_PROFILE
1867 // Count this line for profiling if skip is TRUE.
1868 if (do_profiling == PROF_YES
1869 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1870 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1871 {
1872 int skip = did_emsg || got_int || did_throw;
1873
1874 if (ea.cmdidx == CMD_catch)
1875 skip = !skip && !(cstack->cs_idx >= 0
1876 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1877 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1878 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1879 skip = skip || !(cstack->cs_idx >= 0
1880 && !(cstack->cs_flags[cstack->cs_idx]
1881 & (CSF_ACTIVE | CSF_TRUE)));
1882 else if (ea.cmdidx == CMD_finally)
1883 skip = FALSE;
1884 else if (ea.cmdidx != CMD_endif
1885 && ea.cmdidx != CMD_endfor
1886 && ea.cmdidx != CMD_endtry
1887 && ea.cmdidx != CMD_endwhile)
1888 skip = ea.skip;
1889
1890 if (!skip)
1891 {
1892 if (getline_equal(fgetline, cookie, get_func_line))
1893 func_line_exec(getline_cookie(fgetline, cookie));
1894 else if (getline_equal(fgetline, cookie, getsourceline))
1895 script_line_exec();
1896 }
1897 }
1898# endif
1899
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001900 // May go to debug mode. If this happens and the ">quit" debug command is
1901 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001902 dbg_check_breakpoint(&ea);
1903 if (!ea.skip && got_int)
1904 {
1905 ea.skip = TRUE;
1906 (void)do_intthrow(cstack);
1907 }
1908#endif
1909
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001910/*
1911 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 *
1913 * where 'addr' is:
1914 *
1915 * % (entire file)
1916 * $ [+-NUM]
1917 * 'x [+-NUM] (where x denotes a currently defined mark)
1918 * . [+-NUM]
1919 * [+-NUM]..
1920 * NUM
1921 *
1922 * The ea.cmd pointer is updated to point to the first character following the
1923 * range spec. If an initial address is found, but no second, the upper bound
1924 * is equal to the lower.
1925 */
1926
Bram Moolenaar25190db2019-05-04 15:05:28 +02001927 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001928 if (!IS_USER_CMDIDX(ea.cmdidx))
1929 {
1930 if (ea.cmdidx != CMD_SIZE)
1931 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1932 else
1933 ea.addr_type = ADDR_LINES;
1934
Bram Moolenaar25190db2019-05-04 15:05:28 +02001935 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001936 if (ea.cmdidx == CMD_wincmd && p != NULL)
1937 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001938#ifdef FEAT_QUICKFIX
1939 // :.cc in quickfix window uses line number
1940 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1941 ea.addr_type = ADDR_OTHER;
1942#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001943 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001944
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001945 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001946#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001947 if (!may_have_range)
1948 ea.line1 = ea.line2 = default_address(&ea);
1949 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001950#endif
1951 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1952 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001955 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 */
1957
1958 /*
1959 * Skip ':' and any white space
1960 */
1961 ea.cmd = skipwhite(ea.cmd);
1962 while (*ea.cmd == ':')
1963 ea.cmd = skipwhite(ea.cmd + 1);
1964
1965 /*
1966 * If we got a line, but no command, then go to the line.
1967 * If we find a '|' or '\n' we set ea.nextcmd.
1968 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001969 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1970 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 /*
1973 * strange vi behaviour:
1974 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001975 * ":3|..." prints line 3 (not in Vim9 script)
1976 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001978 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 goto doend;
Bram Moolenaarb8554302021-02-15 21:30:30 +01001980 if ((*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
1981#ifdef FEAT_EVAL
1982 && !vim9script
1983#endif
1984 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
1986 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001987 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if ((errormsg = invalid_range(&ea)) == NULL)
1989 {
1990 correct_range(&ea);
1991 ex_print(&ea);
1992 }
1993 }
1994 else if (ea.addr_count != 0)
1995 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001996 if (ea.line2 > curbuf->b_ml.ml_line_count)
1997 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001998 // With '-' in 'cpoptions' a line number past the file is an
1999 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002000 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2001 ea.line2 = -1;
2002 else
2003 ea.line2 = curbuf->b_ml.ml_line_count;
2004 }
2005
2006 if (ea.line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002007 errormsg = _(e_invalid_range);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 else
2009 {
2010 if (ea.line2 == 0)
2011 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 else
2013 curwin->w_cursor.lnum = ea.line2;
2014 beginline(BL_SOL | BL_FIX);
2015 }
2016 }
2017 goto doend;
2018 }
2019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 // If this looks like an undefined user command and there are CmdUndefined
2021 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002022 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2023 && ASCII_ISUPPER(*ea.cmd)
2024 && has_cmdundefined())
2025 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002026 int ret;
2027
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002028 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002029 while (ASCII_ISALNUM(*p))
2030 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002031 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002032 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2033 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // If the autocommands did something and didn't cause an error, try
2035 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002036 p = (ret
2037#ifdef FEAT_EVAL
2038 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002039#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002041 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (p == NULL)
2044 {
2045 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002046 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 goto doend;
2048 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002049 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002050 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2051 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
2053 errormsg = uc_fun_cmd();
2054 goto doend;
2055 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (ea.cmdidx == CMD_SIZE)
2058 {
2059 if (!ea.skip)
2060 {
2061 STRCPY(IObuff, _("E492: Not an editor command"));
2062 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002063 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002064 // If the modifier was parsed OK the error must be in the
2065 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002066 if (after_modifier != NULL)
2067 append_command(after_modifier);
2068 else
2069 append_command(*cmdlinep);
2070 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002071 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002072 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
2074 goto doend;
2075 }
2076
Bram Moolenaar958636c2014-10-21 20:01:58 +02002077 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2078 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002079#ifdef HAVE_EX_SCRIPT_NI
2080 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2081#endif
2082 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
2084#ifndef FEAT_EVAL
2085 /*
2086 * When the expression evaluation is disabled, recognize the ":if" and
2087 * ":endif" commands and ignore everything in between it.
2088 */
2089 if (ea.cmdidx == CMD_if)
2090 ++if_level;
2091 if (if_level)
2092 {
2093 if (ea.cmdidx == CMD_endif)
2094 --if_level;
2095 goto doend;
2096 }
2097
2098#endif
2099
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002100 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002101 if (*p == '!' && ea.cmdidx != CMD_substitute
2102 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
2104 ++p;
2105 ea.forceit = TRUE;
2106 }
2107 else
2108 ea.forceit = FALSE;
2109
2110/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002111 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002113 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002114 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
2116 if (!ea.skip)
2117 {
2118#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002119 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002121 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002122 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 goto doend;
2124 }
2125#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002126 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002127 {
2128 errormsg = _("E981: Command not allowed in rvim");
2129 goto doend;
2130 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002131 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002133 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002134 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 goto doend;
2136 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002137
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002138 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002140#ifdef FEAT_CMDWIN
2141 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2142 {
2143 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002144 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002145 goto doend;
2146 }
2147#endif
2148 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2149 {
2150 // Command not allowed when text is locked
2151 errormsg = _(get_text_locked_msg());
2152 goto doend;
2153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002155
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002156 // Disallow editing another buffer when "curbuf_lock" is set.
2157 // Do allow ":checktime" (it is postponed).
2158 // Do allow ":edit" (check for an argument later).
2159 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002160 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002161 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002162 && ea.cmdidx != CMD_edit
2163 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002164 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002165 && curbuf_locked())
2166 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002168 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002170 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002171 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 goto doend;
2173 }
2174 }
2175
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002176 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002178 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 goto doend;
2180 }
2181
2182 /*
2183 * Don't complain about the range if it is not used
2184 * (could happen if line_count is accidentally set to 0).
2185 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002186 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
2188 /*
2189 * If the range is backwards, ask for confirmation and, if given, swap
2190 * ea.line1 & ea.line2 so it's forwards again.
2191 * When global command is busy, don't ask, will fail below.
2192 */
2193 if (!global_busy && ea.line1 > ea.line2)
2194 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002195 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002197 if (sourcing || exmode_active)
2198 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002199 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002200 goto doend;
2201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (ask_yesno((char_u *)
2203 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002204 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
2206 lnum = ea.line1;
2207 ea.line1 = ea.line2;
2208 ea.line2 = lnum;
2209 }
2210 if ((errormsg = invalid_range(&ea)) != NULL)
2211 goto doend;
2212 }
2213
Bram Moolenaarb7316892019-05-01 18:08:42 +02002214 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2215 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 ea.line2 = 1;
2217
2218 correct_range(&ea);
2219
2220#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002221 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002222 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002224 // Put the first line at the start of a closed fold, put the last line
2225 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 (void)hasFolding(ea.line1, &ea.line1, NULL);
2227 (void)hasFolding(ea.line2, NULL, &ea.line2);
2228 }
2229#endif
2230
2231#ifdef FEAT_QUICKFIX
2232 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002233 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 * option here, so things like % get expanded.
2235 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002236 p = replace_makeprg(&ea, p, cmdlinep);
2237 if (p == NULL)
2238 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239#endif
2240
2241 /*
2242 * Skip to start of argument.
2243 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2244 */
2245 if (ea.cmdidx == CMD_bang)
2246 ea.arg = p;
2247 else
2248 ea.arg = skipwhite(p);
2249
Bram Moolenaar379fb762018-08-30 15:58:28 +02002250 // ":file" cannot be run with an argument when "curbuf_lock" is set
2251 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2252 goto doend;
2253
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 /*
2255 * Check for "++opt=val" argument.
2256 * Must be first, allow ":w ++enc=utf8 !cmd"
2257 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002258 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2260 if (getargopt(&ea) == FAIL && !ni)
2261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002262 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 goto doend;
2264 }
2265
2266 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2267 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002268 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002270 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002272 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 goto doend;
2274 }
2275 ea.arg = skipwhite(ea.arg + 1);
2276 ea.append = TRUE;
2277 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002278 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
2280 ++ea.arg;
2281 ea.usefilter = TRUE;
2282 }
2283 }
2284
2285 if (ea.cmdidx == CMD_read)
2286 {
2287 if (ea.forceit)
2288 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002289 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 ea.forceit = FALSE;
2291 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
2294 ++ea.arg;
2295 ea.usefilter = TRUE;
2296 }
2297 }
2298
2299 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2300 {
2301 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002302 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 ++ea.arg;
2305 ++ea.amount;
2306 }
2307 ea.arg = skipwhite(ea.arg);
2308 }
2309
2310 /*
2311 * Check for "+command" argument, before checking for next command.
2312 * Don't do this for ":read !cmd" and ":write !cmd".
2313 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002314 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2316
2317 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002318 * For commands that do not use '|' inside their argument: Check for '|' to
2319 * separate commands and '"' or '#' to start comments.
2320 *
2321 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002322 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002323 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002324 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002326 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2327 {
2328 separate_nextcmd(&ea);
2329 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002330 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002331 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002332 || ea.cmdidx == CMD_global
2333 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002334 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002335#ifdef FEAT_EVAL
2336 || inside_block(&ea)
2337#endif
2338 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
2340 for (p = ea.arg; *p; ++p)
2341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 // Remove one backslash before a newline, so that it's possible to
2343 // pass a newline to the shell and also a newline that is preceded
2344 // with a backslash. This makes it impossible to end a shell
2345 // command in a backslash, but that doesn't appear useful.
2346 // Halving the number of backslashes is incompatible with previous
2347 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002349 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 else if (*p == '\n')
2351 {
2352 ea.nextcmd = p + 1;
2353 *p = NUL;
2354 break;
2355 }
2356 }
2357 }
2358
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002359 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002360 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002362 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002363 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002365 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002366 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002367 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002369#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002370 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002371 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002373 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002374 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
2376#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002377 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2378 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002379 {
2380 ea.regname = *ea.arg++;
2381#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002382 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002383 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2384 {
kuuote08d7b1c2021-10-04 22:17:36 +01002385 if (!ea.skip)
2386 {
2387 set_expr_line(vim_strsave(ea.arg), &ea);
2388 did_set_expr_line = TRUE;
2389 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002390 ea.arg += STRLEN(ea.arg);
2391 }
2392#endif
2393 ea.arg = skipwhite(ea.arg);
2394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396
2397 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002398 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 * count, it's a buffer name.
2400 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002401 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002402 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002403 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002405 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002407 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002409 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 goto doend;
2411 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002412 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
2414 ea.line2 = n;
2415 if (ea.addr_count == 0)
2416 ea.addr_count = 1;
2417 }
2418 else
2419 {
2420 ea.line1 = ea.line2;
2421 ea.line2 += n - 1;
2422 ++ea.addr_count;
2423 /*
2424 * Be vi compatible: no error message for out of range.
2425 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002426 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 ea.line2 = curbuf->b_ml.ml_line_count;
2428 }
2429 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002430
2431 /*
2432 * Check for flags: 'l', 'p' and '#'.
2433 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002434 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002435 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002436 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2437 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002439 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002440 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 goto doend;
2442 }
2443
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002444 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002446 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 goto doend;
2448 }
2449
2450#ifdef FEAT_EVAL
2451 /*
2452 * Skip the command when it's not going to be executed.
2453 * The commands like :if, :endif, etc. always need to be executed.
2454 * Also make an exception for commands that handle a trailing command
2455 * themselves.
2456 */
2457 if (ea.skip)
2458 {
2459 switch (ea.cmdidx)
2460 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002461 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_while:
2463 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002464 case CMD_for:
2465 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case CMD_if:
2467 case CMD_elseif:
2468 case CMD_else:
2469 case CMD_endif:
2470 case CMD_try:
2471 case CMD_catch:
2472 case CMD_finally:
2473 case CMD_endtry:
2474 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002475 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 break;
2477
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002478 // Commands that handle '|' themselves. Check: A command should
2479 // either have the EX_TRLBAR flag, appear in this list or appear in
2480 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 case CMD_aboveleft:
2482 case CMD_and:
2483 case CMD_belowright:
2484 case CMD_botright:
2485 case CMD_browse:
2486 case CMD_call:
2487 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002488 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 case CMD_delfunction:
2490 case CMD_djump:
2491 case CMD_dlist:
2492 case CMD_dsearch:
2493 case CMD_dsplit:
2494 case CMD_echo:
2495 case CMD_echoerr:
2496 case CMD_echomsg:
2497 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002498 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002500 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002501 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 case CMD_help:
2503 case CMD_hide:
2504 case CMD_ijump:
2505 case CMD_ilist:
2506 case CMD_isearch:
2507 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002508 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_keepjumps:
2510 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002511 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 case CMD_leftabove:
2513 case CMD_let:
2514 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002515 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002516 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002518 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002519 case CMD_noautocmd:
2520 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 case CMD_perl:
2522 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002523 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002524 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002525 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 case CMD_return:
2527 case CMD_rightbelow:
2528 case CMD_ruby:
2529 case CMD_silent:
2530 case CMD_smagic:
2531 case CMD_snomagic:
2532 case CMD_substitute:
2533 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002534 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 case CMD_tcl:
2536 case CMD_throw:
2537 case CMD_tilde:
2538 case CMD_topleft:
2539 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002540 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002541 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 case CMD_verbose:
2543 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002544 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 break;
2546
2547 default: goto doend;
2548 }
2549 }
2550#endif
2551
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002552 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
2554 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2555 goto doend;
2556 }
2557
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 /*
2559 * Accept buffer name. Cannot be used at the same time with a buffer
2560 * number. Don't do this for a user command.
2561 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002562 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002563 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
2565 /*
2566 * :bdelete, :bwipeout and :bunload take several arguments, separated
2567 * by spaces: find next space (skipping over escaped characters).
2568 * The others take one argument: ignore trailing spaces.
2569 */
2570 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2571 || ea.cmdidx == CMD_bunload)
2572 p = skiptowhite_esc(ea.arg);
2573 else
2574 {
2575 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002576 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 --p;
2578 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002579 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002580 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002581 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 goto doend;
2583 ea.addr_count = 1;
2584 ea.arg = skipwhite(p);
2585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002587 // The :try command saves the emsg_silent flag, reset it here when
2588 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002589 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002590 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002591 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002592 if (emsg_silent < 0)
2593 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002594 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002595 }
2596
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002598 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
Bram Moolenaar958636c2014-10-21 20:01:58 +02002601 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
2603 /*
2604 * Execute a user-defined command.
2605 */
2606 do_ucmd(&ea);
2607 }
2608 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
2610 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002611 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 */
2613 ea.errmsg = NULL;
2614 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2615 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002616 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
2618
2619#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002621 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002622 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002623 && current_sctx.sc_sid > 0
2624 && ea.cmdidx != CMD_endif
2625 && (cstack->cs_idx < 0
2626 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002627 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 /*
2630 * If the command just executed called do_cmdline(), any throw or ":return"
2631 * or ":finish" encountered there must also check the cstack of the still
2632 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2633 * exception, or reanimate a returned function or finished script file and
2634 * return or finish it again.
2635 */
2636 if (need_rethrow)
2637 do_throw(cstack);
2638 else if (check_cstack)
2639 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002640 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002642 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 && current_func_returned())
2644 do_return(&ea, TRUE, FALSE, NULL);
2645 }
2646 need_rethrow = check_cstack = FALSE;
2647#endif
2648
2649doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002650 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002653 curwin->w_cursor.col = 0;
2654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
2656 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2657 {
2658 if (sourcing)
2659 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002660 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 {
2662 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002663 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002665 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667 emsg(errormsg);
2668 }
2669#ifdef FEAT_EVAL
2670 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002671 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2672 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002673
2674 if (did_set_expr_line)
2675 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676#endif
2677
Bram Moolenaare1004402020-10-24 20:49:43 +02002678 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002680 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002682 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 ea.nextcmd = NULL;
2684
2685#ifdef FEAT_EVAL
2686 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002687 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688#endif
2689
2690 return ea.nextcmd;
2691}
2692#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002693 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694#endif
2695
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002696static char ex_error_buf[MSG_BUF_LEN];
2697
2698/*
2699 * Return an error message with argument included.
2700 * Uses a static buffer, only the last error will be kept.
2701 * "msg" will be translated, caller should use N_().
2702 */
2703 char *
2704ex_errmsg(char *msg, char_u *arg)
2705{
2706 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2707 return ex_error_buf;
2708}
2709
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002711 * Check for an Ex command with optional tail.
2712 * If there is a match advance "pp" to the argument and return TRUE.
2713 * If "noparen" is TRUE do not recognize the command followed by "(".
2714 */
2715 static int
2716checkforcmd_opt(
2717 char_u **pp, // start of command
2718 char *cmd, // name of command
2719 int len, // required length
2720 int noparen)
2721{
2722 int i;
2723
2724 for (i = 0; cmd[i] != NUL; ++i)
2725 if (((char_u *)cmd)[i] != (*pp)[i])
2726 break;
2727 if (i >= len && !isalpha((*pp)[i])
2728 && (*pp)[i] != '_' && (!noparen || (*pp)[i] != '('))
2729 {
2730 *pp = skipwhite(*pp + i);
2731 return TRUE;
2732 }
2733 return FALSE;
2734}
2735
2736/*
2737 * Check for an Ex command with optional tail.
2738 * If there is a match advance "pp" to the argument and return TRUE.
2739 */
2740 int
2741checkforcmd(
2742 char_u **pp, // start of command
2743 char *cmd, // name of command
2744 int len) // required length
2745{
2746 return checkforcmd_opt(pp, cmd, len, FALSE);
2747}
2748
2749/*
2750 * Check for an Ex command with optional tail, not followed by "(".
2751 * If there is a match advance "pp" to the argument and return TRUE.
2752 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002753 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002754checkforcmd_noparen(
2755 char_u **pp, // start of command
2756 char *cmd, // name of command
2757 int len) // required length
2758{
2759 return checkforcmd_opt(pp, cmd, len, TRUE);
2760}
2761
2762/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 * Parse and skip over command modifiers:
2764 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002765 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002766 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002767 * When "skip_only" is TRUE the global variables are not changed, except for
2768 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002769 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2770 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002771 * Call apply_cmdmod() to get the side effects of the modifiers:
2772 * - Increment "sandbox" for ":sandbox"
2773 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002774 * - set msg_silent for ":silent"
2775 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002776 * Return FAIL when the command is not to be executed.
2777 * May set "errormsg" to an error message.
2778 */
2779 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002780parse_command_modifiers(
2781 exarg_T *eap,
2782 char **errormsg,
2783 cmdmod_T *cmod,
2784 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002785{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002786 char_u *p;
2787 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002788
Bram Moolenaare1004402020-10-24 20:49:43 +02002789 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002790
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002791 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002792 for (;;)
2793 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002794 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002795 {
2796 if (*eap->cmd == ':')
2797 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002799 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002801 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002802 if (*eap->cmd == NUL && exmode_active
2803 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2804 || getline_equal(eap->getline, eap->cookie, getexline))
2805 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2806 {
2807 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002808 if (!skip_only)
2809 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 }
2811
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002812 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002813 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002814 {
2815 // a comment ends at a NL
2816 if (eap->nextcmd == NULL)
2817 {
2818 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2819 if (eap->nextcmd != NULL)
2820 ++eap->nextcmd;
2821 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002823 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002824 if (*eap->cmd == NUL)
2825 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002826 if (!skip_only)
2827 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002828 return FAIL;
2829 }
2830
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002831 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002832
2833 // In Vim9 script a variable can shadow a command modifier:
2834 // verbose = 123
2835 // verbose += 123
2836 // silent! verbose = func()
2837 // verbose.member = 2
2838 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002839 // But not:
2840 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002841 if (in_vim9script())
2842 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002843 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002844
2845 for (s = p; ASCII_ISALPHA(*s); ++s)
2846 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002847 n = skipwhite(s);
2848 if (vim_strchr((char_u *)".=", *n) != NULL
2849 || *s == '['
2850 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002851 break;
2852 }
2853
Bram Moolenaareffed932018-08-14 13:38:17 +02002854 switch (*p)
2855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002856 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002857 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002858 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002859 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 continue;
2861
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002862 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002863 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002864 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 continue;
2866 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002867 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002868 {
2869#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002870 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002871#endif
2872 continue;
2873 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002874 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002876 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 continue;
2878
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002879 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 break;
2881#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002882 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002883#endif
2884 continue;
2885
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002886 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002887 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002888 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 continue;
2890 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002891 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002892 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002893 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 continue;
2895 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002896 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002898 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002899 continue;
2900 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002901 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002903 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002904 continue;
2905
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002906 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002907 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002908 char_u *reg_pat;
2909 char_u *nulp = NULL;
2910 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002911
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002912 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002913 || *p == NUL
2914 || (ends_excmd(*p)
2915#ifdef FEAT_EVAL
2916 // in ":filter #pat# cmd" # does not
2917 // start a comment
2918 && (!in_vim9script() || VIM_ISWHITE(p[1]))
2919#endif
2920 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 break;
2922 if (*p == '!')
2923 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002924 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002925 p = skipwhite(p + 1);
2926 if (*p == NUL || ends_excmd(*p))
2927 break;
2928 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002929#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002930 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002931 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002932 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002933#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002934 if (skip_only)
2935 p = skip_vimgrep_pat(p, NULL, NULL);
2936 else
2937 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002938 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2939 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002940 if (p == NULL || *p == NUL)
2941 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002942 if (!skip_only)
2943 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002944 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002945 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002946 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002947 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002948 // restore the character overwritten by NUL
2949 if (nulp != NULL)
2950 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002951 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002952 eap->cmd = p;
2953 continue;
2954 }
2955
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002956 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002957 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 || *p == NUL || ends_excmd(*p))
2959 break;
2960 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002961 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002962 continue;
2963
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002964 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002965 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002966 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002967 continue;
2968 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002969 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2970 {
2971 if (ends_excmd2(p, eap->cmd))
2972 {
2973 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02002974 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002975 return FAIL;
2976 }
2977 cmod->cmod_flags |= CMOD_LEGACY;
2978 continue;
2979 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002980
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002981 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002982 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002983 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002984 continue;
2985
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002986 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 continue;
2990 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002991 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002992 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002993 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 continue;
2995
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002996 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002998 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 continue;
3000
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003001 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003003 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 continue;
3005 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003006 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003008 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3010 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003011 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003012 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003013 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003014 }
3015 continue;
3016
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003017 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003018 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003019 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003020 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003021 long tabnr = get_address(eap, &eap->cmd,
3022 ADDR_TABS, eap->skip,
3023 skip_only, FALSE, 1);
3024 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003025 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003026 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003027 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003028 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3029 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003030 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003031 return FAIL;
3032 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003033 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003034 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003035 }
3036 eap->cmd = p;
3037 continue;
3038 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003039 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003040 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003041 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003042 continue;
3043
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003044 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003045 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003046 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003047 continue;
3048
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003049 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003050 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003051 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003052 continue;
3053 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003054 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003055 {
3056 if (ends_excmd2(p, eap->cmd))
3057 {
3058 *errormsg =
3059 _(e_vim9cmd_must_be_followed_by_command);
3060 return FAIL;
3061 }
3062 cmod->cmod_flags |= CMOD_VIM9CMD;
3063 continue;
3064 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003065 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003066 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003067 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003068 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003069 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003070 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003071 eap->cmd = p;
3072 continue;
3073 }
3074 break;
3075 }
3076
3077 return OK;
3078}
3079
3080/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003081 * Return TRUE if "cmod" has anything set.
3082 */
3083 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003084has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003085{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003086 return (cmod->cmod_flags != 0 && (!ignore_silent
3087 || (cmod->cmod_flags
3088 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003089 || cmod->cmod_split != 0
3090 || cmod->cmod_verbose != 0
3091 || cmod->cmod_tab != 0
3092 || cmod->cmod_filter_regmatch.regprog != NULL;
3093}
3094
3095/*
3096 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3097 */
3098 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003099cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003100{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003101 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003102 {
3103 emsg(_(e_misplaced_command_modifier));
3104 return TRUE;
3105 }
3106 return FALSE;
3107}
3108
3109/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003110 * Apply the command modifiers. Saves current state in "cmdmod", call
3111 * undo_cmdmod() later.
3112 */
3113 void
3114apply_cmdmod(cmdmod_T *cmod)
3115{
3116#ifdef HAVE_SANDBOX
3117 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3118 {
3119 ++sandbox;
3120 cmod->cmod_did_sandbox = TRUE;
3121 }
3122#endif
3123 if (cmod->cmod_verbose > 0)
3124 {
3125 if (cmod->cmod_verbose_save == 0)
3126 cmod->cmod_verbose_save = p_verbose + 1;
3127 p_verbose = cmod->cmod_verbose;
3128 }
3129
3130 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3131 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003132 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003133 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003134 cmod->cmod_save_msg_scroll = msg_scroll;
3135 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003136 if (cmod->cmod_flags & CMOD_SILENT)
3137 ++msg_silent;
3138 if (cmod->cmod_flags & CMOD_UNSILENT)
3139 msg_silent = 0;
3140
3141 if (cmod->cmod_flags & CMOD_ERRSILENT)
3142 {
3143 ++emsg_silent;
3144 ++cmod->cmod_did_esilent;
3145 }
3146
Bram Moolenaare1004402020-10-24 20:49:43 +02003147 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003148 {
3149 // Set 'eventignore' to "all".
3150 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003151 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003152 set_string_option_direct((char_u *)"ei", -1,
3153 (char_u *)"all", OPT_FREE, SID_NONE);
3154 }
3155}
3156
3157/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003158 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003159 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003160 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003161undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003162{
Bram Moolenaare1004402020-10-24 20:49:43 +02003163 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003164 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003165 p_verbose = cmod->cmod_verbose_save - 1;
3166 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003167 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003168
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003169#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003170 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003171 {
3172 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003173 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003174 }
3175#endif
3176
Bram Moolenaare1004402020-10-24 20:49:43 +02003177 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003179 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003180 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3181 OPT_FREE, SID_NONE);
3182 free_string_option(cmod->cmod_save_ei);
3183 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003184 }
3185
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003186 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003187
Bram Moolenaare1004402020-10-24 20:49:43 +02003188 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003189 {
3190 // messages could be enabled for a serious error, need to check if the
3191 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003192 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3193 msg_silent = cmod->cmod_save_msg_silent - 1;
3194 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003195 if (emsg_silent < 0)
3196 emsg_silent = 0;
3197 // Restore msg_scroll, it's set by file I/O commands, even when no
3198 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003199 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003200
3201 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3202 // somewhere in the line. Put it back in the first column.
3203 if (redirecting())
3204 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003205
Bram Moolenaare1004402020-10-24 20:49:43 +02003206 cmod->cmod_save_msg_silent = 0;
3207 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003208 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003209}
3210
3211/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003212 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003213 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003214 * Return FAIL and set "errormsg" or return OK.
3215 */
3216 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003217parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003218{
3219 int address_count = 1;
3220 linenr_T lnum;
3221
3222 // Repeat for all ',' or ';' separated addresses.
3223 for (;;)
3224 {
3225 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003226 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003227 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003228 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003229 eap->addr_count == 0, address_count++);
3230 if (eap->cmd == NULL) // error detected
3231 return FAIL;
3232 if (lnum == MAXLNUM)
3233 {
3234 if (*eap->cmd == '%') // '%' - all lines
3235 {
3236 ++eap->cmd;
3237 switch (eap->addr_type)
3238 {
3239 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003240 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003241 eap->line1 = 1;
3242 eap->line2 = curbuf->b_ml.ml_line_count;
3243 break;
3244 case ADDR_LOADED_BUFFERS:
3245 {
3246 buf_T *buf = firstbuf;
3247
3248 while (buf->b_next != NULL
3249 && buf->b_ml.ml_mfp == NULL)
3250 buf = buf->b_next;
3251 eap->line1 = buf->b_fnum;
3252 buf = lastbuf;
3253 while (buf->b_prev != NULL
3254 && buf->b_ml.ml_mfp == NULL)
3255 buf = buf->b_prev;
3256 eap->line2 = buf->b_fnum;
3257 break;
3258 }
3259 case ADDR_BUFFERS:
3260 eap->line1 = firstbuf->b_fnum;
3261 eap->line2 = lastbuf->b_fnum;
3262 break;
3263 case ADDR_WINDOWS:
3264 case ADDR_TABS:
3265 if (IS_USER_CMDIDX(eap->cmdidx))
3266 {
3267 eap->line1 = 1;
3268 eap->line2 = eap->addr_type == ADDR_WINDOWS
3269 ? LAST_WIN_NR : LAST_TAB_NR;
3270 }
3271 else
3272 {
3273 // there is no Vim command which uses '%' and
3274 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003275 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003276 return FAIL;
3277 }
3278 break;
3279 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003280 case ADDR_UNSIGNED:
3281 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003282 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003283 return FAIL;
3284 case ADDR_ARGUMENTS:
3285 if (ARGCOUNT == 0)
3286 eap->line1 = eap->line2 = 0;
3287 else
3288 {
3289 eap->line1 = 1;
3290 eap->line2 = ARGCOUNT;
3291 }
3292 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003293 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003294#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003295 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003296 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003297 if (eap->line2 == 0)
3298 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003299#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003300 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003301 case ADDR_NONE:
3302 // Will give an error later if a range is found.
3303 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003304 }
3305 ++eap->addr_count;
3306 }
3307 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3308 {
3309 pos_T *fp;
3310
3311 // '*' - visual area
3312 if (eap->addr_type != ADDR_LINES)
3313 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003314 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003315 return FAIL;
3316 }
3317
3318 ++eap->cmd;
3319 if (!eap->skip)
3320 {
3321 fp = getmark('<', FALSE);
3322 if (check_mark(fp) == FAIL)
3323 return FAIL;
3324 eap->line1 = fp->lnum;
3325 fp = getmark('>', FALSE);
3326 if (check_mark(fp) == FAIL)
3327 return FAIL;
3328 eap->line2 = fp->lnum;
3329 ++eap->addr_count;
3330 }
3331 }
3332 }
3333 else
3334 eap->line2 = lnum;
3335 eap->addr_count++;
3336
3337 if (*eap->cmd == ';')
3338 {
3339 if (!eap->skip)
3340 {
3341 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003342 // Don't leave the cursor on an illegal line or column, but do
3343 // accept zero as address, so 0;/PATTERN/ works correctly.
3344 if (eap->line2 > 0)
3345 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003346 }
3347 }
3348 else if (*eap->cmd != ',')
3349 break;
3350 ++eap->cmd;
3351 }
3352
3353 // One address given: set start and end lines.
3354 if (eap->addr_count == 1)
3355 {
3356 eap->line1 = eap->line2;
3357 // ... but only implicit: really no address given
3358 if (lnum == MAXLNUM)
3359 eap->addr_count = 0;
3360 }
3361 return OK;
3362}
3363
3364/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003365 * Append "cmd" to the error message in IObuff.
3366 * Takes care of limiting the length and handling 0xa0, which would be
3367 * invisible otherwise.
3368 */
3369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003370append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003371{
3372 char_u *s = cmd;
3373 char_u *d;
3374
3375 STRCAT(IObuff, ": ");
3376 d = IObuff + STRLEN(IObuff);
3377 while (*s != NUL && d - IObuff < IOSIZE - 7)
3378 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003379 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003380 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003381 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003382 STRCPY(d, "<a0>");
3383 d += 4;
3384 }
3385 else
3386 MB_COPY_CHAR(s, d);
3387 }
3388 *d = NUL;
3389}
3390
3391/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003392 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3393 * the name. Otherwise just return "start".
3394 */
3395 char_u *
3396skip_option_env_lead(char_u *start)
3397{
3398 char_u *name = start;
3399
3400 if (*start == '&')
3401 {
3402 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3403 name += 3;
3404 else
3405 name += 1;
3406 }
3407 else if (*start == '$')
3408 name += 1;
3409 return name;
3410}
3411
3412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003414 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003415 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003416 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 *
3418 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3419 * assignment without "let". Sets eap->cmdidx to the command while returning
3420 * "eap->cmd".
3421 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 * Returns NULL for an ambiguous user command.
3423 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 char_u *
3425find_ex_command(
3426 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003427 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003428 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003429 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
3431 int len;
3432 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003433 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003434#ifndef FEAT_EVAL
3435 int vim9 = FALSE;
3436#else
3437 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 /*
3440 * Recognize a Vim9 script function/method call and assignment:
3441 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3442 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003443 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003444 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003446 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003448 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003449 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003451 int oplen;
3452 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003453 char_u *swp;
3454
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003455 if (*eap->cmd == '&'
3456 || *eap->cmd == '$'
3457 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003458 && (valid_yank_reg(eap->cmd[1], FALSE)
3459 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003460 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003461 if (*eap->cmd == '&')
3462 {
3463 p = eap->cmd + 1;
3464 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3465 p += 2;
3466 p = to_name_end(p, FALSE);
3467 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003468 else if (*eap->cmd == '$')
3469 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003470 else
3471 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003472 if (ends_excmd(*skipwhite(p)))
3473 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003474 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3475 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003476 eap->cmdidx = CMD_eval;
3477 return eap->cmd;
3478 }
3479 // "&option" can be followed by "->" or "=", check below
3480 }
3481
3482 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003484 if (
3485 // "(..." is an expression.
3486 // "funcname(" is always a function call.
3487 *p == '('
3488 || (p == eap->cmd
3489 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003490 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003491 *eap->cmd == '{'
3492 // "'string'->func()" is an expression.
3493 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003494 // '"string"->func()' is an expression.
3495 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003496 // "g:varname" is an expression.
3497 || eap->cmd[1] == ':'
3498 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003499 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003500 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003501 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003502 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3503 {
3504 // "{" by itself is the start of a block.
3505 eap->cmdidx = CMD_block;
3506 return eap->cmd + 1;
3507 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003508 eap->cmdidx = CMD_eval;
3509 return eap->cmd;
3510 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003511
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003512 if (p != eap->cmd && (
3513 // "varname[]" is an expression.
3514 *p == '['
3515 // "varname.key" is an expression.
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02003516 || (*p == '.' && (ASCII_ISALPHA(p[1])
3517 || p[1] == '_'))))
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003518 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003519 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003520
3521 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003522 // Skip over the whole thing, it can be:
3523 // name.member = val
3524 // name[a : b] = val
3525 // name[idx] = val
3526 // name[idx].member = val
3527 // etc.
3528 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003529 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003530 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003531 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003532 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003533 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003534 || (after[0] == '.' && after[1] == '.'
3535 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003536 eap->cmdidx = CMD_var;
3537 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003538 --emsg_silent;
3539 return eap->cmd;
3540 }
3541
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003542 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3543 // an assignment.
3544 // If there is no line break inside the "[...]" then "p" is
3545 // advanced to after the "]" by to_name_const_end(): check if a "="
3546 // follows.
3547 // If "[...]" has a line break "p" still points at the "[" and it
3548 // can't be an assignment.
3549 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003550 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003551 char_u *eq;
3552
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003553 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003554 if (p == eap->cmd && *p == '[')
3555 {
3556 int count = 0;
3557 int semicolon = FALSE;
3558
3559 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3560 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003561 eq = p;
3562 if (eq != NULL)
3563 {
3564 eq = skipwhite(eq);
3565 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3566 ++eq;
3567 }
3568 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003569 {
3570 eap->cmdidx = CMD_eval;
3571 return eap->cmd;
3572 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003573 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003574 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003575 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003576 return eap->cmd;
3577 }
3578 }
3579
3580 // Recognize an assignment if we recognize the variable name:
3581 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003582 // "@r = expr"
3583 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003584 // "var = expr" where "var" is a variable name or we are skipping
3585 // (variable declaration might have been skipped).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003586 oplen = assignment_len(skipwhite(p), &heredoc);
3587 if (oplen > 0)
3588 {
3589 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3590 || *eap->cmd == '&'
3591 || *eap->cmd == '$'
3592 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003593 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003594 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003595 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003596 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003597 return eap->cmd;
3598 }
3599 }
3600
3601 // Recognize using a type for a w:, b:, t: or g: variable:
3602 // "w:varname: number = 123".
3603 if (eap->cmd[1] == ':' && *p == ':')
3604 {
3605 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003606 return eap->cmd;
3607 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003608 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003609
Bram Moolenaar7b829262021-10-13 15:04:34 +01003610 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3611 // an expression. A global/substitute/list command needs to use a
3612 // longer name.
3613 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3614 {
3615 eap->cmdidx = CMD_eval;
3616 return eap->cmd;
3617 }
3618
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003619 // If it is an ID it might be a variable with an operator on the next
3620 // line, if the variable exists it can't be an Ex command.
3621 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003622 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003623 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3624 {
3625 eap->cmdidx = CMD_eval;
3626 return eap->cmd;
3627 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003628
3629 // Check for "++nr" and "--nr".
3630 if (p == eap->cmd && p[0] == p[1] && (*p == '+' || *p == '-'))
3631 {
3632 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3633 return eap->cmd + 2;
3634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 }
3636#endif
3637
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 /*
3639 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003640 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003641 * - The 'k' command can directly be followed by any character.
3642 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003644 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003646 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 */
3648 p = eap->cmd;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003649 if (!vim9 && *p == 'k')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
3651 eap->cmdidx = CMD_k;
3652 ++p;
3653 }
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003654 else if (!vim9
3655 && p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003656 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3657 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 || p[1] == 'g'
3659 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3660 || p[1] == 'I'
3661 || (p[1] == 'r' && p[2] != 'e')))
3662 {
3663 eap->cmdidx = CMD_substitute;
3664 ++p;
3665 }
3666 else
3667 {
3668 while (ASCII_ISALPHA(*p))
3669 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003670 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003671 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003673 while (ASCII_ISALNUM(*p))
3674 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 }
3676 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3677 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003678 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 ++p;
3680 while (ASCII_ISALPHA(*p))
3681 ++p;
3682 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003683
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003684 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003685 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 ++p;
3687 len = (int)(p - eap->cmd);
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003688 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003689 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003690 // Check for ":dl", ":dell", etc. to ":deletel": that's
3691 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003692 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003693 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003694 break;
3695 if (i == len - 1)
3696 {
3697 --len;
3698 if (p[-1] == 'l')
3699 eap->flags |= EXFLAG_LIST;
3700 else
3701 eap->flags |= EXFLAG_PRINT;
3702 }
3703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003705 if (ASCII_ISLOWER(eap->cmd[0]))
3706 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003707 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003708 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003709
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003710 if (command_count != (int)CMD_SIZE)
3711 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003712 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003713 getout(1);
3714 }
3715
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003716 // Use a precomputed index for fast look-up in cmdnames[]
3717 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003718 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3719 if (ASCII_ISLOWER(c2))
3720 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3721 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003722 else if (ASCII_ISUPPER(eap->cmd[0]))
3723 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003725 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
3727 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3728 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3729 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3730 (size_t)len) == 0)
3731 {
3732#ifdef FEAT_EVAL
3733 if (full != NULL
3734 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3735 *full = TRUE;
3736#endif
3737 break;
3738 }
3739
Bram Moolenaar95388e32020-11-20 21:07:00 +01003740 // Not not recognize ":*" as the star command unless '*' is in
3741 // 'cpoptions'.
3742 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3743 p = eap->cmd;
3744
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003745 // Look for a user defined command as a last resort. Let ":Print" be
3746 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003747 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3748 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003750 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 while (ASCII_ISALNUM(*p))
3752 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003753 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003755 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 eap->cmdidx = CMD_SIZE;
3757 }
3758
Bram Moolenaar373863e2020-09-26 17:20:53 +02003759 // ":fina" means ":finally" for backwards compatibility.
3760 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3761 eap->cmdidx = CMD_finally;
3762
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003763#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003764 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003765 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003766 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003767 && (eap->cmdidx < 0 ||
3768 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003769 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003770 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3771
3772 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003773 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003774 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003775 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003776#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003777
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return p;
3779}
3780
3781#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003782static struct cmdmod
3783{
3784 char *name;
3785 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003786 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003787} cmdmods[] = {
3788 {"aboveleft", 3, FALSE},
3789 {"belowright", 3, FALSE},
3790 {"botright", 2, FALSE},
3791 {"browse", 3, FALSE},
3792 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003793 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003794 {"hide", 3, FALSE},
3795 {"keepalt", 5, FALSE},
3796 {"keepjumps", 5, FALSE},
3797 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003798 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003799 {"leftabove", 5, FALSE},
3800 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003801 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003802 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003803 {"rightbelow", 6, FALSE},
3804 {"sandbox", 3, FALSE},
3805 {"silent", 3, FALSE},
3806 {"tab", 3, TRUE},
3807 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003808 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003809 {"verbose", 4, TRUE},
3810 {"vertical", 4, FALSE},
3811};
3812
3813/*
3814 * Return length of a command modifier (including optional count).
3815 * Return zero when it's not a modifier.
3816 */
3817 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003818modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003819{
3820 int i, j;
3821 char_u *p = cmd;
3822
3823 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003824 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003825 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003826 {
3827 for (j = 0; p[j] != NUL; ++j)
3828 if (p[j] != cmdmods[i].name[j])
3829 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003830 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003831 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003832 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003833 }
3834 return 0;
3835}
3836
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837/*
3838 * Return > 0 if an Ex command "name" exists.
3839 * Return 2 if there is an exact match.
3840 * Return 3 if there is an ambiguous match.
3841 */
3842 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003843cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844{
3845 exarg_T ea;
3846 int full = FALSE;
3847 int i;
3848 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003849 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003851 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003852 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
3854 for (j = 0; name[j] != NUL; ++j)
3855 if (name[j] != cmdmods[i].name[j])
3856 break;
3857 if (name[j] == NUL && j >= cmdmods[i].minlen)
3858 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3859 }
3860
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003861 // Check built-in commands and user defined commands.
3862 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003863 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003866 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003868 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3869 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003870 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003871 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3873}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003874
3875/*
3876 * "fullcommand" function
3877 */
3878 void
3879f_fullcommand(typval_T *argvars, typval_T *rettv)
3880{
3881 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003882 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003883 char_u *p;
3884
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003885 rettv->v_type = VAR_STRING;
3886 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003887
3888 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3889 return;
3890
3891 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003892 if (name == NULL)
3893 return;
3894
3895 while (*name != NUL && *name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003896 name++;
3897 name = skip_range(name, TRUE, NULL);
3898
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003899 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3900 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003901 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003902 p = find_ex_command(&ea, NULL, NULL, NULL);
3903 if (p == NULL || ea.cmdidx == CMD_SIZE)
3904 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003905 if (in_vim9script())
3906 {
3907 int res;
3908
3909 ++emsg_silent;
3910 res = not_in_vim9(&ea);
3911 --emsg_silent;
3912
3913 if (res == FAIL)
3914 return;
3915 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003916
3917 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02003918 ? get_user_command_name(ea.useridx, ea.cmdidx)
3919 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003920}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921#endif
3922
Bram Moolenaard0190392019-08-23 21:17:35 +02003923 cmdidx_T
3924excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925{
Bram Moolenaard0190392019-08-23 21:17:35 +02003926 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
Bram Moolenaard0190392019-08-23 21:17:35 +02003928 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3929 idx = (cmdidx_T)((int)idx + 1))
3930 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 break;
3932
Bram Moolenaard0190392019-08-23 21:17:35 +02003933 return idx;
3934}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
Bram Moolenaard0190392019-08-23 21:17:35 +02003936 long
3937excmd_get_argt(cmdidx_T idx)
3938{
3939 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940}
3941
3942/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003943 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 *
3945 * Backslashed delimiters after / or ? will be skipped, and commands will
3946 * not be expanded between /'s and ?'s or after "'".
3947 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003948 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 * Returns the "cmd" pointer advanced to beyond the range.
3950 */
3951 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003952skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00003953 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003954 int skip_star, // skip "*" used for Visual range
3955 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00003957 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003958 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003960 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003962 if (*cmd == '\\')
3963 {
3964 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3965 ++cmd;
3966 else
3967 break;
3968 }
3969 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00003971 char_u *p = cmd;
3972
3973 // a quote is only valid at the start or after a separator
3974 while (p > cmd_start)
3975 {
3976 --p;
3977 if (!VIM_ISWHITE(*p))
3978 break;
3979 }
3980 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
3981 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 if (*++cmd == NUL && ctx != NULL)
3983 *ctx = EXPAND_NOTHING;
3984 }
3985 else if (*cmd == '/' || *cmd == '?')
3986 {
3987 delim = *cmd++;
3988 while (*cmd != NUL && *cmd != delim)
3989 if (*cmd++ == '\\' && *cmd != NUL)
3990 ++cmd;
3991 if (*cmd == NUL && ctx != NULL)
3992 *ctx = EXPAND_NOTHING;
3993 }
3994 if (*cmd != NUL)
3995 ++cmd;
3996 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003997
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003998 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003999 while (*cmd == ':')
4000 cmd = skipwhite(cmd + 1);
4001
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004002 // Skip "*" used for Visual range.
4003 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4004 cmd = skipwhite(cmd + 1);
4005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 return cmd;
4007}
4008
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004009 static void
4010addr_error(cmd_addr_T addr_type)
4011{
4012 if (addr_type == ADDR_NONE)
4013 emsg(_(e_norange));
4014 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004015 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004016}
4017
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004019 * Return the default address for an address type.
4020 */
4021 static linenr_T
4022default_address(exarg_T *eap)
4023{
4024 linenr_T lnum = 0;
4025
4026 switch (eap->addr_type)
4027 {
4028 case ADDR_LINES:
4029 case ADDR_OTHER:
4030 // Default is the cursor line number. Avoid using an invalid
4031 // line number though.
4032 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4033 lnum = curbuf->b_ml.ml_line_count;
4034 else
4035 lnum = curwin->w_cursor.lnum;
4036 break;
4037 case ADDR_WINDOWS:
4038 lnum = CURRENT_WIN_NR;
4039 break;
4040 case ADDR_ARGUMENTS:
4041 lnum = curwin->w_arg_idx + 1;
4042 if (lnum > ARGCOUNT)
4043 lnum = ARGCOUNT;
4044 break;
4045 case ADDR_LOADED_BUFFERS:
4046 case ADDR_BUFFERS:
4047 lnum = curbuf->b_fnum;
4048 break;
4049 case ADDR_TABS:
4050 lnum = CURRENT_TAB_NR;
4051 break;
4052 case ADDR_TABS_RELATIVE:
4053 case ADDR_UNSIGNED:
4054 lnum = 1;
4055 break;
4056 case ADDR_QUICKFIX:
4057#ifdef FEAT_QUICKFIX
4058 lnum = qf_get_cur_idx(eap);
4059#endif
4060 break;
4061 case ADDR_QUICKFIX_VALID:
4062#ifdef FEAT_QUICKFIX
4063 lnum = qf_get_cur_valid_idx(eap);
4064#endif
4065 break;
4066 case ADDR_NONE:
4067 // Will give an error later if a range is found.
4068 break;
4069 }
4070 return lnum;
4071}
4072
4073/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004074 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 *
4076 * Set ptr to the next character after the part that was interpreted.
4077 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004078 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 *
4080 * Return MAXLNUM when no Ex address was found.
4081 */
4082 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004083get_address(
4084 exarg_T *eap UNUSED,
4085 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004086 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004087 int skip, // only skip the address, don't use it
4088 int silent, // no errors or side effects
4089 int to_other_file, // flag: may jump to other file
4090 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 int c;
4093 int i;
4094 long n;
4095 char_u *cmd;
4096 pos_T pos;
4097 pos_T *fp;
4098 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004099 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
4101 cmd = skipwhite(*ptr);
4102 lnum = MAXLNUM;
4103 do
4104 {
4105 switch (*cmd)
4106 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004107 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004108 ++cmd;
4109 switch (addr_type)
4110 {
4111 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004112 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 lnum = curwin->w_cursor.lnum;
4114 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004115 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004116 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004117 break;
4118 case ADDR_ARGUMENTS:
4119 lnum = curwin->w_arg_idx + 1;
4120 break;
4121 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004122 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004123 lnum = curbuf->b_fnum;
4124 break;
4125 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004126 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004127 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004128 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004129 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004130 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004131 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004132 cmd = NULL;
4133 goto error;
4134 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004135 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004136#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004137 lnum = qf_get_cur_idx(eap);
4138#endif
4139 break;
4140 case ADDR_QUICKFIX_VALID:
4141#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004142 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004143#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004144 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004145 }
4146 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004148 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004149 ++cmd;
4150 switch (addr_type)
4151 {
4152 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004153 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 lnum = curbuf->b_ml.ml_line_count;
4155 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004156 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004157 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004158 break;
4159 case ADDR_ARGUMENTS:
4160 lnum = ARGCOUNT;
4161 break;
4162 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004163 buf = lastbuf;
4164 while (buf->b_ml.ml_mfp == NULL)
4165 {
4166 if (buf->b_prev == NULL)
4167 break;
4168 buf = buf->b_prev;
4169 }
4170 lnum = buf->b_fnum;
4171 break;
4172 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004173 lnum = lastbuf->b_fnum;
4174 break;
4175 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004176 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004177 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004178 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004179 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004180 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004181 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004182 cmd = NULL;
4183 goto error;
4184 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004185 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004186#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004187 lnum = qf_get_size(eap);
4188 if (lnum == 0)
4189 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004190#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004191 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004192 case ADDR_QUICKFIX_VALID:
4193#ifdef FEAT_QUICKFIX
4194 lnum = qf_get_valid_size(eap);
4195 if (lnum == 0)
4196 lnum = 1;
4197#endif
4198 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004199 }
4200 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004202 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004203 if (*++cmd == NUL)
4204 {
4205 cmd = NULL;
4206 goto error;
4207 }
4208 if (addr_type != ADDR_LINES)
4209 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004210 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004211 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004212 goto error;
4213 }
4214 if (skip)
4215 ++cmd;
4216 else
4217 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004218 // Only accept a mark in another file when it is
4219 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004220 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4221 ++cmd;
4222 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004223 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004224 lnum = curwin->w_cursor.lnum;
4225 else
4226 {
4227 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 cmd = NULL;
4230 goto error;
4231 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004232 lnum = fp->lnum;
4233 }
4234 }
4235 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
4237 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004238 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004239 c = *cmd++;
4240 if (addr_type != ADDR_LINES)
4241 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004242 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004243 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004244 goto error;
4245 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004246 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004247 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004248 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004249 if (*cmd == c)
4250 ++cmd;
4251 }
4252 else
4253 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004254 int flags;
4255
4256 pos = curwin->w_cursor; // save curwin->w_cursor
4257
4258 // When '/' or '?' follows another address, start from
4259 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004260 if (lnum > 0 && lnum != MAXLNUM)
4261 curwin->w_cursor.lnum =
4262 lnum > curbuf->b_ml.ml_line_count
4263 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004264
4265 // Start a forward search at the end of the line (unless
4266 // before the first line).
4267 // Start a backward search at the start of the line.
4268 // This makes sure we never match in the current
4269 // line, and can match anywhere in the
4270 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004271 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004272 curwin->w_cursor.col = MAXCOL;
4273 else
4274 curwin->w_cursor.col = 0;
4275 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004276 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004277 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004278 {
4279 curwin->w_cursor = pos;
4280 cmd = NULL;
4281 goto error;
4282 }
4283 lnum = curwin->w_cursor.lnum;
4284 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004285 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004286 cmd += searchcmdlen;
4287 }
4288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004290 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004291 ++cmd;
4292 if (addr_type != ADDR_LINES)
4293 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004294 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004295 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004296 goto error;
4297 }
4298 if (*cmd == '&')
4299 i = RE_SUBST;
4300 else if (*cmd == '?' || *cmd == '/')
4301 i = RE_SEARCH;
4302 else
4303 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004304 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004305 cmd = NULL;
4306 goto error;
4307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004309 if (!skip)
4310 {
4311 /*
4312 * When search follows another address, start from
4313 * there.
4314 */
4315 if (lnum != MAXLNUM)
4316 pos.lnum = lnum;
4317 else
4318 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004320 /*
4321 * Start the search just like for the above
4322 * do_search().
4323 */
4324 if (*cmd != '?')
4325 pos.col = MAXCOL;
4326 else
4327 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004328 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004329 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004330 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004331 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004332 lnum = pos.lnum;
4333 else
4334 {
4335 cmd = NULL;
4336 goto error;
4337 }
4338 }
4339 ++cmd;
4340 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341
4342 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004343 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004344 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
4346
4347 for (;;)
4348 {
4349 cmd = skipwhite(cmd);
4350 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4351 break;
4352
4353 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004354 {
4355 switch (addr_type)
4356 {
4357 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004358 case ADDR_OTHER:
4359 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004360 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004361 break;
4362 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004363 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004364 break;
4365 case ADDR_ARGUMENTS:
4366 lnum = curwin->w_arg_idx + 1;
4367 break;
4368 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004369 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004370 lnum = curbuf->b_fnum;
4371 break;
4372 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004373 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004374 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004375 case ADDR_TABS_RELATIVE:
4376 lnum = 1;
4377 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004378 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004379#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004380 lnum = qf_get_cur_idx(eap);
4381#endif
4382 break;
4383 case ADDR_QUICKFIX_VALID:
4384#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004385 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004386#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004387 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004388 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004389 case ADDR_UNSIGNED:
4390 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004391 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004392 }
4393 }
4394
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004396 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 else
4398 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004399 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 n = 1;
4401 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004404 if (n == MAXLNUM)
4405 {
4406 emsg(_(e_line_number_out_of_range));
4407 goto error;
4408 }
4409 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004410
4411 if (addr_type == ADDR_TABS_RELATIVE)
4412 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004413 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004414 cmd = NULL;
4415 goto error;
4416 }
4417 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004418 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004419 lnum = compute_buffer_local_count(
4420 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004422 {
4423#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004424 // Relative line addressing, need to adjust for folded lines
4425 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004426 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004427 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004428 (void)hasFolding(lnum, NULL, &lnum);
4429#endif
4430 if (i == '-')
4431 lnum -= n;
4432 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004433 {
4434 if (n >= LONG_MAX - lnum)
4435 {
4436 emsg(_(e_line_number_out_of_range));
4437 goto error;
4438 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004439 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004440 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443 } while (*cmd == '/' || *cmd == '?');
4444
4445error:
4446 *ptr = cmd;
4447 return lnum;
4448}
4449
4450/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004451 * Set eap->line1 and eap->line2 to the whole range.
4452 * Used for commands with the EX_DFLALL flag and no range given.
4453 */
4454 static void
4455address_default_all(exarg_T *eap)
4456{
4457 eap->line1 = 1;
4458 switch (eap->addr_type)
4459 {
4460 case ADDR_LINES:
4461 case ADDR_OTHER:
4462 eap->line2 = curbuf->b_ml.ml_line_count;
4463 break;
4464 case ADDR_LOADED_BUFFERS:
4465 {
4466 buf_T *buf = firstbuf;
4467
4468 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4469 buf = buf->b_next;
4470 eap->line1 = buf->b_fnum;
4471 buf = lastbuf;
4472 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4473 buf = buf->b_prev;
4474 eap->line2 = buf->b_fnum;
4475 }
4476 break;
4477 case ADDR_BUFFERS:
4478 eap->line1 = firstbuf->b_fnum;
4479 eap->line2 = lastbuf->b_fnum;
4480 break;
4481 case ADDR_WINDOWS:
4482 eap->line2 = LAST_WIN_NR;
4483 break;
4484 case ADDR_TABS:
4485 eap->line2 = LAST_TAB_NR;
4486 break;
4487 case ADDR_TABS_RELATIVE:
4488 eap->line2 = 1;
4489 break;
4490 case ADDR_ARGUMENTS:
4491 if (ARGCOUNT == 0)
4492 eap->line1 = eap->line2 = 0;
4493 else
4494 eap->line2 = ARGCOUNT;
4495 break;
4496 case ADDR_QUICKFIX_VALID:
4497#ifdef FEAT_QUICKFIX
4498 eap->line2 = qf_get_valid_size(eap);
4499 if (eap->line2 == 0)
4500 eap->line2 = 1;
4501#endif
4502 break;
4503 case ADDR_NONE:
4504 case ADDR_UNSIGNED:
4505 case ADDR_QUICKFIX:
4506 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4507 break;
4508 }
4509}
4510
4511
4512/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004513 * Get flags from an Ex command argument.
4514 */
4515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004516get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004517{
4518 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4519 {
4520 if (*eap->arg == 'l')
4521 eap->flags |= EXFLAG_LIST;
4522 else if (*eap->arg == 'p')
4523 eap->flags |= EXFLAG_PRINT;
4524 else
4525 eap->flags |= EXFLAG_NR;
4526 eap->arg = skipwhite(eap->arg + 1);
4527 }
4528}
4529
4530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 * Function called for command which is Not Implemented. NI!
4532 */
4533 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004534ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535{
4536 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004537 eap->errmsg =
4538 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539}
4540
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004541#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542/*
4543 * Function called for script command which is Not Implemented. NI!
4544 * Skips over ":perl <<EOF" constructs.
4545 */
4546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004547ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548{
4549 if (!eap->skip)
4550 ex_ni(eap);
4551 else
4552 vim_free(script_get(eap, eap->arg));
4553}
4554#endif
4555
4556/*
4557 * Check range in Ex command for validity.
4558 * Return NULL when valid, error message when invalid.
4559 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004560 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004561invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004563 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004564
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 if ( eap->line1 < 0
4566 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004567 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004568 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004569
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004570 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004571 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004572 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004573 {
4574 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004575 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004576#ifdef FEAT_DIFF
4577 + (eap->cmdidx == CMD_diffget)
4578#endif
4579 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004580 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004581 break;
4582 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004583 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004584 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004585 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004586 break;
4587 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004588 // Only a boundary check, not whether the buffers actually
4589 // exist.
4590 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004591 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004592 break;
4593 case ADDR_LOADED_BUFFERS:
4594 buf = firstbuf;
4595 while (buf->b_ml.ml_mfp == NULL)
4596 {
4597 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004598 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004599 buf = buf->b_next;
4600 }
4601 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004602 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004603 buf = lastbuf;
4604 while (buf->b_ml.ml_mfp == NULL)
4605 {
4606 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004607 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004608 buf = buf->b_prev;
4609 }
4610 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004611 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004612 break;
4613 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004614 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004615 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004616 break;
4617 case ADDR_TABS:
4618 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004619 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004620 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004621 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004622 case ADDR_OTHER:
4623 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004624 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004625 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004626#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004627 // No error for value that is too big, will use the last entry.
4628 if (eap->line2 <= 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004629 return _(e_invalid_range);
Bram Moolenaare906c502015-09-09 21:10:39 +02004630#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004631 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004632 case ADDR_QUICKFIX_VALID:
4633#ifdef FEAT_QUICKFIX
4634 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4635 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004636 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004637#endif
4638 break;
4639 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004640 case ADDR_NONE:
4641 // Will give an error elsewhere.
4642 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004643 }
4644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 return NULL;
4646}
4647
4648/*
4649 * Correct the range for zero line number, if required.
4650 */
4651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004652correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004654 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
4656 if (eap->line1 == 0)
4657 eap->line1 = 1;
4658 if (eap->line2 == 0)
4659 eap->line2 = 1;
4660 }
4661}
4662
Bram Moolenaar748bf032005-02-02 23:04:36 +00004663#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004664/*
4665 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4666 * pattern. Otherwise return eap->arg.
4667 */
4668 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004669skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004670{
4671 char_u *p = eap->arg;
4672
Bram Moolenaara37420f2006-02-04 22:37:47 +00004673 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4674 || eap->cmdidx == CMD_vimgrepadd
4675 || eap->cmdidx == CMD_lvimgrepadd
4676 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004677 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004678 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004679 if (p == NULL)
4680 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004681 }
4682 return p;
4683}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004684
4685/*
4686 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4687 * in the command line, so that things like % get expanded.
4688 */
4689 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004690replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004691{
4692 char_u *new_cmdline;
4693 char_u *program;
4694 char_u *pos;
4695 char_u *ptr;
4696 int len;
4697 int i;
4698
4699 /*
4700 * Don't do it when ":vimgrep" is used for ":grep".
4701 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004702 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4703 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4704 || eap->cmdidx == CMD_grepadd
4705 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004706 && !grep_internal(eap->cmdidx))
4707 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004708 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4709 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004710 {
4711 if (*curbuf->b_p_gp == NUL)
4712 program = p_gp;
4713 else
4714 program = curbuf->b_p_gp;
4715 }
4716 else
4717 {
4718 if (*curbuf->b_p_mp == NUL)
4719 program = p_mp;
4720 else
4721 program = curbuf->b_p_mp;
4722 }
4723
4724 p = skipwhite(p);
4725
4726 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4727 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004728 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004729 i = 1;
4730 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4731 ++i;
4732 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004733 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004734 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004735 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004736 ptr = new_cmdline;
4737 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4738 {
4739 i = (int)(pos - program);
4740 STRNCPY(ptr, program, i);
4741 STRCPY(ptr += i, p);
4742 ptr += len;
4743 program = pos + 2;
4744 }
4745 STRCPY(ptr, program);
4746 }
4747 else
4748 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004749 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004750 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004751 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004752 STRCPY(new_cmdline, program);
4753 STRCAT(new_cmdline, " ");
4754 STRCAT(new_cmdline, p);
4755 }
4756 msg_make(p);
4757
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004758 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004759 vim_free(*cmdlinep);
4760 *cmdlinep = new_cmdline;
4761 p = new_cmdline;
4762 }
4763 return p;
4764}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004765#endif
4766
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767/*
4768 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004769 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 * Return FAIL for failure, OK otherwise.
4771 */
4772 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004773expand_filename(
4774 exarg_T *eap,
4775 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004776 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004778 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 char_u *repl;
4780 int srclen;
4781 char_u *p;
4782 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004783 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
Bram Moolenaar748bf032005-02-02 23:04:36 +00004785#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004786 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004787 p = skip_grep_pat(eap);
4788#else
4789 p = eap->arg;
4790#endif
4791
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 /*
4793 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4794 * the file name contains a wildcard it should not cause expanding.
4795 * (it will be expanded anyway if there is a wildcard before replacing).
4796 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004797 has_wildcards = mch_has_wildcard(p);
4798 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004800#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004801 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004802 if (p[0] == '`' && p[1] == '=')
4803 {
4804 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004805 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004806 if (*p == '`')
4807 ++p;
4808 continue;
4809 }
4810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 /*
4812 * Quick check if this cannot be the start of a special string.
4813 * Also removes backslash before '%', '#' and '<'.
4814 */
4815 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4816 {
4817 ++p;
4818 continue;
4819 }
4820
4821 /*
4822 * Try to find a match at this position.
4823 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004824 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4825 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004826 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004828 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
4830 p += srclen;
4831 continue;
4832 }
4833
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004834 // Wildcards won't be expanded below, the replacement is taken
4835 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004836 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4837 {
4838 char_u *l = repl;
4839
4840 repl = expand_env_save(repl);
4841 vim_free(l);
4842 }
4843
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004844 // Need to escape white space et al. with a backslash.
4845 // Don't do this for:
4846 // - replacement that already has been escaped: "##"
4847 // - shell commands (may have to use quotes instead).
4848 // - non-unix systems when there is a single argument (spaces don't
4849 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004851 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 && eap->cmdidx != CMD_grep
4854 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004855 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004856 && eap->cmdidx != CMD_lgrep
4857 && eap->cmdidx != CMD_lgrepadd
4858 && eap->cmdidx != CMD_lmake
4859 && eap->cmdidx != CMD_make
4860 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004862 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863#endif
4864 )
4865 {
4866 char_u *l;
4867#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004868 // Don't escape a backslash here, because rem_backslash() doesn't
4869 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 static char_u *nobslash = (char_u *)" \t\"|";
4871# define ESCAPE_CHARS nobslash
4872#else
4873# define ESCAPE_CHARS escape_chars
4874#endif
4875
4876 for (l = repl; *l; ++l)
4877 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4878 {
4879 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4880 if (l != NULL)
4881 {
4882 vim_free(repl);
4883 repl = l;
4884 }
4885 break;
4886 }
4887 }
4888
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004889 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004890 if ((eap->usefilter || eap->cmdidx == CMD_bang
4891 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004892 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
4894 char_u *l;
4895
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004896 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 if (l != NULL)
4898 {
4899 vim_free(repl);
4900 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 }
4903
4904 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4905 vim_free(repl);
4906 if (p == NULL)
4907 return FAIL;
4908 }
4909
4910 /*
4911 * One file argument: Expand wildcards.
4912 * Don't do this with ":r !command" or ":w !command".
4913 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004914 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
4916 /*
4917 * May do this twice:
4918 * 1. Replace environment variables.
4919 * 2. Replace any other wildcards, remove backslashes.
4920 */
4921 for (n = 1; n <= 2; ++n)
4922 {
4923 if (n == 2)
4924 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 /*
4926 * Halve the number of backslashes (this is Vi compatible).
4927 * For Unix and OS/2, when wildcards are expanded, this is
4928 * done by ExpandOne() below.
4929 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004930#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (!has_wildcards)
4932#endif
4933 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
4935
4936 if (has_wildcards)
4937 {
4938 if (n == 1)
4939 {
4940 /*
4941 * First loop: May expand environment variables. This
4942 * can be done much faster with expand_env() than with
4943 * something else (e.g., calling a shell).
4944 * After expanding environment variables, check again
4945 * if there are still wildcards present.
4946 */
4947 if (vim_strchr(eap->arg, '$') != NULL
4948 || vim_strchr(eap->arg, '~') != NULL)
4949 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004950 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004951 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 has_wildcards = mch_has_wildcard(NameBuff);
4953 p = NameBuff;
4954 }
4955 else
4956 p = NULL;
4957 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004958 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
4960 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004961 int options = WILD_LIST_NOTFOUND
4962 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
4964 ExpandInit(&xpc);
4965 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004966 if (p_wic)
4967 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004969 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 if (p == NULL)
4971 return FAIL;
4972 }
4973 if (p != NULL)
4974 {
4975 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4976 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004977 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 vim_free(p);
4979 }
4980 }
4981 }
4982 }
4983 return OK;
4984}
4985
4986/*
4987 * Replace part of the command line, keeping eap->cmd, eap->arg and
4988 * eap->nextcmd correct.
4989 * "src" points to the part that is to be replaced, of length "srclen".
4990 * "repl" is the replacement string.
4991 * Returns a pointer to the character after the replaced string.
4992 * Returns NULL for failure.
4993 */
4994 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004995repl_cmdline(
4996 exarg_T *eap,
4997 char_u *src,
4998 int srclen,
4999 char_u *repl,
5000 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
5002 int len;
5003 int i;
5004 char_u *new_cmdline;
5005
5006 /*
5007 * The new command line is build in new_cmdline[].
5008 * First allocate it.
5009 * Careful: a "+cmd" argument may have been NUL terminated.
5010 */
5011 len = (int)STRLEN(repl);
5012 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005013 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005014 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005015 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005016 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018 /*
5019 * Copy the stuff before the expanded part.
5020 * Copy the expanded stuff.
5021 * Copy what came after the expanded part.
5022 * Copy the next commands, if there are any.
5023 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005024 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005026
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005028 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005030 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005032 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
5034 i = (int)STRLEN(new_cmdline) + 1;
5035 STRCPY(new_cmdline + i, eap->nextcmd);
5036 eap->nextcmd = new_cmdline + i;
5037 }
5038 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5039 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5040 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5041 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5042 vim_free(*cmdlinep);
5043 *cmdlinep = new_cmdline;
5044
5045 return src;
5046}
5047
5048/*
5049 * Check for '|' to separate commands and '"' to start comments.
5050 */
5051 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005052separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053{
5054 char_u *p;
5055
Bram Moolenaar86b68352004-12-27 21:59:20 +00005056#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005057 p = skip_grep_pat(eap);
5058#else
5059 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005060#endif
5061
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005062 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 if (*p == Ctrl_V)
5065 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005066 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005067 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005069 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005070 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005071 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005074
5075#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005076 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005077 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005078 {
5079 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005080 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005081 if (*p == NUL) // stop at NUL after CTRL-V
5082 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005083 }
5084#endif
5085
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005086 // Check for '"': start of comment or '|': next command
5087 // :@" and :*" do not start a comment!
5088 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005089 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005090#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005091 && !in_vim9script()
5092#endif
5093 && !(eap->argt & EX_NOTRLCOM)
5094 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5095 || p != eap->arg)
5096 && (eap->cmdidx != CMD_redir
5097 || p != eap->arg + 1 || p[-1] != '@'))
5098#ifdef FEAT_EVAL
5099 || (*p == '#'
5100 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005101 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005102 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 || *p == '|' || *p == '\n')
5105 {
5106 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005107 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 * AND 'b' is present in 'cpoptions'.
5109 */
5110 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005111 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005113 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 --p;
5115 }
5116 else
5117 {
5118 eap->nextcmd = check_nextcmd(p);
5119 *p = NUL;
5120 break;
5121 }
5122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005124
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005125 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 del_trailing_spaces(eap->arg);
5127}
5128
5129/*
5130 * get + command from ex argument
5131 */
5132 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005133getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134{
5135 char_u *arg = *argp;
5136 char_u *command = NULL;
5137
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005138 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
5140 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005141 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 command = dollar_command;
5143 else
5144 {
5145 command = arg;
5146 arg = skip_cmd_arg(command, TRUE);
5147 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005148 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005151 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 *argp = arg;
5153 }
5154 return command;
5155}
5156
5157/*
5158 * Find end of "+command" argument. Skip over "\ " and "\\".
5159 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005160 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005161skip_cmd_arg(
5162 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005163 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164{
5165 while (*p && !vim_isspace(*p))
5166 {
5167 if (*p == '\\' && p[1] != NUL)
5168 {
5169 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005170 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 else
5172 ++p;
5173 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005174 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
5176 return p;
5177}
5178
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005179 int
5180get_bad_opt(char_u *p, exarg_T *eap)
5181{
5182 if (STRICMP(p, "keep") == 0)
5183 eap->bad_char = BAD_KEEP;
5184 else if (STRICMP(p, "drop") == 0)
5185 eap->bad_char = BAD_DROP;
5186 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5187 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005188 else
5189 return FAIL;
5190 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005191}
5192
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193/*
5194 * Get "++opt=arg" argument.
5195 * Return FAIL or OK.
5196 */
5197 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005198getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
5200 char_u *arg = eap->arg + 2;
5201 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005202 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005205 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5207 {
5208 if (*arg == 'n')
5209 {
5210 arg += 2;
5211 eap->force_bin = FORCE_NOBIN;
5212 }
5213 else
5214 eap->force_bin = FORCE_BIN;
5215 if (!checkforcmd(&arg, "binary", 3))
5216 return FAIL;
5217 eap->arg = skipwhite(arg);
5218 return OK;
5219 }
5220
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005221 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005222 if (STRNCMP(arg, "edit", 4) == 0)
5223 {
5224 eap->read_edit = TRUE;
5225 eap->arg = skipwhite(arg + 4);
5226 return OK;
5227 }
5228
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 if (STRNCMP(arg, "ff", 2) == 0)
5230 {
5231 arg += 2;
5232 pp = &eap->force_ff;
5233 }
5234 else if (STRNCMP(arg, "fileformat", 10) == 0)
5235 {
5236 arg += 10;
5237 pp = &eap->force_ff;
5238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 else if (STRNCMP(arg, "enc", 3) == 0)
5240 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005241 if (STRNCMP(arg, "encoding", 8) == 0)
5242 arg += 8;
5243 else
5244 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 pp = &eap->force_enc;
5246 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005247 else if (STRNCMP(arg, "bad", 3) == 0)
5248 {
5249 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005250 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
5253 if (pp == NULL || *arg != '=')
5254 return FAIL;
5255
5256 ++arg;
5257 *pp = (int)(arg - eap->cmd);
5258 arg = skip_cmd_arg(arg, FALSE);
5259 eap->arg = skipwhite(arg);
5260 *arg = NUL;
5261
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 if (pp == &eap->force_ff)
5263 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5265 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005266 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005268 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005270 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5272 *p = TOLOWER_ASC(*p);
5273 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005274 else
5275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005276 // Check ++bad= argument. Must be a single-byte character, "keep" or
5277 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005278 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005279 return FAIL;
5280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
5282 return OK;
5283}
5284
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005286ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
5288 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005289 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 * directory for security reasons.
5291 */
5292 if (secure)
5293 {
5294 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005295 eap->errmsg =
5296 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
5298 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005299 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 else
5301 do_augroup(eap->arg, eap->forceit);
5302}
5303
5304/*
5305 * ":doautocmd": Apply the automatic commands to the current buffer.
5306 */
5307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005308ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005310 char_u *arg = eap->arg;
5311 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005312 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005313
Bram Moolenaar1610d052016-06-09 22:53:01 +02005314 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005315 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005316 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005317 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320/*
5321 * :[N]bunload[!] [N] [bufname] unload buffer
5322 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5323 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5324 */
5325 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005326ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005328 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005329 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 eap->errmsg = do_bufdel(
5331 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5332 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5333 : DOBUF_UNLOAD, eap->arg,
5334 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5335}
5336
5337/*
5338 * :[N]buffer [N] to buffer N
5339 * :[N]sbuffer [N] to buffer N
5340 */
5341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005342ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005344 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005345 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005347 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 else
5349 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005350 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5352 else
5353 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005354 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005355 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357}
5358
5359/*
5360 * :[N]bmodified [N] to next mod. buffer
5361 * :[N]sbmodified [N] to next mod. buffer
5362 */
5363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005364ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
5366 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005367 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005368 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369}
5370
5371/*
5372 * :[N]bnext [N] to next buffer
5373 * :[N]sbnext [N] split and to next buffer
5374 */
5375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005376ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005378 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005379 return;
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005382 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005383 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384}
5385
5386/*
5387 * :[N]bNext [N] to previous buffer
5388 * :[N]bprevious [N] to previous buffer
5389 * :[N]sbNext [N] split and to previous buffer
5390 * :[N]sbprevious [N] split and to previous buffer
5391 */
5392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005393ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005395 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005396 return;
5397
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005399 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005400 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401}
5402
5403/*
5404 * :brewind to first buffer
5405 * :bfirst to first buffer
5406 * :sbrewind split and to first buffer
5407 * :sbfirst split and to first buffer
5408 */
5409 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005410ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005412 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005413 return;
5414
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005416 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005417 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418}
5419
5420/*
5421 * :blast to last buffer
5422 * :sblast split and to last buffer
5423 */
5424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005425ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005427 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005428 return;
5429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005431 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005432 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434
Bram Moolenaar7a092242020-04-16 22:10:49 +02005435/*
5436 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005437 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005440ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005442 int comment_char = '"';
5443
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005444#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005445 if (in_vim9script())
5446 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005447#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005448 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005449}
5450
5451/*
5452 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5453 * to "cmd_start" or has a white space character before it.
5454 */
5455 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005456ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005457{
5458 int c = *cmd;
5459
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005460 if (c == NUL || c == '|' || c == '\n')
5461 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005462#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005463 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005464 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5465 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005466 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005467#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005468 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469}
5470
5471#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5472 || defined(PROTO)
5473/*
5474 * Return the next command, after the first '|' or '\n'.
5475 * Return NULL if not found.
5476 */
5477 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005478find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
5480 while (*p != '|' && *p != '\n')
5481 {
5482 if (*p == NUL)
5483 return NULL;
5484 ++p;
5485 }
5486 return (p + 1);
5487}
5488#endif
5489
5490/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005491 * Check if *p is a separator between Ex commands, skipping over white space.
5492 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 */
5494 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005495check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005497 char_u *s = skipwhite(p);
5498
5499 if (*s == '|' || *s == '\n')
5500 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 else
5502 return NULL;
5503}
5504
5505/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005506 * If "eap->nextcmd" is not set, check for a next command at "p".
5507 */
5508 void
5509set_nextcmd(exarg_T *eap, char_u *arg)
5510{
5511 char_u *p = check_nextcmd(arg);
5512
5513 if (eap->nextcmd == NULL)
5514 eap->nextcmd = p;
5515 else if (p != NULL)
5516 // cannot use "| command" inside a {} block
5517 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5518}
5519
5520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 * - if there are more files to edit
5522 * - and this is the last window
5523 * - and forceit not used
5524 * - and not repeated twice on a row
5525 * return FAIL and give error message if 'message' TRUE
5526 * return OK otherwise
5527 */
5528 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005530 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005531 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5534
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005535 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005536 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
5538 if (message)
5539 {
5540#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005541 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5542 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005544 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005546 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5547 NGETTEXT("%d more file to edit. Quit anyway?",
5548 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5550 return OK;
5551 return FAIL;
5552 }
5553#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005554 semsg(NGETTEXT("E173: %d more file to edit",
5555 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005556 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558 return FAIL;
5559 }
5560 return OK;
5561}
5562
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563/*
5564 * Function given to ExpandGeneric() to obtain the list of command names.
5565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005567get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
5569 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005570 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 return cmdnames[idx].cmd_name;
5572}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005575ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576{
Bram Moolenaare6850792010-05-14 15:28:44 +02005577 if (*eap->arg == NUL)
5578 {
5579#ifdef FEAT_EVAL
5580 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5581 char_u *p = NULL;
5582
5583 if (expr != NULL)
5584 {
5585 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005586 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005587 --emsg_off;
5588 vim_free(expr);
5589 }
5590 if (p != NULL)
5591 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005592 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005593 vim_free(p);
5594 }
5595 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005596 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005597#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005598 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005599#endif
5600 }
5601 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005602 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005603
5604#ifdef FEAT_VTP
5605 else if (has_vtp_working())
5606 {
5607 // background color change requires clear + redraw
5608 update_screen(CLEAR);
5609 redrawcmd();
5610 }
5611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612}
5613
5614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005615ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
5617 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005618 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 do_highlight(eap->arg, eap->forceit, FALSE);
5620}
5621
5622
5623/*
5624 * Call this function if we thought we were going to exit, but we won't
5625 * (because of an error). May need to restore the terminal mode.
5626 */
5627 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005628not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
5630 exiting = FALSE;
5631 settmode(TMODE_RAW);
5632}
5633
Bram Moolenaar3552e742021-05-29 12:21:58 +02005634 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005635before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5636{
5637 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5638
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005639 // Bail out when autocommands closed the window.
5640 // Refuse to quit when the buffer in the last window is being closed (can
5641 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005642 if (!win_valid(wp)
5643 || curbuf_locked()
5644 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5645 return TRUE;
5646
5647 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5648 {
5649 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005650 // Refuse to quit when locked or when the window was closed or the
5651 // buffer in the last window is being closed (can only happen in
5652 // autocommands).
5653 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005654 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5655 return TRUE;
5656 }
5657
5658 return FALSE;
5659}
5660
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005662 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005663 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005664 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005666 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005667ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005669 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671#ifdef FEAT_CMDWIN
5672 if (cmdwin_type != 0)
5673 {
5674 cmdwin_result = Ctrl_C;
5675 return;
5676 }
5677#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005678 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005679 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005680 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005681 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005682 return;
5683 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005684 if (eap->addr_count > 0)
5685 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005686 int wnr = eap->line2;
5687
5688 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5689 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005690 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005691 }
5692 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005693 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005695 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005696 if (curbuf_locked())
5697 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005699 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005700 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005701 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
5703#ifdef FEAT_NETBEANS_INTG
5704 netbeansForcedQuit = eap->forceit;
5705#endif
5706
5707 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005708 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 */
5710 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5711 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005712 if ((!buf_hide(wp->w_buffer)
5713 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005714 | (eap->forceit ? CCGD_FORCEIT : 0)
5715 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005717 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 not_exiting();
5720 }
5721 else
5722 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005723 // quit last window
5724 // Note: only_one_window() returns true, even so a help window is
5725 // still open. In that case only quit, if no address has been
5726 // specified. Example:
5727 // :h|wincmd w|1q - don't quit
5728 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005729 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005731 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005732#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005734#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005735 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005736 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738}
5739
5740/*
5741 * ":cquit".
5742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005744ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005746 // this does not always pass on the exit code to the Manx compiler. why?
5747 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748}
5749
5750/*
5751 * ":qall": try to quit all windows
5752 */
5753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005754ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
5756# ifdef FEAT_CMDWIN
5757 if (cmdwin_type != 0)
5758 {
5759 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005760 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 else
5762 cmdwin_result = K_XF2;
5763 return;
5764 }
5765# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005767 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005768 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005769 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005770 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005771 return;
5772 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005773
5774 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005775 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005776
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005778 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 getout(0);
5780 not_exiting();
5781}
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783/*
5784 * ":close": close current window, unless it is the last one
5785 */
5786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005787ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005789 win_T *win;
5790 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005791#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005793 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005795#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005796 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005797 {
5798 if (eap->addr_count == 0)
5799 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005800 else
5801 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005802 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005803 {
5804 winnr++;
5805 if (winnr == eap->line2)
5806 break;
5807 }
5808 if (win == NULL)
5809 win = lastwin;
5810 ex_win_close(eap->forceit, win, NULL);
5811 }
5812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813}
5814
Bram Moolenaar4033c552017-09-16 20:54:51 +02005815#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005816/*
5817 * ":pclose": Close any preview window.
5818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005820ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005821{
5822 win_T *win;
5823
Bram Moolenaar79648732019-07-18 21:43:07 +02005824 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005825 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005826 if (win->w_p_pvw)
5827 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005828 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005829 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005830 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005831# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005832 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005833 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005834# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005835}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005836#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005837
Bram Moolenaarf740b292006-02-16 22:11:02 +00005838/*
5839 * Close window "win" and take care of handling closing the last window for a
5840 * modified buffer.
5841 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005843ex_win_close(
5844 int forceit,
5845 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005846 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
5848 int need_hide;
5849 buf_T *buf = win->w_buffer;
5850
Bram Moolenaarcf844172020-06-26 19:44:06 +02005851 // Never close the autocommand window.
5852 if (win == aucmd_win)
5853 {
5854 emsg(_(e_autocmd_close));
5855 return;
5856 }
5857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005859 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005861#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005862 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005864 bufref_T bufref;
5865
5866 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005868 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 return;
5870 need_hide = FALSE;
5871 }
5872 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005875 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 return;
5877 }
5878 }
5879
Bram Moolenaar4033c552017-09-16 20:54:51 +02005880#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005882#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005883
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005884 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005885 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005886 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005887 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005888 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889}
5890
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005892 * Handle the argument for a tabpage related ex command.
5893 * Returns a tabpage number.
5894 * When an error is encountered then eap->errmsg is set.
5895 */
5896 static int
5897get_tabpage_arg(exarg_T *eap)
5898{
5899 int tab_number;
5900 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5901
5902 if (eap->arg && *eap->arg != NUL)
5903 {
5904 char_u *p = eap->arg;
5905 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005906 int relative = 0; // argument +N/-N means: go to N places to the
5907 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005908
5909 if (*p == '-')
5910 {
5911 relative = -1;
5912 p++;
5913 }
5914 else if (*p == '+')
5915 {
5916 relative = 1;
5917 p++;
5918 }
5919
5920 p_save = p;
5921 tab_number = getdigits(&p);
5922
5923 if (relative == 0)
5924 {
5925 if (STRCMP(p, "$") == 0)
5926 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005927 else if (STRCMP(p, "#") == 0)
5928 if (valid_tabpage(lastused_tabpage))
5929 tab_number = tabpage_index(lastused_tabpage);
5930 else
5931 {
5932 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5933 tab_number = 0;
5934 goto theend;
5935 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005936 else if (p == p_save || *p_save == '-' || *p != NUL
5937 || tab_number > LAST_TAB_NR)
5938 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005939 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005940 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005941 goto theend;
5942 }
5943 }
5944 else
5945 {
5946 if (*p_save == NUL)
5947 tab_number = 1;
5948 else if (p == p_save || *p_save == '-' || *p != NUL
5949 || tab_number == 0)
5950 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005951 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005952 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005953 goto theend;
5954 }
5955 tab_number = tab_number * relative + tabpage_index(curtab);
5956 if (!unaccept_arg0 && relative == -1)
5957 --tab_number;
5958 }
5959 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005960 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005961 }
5962 else if (eap->addr_count > 0)
5963 {
5964 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005965 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005966 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005967 tab_number = 0;
5968 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005969 else
5970 {
5971 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005972 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005973 {
5974 --tab_number;
5975 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005976 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01005977 }
5978 }
5979 }
5980 else
5981 {
5982 switch (eap->cmdidx)
5983 {
5984 case CMD_tabnext:
5985 tab_number = tabpage_index(curtab) + 1;
5986 if (tab_number > LAST_TAB_NR)
5987 tab_number = 1;
5988 break;
5989 case CMD_tabmove:
5990 tab_number = LAST_TAB_NR;
5991 break;
5992 default:
5993 tab_number = tabpage_index(curtab);
5994 }
5995 }
5996
5997theend:
5998 return tab_number;
5999}
6000
6001/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006002 * ":tabclose": close current tab page, unless it is the last one.
6003 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 */
6005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006006ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006008 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006009 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006010
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006011# ifdef FEAT_CMDWIN
6012 if (cmdwin_type != 0)
6013 cmdwin_result = K_IGNORE;
6014 else
6015# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006016 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006017 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006018 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006020 tab_number = get_tabpage_arg(eap);
6021 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006022 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006023 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006024 if (tp == NULL)
6025 {
6026 beep_flush();
6027 return;
6028 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006029 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006030 {
6031 tabpage_close_other(tp, eap->forceit);
6032 return;
6033 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006034 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006035 tabpage_close(eap->forceit);
6036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006038}
6039
6040/*
6041 * ":tabonly": close all tab pages except the current one
6042 */
6043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006044ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006045{
6046 tabpage_T *tp;
6047 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006048 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006049
6050# ifdef FEAT_CMDWIN
6051 if (cmdwin_type != 0)
6052 cmdwin_result = K_IGNORE;
6053 else
6054# endif
6055 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006056 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006057 else
6058 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006059 tab_number = get_tabpage_arg(eap);
6060 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006061 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006062 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006063 // Repeat this up to a 1000 times, because autocommands may
6064 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006065 for (done = 0; done < 1000; ++done)
6066 {
6067 FOR_ALL_TABPAGES(tp)
6068 if (tp->tp_topframe != topframe)
6069 {
6070 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006071 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006072 if (valid_tabpage(tp))
6073 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006074 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006075 break;
6076 }
6077 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006078 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006079 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006080 }
6081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083
6084/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006085 * Close the current tab page.
6086 */
6087 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006088tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006089{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006090 // First close all the windows but the current one. If that worked then
6091 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006092 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006093 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006094 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006095 ex_win_close(forceit, curwin, NULL);
6096# ifdef FEAT_GUI
6097 need_mouse_correct = TRUE;
6098# endif
6099}
6100
6101/*
6102 * Close tab page "tp", which is not the current tab page.
6103 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006104 * Also takes care of the tab pages line disappearing when closing the
6105 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006106 */
6107 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006108tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006109{
6110 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006111 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006112 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006114 // Limit to 1000 windows, autocommands may add a window while we close
6115 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006116 while (++done < 1000)
6117 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006118 wp = tp->tp_firstwin;
6119 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006120
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006121 // Autocommands may delete the tab page under our fingers and we may
6122 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006123 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006124 break;
6125 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006126
Bram Moolenaar29323592016-07-24 22:04:11 +02006127 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006128
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006129 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006130 if (h != tabline_height())
6131 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006132}
6133
6134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 * ":only".
6136 */
6137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006138ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006140 win_T *wp;
6141 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142# ifdef FEAT_GUI
6143 need_mouse_correct = TRUE;
6144# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006145 if (eap->addr_count > 0)
6146 {
6147 wnr = eap->line2;
6148 for (wp = firstwin; --wnr > 0; )
6149 {
6150 if (wp->w_next == NULL)
6151 break;
6152 else
6153 wp = wp->w_next;
6154 }
6155 win_goto(wp);
6156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 close_others(TRUE, eap->forceit);
6158}
6159
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006161ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006163 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006164 if (!eap->skip)
6165 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006166#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006167 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006168#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006169 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006170 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006171 else
6172 {
6173 int winnr = 0;
6174 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006175
Bram Moolenaar2256c992016-11-15 21:17:07 +01006176 FOR_ALL_WINDOWS(win)
6177 {
6178 winnr++;
6179 if (winnr == eap->line2)
6180 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006181 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006182 if (win == NULL)
6183 win = lastwin;
6184 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 }
6187}
6188
6189/*
6190 * ":stop" and ":suspend": Suspend Vim.
6191 */
6192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006193ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
6195 /*
6196 * Disallow suspending for "rvim".
6197 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006198 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 {
6200 if (!eap->forceit)
6201 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006202 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 windgoto((int)Rows - 1, 0);
6204 out_char('\n');
6205 out_flush();
6206 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006207 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006208 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006209 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006211 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006213 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006215 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006216 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 }
6218}
6219
6220/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006221 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 */
6223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006224ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006226#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006227 if (not_in_vim9(eap) == FAIL)
6228 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230#ifdef FEAT_CMDWIN
6231 if (cmdwin_type != 0)
6232 {
6233 cmdwin_result = Ctrl_C;
6234 return;
6235 }
6236#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006237 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006238 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006239 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006240 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006241 return;
6242 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006243
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006245 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 */
6247 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6248 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006249
6250 // Write the buffer for ":wq" or when it was changed.
6251 // Trigger QuitPre and ExitPre.
6252 // Check if we can exit now, after autocommands have changed things.
6253 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6254 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006256 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 {
6258 not_exiting();
6259 }
6260 else
6261 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006262 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006264 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265# ifdef FEAT_GUI
6266 need_mouse_correct = TRUE;
6267# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006268 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006269 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
6271}
6272
6273/*
6274 * ":print", ":list", ":number".
6275 */
6276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006277ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006279 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006280 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006281 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006283 for ( ;!got_int; ui_breakcheck())
6284 {
6285 print_line(eap->line1,
6286 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6287 || (eap->flags & EXFLAG_NR)),
6288 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6289 if (++eap->line1 > eap->line2)
6290 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006291 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006292 }
6293 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006294 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006295 curwin->w_cursor.lnum = eap->line2;
6296 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 }
6298
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300}
6301
6302#ifdef FEAT_BYTEOFF
6303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006304ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
6306 goto_byte(eap->line2);
6307}
6308#endif
6309
6310/*
6311 * ":shell".
6312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006314ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315{
6316 do_shell(NULL, 0);
6317}
6318
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006319#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006321static int drop_busy = FALSE;
6322static int drop_filec;
6323static char_u **drop_filev = NULL;
6324static int drop_split;
6325static void (*drop_callback)(void *);
6326static void *drop_cookie;
6327
6328 static void
6329handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
6331 exarg_T ea;
6332 int save_msg_scroll = msg_scroll;
6333
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006334 // Setting the argument list may cause screen updates and being called
6335 // recursively. Avoid that by setting drop_busy.
6336 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006338 // Check whether the current buffer is changed. If so, we will need
6339 // to split the current window or data could be lost.
6340 // We don't need to check if the 'hidden' option is set, as in this
6341 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006342 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 {
6344 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006345 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 --emsg_off;
6347 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006348 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 if (win_split(0, 0) == FAIL)
6351 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006352 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006354 // When splitting the window, create a new alist. Otherwise the
6355 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 alist_unlink(curwin->w_alist);
6357 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 }
6359
6360 /*
6361 * Set up the new argument list.
6362 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006363 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365 /*
6366 * Move to the first file.
6367 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006368 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006369 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 ea.cmd = (char_u *)"next";
6371 do_argfile(&ea, 0);
6372
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006373 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6374 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 need_start_insertmode = FALSE;
6376
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006377 // Restore msg_scroll, otherwise a following command may cause scrolling
6378 // unexpectedly. The screen will be redrawn by the caller, thus
6379 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006381
6382 if (drop_callback != NULL)
6383 drop_callback(drop_cookie);
6384
6385 drop_filev = NULL;
6386 drop_busy = FALSE;
6387}
6388
6389/*
6390 * Handle a file drop. The code is here because a drop is *nearly* like an
6391 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006392 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006393 * code is very similar to :args and hence needs access to a lot of the static
6394 * functions in this file.
6395 *
6396 * The "filev" list must have been allocated using alloc(), as should each item
6397 * in the list. This function takes over responsibility for freeing the "filev"
6398 * list.
6399 */
6400 void
6401handle_drop(
6402 int filec, // the number of files dropped
6403 char_u **filev, // the list of files dropped
6404 int split, // force splitting the window
6405 void (*callback)(void *), // to be called after setting the argument
6406 // list
6407 void *cookie) // argument for "callback" (allocated)
6408{
6409 // Cannot handle recursive drops, finish the pending one.
6410 if (drop_busy)
6411 {
6412 FreeWild(filec, filev);
6413 vim_free(cookie);
6414 return;
6415 }
6416
6417 // When calling handle_drop() more than once in a row we only use the last
6418 // one.
6419 if (drop_filev != NULL)
6420 {
6421 FreeWild(drop_filec, drop_filev);
6422 vim_free(drop_cookie);
6423 }
6424
6425 drop_filec = filec;
6426 drop_filev = filev;
6427 drop_split = split;
6428 drop_callback = callback;
6429 drop_cookie = cookie;
6430
6431 // Postpone this when:
6432 // - editing the command line
6433 // - not possible to change the current buffer
6434 // - updating the screen
6435 // As it may change buffers and window structures that are in use and cause
6436 // freed memory to be used.
6437 if (text_locked() || curbuf_locked() || updating_screen)
6438 return;
6439
6440 handle_drop_internal();
6441}
6442
6443/*
6444 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6445 * reset: Handle a postponed drop.
6446 */
6447 void
6448handle_any_postponed_drop(void)
6449{
6450 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006451 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006452 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453}
6454#endif
6455
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 * ":preserve".
6458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006460ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006462 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 ml_preserve(curbuf, TRUE);
6464}
6465
6466/*
6467 * ":recover".
6468 */
6469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006470ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006472 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006474 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6475 | CCGD_MULTWIN
6476 | (eap->forceit ? CCGD_FORCEIT : 0)
6477 | CCGD_EXCMD)
6478
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 && (*eap->arg == NUL
6480 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006481 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 recoverymode = FALSE;
6483}
6484
6485/*
6486 * Command modifier used in a wrong way.
6487 */
6488 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006489ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006491 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492}
6493
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494/*
6495 * :sview [+command] file split window with new file, read-only
6496 * :split [[+command] file] split window with current or new file
6497 * :vsplit [[+command] file] split window vertically with current or new file
6498 * :new [[+command] file] split window with no or new file
6499 * :vnew [[+command] file] split vertically window with no or new file
6500 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006501 *
6502 * :tabedit open new Tab page with empty window
6503 * :tabedit [+command] file open new Tab page and edit "file"
6504 * :tabnew [[+command] file] just like :tabedit
6505 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 */
6507 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006508ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006510 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006511#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006513#endif
6514#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006515 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006516 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006517#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006518 int use_tab = eap->cmdidx == CMD_tabedit
6519 || eap->cmdidx == CMD_tabfind
6520 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006522 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006523 return;
6524
Bram Moolenaar4033c552017-09-16 20:54:51 +02006525#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528
Bram Moolenaar4033c552017-09-16 20:54:51 +02006529#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006530 // A ":split" in the quickfix window works like ":new". Don't want two
6531 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006532 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
6534 if (eap->cmdidx == CMD_split)
6535 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 if (eap->cmdidx == CMD_vsplit)
6537 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540
Bram Moolenaar4033c552017-09-16 20:54:51 +02006541#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006542 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 {
6544 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6545 FNAME_MESS, TRUE, curbuf->b_ffname);
6546 if (fname == NULL)
6547 goto theend;
6548 eap->arg = fname;
6549 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006550# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006551 else
6552# endif
6553#endif
6554#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006555 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 && eap->cmdidx != CMD_new)
6558 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006559 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006560# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006561 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006562# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006563 au_has_group((char_u *)"FileExplorer"))
6564 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006565 // No browsing supported but we do have the file explorer:
6566 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006567 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006568 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006569 }
6570 else
6571 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006572 fname = do_browse(0, (char_u *)(use_tab
6573 ? _("Edit File in new tab page")
6574 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006576 if (fname == NULL)
6577 goto theend;
6578 eap->arg = fname;
6579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006581 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006584 /*
6585 * Either open new tab page or split the window.
6586 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006587 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006588 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006589 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006590 : eap->addr_count == 0 ? 0
6591 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006592 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006593 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006594
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006595 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006596 if (curwin != old_curwin
6597 && win_valid(old_curwin)
6598 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006599 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006600 old_curwin->w_alt_fnum = curbuf->b_fnum;
6601 }
6602 }
6603 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6605 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006606 // Reset 'scrollbind' when editing another file, but keep it when
6607 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006608 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006609 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 else
6611 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 do_exedit(eap, old_curwin);
6613 }
6614
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006615# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006616 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006617# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006619# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620theend:
6621 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006624
6625/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006626 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006627 */
6628 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006629tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006630{
6631 exarg_T ea;
6632
Bram Moolenaara80faa82020-04-12 19:37:17 +02006633 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006634 ea.cmdidx = CMD_tabnew;
6635 ea.cmd = (char_u *)"tabn";
6636 ea.arg = (char_u *)"";
6637 ex_splitview(&ea);
6638}
6639
6640/*
6641 * :tabnext command
6642 */
6643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006644ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006645{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006646 int tab_number;
6647
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006648 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006649 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006650 switch (eap->cmdidx)
6651 {
6652 case CMD_tabfirst:
6653 case CMD_tabrewind:
6654 goto_tabpage(1);
6655 break;
6656 case CMD_tablast:
6657 goto_tabpage(9999);
6658 break;
6659 case CMD_tabprevious:
6660 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006661 if (eap->arg && *eap->arg != NUL)
6662 {
6663 char_u *p = eap->arg;
6664 char_u *p_save = p;
6665
6666 tab_number = getdigits(&p);
6667 if (p == p_save || *p_save == '-' || *p != NUL
6668 || tab_number == 0)
6669 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006670 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006671 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006672 return;
6673 }
6674 }
6675 else
6676 {
6677 if (eap->addr_count == 0)
6678 tab_number = 1;
6679 else
6680 {
6681 tab_number = eap->line2;
6682 if (tab_number < 1)
6683 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006684 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006685 return;
6686 }
6687 }
6688 }
6689 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006690 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006691 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006692 tab_number = get_tabpage_arg(eap);
6693 if (eap->errmsg == NULL)
6694 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006695 break;
6696 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006697}
6698
6699/*
6700 * :tabmove command
6701 */
6702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006703ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006704{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006705 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006706
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006707 tab_number = get_tabpage_arg(eap);
6708 if (eap->errmsg == NULL)
6709 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006710}
6711
6712/*
6713 * :tabs command: List tabs and their contents.
6714 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006716ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006717{
6718 tabpage_T *tp;
6719 win_T *wp;
6720 int tabcount = 1;
6721
6722 msg_start();
6723 msg_scroll = TRUE;
6724 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6725 {
6726 msg_putchar('\n');
6727 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006728 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006729 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006730 ui_breakcheck();
6731
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006732 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006733 wp = firstwin;
6734 else
6735 wp = tp->tp_firstwin;
6736 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6737 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006738 msg_putchar('\n');
6739 msg_putchar(wp == curwin ? '>' : ' ');
6740 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006741 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6742 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006743 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006744 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006745 else
6746 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6747 IObuff, IOSIZE, TRUE);
6748 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006749 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006750 ui_breakcheck();
6751 }
6752 }
6753}
6754
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755/*
6756 * ":mode": Set screen mode.
6757 * If no argument given, just get the screen size and redraw.
6758 */
6759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006760ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
6762 if (*eap->arg == NUL)
6763 shell_resized();
6764 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006765 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766}
6767
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768/*
6769 * ":resize".
6770 * set, increment or decrement current window height
6771 */
6772 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006773ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
6775 int n;
6776 win_T *wp = curwin;
6777
6778 if (eap->addr_count > 0)
6779 {
6780 n = eap->line2;
6781 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6782 ;
6783 }
6784
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006785# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006789 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
6791 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006792 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006793 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 n = 9999;
6795 win_setwidth_win((int)n, wp);
6796 }
6797 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 {
6799 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006800 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006801 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 n = 9999;
6803 win_setheight_win((int)n, wp);
6804 }
6805}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806
6807/*
6808 * ":find [+command] <file>" command.
6809 */
6810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006811ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
6813#ifdef FEAT_SEARCHPATH
6814 char_u *fname;
6815 int count;
6816
6817 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6818 TRUE, curbuf->b_ffname);
6819 if (eap->addr_count > 0)
6820 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006821 // Repeat finding the file "count" times. This matters when it
6822 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 count = eap->line2;
6824 while (fname != NULL && --count > 0)
6825 {
6826 vim_free(fname);
6827 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6828 FALSE, curbuf->b_ffname);
6829 }
6830 }
6831
6832 if (fname != NULL)
6833 {
6834 eap->arg = fname;
6835#endif
6836 do_exedit(eap, NULL);
6837#ifdef FEAT_SEARCHPATH
6838 vim_free(fname);
6839 }
6840#endif
6841}
6842
6843/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006844 * ":open" simulation: for now just work like ":visual".
6845 */
6846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006847ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006848{
6849 regmatch_T regmatch;
6850 char_u *p;
6851
Bram Moolenaar65088802021-03-13 21:07:21 +01006852#ifdef FEAT_EVAL
6853 if (not_in_vim9(eap) == FAIL)
6854 return;
6855#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006856 curwin->w_cursor.lnum = eap->line2;
6857 beginline(BL_SOL | BL_FIX);
6858 if (*eap->arg == '/')
6859 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006860 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006861 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006862 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006863 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006864 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006865 if (regmatch.regprog != NULL)
6866 {
6867 regmatch.rm_ic = p_ic;
6868 p = ml_get_curline();
6869 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006870 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006871 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006872 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006873 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006874 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006875 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006876 eap->arg += STRLEN(eap->arg);
6877 }
6878 check_cursor();
6879
6880 eap->cmdidx = CMD_visual;
6881 do_exedit(eap, NULL);
6882}
6883
6884/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006885 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 */
6887 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006888ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889{
6890 do_exedit(eap, NULL);
6891}
6892
6893/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006894 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006897do_exedit(
6898 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006899 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900{
6901 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006903 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006905 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6906 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006907 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 /*
6909 * ":vi" command ends Ex mode.
6910 */
6911 if (exmode_active && (eap->cmdidx == CMD_visual
6912 || eap->cmdidx == CMD_view))
6913 {
6914 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006915 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006917 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006918 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006919 if (global_busy)
6920 {
6921 int rd = RedrawingDisabled;
6922 int nwr = no_wait_return;
6923 int ms = msg_scroll;
6924#ifdef FEAT_GUI
6925 int he = hold_gui_events;
6926#endif
6927
6928 if (eap->nextcmd != NULL)
6929 {
6930 stuffReadbuff(eap->nextcmd);
6931 eap->nextcmd = NULL;
6932 }
6933
6934 if (exmode_was != EXMODE_VIM)
6935 settmode(TMODE_RAW);
6936 RedrawingDisabled = 0;
6937 no_wait_return = 0;
6938 need_wait_return = FALSE;
6939 msg_scroll = 0;
6940#ifdef FEAT_GUI
6941 hold_gui_events = 0;
6942#endif
6943 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006944 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006945
6946 main_loop(FALSE, TRUE);
6947
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006948 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006949 RedrawingDisabled = rd;
6950 no_wait_return = nwr;
6951 msg_scroll = ms;
6952#ifdef FEAT_GUI
6953 hold_gui_events = he;
6954#endif
6955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 }
6959
6960 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006961 || eap->cmdidx == CMD_tabnew
6962 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006963 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006965 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 setpcmark();
6967 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006968 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6969 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006971 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 || *eap->arg != NUL
6973#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006974 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975#endif
6976 )
6977 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006978 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6979 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006980 if (*eap->arg != NUL && curbuf_locked())
6981 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006982
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 n = readonlymode;
6984 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6985 readonlymode = TRUE;
6986 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006987 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6988 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006989 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6990 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6992 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006993 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6995 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6996 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006997 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006999 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007000 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007001 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7002 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007003 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007005 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 if (old_curwin != NULL)
7007 {
7008 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007009 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007011#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007012 cleanup_T cs;
7013
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007014 // Reset the error/interrupt/exception state here so that
7015 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007016 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007017#endif
7018#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007020#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007021 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007022
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007023#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007024 // Restore the error/interrupt/exception state if not
7025 // discarded by a new aborting error, interrupt, or
7026 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007027 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 }
7030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 }
7032 else if (readonlymode && curbuf->b_nwindows == 1)
7033 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007034 // When editing an already visited buffer, 'readonly' won't be set
7035 // but the previous value is kept. With ":view" and ":sview" we
7036 // want the file to be readonly, except when another window is
7037 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 curbuf->b_p_ro = TRUE;
7039 }
7040 readonlymode = n;
7041 }
7042 else
7043 {
7044 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007045 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 if (n != curwin->w_arg_idx_invalid)
7049 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
7051
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 /*
7053 * if ":split file" worked, set alternate file name in old window to new
7054 * file
7055 */
7056 if (old_curwin != NULL
7057 && *eap->arg != NUL
7058 && curwin != old_curwin
7059 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007060 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007061 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
7064 ex_no_reprint = TRUE;
7065}
7066
7067#ifndef FEAT_GUI
7068/*
7069 * ":gui" and ":gvim" when there is no GUI.
7070 */
7071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007072ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007074 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075}
7076#endif
7077
Bram Moolenaar4f974752019-02-17 17:44:42 +01007078#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007080ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081{
7082 gui_make_tearoff(eap->arg);
7083}
7084#endif
7085
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007086#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7087 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007089ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007091# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7092 if (gui.in_use)
7093 gui_make_popup(eap->arg, eap->forceit);
7094# ifdef FEAT_TERM_POPUP_MENU
7095 else
7096# endif
7097# endif
7098# ifdef FEAT_TERM_POPUP_MENU
7099 pum_make_popup(eap->arg, eap->forceit);
7100# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101}
7102#endif
7103
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007105ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
7107 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007108 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007110 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111}
7112
7113/*
7114 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7115 * offset.
7116 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007119ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007122 win_T *save_curwin = curwin;
7123 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 long topline;
7125 long y;
7126 linenr_T old_linenr = curwin->w_cursor.lnum;
7127
7128 setpcmark();
7129
7130 /*
7131 * determine max topline
7132 */
7133 if (curwin->w_p_scb)
7134 {
7135 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007136 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 {
7138 if (wp->w_p_scb && wp->w_buffer)
7139 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007140 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 if (topline > y)
7142 topline = y;
7143 }
7144 }
7145 if (topline < 1)
7146 topline = 1;
7147 }
7148 else
7149 {
7150 topline = 1;
7151 }
7152
7153
7154 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007155 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007157 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {
7159 if (curwin->w_p_scb)
7160 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007161 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 y = topline - curwin->w_topline;
7163 if (y > 0)
7164 scrollup(y, TRUE);
7165 else
7166 scrolldown(-y, TRUE);
7167 curwin->w_scbind_pos = topline;
7168 redraw_later(VALID);
7169 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 }
7172 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007173 curwin = save_curwin;
7174 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 if (curwin->w_p_scb)
7176 {
7177 did_syncbind = TRUE;
7178 checkpcmark();
7179 if (old_linenr != curwin->w_cursor.lnum)
7180 {
7181 char_u ctrl_o[2];
7182
7183 ctrl_o[0] = Ctrl_O;
7184 ctrl_o[1] = 0;
7185 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7186 }
7187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188}
7189
7190
7191 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007192ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007194 int i;
7195 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7196 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007198 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 do_bang(1, eap, FALSE, FALSE, TRUE);
7200 else
7201 {
7202 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7203 return;
7204
7205#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007206 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 {
7208 char_u *browseFile;
7209
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007210 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 NULL, NULL, NULL, curbuf);
7212 if (browseFile != NULL)
7213 {
7214 i = readfile(browseFile, NULL,
7215 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7216 vim_free(browseFile);
7217 }
7218 else
7219 i = OK;
7220 }
7221 else
7222#endif
7223 if (*eap->arg == NUL)
7224 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007225 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 return;
7227 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7228 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7229 }
7230 else
7231 {
7232 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7233 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7234 i = readfile(eap->arg, NULL,
7235 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7236
7237 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007238 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007240#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 if (!aborting())
7242#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007243 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007246 {
7247 if (empty && exmode_active)
7248 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007249 // Delete the empty line that remains. Historically ex does
7250 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007251 if (eap->line2 == 0)
7252 lnum = curbuf->b_ml.ml_line_count;
7253 else
7254 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007255 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007256 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007257 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007258 if (curwin->w_cursor.lnum > 1
7259 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007260 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007261 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007262 }
7263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 }
7267}
7268
Bram Moolenaarea408852005-06-25 22:49:46 +00007269static char_u *prev_dir = NULL;
7270
7271#if defined(EXITFREE) || defined(PROTO)
7272 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007273free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007274{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007275 VIM_CLEAR(prev_dir);
7276 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007277}
7278#endif
7279
Bram Moolenaarf4258302013-06-02 18:20:17 +02007280/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007281 * Get the previous directory for the given chdir scope.
7282 */
7283 static char_u *
7284get_prevdir(cdscope_T scope)
7285{
7286 if (scope == CDSCOPE_WINDOW)
7287 return curwin->w_prevdir;
7288 else if (scope == CDSCOPE_TABPAGE)
7289 return curtab->tp_prevdir;
7290 return prev_dir;
7291}
7292
7293/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007294 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007295 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7296 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007297 */
7298 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007299post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007300{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007301 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007302 // Clear tab local directory for both :cd and :tcd
7303 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007304 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007305 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007306 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007307 char_u *pdir = get_prevdir(scope);
7308
7309 // If still in the global directory, need to remember current
7310 // directory as the global directory.
7311 if (globaldir == NULL && pdir != NULL)
7312 globaldir = vim_strsave(pdir);
7313
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007314 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007315 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007316 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007317 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007318 curtab->tp_localdir = vim_strsave(NameBuff);
7319 else
7320 curwin->w_localdir = vim_strsave(NameBuff);
7321 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007322 }
7323 else
7324 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007325 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007326 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007327 }
7328
zeertzjq64be6aa2021-11-19 11:59:08 +00007329 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007330 shorten_fnames(TRUE);
7331}
7332
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007333/*
7334 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007335 * chdir() function.
7336 * scope == CDSCOPE_WINDOW: changes the window-local directory
7337 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7338 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007339 * Returns TRUE if the directory is successfully changed.
7340 */
7341 int
7342changedir_func(
7343 char_u *new_dir,
7344 int forceit,
7345 cdscope_T scope)
7346{
7347 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007348 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007349 int dir_differs;
7350 int retval = FALSE;
7351
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007352 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007353 return FALSE;
7354
7355 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7356 {
7357 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7358 return FALSE;
7359 }
7360
7361 // ":cd -": Change to previous directory
7362 if (STRCMP(new_dir, "-") == 0)
7363 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007364 pdir = get_prevdir(scope);
7365 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007366 {
7367 emsg(_("E186: No previous directory"));
7368 return FALSE;
7369 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007370 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007371 }
7372
Bram Moolenaar002bc792020-06-05 22:33:42 +02007373 // Free the previous directory
7374 tofree = get_prevdir(scope);
7375
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007376 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007377 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007378 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007379 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007380 pdir = NULL;
7381 if (scope == CDSCOPE_WINDOW)
7382 curwin->w_prevdir = pdir;
7383 else if (scope == CDSCOPE_TABPAGE)
7384 curtab->tp_prevdir = pdir;
7385 else
7386 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007387
7388#if defined(UNIX) || defined(VMS)
7389 // for UNIX ":cd" means: go to home directory
7390 if (*new_dir == NUL)
7391 {
7392 // use NameBuff for home directory name
7393# ifdef VMS
7394 char_u *p;
7395
7396 p = mch_getenv((char_u *)"SYS$LOGIN");
7397 if (p == NULL || *p == NUL) // empty is the same as not set
7398 NameBuff[0] = NUL;
7399 else
7400 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7401# else
7402 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7403# endif
7404 new_dir = NameBuff;
7405 }
7406#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007407 dir_differs = new_dir == NULL || pdir == NULL
7408 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007409 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7410 emsg(_(e_failed));
7411 else
7412 {
7413 char_u *acmd_fname;
7414
7415 post_chdir(scope);
7416
7417 if (dir_differs)
7418 {
7419 if (scope == CDSCOPE_WINDOW)
7420 acmd_fname = (char_u *)"window";
7421 else if (scope == CDSCOPE_TABPAGE)
7422 acmd_fname = (char_u *)"tabpage";
7423 else
7424 acmd_fname = (char_u *)"global";
7425 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7426 curbuf);
7427 }
7428 retval = TRUE;
7429 }
7430 vim_free(tofree);
7431
7432 return retval;
7433}
Bram Moolenaarea408852005-06-25 22:49:46 +00007434
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007436 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007438 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007439ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007441 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442
7443 new_dir = eap->arg;
7444#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007445 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 if (*new_dir == NUL)
7447 ex_pwd(NULL);
7448 else
7449#endif
7450 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007451 cdscope_T scope = CDSCOPE_GLOBAL;
7452
7453 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7454 scope = CDSCOPE_WINDOW;
7455 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7456 scope = CDSCOPE_TABPAGE;
7457
7458 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007459 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007460 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007461 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 ex_pwd(eap);
7463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 }
7465}
7466
7467/*
7468 * ":pwd".
7469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007471ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472{
7473 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7474 {
7475#ifdef BACKSLASH_IN_FILENAME
7476 slash_adjust(NameBuff);
7477#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007478 if (p_verbose > 0)
7479 {
7480 char *context = "global";
7481
Bram Moolenaar05268152021-11-18 18:53:45 +00007482 if (last_chdir_reason != NULL)
7483 context = last_chdir_reason;
7484 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007485 context = "window";
7486 else if (curtab->tp_localdir != NULL)
7487 context = "tabpage";
7488 smsg("[%s] %s", context, (char *)NameBuff);
7489 }
7490 else
7491 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 }
7493 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007494 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495}
7496
7497/*
7498 * ":=".
7499 */
7500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007501ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007503 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007504 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505}
7506
7507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007508ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007510 int n;
7511 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512
7513 if (cursor_valid())
7514 {
7515 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7516 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007517 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007519
7520 len = eap->line2;
7521 switch (*eap->arg)
7522 {
7523 case 'm': break;
7524 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007525 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007526 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007527
7528 // Hide the cursor if invoked with !
7529 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530}
7531
7532/*
7533 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007534 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 */
7536 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007537do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538{
Bram Moolenaar52953192019-08-16 10:27:13 +02007539 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007540 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007541# ifdef ELAPSED_FUNC
7542 elapsed_T start_tv;
7543
7544 // Remember at what time we started, so that we know how much longer we
7545 // should wait after waiting for a bit.
7546 ELAPSED_INIT(start_tv);
7547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007549 if (hide_cursor)
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007550 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007551 else
7552 cursor_on();
7553
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007554 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007555 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007557 wait_now = msec - done > 1000L ? 1000L : msec - done;
7558#ifdef FEAT_TIMERS
7559 {
7560 long due_time = check_due_timer();
7561
7562 if (due_time > 0 && due_time < wait_now)
7563 wait_now = due_time;
7564 }
7565#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007566#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007567 if (has_any_channel() && wait_now > 20L)
7568 wait_now = 20L;
7569#endif
7570#ifdef FEAT_SOUND
7571 if (has_any_sound_callback() && wait_now > 20L)
7572 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007573#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007574 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007575
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007576#ifdef FEAT_JOB_CHANNEL
7577 if (has_any_channel())
7578 ui_breakcheck_force(TRUE);
7579 else
7580#endif
7581 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007582#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007583 // Process the netbeans and clientserver messages that may have been
7584 // received in the call to ui_breakcheck() when the GUI is in use. This
7585 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007586 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007587#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007588
7589# ifdef ELAPSED_FUNC
7590 // actual time passed
7591 done = ELAPSED_FUNC(start_tv);
7592# else
7593 // guestimate time passed (will actually be more)
7594 done += wait_now;
7595# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007597
7598 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7599 // input buffer, otherwise a following call to input() fails.
7600 if (got_int)
7601 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007602
7603 if (hide_cursor)
7604 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605}
7606
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607/*
7608 * ":winsize" command (obsolete).
7609 */
7610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007611ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
7613 int w, h;
7614 char_u *arg = eap->arg;
7615 char_u *p;
7616
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007617 if (!isdigit(*arg))
7618 {
7619 semsg(_(e_invarg2), arg);
7620 return;
7621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 w = getdigits(&arg);
7623 arg = skipwhite(arg);
7624 p = arg;
7625 h = getdigits(&arg);
7626 if (*p != NUL && *arg == NUL)
7627 set_shellsize(w, h, TRUE);
7628 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007629 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630}
7631
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007633ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634{
7635 int xchar = NUL;
7636 char_u *p;
7637
7638 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7639 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007640 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 if (eap->arg[1] == NUL)
7642 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007643 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 return;
7645 }
7646 xchar = eap->arg[1];
7647 p = eap->arg + 2;
7648 }
7649 else
7650 p = eap->arg + 1;
7651
Bram Moolenaar63b91732021-08-05 20:40:03 +02007652 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007654 if (*p != NUL && *p != (
7655#ifdef FEAT_EVAL
7656 in_vim9script() ? '#' :
7657#endif
7658 '"')
7659 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007660 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007661 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007663 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007664 postponed_split_flags = cmdmod.cmod_split;
7665 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7667 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007668 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 }
7670}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671
Bram Moolenaar843ee412004-06-30 16:16:41 +00007672#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673/*
7674 * ":winpos".
7675 */
7676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007677ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679 int x, y;
7680 char_u *arg = eap->arg;
7681 char_u *p;
7682
7683 if (*arg == NUL)
7684 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007685# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007686# ifdef VIMDLL
7687 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7688 mch_get_winpos(&x, &y) != FAIL)
7689# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007691# else
7692 if (mch_get_winpos(&x, &y) != FAIL)
7693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {
7695 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007696 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 }
7698 else
7699# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007700 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 }
7702 else
7703 {
7704 x = getdigits(&arg);
7705 arg = skipwhite(arg);
7706 p = arg;
7707 y = getdigits(&arg);
7708 if (*p == NUL || *arg != NUL)
7709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007710 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 return;
7712 }
7713# ifdef FEAT_GUI
7714 if (gui.in_use)
7715 gui_mch_set_winpos(x, y);
7716 else if (gui.starting)
7717 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007718 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 gui_win_x = x;
7720 gui_win_y = y;
7721 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007722# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 else
7724# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007725# endif
7726# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007727 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728# endif
7729# ifdef HAVE_TGETENT
7730 if (*T_CWP)
7731 term_set_winpos(x, y);
7732# endif
7733 }
7734}
7735#endif
7736
7737/*
7738 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7739 */
7740 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007741ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742{
7743 oparg_T oa;
7744
7745 clear_oparg(&oa);
7746 oa.regname = eap->regname;
7747 oa.start.lnum = eap->line1;
7748 oa.end.lnum = eap->line2;
7749 oa.line_count = eap->line2 - eap->line1 + 1;
7750 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007752 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 {
7754 setpcmark();
7755 curwin->w_cursor.lnum = eap->line1;
7756 beginline(BL_SOL | BL_FIX);
7757 }
7758
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007759 if (VIsual_active)
7760 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007761
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 switch (eap->cmdidx)
7763 {
7764 case CMD_delete:
7765 oa.op_type = OP_DELETE;
7766 op_delete(&oa);
7767 break;
7768
7769 case CMD_yank:
7770 oa.op_type = OP_YANK;
7771 (void)op_yank(&oa, FALSE, TRUE);
7772 break;
7773
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007774 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007775 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007777 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7778#else
7779 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007781 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 oa.op_type = OP_RSHIFT;
7783 else
7784 oa.op_type = OP_LSHIFT;
7785 op_shift(&oa, FALSE, eap->amount);
7786 break;
7787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007789 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790}
7791
7792/*
7793 * ":put".
7794 */
7795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007796ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007798 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 if (eap->line2 == 0)
7800 {
7801 eap->line2 = 1;
7802 eap->forceit = TRUE;
7803 }
7804 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00007805 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007806 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007807 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808}
7809
7810/*
7811 * Handle ":copy" and ":move".
7812 */
7813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007814ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815{
7816 long n;
7817
Bram Moolenaar491799b2020-08-01 19:23:43 +02007818#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007819 if (not_in_vim9(eap) == FAIL)
7820 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007821#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007822 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007823 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {
7825 eap->nextcmd = NULL;
7826 return;
7827 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007828 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
7830 /*
7831 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7832 */
7833 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7834 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007835 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 return;
7837 }
7838
7839 if (eap->cmdidx == CMD_move)
7840 {
7841 if (do_move(eap->line1, eap->line2, n) == FAIL)
7842 return;
7843 }
7844 else
7845 ex_copy(eap->line1, eap->line2, n);
7846 u_clearline();
7847 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007848 ex_may_print(eap);
7849}
7850
7851/*
7852 * Print the current line if flags were given to the Ex command.
7853 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007854 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007855ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007856{
7857 if (eap->flags != 0)
7858 {
7859 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7860 (eap->flags & EXFLAG_LIST));
7861 ex_no_reprint = TRUE;
7862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863}
7864
7865/*
7866 * ":smagic" and ":snomagic".
7867 */
7868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007869ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007871 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007873 magic_overruled = eap->cmdidx == CMD_smagic
7874 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007875 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007876 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877}
7878
7879/*
7880 * ":join".
7881 */
7882 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007883ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
7885 curwin->w_cursor.lnum = eap->line1;
7886 if (eap->line1 == eap->line2)
7887 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007888 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 return;
7890 if (eap->line2 == curbuf->b_ml.ml_line_count)
7891 {
7892 beep_flush();
7893 return;
7894 }
7895 ++eap->line2;
7896 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007897 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007899 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900}
7901
7902/*
7903 * ":[addr]@r" or ":[addr]*r": execute register
7904 */
7905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007906ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
7908 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007909 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910
7911 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007912 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913
7914#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007915 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916#endif
7917
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007918 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 c = *eap->arg;
7920 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7921 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007922 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007923 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7924 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 else
7929 {
7930 int save_efr = exec_from_reg;
7931
7932 exec_from_reg = TRUE;
7933
7934 /*
7935 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007936 * Continue until the stuff buffer is empty and all added characters
7937 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007939 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7941
7942 exec_from_reg = save_efr;
7943 }
7944}
7945
7946/*
7947 * ":!".
7948 */
7949 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007950ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7953}
7954
7955/*
7956 * ":undo".
7957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007959ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007961 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007962 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007963 else
7964 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965}
7966
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007967#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007969ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007970{
7971 char_u hash[UNDO_HASH_SIZE];
7972
7973 u_compute_hash(hash);
7974 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7975}
7976
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007978ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007979{
7980 char_u hash[UNDO_HASH_SIZE];
7981
7982 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007983 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007984}
7985#endif
7986
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987/*
7988 * ":redo".
7989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007991ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992{
7993 u_redo(1);
7994}
7995
7996/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007997 * ":earlier" and ":later".
7998 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008000ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008001{
8002 long count = 0;
8003 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008004 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008005 char_u *p = eap->arg;
8006
8007 if (*p == NUL)
8008 count = 1;
8009 else if (isdigit(*p))
8010 {
8011 count = getdigits(&p);
8012 switch (*p)
8013 {
8014 case 's': ++p; sec = TRUE; break;
8015 case 'm': ++p; sec = TRUE; count *= 60; break;
8016 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008017 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8018 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008019 }
8020 }
8021
8022 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008023 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008024 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008025 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8026 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008027}
8028
8029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 * ":redir": start/stop redirection.
8031 */
8032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008033ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034{
8035 char *mode;
8036 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008037 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038
Bram Moolenaarba768492016-07-08 15:32:54 +02008039#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008040 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008041 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008042 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008043 return;
8044 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008045#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008046
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 if (STRICMP(eap->arg, "END") == 0)
8048 close_redir();
8049 else
8050 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008051 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008053 ++arg;
8054 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008056 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 mode = "a";
8058 }
8059 else
8060 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008061 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062
8063 close_redir();
8064
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008065 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008066 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 if (fname == NULL)
8068 return;
8069#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008070 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {
8072 char_u *browseFile;
8073
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008074 browseFile = do_browse(BROWSE_SAVE,
8075 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008076 fname, NULL, NULL,
8077 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008079 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 vim_free(fname);
8081 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008082 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 }
8084#endif
8085
8086 redir_fd = open_exfile(fname, eap->forceit, mode);
8087 vim_free(fname);
8088 }
8089#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008090 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008092 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008094 ++arg;
8095 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008097 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008098 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008100 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008102 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008103 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008104 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008107 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008108 if (*arg == '>')
8109 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008110 // Make register empty when not using @A-@Z and the
8111 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008112 if (*arg == NUL && !isupper(redir_reg))
8113 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008115 }
8116 if (*arg != NUL)
8117 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008118 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008119 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008120 }
8121 }
8122 else if (*arg == '=' && arg[1] == '>')
8123 {
8124 int append;
8125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008126 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008127 close_redir();
8128 arg += 2;
8129
8130 if (*arg == '>')
8131 {
8132 ++arg;
8133 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 }
8135 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008136 append = FALSE;
8137
8138 if (var_redir_start(skipwhite(arg), append) == OK)
8139 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 }
8141#endif
8142
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008143 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008146 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008148
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008149 // Make sure redirection is not off. Can happen for cmdline completion
8150 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008151 if (redir_fd != NULL
8152#ifdef FEAT_EVAL
8153 || redir_reg || redir_vname
8154#endif
8155 )
8156 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157}
8158
8159/*
8160 * ":redraw": force redraw
8161 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008162 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008163ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164{
8165 int r = RedrawingDisabled;
8166 int p = p_lz;
8167
8168 RedrawingDisabled = 0;
8169 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008170 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008172 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 if (need_maketitle)
8174 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008175#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8176# ifdef VIMDLL
8177 if (!gui.in_use)
8178# endif
8179 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 RedrawingDisabled = r;
8182 p_lz = p;
8183
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008184 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 msg_didout = FALSE;
8186 msg_col = 0;
8187
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008188 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008189 need_wait_return = FALSE;
8190
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 out_flush();
8192}
8193
8194/*
8195 * ":redrawstatus": force redraw of status line(s)
8196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008198ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 int r = RedrawingDisabled;
8201 int p = p_lz;
8202
8203 RedrawingDisabled = 0;
8204 p_lz = FALSE;
8205 if (eap->forceit)
8206 status_redraw_all();
8207 else
8208 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008209 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 RedrawingDisabled = r;
8211 p_lz = p;
8212 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213}
8214
Bram Moolenaare12bab32019-01-08 22:02:56 +01008215/*
8216 * ":redrawtabline": force redraw of the tabline
8217 */
8218 static void
8219ex_redrawtabline(exarg_T *eap UNUSED)
8220{
8221 int r = RedrawingDisabled;
8222 int p = p_lz;
8223
8224 RedrawingDisabled = 0;
8225 p_lz = FALSE;
8226
8227 draw_tabline();
8228
8229 RedrawingDisabled = r;
8230 p_lz = p;
8231 out_flush();
8232}
8233
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008235close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 if (redir_fd != NULL)
8238 {
8239 fclose(redir_fd);
8240 redir_fd = NULL;
8241 }
8242#ifdef FEAT_EVAL
8243 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008244 if (redir_vname)
8245 {
8246 var_redir_stop();
8247 redir_vname = 0;
8248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249#endif
8250}
8251
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008252#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008253 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008254vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008255{
8256 if (vim_mkdir(name, prot) != 0)
8257 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008258 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008259 return FAIL;
8260 }
8261 return OK;
8262}
8263#endif
8264
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265/*
8266 * Open a file for writing for an Ex command, with some checks.
8267 * Return file descriptor, or NULL on failure.
8268 */
8269 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008270open_exfile(
8271 char_u *fname,
8272 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008273 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
8275 FILE *fd;
8276
8277#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008278 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 if (mch_isdir(fname))
8280 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008281 semsg(_(e_src_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 return NULL;
8283 }
8284#endif
8285 if (!forceit && *mode != 'a' && vim_fexists(fname))
8286 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008287 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 return NULL;
8289 }
8290
8291 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008292 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293
8294 return fd;
8295}
8296
8297/*
8298 * ":mark" and ":k".
8299 */
8300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008301ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303 pos_T pos;
8304
Bram Moolenaar10b94212021-02-19 21:42:57 +01008305#ifdef FEAT_EVAL
8306 if (not_in_vim9(eap) == FAIL)
8307 return;
8308#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008309 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008310 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008311 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008312 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 else
8314 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008315 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 curwin->w_cursor.lnum = eap->line2;
8317 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008318 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008319 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008320 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 }
8322}
8323
8324/*
8325 * Update w_topline, w_leftcol and the cursor position.
8326 */
8327 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008328update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008330 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 update_topline();
8332 if (!curwin->w_p_wrap)
8333 validate_cursor();
8334 update_curswant();
8335}
8336
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008338 * Save the current State and go to Normal mode.
8339 * Return TRUE if the typeahead could be saved.
8340 */
8341 int
8342save_current_state(save_state_T *sst)
8343{
8344 sst->save_msg_scroll = msg_scroll;
8345 sst->save_restart_edit = restart_edit;
8346 sst->save_msg_didout = msg_didout;
8347 sst->save_State = State;
8348 sst->save_insertmode = p_im;
8349 sst->save_finish_op = finish_op;
8350 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008351 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008352
Bram Moolenaar4324d872020-12-01 20:12:24 +01008353 msg_scroll = FALSE; // no msg scrolling in Normal mode
8354 restart_edit = 0; // don't go to Insert mode
8355 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008356
Bram Moolenaara452b802020-12-01 21:47:59 +01008357 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008358 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008359
8360 /*
8361 * Save the current typeahead. This is required to allow using ":normal"
8362 * from an event handler and makes sure we don't hang when the argument
8363 * ends with half a command.
8364 */
8365 save_typeahead(&sst->tabuf);
8366 return sst->tabuf.typebuf_valid;
8367}
8368
8369 void
8370restore_current_state(save_state_T *sst)
8371{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008372 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008373 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008374
8375 msg_scroll = sst->save_msg_scroll;
8376 restart_edit = sst->save_restart_edit;
8377 p_im = sst->save_insertmode;
8378 finish_op = sst->save_finish_op;
8379 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008380 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008381 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008382 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008384 // Restore the state (needed when called from a function executed for
8385 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008386 State = sst->save_State;
8387#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008388 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008389#endif
8390}
8391
8392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 * ":normal[!] {commands}": Execute normal mode commands.
8394 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008395 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008396ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008398 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 char_u *arg = NULL;
8400 int l;
8401 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008403 if (ex_normal_lock > 0)
8404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008405 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008406 return;
8407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 if (ex_normal_busy >= p_mmd)
8409 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008410 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 return;
8412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 /*
8415 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8416 * this for the K_SPECIAL leading byte, otherwise special keys will not
8417 * work.
8418 */
8419 if (has_mbyte)
8420 {
8421 int len = 0;
8422
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008423 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 for (p = eap->arg; *p != NUL; ++p)
8425 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008426#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008427 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008429#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008430 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008431 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008432#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 )
8436 len += 2;
8437 }
8438 if (len > 0)
8439 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008440 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 if (arg != NULL)
8442 {
8443 len = 0;
8444 for (p = eap->arg; *p != NUL; ++p)
8445 {
8446 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008447#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 if (*p == CSI)
8449 {
8450 arg[len++] = KS_EXTRA;
8451 arg[len++] = (int)KE_CSI;
8452 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008453#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008454 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {
8456 arg[len++] = *++p;
8457 if (*p == K_SPECIAL)
8458 {
8459 arg[len++] = KS_SPECIAL;
8460 arg[len++] = KE_FILLER;
8461 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008462#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 else if (*p == CSI)
8464 {
8465 arg[len++] = KS_EXTRA;
8466 arg[len++] = (int)KE_CSI;
8467 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 }
8470 arg[len] = NUL;
8471 }
8472 }
8473 }
8474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008476 ++ex_normal_busy;
8477 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 {
8479 /*
8480 * Repeat the :normal command for each line in the range. When no
8481 * range given, execute it just once, without positioning the cursor
8482 * first.
8483 */
8484 do
8485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 if (eap->addr_count != 0)
8487 {
8488 curwin->w_cursor.lnum = eap->line1++;
8489 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008490 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 }
8492
Bram Moolenaar13505972019-01-24 15:04:48 +01008493 exec_normal_cmd(arg != NULL
8494 ? arg
8495 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 }
8497 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8498 }
8499
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008500 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 update_topline_cursor();
8502
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008503 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008505 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008506#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008507 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008508#endif
8509
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511}
8512
8513/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008514 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 */
8516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008517ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008519 if (eap->forceit)
8520 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008521 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008522 if (!curwin->w_cursor.lnum)
8523 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008524 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008525 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008526#ifdef FEAT_TERMINAL
8527 // Ignore this when running in an active terminal.
8528 if (term_job_running(curbuf->b_term))
8529 return;
8530#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008531
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008532 // Ignore the command when already in Insert mode. Inserting an
8533 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008534 if (State & INSERT)
8535 return;
8536
Bram Moolenaar64969662005-12-14 21:59:55 +00008537 if (eap->cmdidx == CMD_startinsert)
8538 restart_edit = 'a';
8539 else if (eap->cmdidx == CMD_startreplace)
8540 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008542 restart_edit = 'V';
8543
8544 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008546 if (eap->cmdidx == CMD_startinsert)
8547 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008548 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008550
8551 if (VIsual_active)
8552 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553}
8554
8555/*
8556 * ":stopinsert"
8557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008559ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
8561 restart_edit = 0;
8562 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008563 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008566/*
8567 * Execute normal mode command "cmd".
8568 * "remap" can be REMAP_NONE or REMAP_YES.
8569 */
8570 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008571exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008572{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008573 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008574 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008575 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008576}
Bram Moolenaar25281632016-01-21 23:32:32 +01008577
Bram Moolenaar25281632016-01-21 23:32:32 +01008578/*
8579 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008580 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008581 */
8582 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008583exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008584{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008585 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008586 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008587
Bram Moolenaar905dd902019-04-07 14:21:47 +02008588 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8589 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008590 clear_oparg(&oa);
8591 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008592 while ((!stuff_empty()
8593 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008594 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008595 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008596 {
8597 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008598#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008599 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008600 && oa.op_type == OP_NOP && oa.regname == NUL
8601 && !VIsual_active)
8602 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008603 // If terminal_loop() returns OK we got a key that is handled
8604 // in Normal model. With FAIL we first need to position the
8605 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008606 if (terminal_loop(TRUE) == OK)
8607 normal_cmd(&oa, TRUE);
8608 }
8609 else
8610#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008611 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008612 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008613 }
8614}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008615
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616#ifdef FEAT_FIND_ID
8617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008618ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619{
8620 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8621 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8622 (linenr_T)1, (linenr_T)MAXLNUM);
8623}
8624
Bram Moolenaar4033c552017-09-16 20:54:51 +02008625#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626/*
8627 * ":psearch"
8628 */
8629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008630ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631{
8632 g_do_tagpreview = p_pvh;
8633 ex_findpat(eap);
8634 g_do_tagpreview = 0;
8635}
8636#endif
8637
8638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008639ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640{
8641 int whole = TRUE;
8642 long n;
8643 char_u *p;
8644 int action;
8645
8646 switch (cmdnames[eap->cmdidx].cmd_name[2])
8647 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008648 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8650 action = ACTION_GOTO;
8651 else
8652 action = ACTION_SHOW;
8653 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008654 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 action = ACTION_SHOW_ALL;
8656 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008657 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 action = ACTION_GOTO;
8659 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008660 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 action = ACTION_SPLIT;
8662 break;
8663 }
8664
8665 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008666 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 {
8668 n = getdigits(&eap->arg);
8669 eap->arg = skipwhite(eap->arg);
8670 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008671 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 {
8673 whole = FALSE;
8674 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008675 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 if (*p)
8677 {
8678 *p++ = NUL;
8679 p = skipwhite(p);
8680
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008681 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008682 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008683 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008685 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 }
8687 }
8688 if (!eap->skip)
8689 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8690 whole, !eap->forceit,
8691 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8692 n, action, eap->line1, eap->line2);
8693}
8694#endif
8695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
Bram Moolenaar4033c552017-09-16 20:54:51 +02008697#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698/*
8699 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8700 */
8701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008702ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008704 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8706}
8707
8708/*
8709 * ":pedit"
8710 */
8711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008712ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713{
8714 win_T *curwin_save = curwin;
8715
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008716 if (ERROR_IF_ANY_POPUP_WINDOW)
8717 return;
8718
Bram Moolenaar026587b2019-08-17 15:08:00 +02008719 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008721 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008722
Bram Moolenaar026587b2019-08-17 15:08:00 +02008723 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008725
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 if (curwin != curwin_save && win_valid(curwin_save))
8727 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008728 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 validate_cursor();
8730 redraw_later(VALID);
8731 win_enter(curwin_save, TRUE);
8732 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008733# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008734 else if (WIN_IS_POPUP(curwin))
8735 {
8736 // can't keep focus in popup window
8737 win_enter(firstwin, TRUE);
8738 }
8739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 g_do_tagpreview = 0;
8741}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
8744/*
8745 * ":stag", ":stselect" and ":stjump".
8746 */
8747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008748ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749{
8750 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008751 postponed_split_flags = cmdmod.cmod_split;
8752 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8754 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008755 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757
8758/*
8759 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8760 */
8761 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008762ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763{
8764 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8765}
8766
8767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008768ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769{
8770 int cmd;
8771
8772 switch (name[1])
8773 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008774 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008776 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008778 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008780 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008782 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008784 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008786 case 'f': // ":tfirst"
8787 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008789 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008791 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008793 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008795 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 return;
8797 }
8798#endif
8799 cmd = DT_TAG;
8800 break;
8801 }
8802
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008803 if (name[0] == 'l')
8804 {
8805#ifndef FEAT_QUICKFIX
8806 ex_ni(eap);
8807 return;
8808#else
8809 cmd = DT_LTAG;
8810#endif
8811 }
8812
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8814 eap->forceit, TRUE);
8815}
8816
8817/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008818 * Check "str" for starting with a special cmdline variable.
8819 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8820 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8821 */
8822 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008823find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008824{
8825 int len;
8826 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008827 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008828 "%",
8829#define SPEC_PERC 0
8830 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008831#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008832 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008833#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008834 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008835#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008836 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008837#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008838 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008839#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008840 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008841#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008842 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008843#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008844 "<stack>", // call stack
8845#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008846 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008847#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008848 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008849#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008850 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008851#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008852 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008853#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008854 "<SID>", // script ID: <SNR>123_
8855#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008856#ifdef FEAT_CLIENTSERVER
8857 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008858# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008859#endif
8860 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008861
K.Takataeeec2542021-06-02 13:28:16 +02008862 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008863 {
8864 len = (int)STRLEN(spec_str[i]);
8865 if (STRNCMP(src, spec_str[i], len) == 0)
8866 {
8867 *usedlen = len;
8868 return i;
8869 }
8870 }
8871 return -1;
8872}
8873
8874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 * Evaluate cmdline variables.
8876 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008877 * change "%" to curbuf->b_ffname
8878 * "#" to curwin->w_alt_fnum
8879 * "%%" to curwin->w_alt_fnum in Vim9 script
8880 * "<cword>" to word under the cursor
8881 * "<cWORD>" to WORD under the cursor
8882 * "<cexpr>" to C-expression under the cursor
8883 * "<cfile>" to path name under the cursor
8884 * "<sfile>" to sourced file name
8885 * "<stack>" to call stack
8886 * "<slnum>" to sourced file line number
8887 * "<afile>" to file name for autocommand
8888 * "<abuf>" to buffer number for autocommand
8889 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 *
8891 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8892 * "" for error without a message) and NULL is returned.
8893 * Returns an allocated string if a valid match was found.
8894 * Returns NULL if no match was found. "usedlen" then still contains the
8895 * number of characters to skip.
8896 */
8897 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008898eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008899 char_u *src, // pointer into commandline
8900 char_u *srcstart, // beginning of valid memory for src
8901 int *usedlen, // characters after src that are used
8902 linenr_T *lnump, // line number for :e command, or NULL
8903 char **errormsg, // pointer to error message
8904 int *escaped) // return value has escaped white space (can
8905 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
8907 int i;
8908 char_u *s;
8909 char_u *result;
8910 char_u *resultbuf = NULL;
8911 int resultlen;
8912 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008913 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008915 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918
8919 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008920 if (escaped != NULL)
8921 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922
8923 /*
8924 * Check if there is something to do.
8925 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008926 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008927 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 {
8929 *usedlen = 1;
8930 return NULL;
8931 }
8932
8933 /*
8934 * Skip when preceded with a backslash "\%" and "\#".
8935 * Note: In "\\%" the % is also not recognized!
8936 */
8937 if (src > srcstart && src[-1] == '\\')
8938 {
8939 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008940 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 return NULL;
8942 }
8943
8944 /*
8945 * word or WORD under cursor
8946 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008947 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8948 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008950 resultlen = find_ident_under_cursor(&result,
8951 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8952 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8953 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 if (resultlen == 0)
8955 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008956 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 return NULL;
8958 }
8959 }
8960
8961 /*
8962 * '#': Alternate file name
8963 * '%': Current file name
8964 * File name under the cursor
8965 * File name for autocommand
8966 * and following modifiers
8967 */
8968 else
8969 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008970 int off = 0;
8971
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 switch (spec_idx)
8973 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008974 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008975#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008976 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008979 // '%': current file
8980 if (curbuf->b_fname == NULL)
8981 {
8982 result = (char_u *)"";
8983 valid = 0; // Must have ":p:h" to be valid
8984 }
8985 else
8986 {
8987 result = curbuf->b_fname;
8988 tilde_file = STRCMP(result, "~") == 0;
8989 }
8990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008992#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008993 // "%%" alternate file
8994 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008995#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008996 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008997 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008998 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009000 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 result = arg_all();
9002 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009003 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009004 if (escaped != NULL)
9005 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 break;
9008 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009009 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009010 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009011 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009013 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009014 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009015 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009016 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009018 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009020 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009021 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009022 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009023 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009024 return NULL;
9025 }
9026#ifdef FEAT_EVAL
9027 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9028 (long)i);
9029 if (result == NULL)
9030 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009031 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009032 return NULL;
9033 }
9034#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009035 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 }
9039 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009040 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009041 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9042 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009043 buf = buflist_findnr(i);
9044 if (buf == NULL)
9045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009046 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009047 return NULL;
9048 }
9049 if (lnump != NULL)
9050 *lnump = ECMD_LAST;
9051 if (buf->b_fname == NULL)
9052 {
9053 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009054 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009055 }
9056 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009057 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009058 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009059 tilde_file = STRCMP(result, "~") == 0;
9060 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 break;
9063
9064#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009065 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009066 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 if (result == NULL)
9068 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009069 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 return NULL;
9071 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009072 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 break;
9074#endif
9075
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009076 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009078 if (result != NULL && !autocmd_fname_full)
9079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009080 // Still need to turn the fname into a full path. It is
9081 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009082 autocmd_fname_full = TRUE;
9083 result = FullName_save(autocmd_fname, FALSE);
9084 vim_free(autocmd_fname);
9085 autocmd_fname = result;
9086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 if (result == NULL)
9088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009089 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 return NULL;
9091 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009092 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 break;
9094
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009095 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 if (autocmd_bufnr <= 0)
9097 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009098 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 return NULL;
9100 }
9101 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9102 result = strbuf;
9103 break;
9104
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009105 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 result = autocmd_match;
9107 if (result == NULL)
9108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009109 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 return NULL;
9111 }
9112 break;
9113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009114 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02009115 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02009116 result = estack_sfile(spec_idx == SPEC_SFILE
9117 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 if (result == NULL)
9119 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02009120 *errormsg = spec_idx == SPEC_SFILE
9121 ? _("E498: no :source file name to substitute for \"<sfile>\"")
9122 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 return NULL;
9124 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009125 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009127
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009128 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009129 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009131 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009132 return NULL;
9133 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009134 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009135 result = strbuf;
9136 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009137
9138#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009139 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009140 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009142 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009143 return NULL;
9144 }
9145 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009146 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009147 result = strbuf;
9148 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009149
Bram Moolenaar90944302020-08-01 20:45:11 +02009150 case SPEC_SID:
9151 if (current_sctx.sc_sid <= 0)
9152 {
9153 *errormsg = _(e_usingsid);
9154 return NULL;
9155 }
9156 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9157 result = strbuf;
9158 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009159#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009160
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009161#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009162 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009163 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9164 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 result = strbuf;
9166 break;
9167#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009168
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009169 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009170 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 }
9173
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009174 resultlen = (int)STRLEN(result); // length of new string
9175 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 {
9177 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 resultlen = (int)(s - result);
9180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 else if (!skip_mod)
9182 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009183 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 &resultlen);
9185 if (result == NULL)
9186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009187 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 return NULL;
9189 }
9190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 }
9192
9193 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9194 {
9195 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009196 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009197 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009199 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 result = NULL;
9201 }
9202 else
9203 result = vim_strnsave(result, resultlen);
9204 vim_free(resultbuf);
9205 return result;
9206}
9207
9208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 * Expand the <sfile> string in "arg".
9210 *
9211 * Returns an allocated string, or NULL for any error.
9212 */
9213 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009214expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009216 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 int len;
9218 char_u *result;
9219 char_u *newres;
9220 char_u *repl;
9221 int srclen;
9222 char_u *p;
9223
9224 result = vim_strsave(arg);
9225 if (result == NULL)
9226 return NULL;
9227
9228 for (p = result; *p; )
9229 {
9230 if (STRNCMP(p, "<sfile>", 7) != 0)
9231 ++p;
9232 else
9233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009234 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009235 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 if (errormsg != NULL)
9237 {
9238 if (*errormsg)
9239 emsg(errormsg);
9240 vim_free(result);
9241 return NULL;
9242 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009243 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 {
9245 p += srclen;
9246 continue;
9247 }
9248 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9249 newres = alloc(len);
9250 if (newres == NULL)
9251 {
9252 vim_free(repl);
9253 vim_free(result);
9254 return NULL;
9255 }
9256 mch_memmove(newres, result, (size_t)(p - result));
9257 STRCPY(newres + (p - result), repl);
9258 len = (int)STRLEN(newres);
9259 STRCAT(newres, p + srclen);
9260 vim_free(repl);
9261 vim_free(result);
9262 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009263 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 }
9265 }
9266
9267 return result;
9268}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009271/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009272 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009273 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009276dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 if (fname == NULL)
9279 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009280 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281}
9282#endif
9283
9284/*
9285 * ":behave {mswin,xterm}"
9286 */
9287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009288ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289{
9290 if (STRCMP(eap->arg, "mswin") == 0)
9291 {
9292 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9293 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9294 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9295 set_option_value((char_u *)"keymodel", 0L,
9296 (char_u *)"startsel,stopsel", 0);
9297 }
9298 else if (STRCMP(eap->arg, "xterm") == 0)
9299 {
9300 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9301 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9302 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9303 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9304 }
9305 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009306 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307}
9308
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309static int filetype_detect = FALSE;
9310static int filetype_plugin = FALSE;
9311static int filetype_indent = FALSE;
9312
9313/*
9314 * ":filetype [plugin] [indent] {on,off,detect}"
9315 * on: Load the filetype.vim file to install autocommands for file types.
9316 * off: Load the ftoff.vim file to remove all autocommands for file types.
9317 * plugin on: load filetype.vim and ftplugin.vim
9318 * plugin off: load ftplugof.vim
9319 * indent on: load filetype.vim and indent.vim
9320 * indent off: load indoff.vim
9321 */
9322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009323ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
9325 char_u *arg = eap->arg;
9326 int plugin = FALSE;
9327 int indent = FALSE;
9328
9329 if (*eap->arg == NUL)
9330 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009331 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009332 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 filetype_detect ? "ON" : "OFF",
9334 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9335 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9336 return;
9337 }
9338
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009339 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 for (;;)
9341 {
9342 if (STRNCMP(arg, "plugin", 6) == 0)
9343 {
9344 plugin = TRUE;
9345 arg = skipwhite(arg + 6);
9346 continue;
9347 }
9348 if (STRNCMP(arg, "indent", 6) == 0)
9349 {
9350 indent = TRUE;
9351 arg = skipwhite(arg + 6);
9352 continue;
9353 }
9354 break;
9355 }
9356 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9357 {
9358 if (*arg == 'o' || !filetype_detect)
9359 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009360 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 filetype_detect = TRUE;
9362 if (plugin)
9363 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009364 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 filetype_plugin = TRUE;
9366 }
9367 if (indent)
9368 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009369 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 filetype_indent = TRUE;
9371 }
9372 }
9373 if (*arg == 'd')
9374 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009375 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009376 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 }
9378 }
9379 else if (STRCMP(arg, "off") == 0)
9380 {
9381 if (plugin || indent)
9382 {
9383 if (plugin)
9384 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009385 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 filetype_plugin = FALSE;
9387 }
9388 if (indent)
9389 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009390 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 filetype_indent = FALSE;
9392 }
9393 }
9394 else
9395 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009396 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 filetype_detect = FALSE;
9398 }
9399 }
9400 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009401 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402}
9403
9404/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009405 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 */
9407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009408ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
9410 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009411 {
9412 char_u *arg = eap->arg;
9413
9414 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9415 arg += 9;
9416
9417 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9418 if (arg != eap->arg)
9419 did_filetype = FALSE;
9420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009424ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425{
9426#ifdef FEAT_DIGRAPHS
9427 if (*eap->arg != NUL)
9428 putdigraph(eap->arg);
9429 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009430 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431#else
mityu61065042021-07-19 20:07:21 +02009432 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433#endif
9434}
9435
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009436#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9437 void
9438set_no_hlsearch(int flag)
9439{
9440 no_hlsearch = flag;
9441# ifdef FEAT_EVAL
9442 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9443# endif
9444}
9445
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446/*
9447 * ":nohlsearch"
9448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009450ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009452 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009453 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455#endif
9456
9457#ifdef FEAT_CRYPT
9458/*
9459 * ":X": Get crypt key
9460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009462ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009464 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009465 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466}
9467#endif
9468
9469#ifdef FEAT_FOLDING
9470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009471ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472{
9473 if (foldManualAllowed(TRUE))
9474 foldCreate(eap->line1, eap->line2);
9475}
9476
9477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009478ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479{
9480 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9481 eap->forceit, FALSE);
9482}
9483
9484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009485ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486{
9487 linenr_T lnum;
9488
Bram Moolenaar4facea32019-10-12 20:17:40 +02009489# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009490 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009491# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009493 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9495 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9496 ml_setmarked(lnum);
9497
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009498 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009500 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009501# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009502 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504}
9505#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009507#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009508/*
9509 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9510 * instead of a quickfix command.
9511 */
9512 int
9513is_loclist_cmd(int cmdidx)
9514{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009515 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009516 return FALSE;
9517 return cmdnames[cmdidx].cmd_name[0] == 'l';
9518}
9519#endif
9520
Bram Moolenaar4facea32019-10-12 20:17:40 +02009521#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009522 int
9523get_pressedreturn(void)
9524{
9525 return ex_pressedreturn;
9526}
9527
9528 void
9529set_pressedreturn(int val)
9530{
9531 ex_pressedreturn = val;
9532}
9533#endif