blob: 617de6f06d58a7b749b1e991b5b4f3ea18e5b076 [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 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002385 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002386 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002387 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002388 }
2389#endif
2390 ea.arg = skipwhite(ea.arg);
2391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 }
2393
2394 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002395 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 * count, it's a buffer name.
2397 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002398 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002399 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002400 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
2402 n = getdigits(&ea.arg);
2403 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002404 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002406 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 goto doend;
2408 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002409 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
2411 ea.line2 = n;
2412 if (ea.addr_count == 0)
2413 ea.addr_count = 1;
2414 }
2415 else
2416 {
2417 ea.line1 = ea.line2;
2418 ea.line2 += n - 1;
2419 ++ea.addr_count;
2420 /*
2421 * Be vi compatible: no error message for out of range.
2422 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002423 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 ea.line2 = curbuf->b_ml.ml_line_count;
2425 }
2426 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002427
2428 /*
2429 * Check for flags: 'l', 'p' and '#'.
2430 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002431 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002432 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002433 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2434 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002436 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002437 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 goto doend;
2439 }
2440
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002441 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002443 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 goto doend;
2445 }
2446
2447#ifdef FEAT_EVAL
2448 /*
2449 * Skip the command when it's not going to be executed.
2450 * The commands like :if, :endif, etc. always need to be executed.
2451 * Also make an exception for commands that handle a trailing command
2452 * themselves.
2453 */
2454 if (ea.skip)
2455 {
2456 switch (ea.cmdidx)
2457 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002458 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 case CMD_while:
2460 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002461 case CMD_for:
2462 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 case CMD_if:
2464 case CMD_elseif:
2465 case CMD_else:
2466 case CMD_endif:
2467 case CMD_try:
2468 case CMD_catch:
2469 case CMD_finally:
2470 case CMD_endtry:
2471 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002472 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 break;
2474
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002475 // Commands that handle '|' themselves. Check: A command should
2476 // either have the EX_TRLBAR flag, appear in this list or appear in
2477 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 case CMD_aboveleft:
2479 case CMD_and:
2480 case CMD_belowright:
2481 case CMD_botright:
2482 case CMD_browse:
2483 case CMD_call:
2484 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002485 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 case CMD_delfunction:
2487 case CMD_djump:
2488 case CMD_dlist:
2489 case CMD_dsearch:
2490 case CMD_dsplit:
2491 case CMD_echo:
2492 case CMD_echoerr:
2493 case CMD_echomsg:
2494 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002495 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002497 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002498 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 case CMD_help:
2500 case CMD_hide:
2501 case CMD_ijump:
2502 case CMD_ilist:
2503 case CMD_isearch:
2504 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002505 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 case CMD_keepjumps:
2507 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002508 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_leftabove:
2510 case CMD_let:
2511 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002512 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002513 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002515 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002516 case CMD_noautocmd:
2517 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 case CMD_perl:
2519 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002520 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002521 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002522 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 case CMD_return:
2524 case CMD_rightbelow:
2525 case CMD_ruby:
2526 case CMD_silent:
2527 case CMD_smagic:
2528 case CMD_snomagic:
2529 case CMD_substitute:
2530 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002531 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 case CMD_tcl:
2533 case CMD_throw:
2534 case CMD_tilde:
2535 case CMD_topleft:
2536 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002537 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002538 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 case CMD_verbose:
2540 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002541 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 break;
2543
2544 default: goto doend;
2545 }
2546 }
2547#endif
2548
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002549 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
2551 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2552 goto doend;
2553 }
2554
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 /*
2556 * Accept buffer name. Cannot be used at the same time with a buffer
2557 * number. Don't do this for a user command.
2558 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002559 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002560 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
2562 /*
2563 * :bdelete, :bwipeout and :bunload take several arguments, separated
2564 * by spaces: find next space (skipping over escaped characters).
2565 * The others take one argument: ignore trailing spaces.
2566 */
2567 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2568 || ea.cmdidx == CMD_bunload)
2569 p = skiptowhite_esc(ea.arg);
2570 else
2571 {
2572 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002573 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 --p;
2575 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002576 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002577 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002578 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 goto doend;
2580 ea.addr_count = 1;
2581 ea.arg = skipwhite(p);
2582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002584 // The :try command saves the emsg_silent flag, reset it here when
2585 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002586 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002587 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002588 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002589 if (emsg_silent < 0)
2590 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002591 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002592 }
2593
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002595 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
Bram Moolenaar958636c2014-10-21 20:01:58 +02002598 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
2600 /*
2601 * Execute a user-defined command.
2602 */
2603 do_ucmd(&ea);
2604 }
2605 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 {
2607 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002608 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 */
2610 ea.errmsg = NULL;
2611 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2612 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002613 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 }
2615
2616#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002618 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002619 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002620 && current_sctx.sc_sid > 0
2621 && ea.cmdidx != CMD_endif
2622 && (cstack->cs_idx < 0
2623 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002624 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 /*
2627 * If the command just executed called do_cmdline(), any throw or ":return"
2628 * or ":finish" encountered there must also check the cstack of the still
2629 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2630 * exception, or reanimate a returned function or finished script file and
2631 * return or finish it again.
2632 */
2633 if (need_rethrow)
2634 do_throw(cstack);
2635 else if (check_cstack)
2636 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002637 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002639 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 && current_func_returned())
2641 do_return(&ea, TRUE, FALSE, NULL);
2642 }
2643 need_rethrow = check_cstack = FALSE;
2644#endif
2645
2646doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002647 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002648 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002650 curwin->w_cursor.col = 0;
2651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2654 {
2655 if (sourcing)
2656 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002657 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
2659 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002660 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002662 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
2664 emsg(errormsg);
2665 }
2666#ifdef FEAT_EVAL
2667 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002668 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2669 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002670
2671 if (did_set_expr_line)
2672 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673#endif
2674
Bram Moolenaare1004402020-10-24 20:49:43 +02002675 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002677 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002679 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 ea.nextcmd = NULL;
2681
2682#ifdef FEAT_EVAL
2683 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002684 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685#endif
2686
2687 return ea.nextcmd;
2688}
2689#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002690 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691#endif
2692
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002693static char ex_error_buf[MSG_BUF_LEN];
2694
2695/*
2696 * Return an error message with argument included.
2697 * Uses a static buffer, only the last error will be kept.
2698 * "msg" will be translated, caller should use N_().
2699 */
2700 char *
2701ex_errmsg(char *msg, char_u *arg)
2702{
2703 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2704 return ex_error_buf;
2705}
2706
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002708 * Check for an Ex command with optional tail.
2709 * If there is a match advance "pp" to the argument and return TRUE.
2710 * If "noparen" is TRUE do not recognize the command followed by "(".
2711 */
2712 static int
2713checkforcmd_opt(
2714 char_u **pp, // start of command
2715 char *cmd, // name of command
2716 int len, // required length
2717 int noparen)
2718{
2719 int i;
2720
2721 for (i = 0; cmd[i] != NUL; ++i)
2722 if (((char_u *)cmd)[i] != (*pp)[i])
2723 break;
2724 if (i >= len && !isalpha((*pp)[i])
2725 && (*pp)[i] != '_' && (!noparen || (*pp)[i] != '('))
2726 {
2727 *pp = skipwhite(*pp + i);
2728 return TRUE;
2729 }
2730 return FALSE;
2731}
2732
2733/*
2734 * Check for an Ex command with optional tail.
2735 * If there is a match advance "pp" to the argument and return TRUE.
2736 */
2737 int
2738checkforcmd(
2739 char_u **pp, // start of command
2740 char *cmd, // name of command
2741 int len) // required length
2742{
2743 return checkforcmd_opt(pp, cmd, len, FALSE);
2744}
2745
2746/*
2747 * Check for an Ex command with optional tail, not followed by "(".
2748 * If there is a match advance "pp" to the argument and return TRUE.
2749 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002750 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002751checkforcmd_noparen(
2752 char_u **pp, // start of command
2753 char *cmd, // name of command
2754 int len) // required length
2755{
2756 return checkforcmd_opt(pp, cmd, len, TRUE);
2757}
2758
2759/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002760 * Parse and skip over command modifiers:
2761 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002762 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002764 * When "skip_only" is TRUE the global variables are not changed, except for
2765 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002766 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2767 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002768 * Call apply_cmdmod() to get the side effects of the modifiers:
2769 * - Increment "sandbox" for ":sandbox"
2770 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002771 * - set msg_silent for ":silent"
2772 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002773 * Return FAIL when the command is not to be executed.
2774 * May set "errormsg" to an error message.
2775 */
2776 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002777parse_command_modifiers(
2778 exarg_T *eap,
2779 char **errormsg,
2780 cmdmod_T *cmod,
2781 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002782{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002783 char_u *p;
2784 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002785
Bram Moolenaare1004402020-10-24 20:49:43 +02002786 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002787
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002788 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002789 for (;;)
2790 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002792 {
2793 if (*eap->cmd == ':')
2794 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002795 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002796 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002797
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002798 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 if (*eap->cmd == NUL && exmode_active
2800 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2801 || getline_equal(eap->getline, eap->cookie, getexline))
2802 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2803 {
2804 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002805 if (!skip_only)
2806 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002807 }
2808
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002809 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002810 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002811 {
2812 // a comment ends at a NL
2813 if (eap->nextcmd == NULL)
2814 {
2815 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2816 if (eap->nextcmd != NULL)
2817 ++eap->nextcmd;
2818 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002820 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002821 if (*eap->cmd == NUL)
2822 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002823 if (!skip_only)
2824 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002825 return FAIL;
2826 }
2827
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002828 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002829
2830 // In Vim9 script a variable can shadow a command modifier:
2831 // verbose = 123
2832 // verbose += 123
2833 // silent! verbose = func()
2834 // verbose.member = 2
2835 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002836 // But not:
2837 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002838 if (in_vim9script())
2839 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002840 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002841
2842 for (s = p; ASCII_ISALPHA(*s); ++s)
2843 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002844 n = skipwhite(s);
2845 if (vim_strchr((char_u *)".=", *n) != NULL
2846 || *s == '['
2847 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002848 break;
2849 }
2850
Bram Moolenaareffed932018-08-14 13:38:17 +02002851 switch (*p)
2852 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002853 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002854 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002855 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002856 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 continue;
2858
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002859 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002861 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002862 continue;
2863 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002864 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 {
2866#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002867 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002868#endif
2869 continue;
2870 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002871 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002872 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002873 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002874 continue;
2875
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002876 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 break;
2878#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002879 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002880#endif
2881 continue;
2882
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002883 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002885 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 continue;
2887 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002888 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002890 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002891 continue;
2892 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002893 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002895 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002896 continue;
2897 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002898 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002899 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002900 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002901 continue;
2902
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002903 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002904 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002905 char_u *reg_pat;
2906 char_u *nulp = NULL;
2907 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002908
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002909 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaareffed932018-08-14 13:38:17 +02002910 || *p == NUL || ends_excmd(*p))
2911 break;
2912 if (*p == '!')
2913 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002914 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002915 p = skipwhite(p + 1);
2916 if (*p == NUL || ends_excmd(*p))
2917 break;
2918 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002919#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002920 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002921 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002922 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002923#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002924 if (skip_only)
2925 p = skip_vimgrep_pat(p, NULL, NULL);
2926 else
2927 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002928 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2929 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002930 if (p == NULL || *p == NUL)
2931 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002932 if (!skip_only)
2933 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002934 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002935 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002936 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002937 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002938 // restore the character overwritten by NUL
2939 if (nulp != NULL)
2940 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002941 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002942 eap->cmd = p;
2943 continue;
2944 }
2945
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002946 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002947 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002948 || *p == NUL || ends_excmd(*p))
2949 break;
2950 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002951 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002952 continue;
2953
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002954 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002955 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002956 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002957 continue;
2958 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002959 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2960 {
2961 if (ends_excmd2(p, eap->cmd))
2962 {
2963 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02002964 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002965 return FAIL;
2966 }
2967 cmod->cmod_flags |= CMOD_LEGACY;
2968 continue;
2969 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002970
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002971 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002972 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002973 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002974 continue;
2975
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002976 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002977 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002978 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002979 continue;
2980 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002981 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002982 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002983 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002984 continue;
2985
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002986 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 continue;
2990
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002991 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002992 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002993 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 continue;
2995 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002996 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002998 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3000 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003001 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003003 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 }
3005 continue;
3006
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003007 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003008 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003009 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003010 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003011 long tabnr = get_address(eap, &eap->cmd,
3012 ADDR_TABS, eap->skip,
3013 skip_only, FALSE, 1);
3014 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003015 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003016 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003017 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003018 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3019 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003020 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003021 return FAIL;
3022 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003023 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003024 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003025 }
3026 eap->cmd = p;
3027 continue;
3028 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003029 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003030 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003031 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003032 continue;
3033
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003034 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003035 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003036 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003037 continue;
3038
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003039 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003040 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003041 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003042 continue;
3043 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003044 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003045 {
3046 if (ends_excmd2(p, eap->cmd))
3047 {
3048 *errormsg =
3049 _(e_vim9cmd_must_be_followed_by_command);
3050 return FAIL;
3051 }
3052 cmod->cmod_flags |= CMOD_VIM9CMD;
3053 continue;
3054 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003055 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003056 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003057 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003058 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003059 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003060 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003061 eap->cmd = p;
3062 continue;
3063 }
3064 break;
3065 }
3066
3067 return OK;
3068}
3069
3070/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003071 * Return TRUE if "cmod" has anything set.
3072 */
3073 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003074has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003075{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003076 return (cmod->cmod_flags != 0 && (!ignore_silent
3077 || (cmod->cmod_flags
3078 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003079 || cmod->cmod_split != 0
3080 || cmod->cmod_verbose != 0
3081 || cmod->cmod_tab != 0
3082 || cmod->cmod_filter_regmatch.regprog != NULL;
3083}
3084
3085/*
3086 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3087 */
3088 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003089cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003090{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003091 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003092 {
3093 emsg(_(e_misplaced_command_modifier));
3094 return TRUE;
3095 }
3096 return FALSE;
3097}
3098
3099/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003100 * Apply the command modifiers. Saves current state in "cmdmod", call
3101 * undo_cmdmod() later.
3102 */
3103 void
3104apply_cmdmod(cmdmod_T *cmod)
3105{
3106#ifdef HAVE_SANDBOX
3107 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3108 {
3109 ++sandbox;
3110 cmod->cmod_did_sandbox = TRUE;
3111 }
3112#endif
3113 if (cmod->cmod_verbose > 0)
3114 {
3115 if (cmod->cmod_verbose_save == 0)
3116 cmod->cmod_verbose_save = p_verbose + 1;
3117 p_verbose = cmod->cmod_verbose;
3118 }
3119
3120 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3121 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003122 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003123 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003124 cmod->cmod_save_msg_scroll = msg_scroll;
3125 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003126 if (cmod->cmod_flags & CMOD_SILENT)
3127 ++msg_silent;
3128 if (cmod->cmod_flags & CMOD_UNSILENT)
3129 msg_silent = 0;
3130
3131 if (cmod->cmod_flags & CMOD_ERRSILENT)
3132 {
3133 ++emsg_silent;
3134 ++cmod->cmod_did_esilent;
3135 }
3136
Bram Moolenaare1004402020-10-24 20:49:43 +02003137 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003138 {
3139 // Set 'eventignore' to "all".
3140 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003141 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003142 set_string_option_direct((char_u *)"ei", -1,
3143 (char_u *)"all", OPT_FREE, SID_NONE);
3144 }
3145}
3146
3147/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003148 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003149 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003150 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003151undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003152{
Bram Moolenaare1004402020-10-24 20:49:43 +02003153 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003154 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003155 p_verbose = cmod->cmod_verbose_save - 1;
3156 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003157 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003158
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003159#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003160 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003161 {
3162 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003163 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003164 }
3165#endif
3166
Bram Moolenaare1004402020-10-24 20:49:43 +02003167 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003169 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003170 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3171 OPT_FREE, SID_NONE);
3172 free_string_option(cmod->cmod_save_ei);
3173 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003174 }
3175
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003176 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003177
Bram Moolenaare1004402020-10-24 20:49:43 +02003178 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003179 {
3180 // messages could be enabled for a serious error, need to check if the
3181 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003182 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3183 msg_silent = cmod->cmod_save_msg_silent - 1;
3184 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003185 if (emsg_silent < 0)
3186 emsg_silent = 0;
3187 // Restore msg_scroll, it's set by file I/O commands, even when no
3188 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003189 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003190
3191 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3192 // somewhere in the line. Put it back in the first column.
3193 if (redirecting())
3194 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003195
Bram Moolenaare1004402020-10-24 20:49:43 +02003196 cmod->cmod_save_msg_silent = 0;
3197 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003198 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003199}
3200
3201/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003202 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003203 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003204 * Return FAIL and set "errormsg" or return OK.
3205 */
3206 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003207parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003208{
3209 int address_count = 1;
3210 linenr_T lnum;
3211
3212 // Repeat for all ',' or ';' separated addresses.
3213 for (;;)
3214 {
3215 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003216 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003217 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003218 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003219 eap->addr_count == 0, address_count++);
3220 if (eap->cmd == NULL) // error detected
3221 return FAIL;
3222 if (lnum == MAXLNUM)
3223 {
3224 if (*eap->cmd == '%') // '%' - all lines
3225 {
3226 ++eap->cmd;
3227 switch (eap->addr_type)
3228 {
3229 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003230 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003231 eap->line1 = 1;
3232 eap->line2 = curbuf->b_ml.ml_line_count;
3233 break;
3234 case ADDR_LOADED_BUFFERS:
3235 {
3236 buf_T *buf = firstbuf;
3237
3238 while (buf->b_next != NULL
3239 && buf->b_ml.ml_mfp == NULL)
3240 buf = buf->b_next;
3241 eap->line1 = buf->b_fnum;
3242 buf = lastbuf;
3243 while (buf->b_prev != NULL
3244 && buf->b_ml.ml_mfp == NULL)
3245 buf = buf->b_prev;
3246 eap->line2 = buf->b_fnum;
3247 break;
3248 }
3249 case ADDR_BUFFERS:
3250 eap->line1 = firstbuf->b_fnum;
3251 eap->line2 = lastbuf->b_fnum;
3252 break;
3253 case ADDR_WINDOWS:
3254 case ADDR_TABS:
3255 if (IS_USER_CMDIDX(eap->cmdidx))
3256 {
3257 eap->line1 = 1;
3258 eap->line2 = eap->addr_type == ADDR_WINDOWS
3259 ? LAST_WIN_NR : LAST_TAB_NR;
3260 }
3261 else
3262 {
3263 // there is no Vim command which uses '%' and
3264 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003265 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003266 return FAIL;
3267 }
3268 break;
3269 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003270 case ADDR_UNSIGNED:
3271 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003272 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003273 return FAIL;
3274 case ADDR_ARGUMENTS:
3275 if (ARGCOUNT == 0)
3276 eap->line1 = eap->line2 = 0;
3277 else
3278 {
3279 eap->line1 = 1;
3280 eap->line2 = ARGCOUNT;
3281 }
3282 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003283 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003284#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003285 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003286 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003287 if (eap->line2 == 0)
3288 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003289#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003290 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003291 case ADDR_NONE:
3292 // Will give an error later if a range is found.
3293 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003294 }
3295 ++eap->addr_count;
3296 }
3297 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3298 {
3299 pos_T *fp;
3300
3301 // '*' - visual area
3302 if (eap->addr_type != ADDR_LINES)
3303 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003304 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003305 return FAIL;
3306 }
3307
3308 ++eap->cmd;
3309 if (!eap->skip)
3310 {
3311 fp = getmark('<', FALSE);
3312 if (check_mark(fp) == FAIL)
3313 return FAIL;
3314 eap->line1 = fp->lnum;
3315 fp = getmark('>', FALSE);
3316 if (check_mark(fp) == FAIL)
3317 return FAIL;
3318 eap->line2 = fp->lnum;
3319 ++eap->addr_count;
3320 }
3321 }
3322 }
3323 else
3324 eap->line2 = lnum;
3325 eap->addr_count++;
3326
3327 if (*eap->cmd == ';')
3328 {
3329 if (!eap->skip)
3330 {
3331 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003332 // Don't leave the cursor on an illegal line or column, but do
3333 // accept zero as address, so 0;/PATTERN/ works correctly.
3334 if (eap->line2 > 0)
3335 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003336 }
3337 }
3338 else if (*eap->cmd != ',')
3339 break;
3340 ++eap->cmd;
3341 }
3342
3343 // One address given: set start and end lines.
3344 if (eap->addr_count == 1)
3345 {
3346 eap->line1 = eap->line2;
3347 // ... but only implicit: really no address given
3348 if (lnum == MAXLNUM)
3349 eap->addr_count = 0;
3350 }
3351 return OK;
3352}
3353
3354/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003355 * Append "cmd" to the error message in IObuff.
3356 * Takes care of limiting the length and handling 0xa0, which would be
3357 * invisible otherwise.
3358 */
3359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003360append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003361{
3362 char_u *s = cmd;
3363 char_u *d;
3364
3365 STRCAT(IObuff, ": ");
3366 d = IObuff + STRLEN(IObuff);
3367 while (*s != NUL && d - IObuff < IOSIZE - 7)
3368 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003369 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003370 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003371 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003372 STRCPY(d, "<a0>");
3373 d += 4;
3374 }
3375 else
3376 MB_COPY_CHAR(s, d);
3377 }
3378 *d = NUL;
3379}
3380
3381/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003382 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3383 * the name. Otherwise just return "start".
3384 */
3385 char_u *
3386skip_option_env_lead(char_u *start)
3387{
3388 char_u *name = start;
3389
3390 if (*start == '&')
3391 {
3392 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3393 name += 3;
3394 else
3395 name += 1;
3396 }
3397 else if (*start == '$')
3398 name += 1;
3399 return name;
3400}
3401
3402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003404 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003405 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003406 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 *
3408 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3409 * assignment without "let". Sets eap->cmdidx to the command while returning
3410 * "eap->cmd".
3411 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 * Returns NULL for an ambiguous user command.
3413 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 char_u *
3415find_ex_command(
3416 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003417 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003418 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003419 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
3421 int len;
3422 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003423 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003424#ifndef FEAT_EVAL
3425 int vim9 = FALSE;
3426#else
3427 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 /*
3430 * Recognize a Vim9 script function/method call and assignment:
3431 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3432 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003433 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003434 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003436 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003438 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003439 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003441 int oplen;
3442 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003443 char_u *swp;
3444
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003445 if (*eap->cmd == '&'
3446 || *eap->cmd == '$'
3447 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003448 && (valid_yank_reg(eap->cmd[1], FALSE)
3449 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003450 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003451 if (*eap->cmd == '&')
3452 {
3453 p = eap->cmd + 1;
3454 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3455 p += 2;
3456 p = to_name_end(p, FALSE);
3457 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003458 else if (*eap->cmd == '$')
3459 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003460 else
3461 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003462 if (ends_excmd(*skipwhite(p)))
3463 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003464 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3465 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003466 eap->cmdidx = CMD_eval;
3467 return eap->cmd;
3468 }
3469 // "&option" can be followed by "->" or "=", check below
3470 }
3471
3472 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003474 if (
3475 // "(..." is an expression.
3476 // "funcname(" is always a function call.
3477 *p == '('
3478 || (p == eap->cmd
3479 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003480 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003481 *eap->cmd == '{'
3482 // "'string'->func()" is an expression.
3483 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003484 // '"string"->func()' is an expression.
3485 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003486 // "g:varname" is an expression.
3487 || eap->cmd[1] == ':'
3488 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003489 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003490 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003491 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003492 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3493 {
3494 // "{" by itself is the start of a block.
3495 eap->cmdidx = CMD_block;
3496 return eap->cmd + 1;
3497 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003498 eap->cmdidx = CMD_eval;
3499 return eap->cmd;
3500 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003501
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003502 if (p != eap->cmd && (
3503 // "varname[]" is an expression.
3504 *p == '['
3505 // "varname.key" is an expression.
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02003506 || (*p == '.' && (ASCII_ISALPHA(p[1])
3507 || p[1] == '_'))))
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003508 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003509 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003510
3511 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003512 // Skip over the whole thing, it can be:
3513 // name.member = val
3514 // name[a : b] = val
3515 // name[idx] = val
3516 // name[idx].member = val
3517 // etc.
3518 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003519 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003520 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003521 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003522 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003523 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003524 || (after[0] == '.' && after[1] == '.'
3525 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003526 eap->cmdidx = CMD_var;
3527 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003528 --emsg_silent;
3529 return eap->cmd;
3530 }
3531
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003532 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3533 // an assignment.
3534 // If there is no line break inside the "[...]" then "p" is
3535 // advanced to after the "]" by to_name_const_end(): check if a "="
3536 // follows.
3537 // If "[...]" has a line break "p" still points at the "[" and it
3538 // can't be an assignment.
3539 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003540 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003541 char_u *eq;
3542
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003543 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003544 if (p == eap->cmd && *p == '[')
3545 {
3546 int count = 0;
3547 int semicolon = FALSE;
3548
3549 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3550 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003551 eq = p;
3552 if (eq != NULL)
3553 {
3554 eq = skipwhite(eq);
3555 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3556 ++eq;
3557 }
3558 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003559 {
3560 eap->cmdidx = CMD_eval;
3561 return eap->cmd;
3562 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003563 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003564 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003565 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003566 return eap->cmd;
3567 }
3568 }
3569
3570 // Recognize an assignment if we recognize the variable name:
3571 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003572 // "@r = expr"
3573 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003574 // "var = expr" where "var" is a variable name or we are skipping
3575 // (variable declaration might have been skipped).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003576 oplen = assignment_len(skipwhite(p), &heredoc);
3577 if (oplen > 0)
3578 {
3579 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3580 || *eap->cmd == '&'
3581 || *eap->cmd == '$'
3582 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003583 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003584 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003585 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003586 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003587 return eap->cmd;
3588 }
3589 }
3590
3591 // Recognize using a type for a w:, b:, t: or g: variable:
3592 // "w:varname: number = 123".
3593 if (eap->cmd[1] == ':' && *p == ':')
3594 {
3595 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003596 return eap->cmd;
3597 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003598 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003599
3600 // If it is an ID it might be a variable with an operator on the next
3601 // line, if the variable exists it can't be an Ex command.
3602 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003603 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003604 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3605 {
3606 eap->cmdidx = CMD_eval;
3607 return eap->cmd;
3608 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003609
3610 // Check for "++nr" and "--nr".
3611 if (p == eap->cmd && p[0] == p[1] && (*p == '+' || *p == '-'))
3612 {
3613 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3614 return eap->cmd + 2;
3615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 }
3617#endif
3618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 /*
3620 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003621 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003622 * - The 'k' command can directly be followed by any character.
3623 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003625 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003627 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 */
3629 p = eap->cmd;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003630 if (!vim9 && *p == 'k')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
3632 eap->cmdidx = CMD_k;
3633 ++p;
3634 }
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003635 else if (!vim9
3636 && p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003637 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3638 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 || p[1] == 'g'
3640 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3641 || p[1] == 'I'
3642 || (p[1] == 'r' && p[2] != 'e')))
3643 {
3644 eap->cmdidx = CMD_substitute;
3645 ++p;
3646 }
3647 else
3648 {
3649 while (ASCII_ISALPHA(*p))
3650 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003651 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003652 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003654 while (ASCII_ISALNUM(*p))
3655 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 }
3657 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3658 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003659 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 ++p;
3661 while (ASCII_ISALPHA(*p))
3662 ++p;
3663 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003665 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003666 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 ++p;
3668 len = (int)(p - eap->cmd);
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003669 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003670 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003671 // Check for ":dl", ":dell", etc. to ":deletel": that's
3672 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003673 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003674 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003675 break;
3676 if (i == len - 1)
3677 {
3678 --len;
3679 if (p[-1] == 'l')
3680 eap->flags |= EXFLAG_LIST;
3681 else
3682 eap->flags |= EXFLAG_PRINT;
3683 }
3684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003686 if (ASCII_ISLOWER(eap->cmd[0]))
3687 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003688 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003689 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003690
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003691 if (command_count != (int)CMD_SIZE)
3692 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003693 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003694 getout(1);
3695 }
3696
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003697 // Use a precomputed index for fast look-up in cmdnames[]
3698 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003699 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3700 if (ASCII_ISLOWER(c2))
3701 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3702 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003703 else if (ASCII_ISUPPER(eap->cmd[0]))
3704 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003706 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3709 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3710 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3711 (size_t)len) == 0)
3712 {
3713#ifdef FEAT_EVAL
3714 if (full != NULL
3715 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3716 *full = TRUE;
3717#endif
3718 break;
3719 }
3720
Bram Moolenaar95388e32020-11-20 21:07:00 +01003721 // Not not recognize ":*" as the star command unless '*' is in
3722 // 'cpoptions'.
3723 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3724 p = eap->cmd;
3725
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003726 // Look for a user defined command as a last resort. Let ":Print" be
3727 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003728 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3729 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003731 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 while (ASCII_ISALNUM(*p))
3733 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003734 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003736 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 eap->cmdidx = CMD_SIZE;
3738 }
3739
Bram Moolenaar373863e2020-09-26 17:20:53 +02003740 // ":fina" means ":finally" for backwards compatibility.
3741 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3742 eap->cmdidx = CMD_finally;
3743
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003744#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003745 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003746 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003747 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003748 && (eap->cmdidx < 0 ||
3749 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003750 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003751 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3752
3753 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003754 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003755 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003756 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003757#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003758
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 return p;
3760}
3761
3762#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003763static struct cmdmod
3764{
3765 char *name;
3766 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003767 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003768} cmdmods[] = {
3769 {"aboveleft", 3, FALSE},
3770 {"belowright", 3, FALSE},
3771 {"botright", 2, FALSE},
3772 {"browse", 3, FALSE},
3773 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003774 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003775 {"hide", 3, FALSE},
3776 {"keepalt", 5, FALSE},
3777 {"keepjumps", 5, FALSE},
3778 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003779 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003780 {"leftabove", 5, FALSE},
3781 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003782 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003783 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003784 {"rightbelow", 6, FALSE},
3785 {"sandbox", 3, FALSE},
3786 {"silent", 3, FALSE},
3787 {"tab", 3, TRUE},
3788 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003789 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003790 {"verbose", 4, TRUE},
3791 {"vertical", 4, FALSE},
3792};
3793
3794/*
3795 * Return length of a command modifier (including optional count).
3796 * Return zero when it's not a modifier.
3797 */
3798 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003799modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003800{
3801 int i, j;
3802 char_u *p = cmd;
3803
3804 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003805 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003806 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003807 {
3808 for (j = 0; p[j] != NUL; ++j)
3809 if (p[j] != cmdmods[i].name[j])
3810 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003811 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003812 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003813 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003814 }
3815 return 0;
3816}
3817
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818/*
3819 * Return > 0 if an Ex command "name" exists.
3820 * Return 2 if there is an exact match.
3821 * Return 3 if there is an ambiguous match.
3822 */
3823 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003824cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825{
3826 exarg_T ea;
3827 int full = FALSE;
3828 int i;
3829 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003830 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003832 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003833 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 {
3835 for (j = 0; name[j] != NUL; ++j)
3836 if (name[j] != cmdmods[i].name[j])
3837 break;
3838 if (name[j] == NUL && j >= cmdmods[i].minlen)
3839 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3840 }
3841
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003842 // Check built-in commands and user defined commands.
3843 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003844 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003847 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003849 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3850 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003851 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003852 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3854}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003855
3856/*
3857 * "fullcommand" function
3858 */
3859 void
3860f_fullcommand(typval_T *argvars, typval_T *rettv)
3861{
3862 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003863 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003864 char_u *p;
3865
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003866 rettv->v_type = VAR_STRING;
3867 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003868
3869 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3870 return;
3871
3872 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003873 if (name == NULL)
3874 return;
3875
3876 while (*name != NUL && *name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003877 name++;
3878 name = skip_range(name, TRUE, NULL);
3879
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003880 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3881 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003882 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003883 p = find_ex_command(&ea, NULL, NULL, NULL);
3884 if (p == NULL || ea.cmdidx == CMD_SIZE)
3885 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003886 if (in_vim9script())
3887 {
3888 int res;
3889
3890 ++emsg_silent;
3891 res = not_in_vim9(&ea);
3892 --emsg_silent;
3893
3894 if (res == FAIL)
3895 return;
3896 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003897
3898 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02003899 ? get_user_command_name(ea.useridx, ea.cmdidx)
3900 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003901}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902#endif
3903
Bram Moolenaard0190392019-08-23 21:17:35 +02003904 cmdidx_T
3905excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906{
Bram Moolenaard0190392019-08-23 21:17:35 +02003907 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
Bram Moolenaard0190392019-08-23 21:17:35 +02003909 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3910 idx = (cmdidx_T)((int)idx + 1))
3911 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 break;
3913
Bram Moolenaard0190392019-08-23 21:17:35 +02003914 return idx;
3915}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
Bram Moolenaard0190392019-08-23 21:17:35 +02003917 long
3918excmd_get_argt(cmdidx_T idx)
3919{
3920 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921}
3922
3923/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003924 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 *
3926 * Backslashed delimiters after / or ? will be skipped, and commands will
3927 * not be expanded between /'s and ?'s or after "'".
3928 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003929 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 * Returns the "cmd" pointer advanced to beyond the range.
3931 */
3932 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003933skip_range(
3934 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003935 int skip_star, // skip "*" used for Visual range
3936 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003938 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003940 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003942 if (*cmd == '\\')
3943 {
3944 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3945 ++cmd;
3946 else
3947 break;
3948 }
3949 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
3951 if (*++cmd == NUL && ctx != NULL)
3952 *ctx = EXPAND_NOTHING;
3953 }
3954 else if (*cmd == '/' || *cmd == '?')
3955 {
3956 delim = *cmd++;
3957 while (*cmd != NUL && *cmd != delim)
3958 if (*cmd++ == '\\' && *cmd != NUL)
3959 ++cmd;
3960 if (*cmd == NUL && ctx != NULL)
3961 *ctx = EXPAND_NOTHING;
3962 }
3963 if (*cmd != NUL)
3964 ++cmd;
3965 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003966
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003967 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003968 while (*cmd == ':')
3969 cmd = skipwhite(cmd + 1);
3970
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003971 // Skip "*" used for Visual range.
3972 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3973 cmd = skipwhite(cmd + 1);
3974
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return cmd;
3976}
3977
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003978 static void
3979addr_error(cmd_addr_T addr_type)
3980{
3981 if (addr_type == ADDR_NONE)
3982 emsg(_(e_norange));
3983 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003984 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003985}
3986
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003988 * Return the default address for an address type.
3989 */
3990 static linenr_T
3991default_address(exarg_T *eap)
3992{
3993 linenr_T lnum = 0;
3994
3995 switch (eap->addr_type)
3996 {
3997 case ADDR_LINES:
3998 case ADDR_OTHER:
3999 // Default is the cursor line number. Avoid using an invalid
4000 // line number though.
4001 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4002 lnum = curbuf->b_ml.ml_line_count;
4003 else
4004 lnum = curwin->w_cursor.lnum;
4005 break;
4006 case ADDR_WINDOWS:
4007 lnum = CURRENT_WIN_NR;
4008 break;
4009 case ADDR_ARGUMENTS:
4010 lnum = curwin->w_arg_idx + 1;
4011 if (lnum > ARGCOUNT)
4012 lnum = ARGCOUNT;
4013 break;
4014 case ADDR_LOADED_BUFFERS:
4015 case ADDR_BUFFERS:
4016 lnum = curbuf->b_fnum;
4017 break;
4018 case ADDR_TABS:
4019 lnum = CURRENT_TAB_NR;
4020 break;
4021 case ADDR_TABS_RELATIVE:
4022 case ADDR_UNSIGNED:
4023 lnum = 1;
4024 break;
4025 case ADDR_QUICKFIX:
4026#ifdef FEAT_QUICKFIX
4027 lnum = qf_get_cur_idx(eap);
4028#endif
4029 break;
4030 case ADDR_QUICKFIX_VALID:
4031#ifdef FEAT_QUICKFIX
4032 lnum = qf_get_cur_valid_idx(eap);
4033#endif
4034 break;
4035 case ADDR_NONE:
4036 // Will give an error later if a range is found.
4037 break;
4038 }
4039 return lnum;
4040}
4041
4042/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004043 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 *
4045 * Set ptr to the next character after the part that was interpreted.
4046 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004047 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 *
4049 * Return MAXLNUM when no Ex address was found.
4050 */
4051 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004052get_address(
4053 exarg_T *eap UNUSED,
4054 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004055 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004056 int skip, // only skip the address, don't use it
4057 int silent, // no errors or side effects
4058 int to_other_file, // flag: may jump to other file
4059 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060{
4061 int c;
4062 int i;
4063 long n;
4064 char_u *cmd;
4065 pos_T pos;
4066 pos_T *fp;
4067 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004068 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070 cmd = skipwhite(*ptr);
4071 lnum = MAXLNUM;
4072 do
4073 {
4074 switch (*cmd)
4075 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004076 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004077 ++cmd;
4078 switch (addr_type)
4079 {
4080 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004081 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 lnum = curwin->w_cursor.lnum;
4083 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004084 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004085 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004086 break;
4087 case ADDR_ARGUMENTS:
4088 lnum = curwin->w_arg_idx + 1;
4089 break;
4090 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004091 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004092 lnum = curbuf->b_fnum;
4093 break;
4094 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004095 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004096 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004097 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004098 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004099 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004100 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004101 cmd = NULL;
4102 goto error;
4103 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004104 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004105#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004106 lnum = qf_get_cur_idx(eap);
4107#endif
4108 break;
4109 case ADDR_QUICKFIX_VALID:
4110#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004111 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004112#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004113 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004114 }
4115 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004117 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004118 ++cmd;
4119 switch (addr_type)
4120 {
4121 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004122 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 lnum = curbuf->b_ml.ml_line_count;
4124 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004125 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004126 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004127 break;
4128 case ADDR_ARGUMENTS:
4129 lnum = ARGCOUNT;
4130 break;
4131 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004132 buf = lastbuf;
4133 while (buf->b_ml.ml_mfp == NULL)
4134 {
4135 if (buf->b_prev == NULL)
4136 break;
4137 buf = buf->b_prev;
4138 }
4139 lnum = buf->b_fnum;
4140 break;
4141 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004142 lnum = lastbuf->b_fnum;
4143 break;
4144 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004145 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004146 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004147 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004148 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004149 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004150 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004151 cmd = NULL;
4152 goto error;
4153 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004154 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004155#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004156 lnum = qf_get_size(eap);
4157 if (lnum == 0)
4158 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004159#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004160 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004161 case ADDR_QUICKFIX_VALID:
4162#ifdef FEAT_QUICKFIX
4163 lnum = qf_get_valid_size(eap);
4164 if (lnum == 0)
4165 lnum = 1;
4166#endif
4167 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004168 }
4169 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004171 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004172 if (*++cmd == NUL)
4173 {
4174 cmd = NULL;
4175 goto error;
4176 }
4177 if (addr_type != ADDR_LINES)
4178 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004179 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004180 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004181 goto error;
4182 }
4183 if (skip)
4184 ++cmd;
4185 else
4186 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004187 // Only accept a mark in another file when it is
4188 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004189 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4190 ++cmd;
4191 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004192 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004193 lnum = curwin->w_cursor.lnum;
4194 else
4195 {
4196 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
4198 cmd = NULL;
4199 goto error;
4200 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004201 lnum = fp->lnum;
4202 }
4203 }
4204 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
4206 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004207 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004208 c = *cmd++;
4209 if (addr_type != ADDR_LINES)
4210 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004211 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004212 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004213 goto error;
4214 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004215 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004216 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004217 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004218 if (*cmd == c)
4219 ++cmd;
4220 }
4221 else
4222 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004223 int flags;
4224
4225 pos = curwin->w_cursor; // save curwin->w_cursor
4226
4227 // When '/' or '?' follows another address, start from
4228 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004229 if (lnum != MAXLNUM)
4230 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004231
4232 // Start a forward search at the end of the line (unless
4233 // before the first line).
4234 // Start a backward search at the start of the line.
4235 // This makes sure we never match in the current
4236 // line, and can match anywhere in the
4237 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004238 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004239 curwin->w_cursor.col = MAXCOL;
4240 else
4241 curwin->w_cursor.col = 0;
4242 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004243 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004244 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004245 {
4246 curwin->w_cursor = pos;
4247 cmd = NULL;
4248 goto error;
4249 }
4250 lnum = curwin->w_cursor.lnum;
4251 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004252 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004253 cmd += searchcmdlen;
4254 }
4255 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004257 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004258 ++cmd;
4259 if (addr_type != ADDR_LINES)
4260 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004261 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004262 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004263 goto error;
4264 }
4265 if (*cmd == '&')
4266 i = RE_SUBST;
4267 else if (*cmd == '?' || *cmd == '/')
4268 i = RE_SEARCH;
4269 else
4270 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004271 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004272 cmd = NULL;
4273 goto error;
4274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004276 if (!skip)
4277 {
4278 /*
4279 * When search follows another address, start from
4280 * there.
4281 */
4282 if (lnum != MAXLNUM)
4283 pos.lnum = lnum;
4284 else
4285 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004287 /*
4288 * Start the search just like for the above
4289 * do_search().
4290 */
4291 if (*cmd != '?')
4292 pos.col = MAXCOL;
4293 else
4294 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004295 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004296 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004297 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004298 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004299 lnum = pos.lnum;
4300 else
4301 {
4302 cmd = NULL;
4303 goto error;
4304 }
4305 }
4306 ++cmd;
4307 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
4309 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004310 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004311 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313
4314 for (;;)
4315 {
4316 cmd = skipwhite(cmd);
4317 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4318 break;
4319
4320 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004321 {
4322 switch (addr_type)
4323 {
4324 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004325 case ADDR_OTHER:
4326 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004327 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004328 break;
4329 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004330 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004331 break;
4332 case ADDR_ARGUMENTS:
4333 lnum = curwin->w_arg_idx + 1;
4334 break;
4335 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004336 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004337 lnum = curbuf->b_fnum;
4338 break;
4339 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004340 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004341 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004342 case ADDR_TABS_RELATIVE:
4343 lnum = 1;
4344 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004345 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004346#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004347 lnum = qf_get_cur_idx(eap);
4348#endif
4349 break;
4350 case ADDR_QUICKFIX_VALID:
4351#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004352 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004353#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004354 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004355 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004356 case ADDR_UNSIGNED:
4357 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004358 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004359 }
4360 }
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004363 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 else
4365 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004366 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 n = 1;
4368 else
4369 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004370
4371 if (addr_type == ADDR_TABS_RELATIVE)
4372 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004373 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004374 cmd = NULL;
4375 goto error;
4376 }
4377 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004378 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004379 lnum = compute_buffer_local_count(
4380 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004382 {
4383#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004384 // Relative line addressing, need to adjust for folded lines
4385 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004386 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4387 && address_count >= 2)
4388 (void)hasFolding(lnum, NULL, &lnum);
4389#endif
4390 if (i == '-')
4391 lnum -= n;
4392 else
4393 lnum += n;
4394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
4396 } while (*cmd == '/' || *cmd == '?');
4397
4398error:
4399 *ptr = cmd;
4400 return lnum;
4401}
4402
4403/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004404 * Set eap->line1 and eap->line2 to the whole range.
4405 * Used for commands with the EX_DFLALL flag and no range given.
4406 */
4407 static void
4408address_default_all(exarg_T *eap)
4409{
4410 eap->line1 = 1;
4411 switch (eap->addr_type)
4412 {
4413 case ADDR_LINES:
4414 case ADDR_OTHER:
4415 eap->line2 = curbuf->b_ml.ml_line_count;
4416 break;
4417 case ADDR_LOADED_BUFFERS:
4418 {
4419 buf_T *buf = firstbuf;
4420
4421 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4422 buf = buf->b_next;
4423 eap->line1 = buf->b_fnum;
4424 buf = lastbuf;
4425 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4426 buf = buf->b_prev;
4427 eap->line2 = buf->b_fnum;
4428 }
4429 break;
4430 case ADDR_BUFFERS:
4431 eap->line1 = firstbuf->b_fnum;
4432 eap->line2 = lastbuf->b_fnum;
4433 break;
4434 case ADDR_WINDOWS:
4435 eap->line2 = LAST_WIN_NR;
4436 break;
4437 case ADDR_TABS:
4438 eap->line2 = LAST_TAB_NR;
4439 break;
4440 case ADDR_TABS_RELATIVE:
4441 eap->line2 = 1;
4442 break;
4443 case ADDR_ARGUMENTS:
4444 if (ARGCOUNT == 0)
4445 eap->line1 = eap->line2 = 0;
4446 else
4447 eap->line2 = ARGCOUNT;
4448 break;
4449 case ADDR_QUICKFIX_VALID:
4450#ifdef FEAT_QUICKFIX
4451 eap->line2 = qf_get_valid_size(eap);
4452 if (eap->line2 == 0)
4453 eap->line2 = 1;
4454#endif
4455 break;
4456 case ADDR_NONE:
4457 case ADDR_UNSIGNED:
4458 case ADDR_QUICKFIX:
4459 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4460 break;
4461 }
4462}
4463
4464
4465/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004466 * Get flags from an Ex command argument.
4467 */
4468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004469get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004470{
4471 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4472 {
4473 if (*eap->arg == 'l')
4474 eap->flags |= EXFLAG_LIST;
4475 else if (*eap->arg == 'p')
4476 eap->flags |= EXFLAG_PRINT;
4477 else
4478 eap->flags |= EXFLAG_NR;
4479 eap->arg = skipwhite(eap->arg + 1);
4480 }
4481}
4482
4483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 * Function called for command which is Not Implemented. NI!
4485 */
4486 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004487ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
4489 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004490 eap->errmsg =
4491 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492}
4493
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004494#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495/*
4496 * Function called for script command which is Not Implemented. NI!
4497 * Skips over ":perl <<EOF" constructs.
4498 */
4499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004500ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501{
4502 if (!eap->skip)
4503 ex_ni(eap);
4504 else
4505 vim_free(script_get(eap, eap->arg));
4506}
4507#endif
4508
4509/*
4510 * Check range in Ex command for validity.
4511 * Return NULL when valid, error message when invalid.
4512 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004513 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004514invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004516 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004517
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 if ( eap->line1 < 0
4519 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004520 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004521 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004522
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004523 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004524 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004525 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004526 {
4527 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004528 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004529#ifdef FEAT_DIFF
4530 + (eap->cmdidx == CMD_diffget)
4531#endif
4532 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004533 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004534 break;
4535 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004536 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004537 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004538 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004539 break;
4540 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004541 // Only a boundary check, not whether the buffers actually
4542 // exist.
4543 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004544 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004545 break;
4546 case ADDR_LOADED_BUFFERS:
4547 buf = firstbuf;
4548 while (buf->b_ml.ml_mfp == NULL)
4549 {
4550 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004551 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004552 buf = buf->b_next;
4553 }
4554 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004555 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004556 buf = lastbuf;
4557 while (buf->b_ml.ml_mfp == NULL)
4558 {
4559 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004560 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004561 buf = buf->b_prev;
4562 }
4563 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004564 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004565 break;
4566 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004567 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004568 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004569 break;
4570 case ADDR_TABS:
4571 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004572 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004573 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004574 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004575 case ADDR_OTHER:
4576 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004577 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004578 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004579#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004580 // No error for value that is too big, will use the last entry.
4581 if (eap->line2 <= 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004582 return _(e_invalid_range);
Bram Moolenaare906c502015-09-09 21:10:39 +02004583#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004584 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004585 case ADDR_QUICKFIX_VALID:
4586#ifdef FEAT_QUICKFIX
4587 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4588 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004589 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004590#endif
4591 break;
4592 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004593 case ADDR_NONE:
4594 // Will give an error elsewhere.
4595 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004596 }
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 return NULL;
4599}
4600
4601/*
4602 * Correct the range for zero line number, if required.
4603 */
4604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004605correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004607 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
4609 if (eap->line1 == 0)
4610 eap->line1 = 1;
4611 if (eap->line2 == 0)
4612 eap->line2 = 1;
4613 }
4614}
4615
Bram Moolenaar748bf032005-02-02 23:04:36 +00004616#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004617/*
4618 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4619 * pattern. Otherwise return eap->arg.
4620 */
4621 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004622skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004623{
4624 char_u *p = eap->arg;
4625
Bram Moolenaara37420f2006-02-04 22:37:47 +00004626 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4627 || eap->cmdidx == CMD_vimgrepadd
4628 || eap->cmdidx == CMD_lvimgrepadd
4629 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004630 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004631 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004632 if (p == NULL)
4633 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004634 }
4635 return p;
4636}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004637
4638/*
4639 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4640 * in the command line, so that things like % get expanded.
4641 */
4642 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004643replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004644{
4645 char_u *new_cmdline;
4646 char_u *program;
4647 char_u *pos;
4648 char_u *ptr;
4649 int len;
4650 int i;
4651
4652 /*
4653 * Don't do it when ":vimgrep" is used for ":grep".
4654 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004655 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4656 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4657 || eap->cmdidx == CMD_grepadd
4658 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004659 && !grep_internal(eap->cmdidx))
4660 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004661 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4662 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004663 {
4664 if (*curbuf->b_p_gp == NUL)
4665 program = p_gp;
4666 else
4667 program = curbuf->b_p_gp;
4668 }
4669 else
4670 {
4671 if (*curbuf->b_p_mp == NUL)
4672 program = p_mp;
4673 else
4674 program = curbuf->b_p_mp;
4675 }
4676
4677 p = skipwhite(p);
4678
4679 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4680 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004681 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004682 i = 1;
4683 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4684 ++i;
4685 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004686 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004687 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004688 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004689 ptr = new_cmdline;
4690 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4691 {
4692 i = (int)(pos - program);
4693 STRNCPY(ptr, program, i);
4694 STRCPY(ptr += i, p);
4695 ptr += len;
4696 program = pos + 2;
4697 }
4698 STRCPY(ptr, program);
4699 }
4700 else
4701 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004702 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004703 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004704 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004705 STRCPY(new_cmdline, program);
4706 STRCAT(new_cmdline, " ");
4707 STRCAT(new_cmdline, p);
4708 }
4709 msg_make(p);
4710
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004711 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004712 vim_free(*cmdlinep);
4713 *cmdlinep = new_cmdline;
4714 p = new_cmdline;
4715 }
4716 return p;
4717}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004718#endif
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720/*
4721 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004722 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 * Return FAIL for failure, OK otherwise.
4724 */
4725 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004726expand_filename(
4727 exarg_T *eap,
4728 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004729 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004731 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 char_u *repl;
4733 int srclen;
4734 char_u *p;
4735 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004736 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
Bram Moolenaar748bf032005-02-02 23:04:36 +00004738#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004739 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004740 p = skip_grep_pat(eap);
4741#else
4742 p = eap->arg;
4743#endif
4744
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 /*
4746 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4747 * the file name contains a wildcard it should not cause expanding.
4748 * (it will be expanded anyway if there is a wildcard before replacing).
4749 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004750 has_wildcards = mch_has_wildcard(p);
4751 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004753#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004754 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004755 if (p[0] == '`' && p[1] == '=')
4756 {
4757 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004758 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004759 if (*p == '`')
4760 ++p;
4761 continue;
4762 }
4763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 /*
4765 * Quick check if this cannot be the start of a special string.
4766 * Also removes backslash before '%', '#' and '<'.
4767 */
4768 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4769 {
4770 ++p;
4771 continue;
4772 }
4773
4774 /*
4775 * Try to find a match at this position.
4776 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004777 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4778 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004779 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004781 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 {
4783 p += srclen;
4784 continue;
4785 }
4786
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004787 // Wildcards won't be expanded below, the replacement is taken
4788 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004789 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4790 {
4791 char_u *l = repl;
4792
4793 repl = expand_env_save(repl);
4794 vim_free(l);
4795 }
4796
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004797 // Need to escape white space et al. with a backslash.
4798 // Don't do this for:
4799 // - replacement that already has been escaped: "##"
4800 // - shell commands (may have to use quotes instead).
4801 // - non-unix systems when there is a single argument (spaces don't
4802 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004804 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 && eap->cmdidx != CMD_grep
4807 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004808 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004809 && eap->cmdidx != CMD_lgrep
4810 && eap->cmdidx != CMD_lgrepadd
4811 && eap->cmdidx != CMD_lmake
4812 && eap->cmdidx != CMD_make
4813 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004815 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816#endif
4817 )
4818 {
4819 char_u *l;
4820#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004821 // Don't escape a backslash here, because rem_backslash() doesn't
4822 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 static char_u *nobslash = (char_u *)" \t\"|";
4824# define ESCAPE_CHARS nobslash
4825#else
4826# define ESCAPE_CHARS escape_chars
4827#endif
4828
4829 for (l = repl; *l; ++l)
4830 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4831 {
4832 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4833 if (l != NULL)
4834 {
4835 vim_free(repl);
4836 repl = l;
4837 }
4838 break;
4839 }
4840 }
4841
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004842 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004843 if ((eap->usefilter || eap->cmdidx == CMD_bang
4844 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004845 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 {
4847 char_u *l;
4848
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004849 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (l != NULL)
4851 {
4852 vim_free(repl);
4853 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 }
4856
4857 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4858 vim_free(repl);
4859 if (p == NULL)
4860 return FAIL;
4861 }
4862
4863 /*
4864 * One file argument: Expand wildcards.
4865 * Don't do this with ":r !command" or ":w !command".
4866 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004867 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
4869 /*
4870 * May do this twice:
4871 * 1. Replace environment variables.
4872 * 2. Replace any other wildcards, remove backslashes.
4873 */
4874 for (n = 1; n <= 2; ++n)
4875 {
4876 if (n == 2)
4877 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 /*
4879 * Halve the number of backslashes (this is Vi compatible).
4880 * For Unix and OS/2, when wildcards are expanded, this is
4881 * done by ExpandOne() below.
4882 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004883#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 if (!has_wildcards)
4885#endif
4886 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888
4889 if (has_wildcards)
4890 {
4891 if (n == 1)
4892 {
4893 /*
4894 * First loop: May expand environment variables. This
4895 * can be done much faster with expand_env() than with
4896 * something else (e.g., calling a shell).
4897 * After expanding environment variables, check again
4898 * if there are still wildcards present.
4899 */
4900 if (vim_strchr(eap->arg, '$') != NULL
4901 || vim_strchr(eap->arg, '~') != NULL)
4902 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004903 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004904 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 has_wildcards = mch_has_wildcard(NameBuff);
4906 p = NameBuff;
4907 }
4908 else
4909 p = NULL;
4910 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004911 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
4913 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004914 int options = WILD_LIST_NOTFOUND
4915 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 ExpandInit(&xpc);
4918 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004919 if (p_wic)
4920 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004922 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 if (p == NULL)
4924 return FAIL;
4925 }
4926 if (p != NULL)
4927 {
4928 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4929 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004930 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 vim_free(p);
4932 }
4933 }
4934 }
4935 }
4936 return OK;
4937}
4938
4939/*
4940 * Replace part of the command line, keeping eap->cmd, eap->arg and
4941 * eap->nextcmd correct.
4942 * "src" points to the part that is to be replaced, of length "srclen".
4943 * "repl" is the replacement string.
4944 * Returns a pointer to the character after the replaced string.
4945 * Returns NULL for failure.
4946 */
4947 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004948repl_cmdline(
4949 exarg_T *eap,
4950 char_u *src,
4951 int srclen,
4952 char_u *repl,
4953 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
4955 int len;
4956 int i;
4957 char_u *new_cmdline;
4958
4959 /*
4960 * The new command line is build in new_cmdline[].
4961 * First allocate it.
4962 * Careful: a "+cmd" argument may have been NUL terminated.
4963 */
4964 len = (int)STRLEN(repl);
4965 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004966 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004967 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004968 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004969 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
4971 /*
4972 * Copy the stuff before the expanded part.
4973 * Copy the expanded stuff.
4974 * Copy what came after the expanded part.
4975 * Copy the next commands, if there are any.
4976 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004977 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004979
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004981 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004983 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004985 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
4987 i = (int)STRLEN(new_cmdline) + 1;
4988 STRCPY(new_cmdline + i, eap->nextcmd);
4989 eap->nextcmd = new_cmdline + i;
4990 }
4991 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4992 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4993 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4994 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4995 vim_free(*cmdlinep);
4996 *cmdlinep = new_cmdline;
4997
4998 return src;
4999}
5000
5001/*
5002 * Check for '|' to separate commands and '"' to start comments.
5003 */
5004 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005005separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006{
5007 char_u *p;
5008
Bram Moolenaar86b68352004-12-27 21:59:20 +00005009#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005010 p = skip_grep_pat(eap);
5011#else
5012 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005013#endif
5014
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005015 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 if (*p == Ctrl_V)
5018 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005019 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005020 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005022 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005023 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005024 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 break;
5026 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005027
5028#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005029 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005030 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005031 {
5032 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005033 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005034 if (*p == NUL) // stop at NUL after CTRL-V
5035 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005036 }
5037#endif
5038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005039 // Check for '"': start of comment or '|': next command
5040 // :@" and :*" do not start a comment!
5041 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005042 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005043#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005044 && !in_vim9script()
5045#endif
5046 && !(eap->argt & EX_NOTRLCOM)
5047 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5048 || p != eap->arg)
5049 && (eap->cmdidx != CMD_redir
5050 || p != eap->arg + 1 || p[-1] != '@'))
5051#ifdef FEAT_EVAL
5052 || (*p == '#'
5053 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005054 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005055 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 || *p == '|' || *p == '\n')
5058 {
5059 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005060 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 * AND 'b' is present in 'cpoptions'.
5062 */
5063 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005064 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005066 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 --p;
5068 }
5069 else
5070 {
5071 eap->nextcmd = check_nextcmd(p);
5072 *p = NUL;
5073 break;
5074 }
5075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005078 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 del_trailing_spaces(eap->arg);
5080}
5081
5082/*
5083 * get + command from ex argument
5084 */
5085 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005086getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087{
5088 char_u *arg = *argp;
5089 char_u *command = NULL;
5090
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005091 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
5093 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005094 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 command = dollar_command;
5096 else
5097 {
5098 command = arg;
5099 arg = skip_cmd_arg(command, TRUE);
5100 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005101 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
5103
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005104 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 *argp = arg;
5106 }
5107 return command;
5108}
5109
5110/*
5111 * Find end of "+command" argument. Skip over "\ " and "\\".
5112 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005113 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005114skip_cmd_arg(
5115 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005116 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117{
5118 while (*p && !vim_isspace(*p))
5119 {
5120 if (*p == '\\' && p[1] != NUL)
5121 {
5122 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005123 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 else
5125 ++p;
5126 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005127 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129 return p;
5130}
5131
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005132 int
5133get_bad_opt(char_u *p, exarg_T *eap)
5134{
5135 if (STRICMP(p, "keep") == 0)
5136 eap->bad_char = BAD_KEEP;
5137 else if (STRICMP(p, "drop") == 0)
5138 eap->bad_char = BAD_DROP;
5139 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5140 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005141 else
5142 return FAIL;
5143 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005144}
5145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146/*
5147 * Get "++opt=arg" argument.
5148 * Return FAIL or OK.
5149 */
5150 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005151getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
5153 char_u *arg = eap->arg + 2;
5154 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005155 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005158 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5160 {
5161 if (*arg == 'n')
5162 {
5163 arg += 2;
5164 eap->force_bin = FORCE_NOBIN;
5165 }
5166 else
5167 eap->force_bin = FORCE_BIN;
5168 if (!checkforcmd(&arg, "binary", 3))
5169 return FAIL;
5170 eap->arg = skipwhite(arg);
5171 return OK;
5172 }
5173
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005174 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005175 if (STRNCMP(arg, "edit", 4) == 0)
5176 {
5177 eap->read_edit = TRUE;
5178 eap->arg = skipwhite(arg + 4);
5179 return OK;
5180 }
5181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 if (STRNCMP(arg, "ff", 2) == 0)
5183 {
5184 arg += 2;
5185 pp = &eap->force_ff;
5186 }
5187 else if (STRNCMP(arg, "fileformat", 10) == 0)
5188 {
5189 arg += 10;
5190 pp = &eap->force_ff;
5191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 else if (STRNCMP(arg, "enc", 3) == 0)
5193 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005194 if (STRNCMP(arg, "encoding", 8) == 0)
5195 arg += 8;
5196 else
5197 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 pp = &eap->force_enc;
5199 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005200 else if (STRNCMP(arg, "bad", 3) == 0)
5201 {
5202 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005203 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205
5206 if (pp == NULL || *arg != '=')
5207 return FAIL;
5208
5209 ++arg;
5210 *pp = (int)(arg - eap->cmd);
5211 arg = skip_cmd_arg(arg, FALSE);
5212 eap->arg = skipwhite(arg);
5213 *arg = NUL;
5214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (pp == &eap->force_ff)
5216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5218 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005219 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005221 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005223 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5225 *p = TOLOWER_ASC(*p);
5226 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005227 else
5228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005229 // Check ++bad= argument. Must be a single-byte character, "keep" or
5230 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005231 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005232 return FAIL;
5233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
5235 return OK;
5236}
5237
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005239ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240{
5241 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005242 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 * directory for security reasons.
5244 */
5245 if (secure)
5246 {
5247 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005248 eap->errmsg =
5249 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
5251 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005252 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 else
5254 do_augroup(eap->arg, eap->forceit);
5255}
5256
5257/*
5258 * ":doautocmd": Apply the automatic commands to the current buffer.
5259 */
5260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005261ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005263 char_u *arg = eap->arg;
5264 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005265 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005266
Bram Moolenaar1610d052016-06-09 22:53:01 +02005267 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005268 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005269 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005270 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273/*
5274 * :[N]bunload[!] [N] [bufname] unload buffer
5275 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5276 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5277 */
5278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005279ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005281 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005282 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 eap->errmsg = do_bufdel(
5284 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5285 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5286 : DOBUF_UNLOAD, eap->arg,
5287 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5288}
5289
5290/*
5291 * :[N]buffer [N] to buffer N
5292 * :[N]sbuffer [N] to buffer N
5293 */
5294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005295ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005297 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005298 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005300 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 else
5302 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005303 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5305 else
5306 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005307 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005308 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
5310}
5311
5312/*
5313 * :[N]bmodified [N] to next mod. buffer
5314 * :[N]sbmodified [N] to next mod. buffer
5315 */
5316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005317ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318{
5319 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005320 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005321 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322}
5323
5324/*
5325 * :[N]bnext [N] to next buffer
5326 * :[N]sbnext [N] split and to next buffer
5327 */
5328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005329ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005331 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005332 return;
5333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005335 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005336 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337}
5338
5339/*
5340 * :[N]bNext [N] to previous buffer
5341 * :[N]bprevious [N] to previous buffer
5342 * :[N]sbNext [N] split and to previous buffer
5343 * :[N]sbprevious [N] split and to previous buffer
5344 */
5345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005346ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005348 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005349 return;
5350
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005352 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005353 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354}
5355
5356/*
5357 * :brewind to first buffer
5358 * :bfirst to first buffer
5359 * :sbrewind split and to first buffer
5360 * :sbfirst split and to first buffer
5361 */
5362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005363ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005365 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005366 return;
5367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005369 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005370 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371}
5372
5373/*
5374 * :blast to last buffer
5375 * :sblast split and to last buffer
5376 */
5377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005378ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005380 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005381 return;
5382
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005384 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005385 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387
Bram Moolenaar7a092242020-04-16 22:10:49 +02005388/*
5389 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005390 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005393ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005395 int comment_char = '"';
5396
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005397#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005398 if (in_vim9script())
5399 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005400#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005401 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005402}
5403
5404/*
5405 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5406 * to "cmd_start" or has a white space character before it.
5407 */
5408 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005409ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005410{
5411 int c = *cmd;
5412
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005413 if (c == NUL || c == '|' || c == '\n')
5414 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005415#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005416 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005417 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5418 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005419 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005420#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005421 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422}
5423
5424#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5425 || defined(PROTO)
5426/*
5427 * Return the next command, after the first '|' or '\n'.
5428 * Return NULL if not found.
5429 */
5430 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005431find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
5433 while (*p != '|' && *p != '\n')
5434 {
5435 if (*p == NUL)
5436 return NULL;
5437 ++p;
5438 }
5439 return (p + 1);
5440}
5441#endif
5442
5443/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005444 * Check if *p is a separator between Ex commands, skipping over white space.
5445 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 */
5447 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005448check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005450 char_u *s = skipwhite(p);
5451
5452 if (*s == '|' || *s == '\n')
5453 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 else
5455 return NULL;
5456}
5457
5458/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005459 * If "eap->nextcmd" is not set, check for a next command at "p".
5460 */
5461 void
5462set_nextcmd(exarg_T *eap, char_u *arg)
5463{
5464 char_u *p = check_nextcmd(arg);
5465
5466 if (eap->nextcmd == NULL)
5467 eap->nextcmd = p;
5468 else if (p != NULL)
5469 // cannot use "| command" inside a {} block
5470 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5471}
5472
5473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 * - if there are more files to edit
5475 * - and this is the last window
5476 * - and forceit not used
5477 * - and not repeated twice on a row
5478 * return FAIL and give error message if 'message' TRUE
5479 * return OK otherwise
5480 */
5481 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005482check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005483 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005484 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
5486 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5487
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005488 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005489 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 {
5491 if (message)
5492 {
5493#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005494 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5495 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005497 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005499 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5500 NGETTEXT("%d more file to edit. Quit anyway?",
5501 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5503 return OK;
5504 return FAIL;
5505 }
5506#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005507 semsg(NGETTEXT("E173: %d more file to edit",
5508 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005509 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 }
5511 return FAIL;
5512 }
5513 return OK;
5514}
5515
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516/*
5517 * Function given to ExpandGeneric() to obtain the list of command names.
5518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005520get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
5522 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005523 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 return cmdnames[idx].cmd_name;
5525}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005528ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
Bram Moolenaare6850792010-05-14 15:28:44 +02005530 if (*eap->arg == NUL)
5531 {
5532#ifdef FEAT_EVAL
5533 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5534 char_u *p = NULL;
5535
5536 if (expr != NULL)
5537 {
5538 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005539 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005540 --emsg_off;
5541 vim_free(expr);
5542 }
5543 if (p != NULL)
5544 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005545 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005546 vim_free(p);
5547 }
5548 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005549 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005550#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005551 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005552#endif
5553 }
5554 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005555 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005556
5557#ifdef FEAT_VTP
5558 else if (has_vtp_working())
5559 {
5560 // background color change requires clear + redraw
5561 update_screen(CLEAR);
5562 redrawcmd();
5563 }
5564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565}
5566
5567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005568ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005571 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 do_highlight(eap->arg, eap->forceit, FALSE);
5573}
5574
5575
5576/*
5577 * Call this function if we thought we were going to exit, but we won't
5578 * (because of an error). May need to restore the terminal mode.
5579 */
5580 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005581not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 exiting = FALSE;
5584 settmode(TMODE_RAW);
5585}
5586
Bram Moolenaar3552e742021-05-29 12:21:58 +02005587 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005588before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5589{
5590 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5591
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005592 // Bail out when autocommands closed the window.
5593 // Refuse to quit when the buffer in the last window is being closed (can
5594 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005595 if (!win_valid(wp)
5596 || curbuf_locked()
5597 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5598 return TRUE;
5599
5600 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5601 {
5602 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005603 // Refuse to quit when locked or when the window was closed or the
5604 // buffer in the last window is being closed (can only happen in
5605 // autocommands).
5606 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005607 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5608 return TRUE;
5609 }
5610
5611 return FALSE;
5612}
5613
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005615 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005616 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005617 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005619 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005620ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005622 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005623
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624#ifdef FEAT_CMDWIN
5625 if (cmdwin_type != 0)
5626 {
5627 cmdwin_result = Ctrl_C;
5628 return;
5629 }
5630#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005631 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005632 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005633 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005634 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005635 return;
5636 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005637 if (eap->addr_count > 0)
5638 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005639 int wnr = eap->line2;
5640
5641 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5642 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005643 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005644 }
5645 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005646 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005647
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005648 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005649 if (curbuf_locked())
5650 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005652 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005653 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005654 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655
5656#ifdef FEAT_NETBEANS_INTG
5657 netbeansForcedQuit = eap->forceit;
5658#endif
5659
5660 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005661 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 */
5663 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5664 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005665 if ((!buf_hide(wp->w_buffer)
5666 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005667 | (eap->forceit ? CCGD_FORCEIT : 0)
5668 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005670 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 {
5672 not_exiting();
5673 }
5674 else
5675 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005676 // quit last window
5677 // Note: only_one_window() returns true, even so a help window is
5678 // still open. In that case only quit, if no address has been
5679 // specified. Example:
5680 // :h|wincmd w|1q - don't quit
5681 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005682 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005684 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005685#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005687#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005688 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005689 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691}
5692
5693/*
5694 * ":cquit".
5695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005697ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005699 // this does not always pass on the exit code to the Manx compiler. why?
5700 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701}
5702
5703/*
5704 * ":qall": try to quit all windows
5705 */
5706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005707ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709# ifdef FEAT_CMDWIN
5710 if (cmdwin_type != 0)
5711 {
5712 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005713 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 else
5715 cmdwin_result = K_XF2;
5716 return;
5717 }
5718# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005719
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005720 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005721 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005722 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005723 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005724 return;
5725 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005726
5727 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005728 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005731 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 getout(0);
5733 not_exiting();
5734}
5735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736/*
5737 * ":close": close current window, unless it is the last one
5738 */
5739 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005740ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005742 win_T *win;
5743 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005744#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005746 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005748#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005749 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005750 {
5751 if (eap->addr_count == 0)
5752 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005753 else
5754 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005755 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005756 {
5757 winnr++;
5758 if (winnr == eap->line2)
5759 break;
5760 }
5761 if (win == NULL)
5762 win = lastwin;
5763 ex_win_close(eap->forceit, win, NULL);
5764 }
5765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766}
5767
Bram Moolenaar4033c552017-09-16 20:54:51 +02005768#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005769/*
5770 * ":pclose": Close any preview window.
5771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005773ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005774{
5775 win_T *win;
5776
Bram Moolenaar79648732019-07-18 21:43:07 +02005777 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005778 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005779 if (win->w_p_pvw)
5780 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005781 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005782 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005783 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005784# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005785 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005786 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005787# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005788}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005789#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005790
Bram Moolenaarf740b292006-02-16 22:11:02 +00005791/*
5792 * Close window "win" and take care of handling closing the last window for a
5793 * modified buffer.
5794 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005796ex_win_close(
5797 int forceit,
5798 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005799 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800{
5801 int need_hide;
5802 buf_T *buf = win->w_buffer;
5803
Bram Moolenaarcf844172020-06-26 19:44:06 +02005804 // Never close the autocommand window.
5805 if (win == aucmd_win)
5806 {
5807 emsg(_(e_autocmd_close));
5808 return;
5809 }
5810
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005812 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005814#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005815 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005817 bufref_T bufref;
5818
5819 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005821 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 return;
5823 need_hide = FALSE;
5824 }
5825 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005828 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 return;
5830 }
5831 }
5832
Bram Moolenaar4033c552017-09-16 20:54:51 +02005833#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005835#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005836
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005837 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005838 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005839 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005840 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005841 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842}
5843
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005845 * Handle the argument for a tabpage related ex command.
5846 * Returns a tabpage number.
5847 * When an error is encountered then eap->errmsg is set.
5848 */
5849 static int
5850get_tabpage_arg(exarg_T *eap)
5851{
5852 int tab_number;
5853 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5854
5855 if (eap->arg && *eap->arg != NUL)
5856 {
5857 char_u *p = eap->arg;
5858 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005859 int relative = 0; // argument +N/-N means: go to N places to the
5860 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005861
5862 if (*p == '-')
5863 {
5864 relative = -1;
5865 p++;
5866 }
5867 else if (*p == '+')
5868 {
5869 relative = 1;
5870 p++;
5871 }
5872
5873 p_save = p;
5874 tab_number = getdigits(&p);
5875
5876 if (relative == 0)
5877 {
5878 if (STRCMP(p, "$") == 0)
5879 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005880 else if (STRCMP(p, "#") == 0)
5881 if (valid_tabpage(lastused_tabpage))
5882 tab_number = tabpage_index(lastused_tabpage);
5883 else
5884 {
5885 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5886 tab_number = 0;
5887 goto theend;
5888 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005889 else if (p == p_save || *p_save == '-' || *p != NUL
5890 || tab_number > LAST_TAB_NR)
5891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005892 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005893 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005894 goto theend;
5895 }
5896 }
5897 else
5898 {
5899 if (*p_save == NUL)
5900 tab_number = 1;
5901 else if (p == p_save || *p_save == '-' || *p != NUL
5902 || tab_number == 0)
5903 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005904 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005905 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005906 goto theend;
5907 }
5908 tab_number = tab_number * relative + tabpage_index(curtab);
5909 if (!unaccept_arg0 && relative == -1)
5910 --tab_number;
5911 }
5912 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005913 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005914 }
5915 else if (eap->addr_count > 0)
5916 {
5917 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005918 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005919 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005920 tab_number = 0;
5921 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005922 else
5923 {
5924 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005925 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005926 {
5927 --tab_number;
5928 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005929 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01005930 }
5931 }
5932 }
5933 else
5934 {
5935 switch (eap->cmdidx)
5936 {
5937 case CMD_tabnext:
5938 tab_number = tabpage_index(curtab) + 1;
5939 if (tab_number > LAST_TAB_NR)
5940 tab_number = 1;
5941 break;
5942 case CMD_tabmove:
5943 tab_number = LAST_TAB_NR;
5944 break;
5945 default:
5946 tab_number = tabpage_index(curtab);
5947 }
5948 }
5949
5950theend:
5951 return tab_number;
5952}
5953
5954/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005955 * ":tabclose": close current tab page, unless it is the last one.
5956 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 */
5958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005959ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005961 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005962 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005963
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005964# ifdef FEAT_CMDWIN
5965 if (cmdwin_type != 0)
5966 cmdwin_result = K_IGNORE;
5967 else
5968# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005969 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005970 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005971 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005973 tab_number = get_tabpage_arg(eap);
5974 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005975 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005976 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005977 if (tp == NULL)
5978 {
5979 beep_flush();
5980 return;
5981 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005982 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005983 {
5984 tabpage_close_other(tp, eap->forceit);
5985 return;
5986 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005987 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005988 tabpage_close(eap->forceit);
5989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005991}
5992
5993/*
5994 * ":tabonly": close all tab pages except the current one
5995 */
5996 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005997ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005998{
5999 tabpage_T *tp;
6000 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006001 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006002
6003# ifdef FEAT_CMDWIN
6004 if (cmdwin_type != 0)
6005 cmdwin_result = K_IGNORE;
6006 else
6007# endif
6008 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006009 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006010 else
6011 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006012 tab_number = get_tabpage_arg(eap);
6013 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006014 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006015 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006016 // Repeat this up to a 1000 times, because autocommands may
6017 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006018 for (done = 0; done < 1000; ++done)
6019 {
6020 FOR_ALL_TABPAGES(tp)
6021 if (tp->tp_topframe != topframe)
6022 {
6023 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006024 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006025 if (valid_tabpage(tp))
6026 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006027 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006028 break;
6029 }
6030 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006031 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006032 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006033 }
6034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036
6037/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006038 * Close the current tab page.
6039 */
6040 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006041tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006042{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006043 // First close all the windows but the current one. If that worked then
6044 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006045 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006046 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006047 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006048 ex_win_close(forceit, curwin, NULL);
6049# ifdef FEAT_GUI
6050 need_mouse_correct = TRUE;
6051# endif
6052}
6053
6054/*
6055 * Close tab page "tp", which is not the current tab page.
6056 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006057 * Also takes care of the tab pages line disappearing when closing the
6058 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006059 */
6060 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006061tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006062{
6063 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006064 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006065 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006066
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006067 // Limit to 1000 windows, autocommands may add a window while we close
6068 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006069 while (++done < 1000)
6070 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006071 wp = tp->tp_firstwin;
6072 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006074 // Autocommands may delete the tab page under our fingers and we may
6075 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006076 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006077 break;
6078 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006079
Bram Moolenaar29323592016-07-24 22:04:11 +02006080 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006081
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006082 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006083 if (h != tabline_height())
6084 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006085}
6086
6087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 * ":only".
6089 */
6090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006091ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006093 win_T *wp;
6094 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095# ifdef FEAT_GUI
6096 need_mouse_correct = TRUE;
6097# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006098 if (eap->addr_count > 0)
6099 {
6100 wnr = eap->line2;
6101 for (wp = firstwin; --wnr > 0; )
6102 {
6103 if (wp->w_next == NULL)
6104 break;
6105 else
6106 wp = wp->w_next;
6107 }
6108 win_goto(wp);
6109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 close_others(TRUE, eap->forceit);
6111}
6112
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006114ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006116 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006117 if (!eap->skip)
6118 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006119#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006120 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006121#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006122 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006123 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006124 else
6125 {
6126 int winnr = 0;
6127 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006128
Bram Moolenaar2256c992016-11-15 21:17:07 +01006129 FOR_ALL_WINDOWS(win)
6130 {
6131 winnr++;
6132 if (winnr == eap->line2)
6133 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006134 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006135 if (win == NULL)
6136 win = lastwin;
6137 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 }
6140}
6141
6142/*
6143 * ":stop" and ":suspend": Suspend Vim.
6144 */
6145 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006146ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148 /*
6149 * Disallow suspending for "rvim".
6150 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006151 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 {
6153 if (!eap->forceit)
6154 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006155 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 windgoto((int)Rows - 1, 0);
6157 out_char('\n');
6158 out_flush();
6159 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006160 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006162 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006164 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165#ifdef FEAT_TITLE
6166 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006167 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168#endif
6169 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006170 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006172 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006173 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 }
6175}
6176
6177/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006178 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 */
6180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006181ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006183#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006184 if (not_in_vim9(eap) == FAIL)
6185 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187#ifdef FEAT_CMDWIN
6188 if (cmdwin_type != 0)
6189 {
6190 cmdwin_result = Ctrl_C;
6191 return;
6192 }
6193#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006194 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006195 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006196 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006197 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006198 return;
6199 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006200
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006202 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 */
6204 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6205 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006206
6207 // Write the buffer for ":wq" or when it was changed.
6208 // Trigger QuitPre and ExitPre.
6209 // Check if we can exit now, after autocommands have changed things.
6210 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6211 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006213 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
6215 not_exiting();
6216 }
6217 else
6218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006219 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006221 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222# ifdef FEAT_GUI
6223 need_mouse_correct = TRUE;
6224# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006225 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006226 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 }
6228}
6229
6230/*
6231 * ":print", ":list", ":number".
6232 */
6233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006234ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006236 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006237 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006238 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006240 for ( ;!got_int; ui_breakcheck())
6241 {
6242 print_line(eap->line1,
6243 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6244 || (eap->flags & EXFLAG_NR)),
6245 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6246 if (++eap->line1 > eap->line2)
6247 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006248 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006249 }
6250 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006251 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006252 curwin->w_cursor.lnum = eap->line2;
6253 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257}
6258
6259#ifdef FEAT_BYTEOFF
6260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006261ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 goto_byte(eap->line2);
6264}
6265#endif
6266
6267/*
6268 * ":shell".
6269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006271ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 do_shell(NULL, 0);
6274}
6275
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006276#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006278static int drop_busy = FALSE;
6279static int drop_filec;
6280static char_u **drop_filev = NULL;
6281static int drop_split;
6282static void (*drop_callback)(void *);
6283static void *drop_cookie;
6284
6285 static void
6286handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287{
6288 exarg_T ea;
6289 int save_msg_scroll = msg_scroll;
6290
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006291 // Setting the argument list may cause screen updates and being called
6292 // recursively. Avoid that by setting drop_busy.
6293 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006295 // Check whether the current buffer is changed. If so, we will need
6296 // to split the current window or data could be lost.
6297 // We don't need to check if the 'hidden' option is set, as in this
6298 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006299 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 {
6301 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006302 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 --emsg_off;
6304 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006305 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 if (win_split(0, 0) == FAIL)
6308 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006309 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006311 // When splitting the window, create a new alist. Otherwise the
6312 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 alist_unlink(curwin->w_alist);
6314 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
6316
6317 /*
6318 * Set up the new argument list.
6319 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006320 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
6322 /*
6323 * Move to the first file.
6324 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006325 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006326 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 ea.cmd = (char_u *)"next";
6328 do_argfile(&ea, 0);
6329
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006330 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6331 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 need_start_insertmode = FALSE;
6333
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006334 // Restore msg_scroll, otherwise a following command may cause scrolling
6335 // unexpectedly. The screen will be redrawn by the caller, thus
6336 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006338
6339 if (drop_callback != NULL)
6340 drop_callback(drop_cookie);
6341
6342 drop_filev = NULL;
6343 drop_busy = FALSE;
6344}
6345
6346/*
6347 * Handle a file drop. The code is here because a drop is *nearly* like an
6348 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006349 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006350 * code is very similar to :args and hence needs access to a lot of the static
6351 * functions in this file.
6352 *
6353 * The "filev" list must have been allocated using alloc(), as should each item
6354 * in the list. This function takes over responsibility for freeing the "filev"
6355 * list.
6356 */
6357 void
6358handle_drop(
6359 int filec, // the number of files dropped
6360 char_u **filev, // the list of files dropped
6361 int split, // force splitting the window
6362 void (*callback)(void *), // to be called after setting the argument
6363 // list
6364 void *cookie) // argument for "callback" (allocated)
6365{
6366 // Cannot handle recursive drops, finish the pending one.
6367 if (drop_busy)
6368 {
6369 FreeWild(filec, filev);
6370 vim_free(cookie);
6371 return;
6372 }
6373
6374 // When calling handle_drop() more than once in a row we only use the last
6375 // one.
6376 if (drop_filev != NULL)
6377 {
6378 FreeWild(drop_filec, drop_filev);
6379 vim_free(drop_cookie);
6380 }
6381
6382 drop_filec = filec;
6383 drop_filev = filev;
6384 drop_split = split;
6385 drop_callback = callback;
6386 drop_cookie = cookie;
6387
6388 // Postpone this when:
6389 // - editing the command line
6390 // - not possible to change the current buffer
6391 // - updating the screen
6392 // As it may change buffers and window structures that are in use and cause
6393 // freed memory to be used.
6394 if (text_locked() || curbuf_locked() || updating_screen)
6395 return;
6396
6397 handle_drop_internal();
6398}
6399
6400/*
6401 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6402 * reset: Handle a postponed drop.
6403 */
6404 void
6405handle_any_postponed_drop(void)
6406{
6407 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006408 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006409 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410}
6411#endif
6412
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 * ":preserve".
6415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006417ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006419 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 ml_preserve(curbuf, TRUE);
6421}
6422
6423/*
6424 * ":recover".
6425 */
6426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006427ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006429 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006431 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6432 | CCGD_MULTWIN
6433 | (eap->forceit ? CCGD_FORCEIT : 0)
6434 | CCGD_EXCMD)
6435
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 && (*eap->arg == NUL
6437 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006438 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 recoverymode = FALSE;
6440}
6441
6442/*
6443 * Command modifier used in a wrong way.
6444 */
6445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006446ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006448 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449}
6450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451/*
6452 * :sview [+command] file split window with new file, read-only
6453 * :split [[+command] file] split window with current or new file
6454 * :vsplit [[+command] file] split window vertically with current or new file
6455 * :new [[+command] file] split window with no or new file
6456 * :vnew [[+command] file] split vertically window with no or new file
6457 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006458 *
6459 * :tabedit open new Tab page with empty window
6460 * :tabedit [+command] file open new Tab page and edit "file"
6461 * :tabnew [[+command] file] just like :tabedit
6462 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 */
6464 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006465ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006467 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006468#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006470#endif
6471#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006472 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006473 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006474#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006475 int use_tab = eap->cmdidx == CMD_tabedit
6476 || eap->cmdidx == CMD_tabfind
6477 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006479 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006480 return;
6481
Bram Moolenaar4033c552017-09-16 20:54:51 +02006482#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
Bram Moolenaar4033c552017-09-16 20:54:51 +02006486#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006487 // A ":split" in the quickfix window works like ":new". Don't want two
6488 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006489 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 {
6491 if (eap->cmdidx == CMD_split)
6492 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 if (eap->cmdidx == CMD_vsplit)
6494 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
Bram Moolenaar4033c552017-09-16 20:54:51 +02006498#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006499 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 {
6501 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6502 FNAME_MESS, TRUE, curbuf->b_ffname);
6503 if (fname == NULL)
6504 goto theend;
6505 eap->arg = fname;
6506 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006507# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006508 else
6509# endif
6510#endif
6511#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006512 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 && eap->cmdidx != CMD_new)
6515 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006516 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006517# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006518 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006519# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006520 au_has_group((char_u *)"FileExplorer"))
6521 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006522 // No browsing supported but we do have the file explorer:
6523 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006524 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006525 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006526 }
6527 else
6528 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006529 fname = do_browse(0, (char_u *)(use_tab
6530 ? _("Edit File in new tab page")
6531 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006533 if (fname == NULL)
6534 goto theend;
6535 eap->arg = fname;
6536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006538 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006541 /*
6542 * Either open new tab page or split the window.
6543 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006544 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006545 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006546 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006547 : eap->addr_count == 0 ? 0
6548 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006549 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006550 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006552 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006553 if (curwin != old_curwin
6554 && win_valid(old_curwin)
6555 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006556 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006557 old_curwin->w_alt_fnum = curbuf->b_fnum;
6558 }
6559 }
6560 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6562 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006563 // Reset 'scrollbind' when editing another file, but keep it when
6564 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006565 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006566 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 else
6568 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 do_exedit(eap, old_curwin);
6570 }
6571
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006572# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006573 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006576# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577theend:
6578 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006579# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006581
6582/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006583 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006584 */
6585 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006586tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006587{
6588 exarg_T ea;
6589
Bram Moolenaara80faa82020-04-12 19:37:17 +02006590 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006591 ea.cmdidx = CMD_tabnew;
6592 ea.cmd = (char_u *)"tabn";
6593 ea.arg = (char_u *)"";
6594 ex_splitview(&ea);
6595}
6596
6597/*
6598 * :tabnext command
6599 */
6600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006601ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006602{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006603 int tab_number;
6604
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006605 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006606 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006607 switch (eap->cmdidx)
6608 {
6609 case CMD_tabfirst:
6610 case CMD_tabrewind:
6611 goto_tabpage(1);
6612 break;
6613 case CMD_tablast:
6614 goto_tabpage(9999);
6615 break;
6616 case CMD_tabprevious:
6617 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006618 if (eap->arg && *eap->arg != NUL)
6619 {
6620 char_u *p = eap->arg;
6621 char_u *p_save = p;
6622
6623 tab_number = getdigits(&p);
6624 if (p == p_save || *p_save == '-' || *p != NUL
6625 || tab_number == 0)
6626 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006627 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006628 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006629 return;
6630 }
6631 }
6632 else
6633 {
6634 if (eap->addr_count == 0)
6635 tab_number = 1;
6636 else
6637 {
6638 tab_number = eap->line2;
6639 if (tab_number < 1)
6640 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006641 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006642 return;
6643 }
6644 }
6645 }
6646 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006647 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006648 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006649 tab_number = get_tabpage_arg(eap);
6650 if (eap->errmsg == NULL)
6651 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006652 break;
6653 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006654}
6655
6656/*
6657 * :tabmove command
6658 */
6659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006660ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006661{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006662 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006663
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006664 tab_number = get_tabpage_arg(eap);
6665 if (eap->errmsg == NULL)
6666 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006667}
6668
6669/*
6670 * :tabs command: List tabs and their contents.
6671 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006673ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006674{
6675 tabpage_T *tp;
6676 win_T *wp;
6677 int tabcount = 1;
6678
6679 msg_start();
6680 msg_scroll = TRUE;
6681 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6682 {
6683 msg_putchar('\n');
6684 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006685 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006686 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006687 ui_breakcheck();
6688
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006689 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006690 wp = firstwin;
6691 else
6692 wp = tp->tp_firstwin;
6693 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6694 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006695 msg_putchar('\n');
6696 msg_putchar(wp == curwin ? '>' : ' ');
6697 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006698 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6699 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006700 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006701 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006702 else
6703 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6704 IObuff, IOSIZE, TRUE);
6705 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006706 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006707 ui_breakcheck();
6708 }
6709 }
6710}
6711
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712/*
6713 * ":mode": Set screen mode.
6714 * If no argument given, just get the screen size and redraw.
6715 */
6716 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006717ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718{
6719 if (*eap->arg == NUL)
6720 shell_resized();
6721 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006722 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723}
6724
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725/*
6726 * ":resize".
6727 * set, increment or decrement current window height
6728 */
6729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006730ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
6732 int n;
6733 win_T *wp = curwin;
6734
6735 if (eap->addr_count > 0)
6736 {
6737 n = eap->line2;
6738 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6739 ;
6740 }
6741
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006742# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006744# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006746 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 {
6748 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006749 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006750 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 n = 9999;
6752 win_setwidth_win((int)n, wp);
6753 }
6754 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 {
6756 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006757 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006758 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 n = 9999;
6760 win_setheight_win((int)n, wp);
6761 }
6762}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763
6764/*
6765 * ":find [+command] <file>" command.
6766 */
6767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006768ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769{
6770#ifdef FEAT_SEARCHPATH
6771 char_u *fname;
6772 int count;
6773
6774 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6775 TRUE, curbuf->b_ffname);
6776 if (eap->addr_count > 0)
6777 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006778 // Repeat finding the file "count" times. This matters when it
6779 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 count = eap->line2;
6781 while (fname != NULL && --count > 0)
6782 {
6783 vim_free(fname);
6784 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6785 FALSE, curbuf->b_ffname);
6786 }
6787 }
6788
6789 if (fname != NULL)
6790 {
6791 eap->arg = fname;
6792#endif
6793 do_exedit(eap, NULL);
6794#ifdef FEAT_SEARCHPATH
6795 vim_free(fname);
6796 }
6797#endif
6798}
6799
6800/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006801 * ":open" simulation: for now just work like ":visual".
6802 */
6803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006804ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006805{
6806 regmatch_T regmatch;
6807 char_u *p;
6808
Bram Moolenaar65088802021-03-13 21:07:21 +01006809#ifdef FEAT_EVAL
6810 if (not_in_vim9(eap) == FAIL)
6811 return;
6812#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006813 curwin->w_cursor.lnum = eap->line2;
6814 beginline(BL_SOL | BL_FIX);
6815 if (*eap->arg == '/')
6816 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006817 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006818 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006819 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006820 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006821 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006822 if (regmatch.regprog != NULL)
6823 {
6824 regmatch.rm_ic = p_ic;
6825 p = ml_get_curline();
6826 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006827 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006828 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006829 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006830 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006831 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006832 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006833 eap->arg += STRLEN(eap->arg);
6834 }
6835 check_cursor();
6836
6837 eap->cmdidx = CMD_visual;
6838 do_exedit(eap, NULL);
6839}
6840
6841/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006842 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 */
6844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006845ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846{
6847 do_exedit(eap, NULL);
6848}
6849
6850/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006851 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006854do_exedit(
6855 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006856 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006860 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006862 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6863 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006864 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 /*
6866 * ":vi" command ends Ex mode.
6867 */
6868 if (exmode_active && (eap->cmdidx == CMD_visual
6869 || eap->cmdidx == CMD_view))
6870 {
6871 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006872 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006875 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006876 if (global_busy)
6877 {
6878 int rd = RedrawingDisabled;
6879 int nwr = no_wait_return;
6880 int ms = msg_scroll;
6881#ifdef FEAT_GUI
6882 int he = hold_gui_events;
6883#endif
6884
6885 if (eap->nextcmd != NULL)
6886 {
6887 stuffReadbuff(eap->nextcmd);
6888 eap->nextcmd = NULL;
6889 }
6890
6891 if (exmode_was != EXMODE_VIM)
6892 settmode(TMODE_RAW);
6893 RedrawingDisabled = 0;
6894 no_wait_return = 0;
6895 need_wait_return = FALSE;
6896 msg_scroll = 0;
6897#ifdef FEAT_GUI
6898 hold_gui_events = 0;
6899#endif
6900 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006901 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006902
6903 main_loop(FALSE, TRUE);
6904
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006905 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006906 RedrawingDisabled = rd;
6907 no_wait_return = nwr;
6908 msg_scroll = ms;
6909#ifdef FEAT_GUI
6910 hold_gui_events = he;
6911#endif
6912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 }
6916
6917 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006918 || eap->cmdidx == CMD_tabnew
6919 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006920 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006922 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 setpcmark();
6924 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006925 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6926 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006928 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 || *eap->arg != NUL
6930#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006931 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932#endif
6933 )
6934 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006935 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6936 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006937 if (*eap->arg != NUL && curbuf_locked())
6938 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006939
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 n = readonlymode;
6941 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6942 readonlymode = TRUE;
6943 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006944 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6945 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006946 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6947 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6949 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006950 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6952 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6953 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006954 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006956 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006957 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006958 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6959 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006960 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006962 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (old_curwin != NULL)
6964 {
6965 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006966 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006968#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006969 cleanup_T cs;
6970
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006971 // Reset the error/interrupt/exception state here so that
6972 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006973 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006974#endif
6975#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006977#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006978 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006979
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006980#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006981 // Restore the error/interrupt/exception state if not
6982 // discarded by a new aborting error, interrupt, or
6983 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006984 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 }
6987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 }
6989 else if (readonlymode && curbuf->b_nwindows == 1)
6990 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006991 // When editing an already visited buffer, 'readonly' won't be set
6992 // but the previous value is kept. With ":view" and ":sview" we
6993 // want the file to be readonly, except when another window is
6994 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 curbuf->b_p_ro = TRUE;
6996 }
6997 readonlymode = n;
6998 }
6999 else
7000 {
7001 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007002 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003#ifdef FEAT_TITLE
7004 n = curwin->w_arg_idx_invalid;
7005#endif
7006 check_arg_idx(curwin);
7007#ifdef FEAT_TITLE
7008 if (n != curwin->w_arg_idx_invalid)
7009 maketitle();
7010#endif
7011 }
7012
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 /*
7014 * if ":split file" worked, set alternate file name in old window to new
7015 * file
7016 */
7017 if (old_curwin != NULL
7018 && *eap->arg != NUL
7019 && curwin != old_curwin
7020 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007021 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007022 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
7025 ex_no_reprint = TRUE;
7026}
7027
7028#ifndef FEAT_GUI
7029/*
7030 * ":gui" and ":gvim" when there is no GUI.
7031 */
7032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007033ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007035 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036}
7037#endif
7038
Bram Moolenaar4f974752019-02-17 17:44:42 +01007039#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007041ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 gui_make_tearoff(eap->arg);
7044}
7045#endif
7046
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007047#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7048 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007050ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007052# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7053 if (gui.in_use)
7054 gui_make_popup(eap->arg, eap->forceit);
7055# ifdef FEAT_TERM_POPUP_MENU
7056 else
7057# endif
7058# endif
7059# ifdef FEAT_TERM_POPUP_MENU
7060 pum_make_popup(eap->arg, eap->forceit);
7061# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062}
7063#endif
7064
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007066ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067{
7068 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007069 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007071 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072}
7073
7074/*
7075 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7076 * offset.
7077 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007080ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007083 win_T *save_curwin = curwin;
7084 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 long topline;
7086 long y;
7087 linenr_T old_linenr = curwin->w_cursor.lnum;
7088
7089 setpcmark();
7090
7091 /*
7092 * determine max topline
7093 */
7094 if (curwin->w_p_scb)
7095 {
7096 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007097 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 {
7099 if (wp->w_p_scb && wp->w_buffer)
7100 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007101 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 if (topline > y)
7103 topline = y;
7104 }
7105 }
7106 if (topline < 1)
7107 topline = 1;
7108 }
7109 else
7110 {
7111 topline = 1;
7112 }
7113
7114
7115 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007116 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007118 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 {
7120 if (curwin->w_p_scb)
7121 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007122 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 y = topline - curwin->w_topline;
7124 if (y > 0)
7125 scrollup(y, TRUE);
7126 else
7127 scrolldown(-y, TRUE);
7128 curwin->w_scbind_pos = topline;
7129 redraw_later(VALID);
7130 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 }
7133 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007134 curwin = save_curwin;
7135 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 if (curwin->w_p_scb)
7137 {
7138 did_syncbind = TRUE;
7139 checkpcmark();
7140 if (old_linenr != curwin->w_cursor.lnum)
7141 {
7142 char_u ctrl_o[2];
7143
7144 ctrl_o[0] = Ctrl_O;
7145 ctrl_o[1] = 0;
7146 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7147 }
7148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149}
7150
7151
7152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007153ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007155 int i;
7156 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7157 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007159 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 do_bang(1, eap, FALSE, FALSE, TRUE);
7161 else
7162 {
7163 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7164 return;
7165
7166#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007167 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {
7169 char_u *browseFile;
7170
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007171 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 NULL, NULL, NULL, curbuf);
7173 if (browseFile != NULL)
7174 {
7175 i = readfile(browseFile, NULL,
7176 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7177 vim_free(browseFile);
7178 }
7179 else
7180 i = OK;
7181 }
7182 else
7183#endif
7184 if (*eap->arg == NUL)
7185 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007186 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 return;
7188 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7189 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7190 }
7191 else
7192 {
7193 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7194 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7195 i = readfile(eap->arg, NULL,
7196 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7197
7198 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007199 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007201#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 if (!aborting())
7203#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007204 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 }
7206 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007207 {
7208 if (empty && exmode_active)
7209 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007210 // Delete the empty line that remains. Historically ex does
7211 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007212 if (eap->line2 == 0)
7213 lnum = curbuf->b_ml.ml_line_count;
7214 else
7215 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007216 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007217 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007218 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007219 if (curwin->w_cursor.lnum > 1
7220 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007221 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007222 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007223 }
7224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 }
7228}
7229
Bram Moolenaarea408852005-06-25 22:49:46 +00007230static char_u *prev_dir = NULL;
7231
7232#if defined(EXITFREE) || defined(PROTO)
7233 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007234free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007235{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007236 VIM_CLEAR(prev_dir);
7237 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007238}
7239#endif
7240
Bram Moolenaarf4258302013-06-02 18:20:17 +02007241/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007242 * Get the previous directory for the given chdir scope.
7243 */
7244 static char_u *
7245get_prevdir(cdscope_T scope)
7246{
7247 if (scope == CDSCOPE_WINDOW)
7248 return curwin->w_prevdir;
7249 else if (scope == CDSCOPE_TABPAGE)
7250 return curtab->tp_prevdir;
7251 return prev_dir;
7252}
7253
7254/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007255 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007256 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7257 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007258 */
7259 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007260post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007261{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007262 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007263 // Clear tab local directory for both :cd and :tcd
7264 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007265 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007266 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007267 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007268 char_u *pdir = get_prevdir(scope);
7269
7270 // If still in the global directory, need to remember current
7271 // directory as the global directory.
7272 if (globaldir == NULL && pdir != NULL)
7273 globaldir = vim_strsave(pdir);
7274
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007275 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007276 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007277 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007278 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007279 curtab->tp_localdir = vim_strsave(NameBuff);
7280 else
7281 curwin->w_localdir = vim_strsave(NameBuff);
7282 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007283 }
7284 else
7285 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007286 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007287 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007288 }
7289
7290 shorten_fnames(TRUE);
7291}
7292
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007293/*
7294 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007295 * chdir() function.
7296 * scope == CDSCOPE_WINDOW: changes the window-local directory
7297 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7298 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007299 * Returns TRUE if the directory is successfully changed.
7300 */
7301 int
7302changedir_func(
7303 char_u *new_dir,
7304 int forceit,
7305 cdscope_T scope)
7306{
7307 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007308 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007309 int dir_differs;
7310 int retval = FALSE;
7311
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007312 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007313 return FALSE;
7314
7315 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7316 {
7317 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7318 return FALSE;
7319 }
7320
7321 // ":cd -": Change to previous directory
7322 if (STRCMP(new_dir, "-") == 0)
7323 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007324 pdir = get_prevdir(scope);
7325 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007326 {
7327 emsg(_("E186: No previous directory"));
7328 return FALSE;
7329 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007330 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007331 }
7332
Bram Moolenaar002bc792020-06-05 22:33:42 +02007333 // Free the previous directory
7334 tofree = get_prevdir(scope);
7335
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007336 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007337 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007338 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007339 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007340 pdir = NULL;
7341 if (scope == CDSCOPE_WINDOW)
7342 curwin->w_prevdir = pdir;
7343 else if (scope == CDSCOPE_TABPAGE)
7344 curtab->tp_prevdir = pdir;
7345 else
7346 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007347
7348#if defined(UNIX) || defined(VMS)
7349 // for UNIX ":cd" means: go to home directory
7350 if (*new_dir == NUL)
7351 {
7352 // use NameBuff for home directory name
7353# ifdef VMS
7354 char_u *p;
7355
7356 p = mch_getenv((char_u *)"SYS$LOGIN");
7357 if (p == NULL || *p == NUL) // empty is the same as not set
7358 NameBuff[0] = NUL;
7359 else
7360 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7361# else
7362 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7363# endif
7364 new_dir = NameBuff;
7365 }
7366#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007367 dir_differs = new_dir == NULL || pdir == NULL
7368 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007369 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7370 emsg(_(e_failed));
7371 else
7372 {
7373 char_u *acmd_fname;
7374
7375 post_chdir(scope);
7376
7377 if (dir_differs)
7378 {
7379 if (scope == CDSCOPE_WINDOW)
7380 acmd_fname = (char_u *)"window";
7381 else if (scope == CDSCOPE_TABPAGE)
7382 acmd_fname = (char_u *)"tabpage";
7383 else
7384 acmd_fname = (char_u *)"global";
7385 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7386 curbuf);
7387 }
7388 retval = TRUE;
7389 }
7390 vim_free(tofree);
7391
7392 return retval;
7393}
Bram Moolenaarea408852005-06-25 22:49:46 +00007394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007396 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007398 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007399ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007401 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
7403 new_dir = eap->arg;
7404#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007405 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 if (*new_dir == NUL)
7407 ex_pwd(NULL);
7408 else
7409#endif
7410 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007411 cdscope_T scope = CDSCOPE_GLOBAL;
7412
7413 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7414 scope = CDSCOPE_WINDOW;
7415 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7416 scope = CDSCOPE_TABPAGE;
7417
7418 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007419 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007420 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007421 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 ex_pwd(eap);
7423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 }
7425}
7426
7427/*
7428 * ":pwd".
7429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007431ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432{
7433 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7434 {
7435#ifdef BACKSLASH_IN_FILENAME
7436 slash_adjust(NameBuff);
7437#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007438 if (p_verbose > 0)
7439 {
7440 char *context = "global";
7441
7442 if (curwin->w_localdir != NULL)
7443 context = "window";
7444 else if (curtab->tp_localdir != NULL)
7445 context = "tabpage";
7446 smsg("[%s] %s", context, (char *)NameBuff);
7447 }
7448 else
7449 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007452 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453}
7454
7455/*
7456 * ":=".
7457 */
7458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007459ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007461 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007462 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463}
7464
7465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007466ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007468 int n;
7469 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470
7471 if (cursor_valid())
7472 {
7473 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7474 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007475 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007477
7478 len = eap->line2;
7479 switch (*eap->arg)
7480 {
7481 case 'm': break;
7482 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007483 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007484 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007485
7486 // Hide the cursor if invoked with !
7487 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488}
7489
7490/*
7491 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007492 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 */
7494 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007495do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496{
Bram Moolenaar52953192019-08-16 10:27:13 +02007497 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007498 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007499# ifdef ELAPSED_FUNC
7500 elapsed_T start_tv;
7501
7502 // Remember at what time we started, so that we know how much longer we
7503 // should wait after waiting for a bit.
7504 ELAPSED_INIT(start_tv);
7505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007507 if (hide_cursor)
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007508 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007509 else
7510 cursor_on();
7511
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007512 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007513 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007515 wait_now = msec - done > 1000L ? 1000L : msec - done;
7516#ifdef FEAT_TIMERS
7517 {
7518 long due_time = check_due_timer();
7519
7520 if (due_time > 0 && due_time < wait_now)
7521 wait_now = due_time;
7522 }
7523#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007524#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007525 if (has_any_channel() && wait_now > 20L)
7526 wait_now = 20L;
7527#endif
7528#ifdef FEAT_SOUND
7529 if (has_any_sound_callback() && wait_now > 20L)
7530 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007531#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007532 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007533
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007534#ifdef FEAT_JOB_CHANNEL
7535 if (has_any_channel())
7536 ui_breakcheck_force(TRUE);
7537 else
7538#endif
7539 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007540#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007541 // Process the netbeans and clientserver messages that may have been
7542 // received in the call to ui_breakcheck() when the GUI is in use. This
7543 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007544 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007545#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007546
7547# ifdef ELAPSED_FUNC
7548 // actual time passed
7549 done = ELAPSED_FUNC(start_tv);
7550# else
7551 // guestimate time passed (will actually be more)
7552 done += wait_now;
7553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007555
7556 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7557 // input buffer, otherwise a following call to input() fails.
7558 if (got_int)
7559 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007560
7561 if (hide_cursor)
7562 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563}
7564
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565/*
7566 * ":winsize" command (obsolete).
7567 */
7568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007569ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570{
7571 int w, h;
7572 char_u *arg = eap->arg;
7573 char_u *p;
7574
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007575 if (!isdigit(*arg))
7576 {
7577 semsg(_(e_invarg2), arg);
7578 return;
7579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 w = getdigits(&arg);
7581 arg = skipwhite(arg);
7582 p = arg;
7583 h = getdigits(&arg);
7584 if (*p != NUL && *arg == NUL)
7585 set_shellsize(w, h, TRUE);
7586 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007587 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588}
7589
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007591ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
7593 int xchar = NUL;
7594 char_u *p;
7595
7596 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7597 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007598 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 if (eap->arg[1] == NUL)
7600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007601 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 return;
7603 }
7604 xchar = eap->arg[1];
7605 p = eap->arg + 2;
7606 }
7607 else
7608 p = eap->arg + 1;
7609
Bram Moolenaar63b91732021-08-05 20:40:03 +02007610 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007612 if (*p != NUL && *p != (
7613#ifdef FEAT_EVAL
7614 in_vim9script() ? '#' :
7615#endif
7616 '"')
7617 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007618 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007619 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007621 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007622 postponed_split_flags = cmdmod.cmod_split;
7623 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7625 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007626 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 }
7628}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629
Bram Moolenaar843ee412004-06-30 16:16:41 +00007630#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631/*
7632 * ":winpos".
7633 */
7634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007635ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636{
7637 int x, y;
7638 char_u *arg = eap->arg;
7639 char_u *p;
7640
7641 if (*arg == NUL)
7642 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007643# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007644# ifdef VIMDLL
7645 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7646 mch_get_winpos(&x, &y) != FAIL)
7647# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007649# else
7650 if (mch_get_winpos(&x, &y) != FAIL)
7651# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {
7653 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007654 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 }
7656 else
7657# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007658 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 }
7660 else
7661 {
7662 x = getdigits(&arg);
7663 arg = skipwhite(arg);
7664 p = arg;
7665 y = getdigits(&arg);
7666 if (*p == NUL || *arg != NUL)
7667 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007668 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 return;
7670 }
7671# ifdef FEAT_GUI
7672 if (gui.in_use)
7673 gui_mch_set_winpos(x, y);
7674 else if (gui.starting)
7675 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007676 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 gui_win_x = x;
7678 gui_win_y = y;
7679 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007680# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 else
7682# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007683# endif
7684# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007685 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686# endif
7687# ifdef HAVE_TGETENT
7688 if (*T_CWP)
7689 term_set_winpos(x, y);
7690# endif
7691 }
7692}
7693#endif
7694
7695/*
7696 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7697 */
7698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007699ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701 oparg_T oa;
7702
7703 clear_oparg(&oa);
7704 oa.regname = eap->regname;
7705 oa.start.lnum = eap->line1;
7706 oa.end.lnum = eap->line2;
7707 oa.line_count = eap->line2 - eap->line1 + 1;
7708 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007710 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {
7712 setpcmark();
7713 curwin->w_cursor.lnum = eap->line1;
7714 beginline(BL_SOL | BL_FIX);
7715 }
7716
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007717 if (VIsual_active)
7718 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007719
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 switch (eap->cmdidx)
7721 {
7722 case CMD_delete:
7723 oa.op_type = OP_DELETE;
7724 op_delete(&oa);
7725 break;
7726
7727 case CMD_yank:
7728 oa.op_type = OP_YANK;
7729 (void)op_yank(&oa, FALSE, TRUE);
7730 break;
7731
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007732 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007733 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007735 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7736#else
7737 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007739 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 oa.op_type = OP_RSHIFT;
7741 else
7742 oa.op_type = OP_LSHIFT;
7743 op_shift(&oa, FALSE, eap->amount);
7744 break;
7745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007747 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748}
7749
7750/*
7751 * ":put".
7752 */
7753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007754ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007756 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 if (eap->line2 == 0)
7758 {
7759 eap->line2 = 1;
7760 eap->forceit = TRUE;
7761 }
7762 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007763 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007764 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765}
7766
7767/*
7768 * Handle ":copy" and ":move".
7769 */
7770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007771ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773 long n;
7774
Bram Moolenaar491799b2020-08-01 19:23:43 +02007775#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007776 if (not_in_vim9(eap) == FAIL)
7777 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007778#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007779 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007780 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 {
7782 eap->nextcmd = NULL;
7783 return;
7784 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007785 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786
7787 /*
7788 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7789 */
7790 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7791 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007792 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 return;
7794 }
7795
7796 if (eap->cmdidx == CMD_move)
7797 {
7798 if (do_move(eap->line1, eap->line2, n) == FAIL)
7799 return;
7800 }
7801 else
7802 ex_copy(eap->line1, eap->line2, n);
7803 u_clearline();
7804 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007805 ex_may_print(eap);
7806}
7807
7808/*
7809 * Print the current line if flags were given to the Ex command.
7810 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007811 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007812ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007813{
7814 if (eap->flags != 0)
7815 {
7816 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7817 (eap->flags & EXFLAG_LIST));
7818 ex_no_reprint = TRUE;
7819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820}
7821
7822/*
7823 * ":smagic" and ":snomagic".
7824 */
7825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007826ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007828 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007830 magic_overruled = eap->cmdidx == CMD_smagic
7831 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007832 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007833 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834}
7835
7836/*
7837 * ":join".
7838 */
7839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007840ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
7842 curwin->w_cursor.lnum = eap->line1;
7843 if (eap->line1 == eap->line2)
7844 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007845 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 return;
7847 if (eap->line2 == curbuf->b_ml.ml_line_count)
7848 {
7849 beep_flush();
7850 return;
7851 }
7852 ++eap->line2;
7853 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007854 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007856 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857}
7858
7859/*
7860 * ":[addr]@r" or ":[addr]*r": execute register
7861 */
7862 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007863ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864{
7865 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007866 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867
7868 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007869 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870
7871#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007872 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873#endif
7874
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007875 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 c = *eap->arg;
7877 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7878 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007879 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007880 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7881 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 else
7886 {
7887 int save_efr = exec_from_reg;
7888
7889 exec_from_reg = TRUE;
7890
7891 /*
7892 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007893 * Continue until the stuff buffer is empty and all added characters
7894 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007896 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7898
7899 exec_from_reg = save_efr;
7900 }
7901}
7902
7903/*
7904 * ":!".
7905 */
7906 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007907ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908{
7909 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7910}
7911
7912/*
7913 * ":undo".
7914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007916ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007918 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007919 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007920 else
7921 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922}
7923
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007924#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007926ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007927{
7928 char_u hash[UNDO_HASH_SIZE];
7929
7930 u_compute_hash(hash);
7931 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7932}
7933
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007935ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007936{
7937 char_u hash[UNDO_HASH_SIZE];
7938
7939 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007940 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007941}
7942#endif
7943
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944/*
7945 * ":redo".
7946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007948ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949{
7950 u_redo(1);
7951}
7952
7953/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007954 * ":earlier" and ":later".
7955 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007956 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007957ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007958{
7959 long count = 0;
7960 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007961 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007962 char_u *p = eap->arg;
7963
7964 if (*p == NUL)
7965 count = 1;
7966 else if (isdigit(*p))
7967 {
7968 count = getdigits(&p);
7969 switch (*p)
7970 {
7971 case 's': ++p; sec = TRUE; break;
7972 case 'm': ++p; sec = TRUE; count *= 60; break;
7973 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007974 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7975 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007976 }
7977 }
7978
7979 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007980 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007981 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007982 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7983 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007984}
7985
7986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 * ":redir": start/stop redirection.
7988 */
7989 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007990ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991{
7992 char *mode;
7993 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007994 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995
Bram Moolenaarba768492016-07-08 15:32:54 +02007996#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007997 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007999 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008000 return;
8001 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008002#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008003
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 if (STRICMP(eap->arg, "END") == 0)
8005 close_redir();
8006 else
8007 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008008 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008010 ++arg;
8011 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008013 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 mode = "a";
8015 }
8016 else
8017 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008018 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
8020 close_redir();
8021
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008022 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008023 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 if (fname == NULL)
8025 return;
8026#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008027 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 {
8029 char_u *browseFile;
8030
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008031 browseFile = do_browse(BROWSE_SAVE,
8032 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008033 fname, NULL, NULL,
8034 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008036 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 vim_free(fname);
8038 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008039 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 }
8041#endif
8042
8043 redir_fd = open_exfile(fname, eap->forceit, mode);
8044 vim_free(fname);
8045 }
8046#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008047 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008049 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008051 ++arg;
8052 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008054 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008055 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008057 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008059 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008060 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008061 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008062 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008064 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008065 if (*arg == '>')
8066 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008067 // Make register empty when not using @A-@Z and the
8068 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008069 if (*arg == NUL && !isupper(redir_reg))
8070 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008072 }
8073 if (*arg != NUL)
8074 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008075 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008076 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008077 }
8078 }
8079 else if (*arg == '=' && arg[1] == '>')
8080 {
8081 int append;
8082
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008083 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008084 close_redir();
8085 arg += 2;
8086
8087 if (*arg == '>')
8088 {
8089 ++arg;
8090 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 }
8092 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008093 append = FALSE;
8094
8095 if (var_redir_start(skipwhite(arg), append) == OK)
8096 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 }
8098#endif
8099
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008100 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008103 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008105
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008106 // Make sure redirection is not off. Can happen for cmdline completion
8107 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008108 if (redir_fd != NULL
8109#ifdef FEAT_EVAL
8110 || redir_reg || redir_vname
8111#endif
8112 )
8113 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114}
8115
8116/*
8117 * ":redraw": force redraw
8118 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008119 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008120ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121{
8122 int r = RedrawingDisabled;
8123 int p = p_lz;
8124
8125 RedrawingDisabled = 0;
8126 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008127 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008129 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130#ifdef FEAT_TITLE
8131 if (need_maketitle)
8132 maketitle();
8133#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008134#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8135# ifdef VIMDLL
8136 if (!gui.in_use)
8137# endif
8138 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 RedrawingDisabled = r;
8141 p_lz = p;
8142
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008143 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 msg_didout = FALSE;
8145 msg_col = 0;
8146
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008147 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008148 need_wait_return = FALSE;
8149
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 out_flush();
8151}
8152
8153/*
8154 * ":redrawstatus": force redraw of status line(s)
8155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008157ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 int r = RedrawingDisabled;
8160 int p = p_lz;
8161
8162 RedrawingDisabled = 0;
8163 p_lz = FALSE;
8164 if (eap->forceit)
8165 status_redraw_all();
8166 else
8167 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008168 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 RedrawingDisabled = r;
8170 p_lz = p;
8171 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172}
8173
Bram Moolenaare12bab32019-01-08 22:02:56 +01008174/*
8175 * ":redrawtabline": force redraw of the tabline
8176 */
8177 static void
8178ex_redrawtabline(exarg_T *eap UNUSED)
8179{
8180 int r = RedrawingDisabled;
8181 int p = p_lz;
8182
8183 RedrawingDisabled = 0;
8184 p_lz = FALSE;
8185
8186 draw_tabline();
8187
8188 RedrawingDisabled = r;
8189 p_lz = p;
8190 out_flush();
8191}
8192
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008194close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195{
8196 if (redir_fd != NULL)
8197 {
8198 fclose(redir_fd);
8199 redir_fd = NULL;
8200 }
8201#ifdef FEAT_EVAL
8202 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008203 if (redir_vname)
8204 {
8205 var_redir_stop();
8206 redir_vname = 0;
8207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208#endif
8209}
8210
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008211#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008212 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008213vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008214{
8215 if (vim_mkdir(name, prot) != 0)
8216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008217 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008218 return FAIL;
8219 }
8220 return OK;
8221}
8222#endif
8223
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224/*
8225 * Open a file for writing for an Ex command, with some checks.
8226 * Return file descriptor, or NULL on failure.
8227 */
8228 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008229open_exfile(
8230 char_u *fname,
8231 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008232 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
8234 FILE *fd;
8235
8236#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008237 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 if (mch_isdir(fname))
8239 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008240 semsg(_(e_src_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 return NULL;
8242 }
8243#endif
8244 if (!forceit && *mode != 'a' && vim_fexists(fname))
8245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008246 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 return NULL;
8248 }
8249
8250 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008251 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252
8253 return fd;
8254}
8255
8256/*
8257 * ":mark" and ":k".
8258 */
8259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008260ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
8262 pos_T pos;
8263
Bram Moolenaar10b94212021-02-19 21:42:57 +01008264#ifdef FEAT_EVAL
8265 if (not_in_vim9(eap) == FAIL)
8266 return;
8267#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008268 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008269 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008270 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008271 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 else
8273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008274 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 curwin->w_cursor.lnum = eap->line2;
8276 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008277 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008278 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008279 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 }
8281}
8282
8283/*
8284 * Update w_topline, w_leftcol and the cursor position.
8285 */
8286 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008287update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008289 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 update_topline();
8291 if (!curwin->w_p_wrap)
8292 validate_cursor();
8293 update_curswant();
8294}
8295
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008297 * Save the current State and go to Normal mode.
8298 * Return TRUE if the typeahead could be saved.
8299 */
8300 int
8301save_current_state(save_state_T *sst)
8302{
8303 sst->save_msg_scroll = msg_scroll;
8304 sst->save_restart_edit = restart_edit;
8305 sst->save_msg_didout = msg_didout;
8306 sst->save_State = State;
8307 sst->save_insertmode = p_im;
8308 sst->save_finish_op = finish_op;
8309 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008310 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008311
Bram Moolenaar4324d872020-12-01 20:12:24 +01008312 msg_scroll = FALSE; // no msg scrolling in Normal mode
8313 restart_edit = 0; // don't go to Insert mode
8314 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008315
Bram Moolenaara452b802020-12-01 21:47:59 +01008316 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008317 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008318
8319 /*
8320 * Save the current typeahead. This is required to allow using ":normal"
8321 * from an event handler and makes sure we don't hang when the argument
8322 * ends with half a command.
8323 */
8324 save_typeahead(&sst->tabuf);
8325 return sst->tabuf.typebuf_valid;
8326}
8327
8328 void
8329restore_current_state(save_state_T *sst)
8330{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008331 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008332 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008333
8334 msg_scroll = sst->save_msg_scroll;
8335 restart_edit = sst->save_restart_edit;
8336 p_im = sst->save_insertmode;
8337 finish_op = sst->save_finish_op;
8338 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008339 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008340 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008341 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008342
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008343 // Restore the state (needed when called from a function executed for
8344 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008345 State = sst->save_State;
8346#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008347 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008348#endif
8349}
8350
8351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 * ":normal[!] {commands}": Execute normal mode commands.
8353 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008354 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008355ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008357 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 char_u *arg = NULL;
8359 int l;
8360 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008362 if (ex_normal_lock > 0)
8363 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008364 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008365 return;
8366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 if (ex_normal_busy >= p_mmd)
8368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008369 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 return;
8371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 /*
8374 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8375 * this for the K_SPECIAL leading byte, otherwise special keys will not
8376 * work.
8377 */
8378 if (has_mbyte)
8379 {
8380 int len = 0;
8381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008382 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 for (p = eap->arg; *p != NUL; ++p)
8384 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008385#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008386 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008388#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008389 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008390 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008391#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 )
8395 len += 2;
8396 }
8397 if (len > 0)
8398 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008399 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 if (arg != NULL)
8401 {
8402 len = 0;
8403 for (p = eap->arg; *p != NUL; ++p)
8404 {
8405 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008406#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 if (*p == CSI)
8408 {
8409 arg[len++] = KS_EXTRA;
8410 arg[len++] = (int)KE_CSI;
8411 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008412#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008413 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
8415 arg[len++] = *++p;
8416 if (*p == K_SPECIAL)
8417 {
8418 arg[len++] = KS_SPECIAL;
8419 arg[len++] = KE_FILLER;
8420 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008421#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 else if (*p == CSI)
8423 {
8424 arg[len++] = KS_EXTRA;
8425 arg[len++] = (int)KE_CSI;
8426 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 }
8429 arg[len] = NUL;
8430 }
8431 }
8432 }
8433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008435 ++ex_normal_busy;
8436 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {
8438 /*
8439 * Repeat the :normal command for each line in the range. When no
8440 * range given, execute it just once, without positioning the cursor
8441 * first.
8442 */
8443 do
8444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 if (eap->addr_count != 0)
8446 {
8447 curwin->w_cursor.lnum = eap->line1++;
8448 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008449 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 }
8451
Bram Moolenaar13505972019-01-24 15:04:48 +01008452 exec_normal_cmd(arg != NULL
8453 ? arg
8454 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 }
8456 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8457 }
8458
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008459 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 update_topline_cursor();
8461
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008462 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008464 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008465#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008466 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008467#endif
8468
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470}
8471
8472/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008473 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 */
8475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008476ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008478 if (eap->forceit)
8479 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008480 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008481 if (!curwin->w_cursor.lnum)
8482 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008483 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008484 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008485#ifdef FEAT_TERMINAL
8486 // Ignore this when running in an active terminal.
8487 if (term_job_running(curbuf->b_term))
8488 return;
8489#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008490
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008491 // Ignore the command when already in Insert mode. Inserting an
8492 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008493 if (State & INSERT)
8494 return;
8495
Bram Moolenaar64969662005-12-14 21:59:55 +00008496 if (eap->cmdidx == CMD_startinsert)
8497 restart_edit = 'a';
8498 else if (eap->cmdidx == CMD_startreplace)
8499 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008501 restart_edit = 'V';
8502
8503 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008505 if (eap->cmdidx == CMD_startinsert)
8506 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008507 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008509
8510 if (VIsual_active)
8511 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512}
8513
8514/*
8515 * ":stopinsert"
8516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008518ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519{
8520 restart_edit = 0;
8521 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008522 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008525/*
8526 * Execute normal mode command "cmd".
8527 * "remap" can be REMAP_NONE or REMAP_YES.
8528 */
8529 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008530exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008531{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008532 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008533 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008534 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008535}
Bram Moolenaar25281632016-01-21 23:32:32 +01008536
Bram Moolenaar25281632016-01-21 23:32:32 +01008537/*
8538 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008539 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008540 */
8541 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008542exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008543{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008544 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008545 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008546
Bram Moolenaar905dd902019-04-07 14:21:47 +02008547 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8548 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008549 clear_oparg(&oa);
8550 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008551 while ((!stuff_empty()
8552 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008553 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008554 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008555 {
8556 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008557#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008558 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008559 && oa.op_type == OP_NOP && oa.regname == NUL
8560 && !VIsual_active)
8561 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008562 // If terminal_loop() returns OK we got a key that is handled
8563 // in Normal model. With FAIL we first need to position the
8564 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008565 if (terminal_loop(TRUE) == OK)
8566 normal_cmd(&oa, TRUE);
8567 }
8568 else
8569#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008570 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008571 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008572 }
8573}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008574
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575#ifdef FEAT_FIND_ID
8576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008577ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8580 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8581 (linenr_T)1, (linenr_T)MAXLNUM);
8582}
8583
Bram Moolenaar4033c552017-09-16 20:54:51 +02008584#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585/*
8586 * ":psearch"
8587 */
8588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008589ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590{
8591 g_do_tagpreview = p_pvh;
8592 ex_findpat(eap);
8593 g_do_tagpreview = 0;
8594}
8595#endif
8596
8597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008598ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
8600 int whole = TRUE;
8601 long n;
8602 char_u *p;
8603 int action;
8604
8605 switch (cmdnames[eap->cmdidx].cmd_name[2])
8606 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008607 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8609 action = ACTION_GOTO;
8610 else
8611 action = ACTION_SHOW;
8612 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008613 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 action = ACTION_SHOW_ALL;
8615 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008616 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 action = ACTION_GOTO;
8618 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008619 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 action = ACTION_SPLIT;
8621 break;
8622 }
8623
8624 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008625 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {
8627 n = getdigits(&eap->arg);
8628 eap->arg = skipwhite(eap->arg);
8629 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008630 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 {
8632 whole = FALSE;
8633 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008634 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 if (*p)
8636 {
8637 *p++ = NUL;
8638 p = skipwhite(p);
8639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008640 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008641 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008642 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008644 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 }
8646 }
8647 if (!eap->skip)
8648 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8649 whole, !eap->forceit,
8650 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8651 n, action, eap->line1, eap->line2);
8652}
8653#endif
8654
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655
Bram Moolenaar4033c552017-09-16 20:54:51 +02008656#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657/*
8658 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8659 */
8660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008661ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008663 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8665}
8666
8667/*
8668 * ":pedit"
8669 */
8670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008671ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672{
8673 win_T *curwin_save = curwin;
8674
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008675 if (ERROR_IF_ANY_POPUP_WINDOW)
8676 return;
8677
Bram Moolenaar026587b2019-08-17 15:08:00 +02008678 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008680 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008681
Bram Moolenaar026587b2019-08-17 15:08:00 +02008682 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008684
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 if (curwin != curwin_save && win_valid(curwin_save))
8686 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008687 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 validate_cursor();
8689 redraw_later(VALID);
8690 win_enter(curwin_save, TRUE);
8691 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008692# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008693 else if (WIN_IS_POPUP(curwin))
8694 {
8695 // can't keep focus in popup window
8696 win_enter(firstwin, TRUE);
8697 }
8698# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 g_do_tagpreview = 0;
8700}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702
8703/*
8704 * ":stag", ":stselect" and ":stjump".
8705 */
8706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008707ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708{
8709 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008710 postponed_split_flags = cmdmod.cmod_split;
8711 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8713 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008714 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
8717/*
8718 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8719 */
8720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008721ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722{
8723 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8724}
8725
8726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008727ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
8729 int cmd;
8730
8731 switch (name[1])
8732 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008733 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008735 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008737 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008739 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008741 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008743 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008745 case 'f': // ":tfirst"
8746 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008748 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008750 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008752 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008754 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 return;
8756 }
8757#endif
8758 cmd = DT_TAG;
8759 break;
8760 }
8761
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008762 if (name[0] == 'l')
8763 {
8764#ifndef FEAT_QUICKFIX
8765 ex_ni(eap);
8766 return;
8767#else
8768 cmd = DT_LTAG;
8769#endif
8770 }
8771
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8773 eap->forceit, TRUE);
8774}
8775
8776/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008777 * Check "str" for starting with a special cmdline variable.
8778 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8779 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8780 */
8781 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008782find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008783{
8784 int len;
8785 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008786 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008787 "%",
8788#define SPEC_PERC 0
8789 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008790#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008791 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008792#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008793 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008794#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008795 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008796#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008797 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008798#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008799 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008800#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008801 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008802#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008803 "<stack>", // call stack
8804#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008805 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008806#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008807 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008808#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008809 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008810#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008811 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008812#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008813 "<SID>", // script ID: <SNR>123_
8814#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008815#ifdef FEAT_CLIENTSERVER
8816 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008817# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008818#endif
8819 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008820
K.Takataeeec2542021-06-02 13:28:16 +02008821 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008822 {
8823 len = (int)STRLEN(spec_str[i]);
8824 if (STRNCMP(src, spec_str[i], len) == 0)
8825 {
8826 *usedlen = len;
8827 return i;
8828 }
8829 }
8830 return -1;
8831}
8832
8833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 * Evaluate cmdline variables.
8835 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008836 * change "%" to curbuf->b_ffname
8837 * "#" to curwin->w_alt_fnum
8838 * "%%" to curwin->w_alt_fnum in Vim9 script
8839 * "<cword>" to word under the cursor
8840 * "<cWORD>" to WORD under the cursor
8841 * "<cexpr>" to C-expression under the cursor
8842 * "<cfile>" to path name under the cursor
8843 * "<sfile>" to sourced file name
8844 * "<stack>" to call stack
8845 * "<slnum>" to sourced file line number
8846 * "<afile>" to file name for autocommand
8847 * "<abuf>" to buffer number for autocommand
8848 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 *
8850 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8851 * "" for error without a message) and NULL is returned.
8852 * Returns an allocated string if a valid match was found.
8853 * Returns NULL if no match was found. "usedlen" then still contains the
8854 * number of characters to skip.
8855 */
8856 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008857eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008858 char_u *src, // pointer into commandline
8859 char_u *srcstart, // beginning of valid memory for src
8860 int *usedlen, // characters after src that are used
8861 linenr_T *lnump, // line number for :e command, or NULL
8862 char **errormsg, // pointer to error message
8863 int *escaped) // return value has escaped white space (can
8864 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
8866 int i;
8867 char_u *s;
8868 char_u *result;
8869 char_u *resultbuf = NULL;
8870 int resultlen;
8871 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008872 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008874 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
8878 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008879 if (escaped != NULL)
8880 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
8882 /*
8883 * Check if there is something to do.
8884 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008885 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008886 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 {
8888 *usedlen = 1;
8889 return NULL;
8890 }
8891
8892 /*
8893 * Skip when preceded with a backslash "\%" and "\#".
8894 * Note: In "\\%" the % is also not recognized!
8895 */
8896 if (src > srcstart && src[-1] == '\\')
8897 {
8898 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008899 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 return NULL;
8901 }
8902
8903 /*
8904 * word or WORD under cursor
8905 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008906 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8907 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008909 resultlen = find_ident_under_cursor(&result,
8910 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8911 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8912 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 if (resultlen == 0)
8914 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008915 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 return NULL;
8917 }
8918 }
8919
8920 /*
8921 * '#': Alternate file name
8922 * '%': Current file name
8923 * File name under the cursor
8924 * File name for autocommand
8925 * and following modifiers
8926 */
8927 else
8928 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008929 int off = 0;
8930
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 switch (spec_idx)
8932 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008933 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008934#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008935 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008938 // '%': current file
8939 if (curbuf->b_fname == NULL)
8940 {
8941 result = (char_u *)"";
8942 valid = 0; // Must have ":p:h" to be valid
8943 }
8944 else
8945 {
8946 result = curbuf->b_fname;
8947 tilde_file = STRCMP(result, "~") == 0;
8948 }
8949 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008951#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008952 // "%%" alternate file
8953 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008954#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008955 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008956 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008957 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008959 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 result = arg_all();
8961 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008962 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008963 if (escaped != NULL)
8964 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 break;
8967 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008968 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008969 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008970 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008972 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008973 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008974 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008975 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008977 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008979 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00008980 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008981 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008982 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008983 return NULL;
8984 }
8985#ifdef FEAT_EVAL
8986 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8987 (long)i);
8988 if (result == NULL)
8989 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008990 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008991 return NULL;
8992 }
8993#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008994 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 }
8998 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008999 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009000 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9001 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009002 buf = buflist_findnr(i);
9003 if (buf == NULL)
9004 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009005 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009006 return NULL;
9007 }
9008 if (lnump != NULL)
9009 *lnump = ECMD_LAST;
9010 if (buf->b_fname == NULL)
9011 {
9012 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009013 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009014 }
9015 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009016 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009017 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009018 tilde_file = STRCMP(result, "~") == 0;
9019 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 break;
9022
9023#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009024 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009025 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 if (result == NULL)
9027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009028 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 return NULL;
9030 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009031 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 break;
9033#endif
9034
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009035 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009037 if (result != NULL && !autocmd_fname_full)
9038 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009039 // Still need to turn the fname into a full path. It is
9040 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009041 autocmd_fname_full = TRUE;
9042 result = FullName_save(autocmd_fname, FALSE);
9043 vim_free(autocmd_fname);
9044 autocmd_fname = result;
9045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 if (result == NULL)
9047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009048 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 return NULL;
9050 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009051 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 break;
9053
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009054 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 if (autocmd_bufnr <= 0)
9056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009057 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 return NULL;
9059 }
9060 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9061 result = strbuf;
9062 break;
9063
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009064 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 result = autocmd_match;
9066 if (result == NULL)
9067 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009068 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 return NULL;
9070 }
9071 break;
9072
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009073 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02009074 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02009075 result = estack_sfile(spec_idx == SPEC_SFILE
9076 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 if (result == NULL)
9078 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02009079 *errormsg = spec_idx == SPEC_SFILE
9080 ? _("E498: no :source file name to substitute for \"<sfile>\"")
9081 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 return NULL;
9083 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009084 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009086
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009087 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009088 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009090 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009091 return NULL;
9092 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009093 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009094 result = strbuf;
9095 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009096
9097#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009098 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009099 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009100 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009101 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009102 return NULL;
9103 }
9104 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009105 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009106 result = strbuf;
9107 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009108
Bram Moolenaar90944302020-08-01 20:45:11 +02009109 case SPEC_SID:
9110 if (current_sctx.sc_sid <= 0)
9111 {
9112 *errormsg = _(e_usingsid);
9113 return NULL;
9114 }
9115 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9116 result = strbuf;
9117 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009118#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009119
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009120#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009121 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009122 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9123 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 result = strbuf;
9125 break;
9126#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009127
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009128 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009129 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009130 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 }
9132
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009133 resultlen = (int)STRLEN(result); // length of new string
9134 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 {
9136 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 resultlen = (int)(s - result);
9139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 else if (!skip_mod)
9141 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009142 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 &resultlen);
9144 if (result == NULL)
9145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009146 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 return NULL;
9148 }
9149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 }
9151
9152 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9153 {
9154 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009155 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009156 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009158 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 result = NULL;
9160 }
9161 else
9162 result = vim_strnsave(result, resultlen);
9163 vim_free(resultbuf);
9164 return result;
9165}
9166
9167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 * Expand the <sfile> string in "arg".
9169 *
9170 * Returns an allocated string, or NULL for any error.
9171 */
9172 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009173expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009175 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 int len;
9177 char_u *result;
9178 char_u *newres;
9179 char_u *repl;
9180 int srclen;
9181 char_u *p;
9182
9183 result = vim_strsave(arg);
9184 if (result == NULL)
9185 return NULL;
9186
9187 for (p = result; *p; )
9188 {
9189 if (STRNCMP(p, "<sfile>", 7) != 0)
9190 ++p;
9191 else
9192 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009193 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009194 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 if (errormsg != NULL)
9196 {
9197 if (*errormsg)
9198 emsg(errormsg);
9199 vim_free(result);
9200 return NULL;
9201 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009202 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 {
9204 p += srclen;
9205 continue;
9206 }
9207 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9208 newres = alloc(len);
9209 if (newres == NULL)
9210 {
9211 vim_free(repl);
9212 vim_free(result);
9213 return NULL;
9214 }
9215 mch_memmove(newres, result, (size_t)(p - result));
9216 STRCPY(newres + (p - result), repl);
9217 len = (int)STRLEN(newres);
9218 STRCAT(newres, p + srclen);
9219 vim_free(repl);
9220 vim_free(result);
9221 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009222 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 }
9224 }
9225
9226 return result;
9227}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009230/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009231 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009232 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009235dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 if (fname == NULL)
9238 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009239 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240}
9241#endif
9242
9243/*
9244 * ":behave {mswin,xterm}"
9245 */
9246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009247ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248{
9249 if (STRCMP(eap->arg, "mswin") == 0)
9250 {
9251 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9252 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9253 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9254 set_option_value((char_u *)"keymodel", 0L,
9255 (char_u *)"startsel,stopsel", 0);
9256 }
9257 else if (STRCMP(eap->arg, "xterm") == 0)
9258 {
9259 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9260 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9261 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9262 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9263 }
9264 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009265 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266}
9267
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268static int filetype_detect = FALSE;
9269static int filetype_plugin = FALSE;
9270static int filetype_indent = FALSE;
9271
9272/*
9273 * ":filetype [plugin] [indent] {on,off,detect}"
9274 * on: Load the filetype.vim file to install autocommands for file types.
9275 * off: Load the ftoff.vim file to remove all autocommands for file types.
9276 * plugin on: load filetype.vim and ftplugin.vim
9277 * plugin off: load ftplugof.vim
9278 * indent on: load filetype.vim and indent.vim
9279 * indent off: load indoff.vim
9280 */
9281 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009282ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283{
9284 char_u *arg = eap->arg;
9285 int plugin = FALSE;
9286 int indent = FALSE;
9287
9288 if (*eap->arg == NUL)
9289 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009290 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009291 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 filetype_detect ? "ON" : "OFF",
9293 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9294 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9295 return;
9296 }
9297
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009298 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 for (;;)
9300 {
9301 if (STRNCMP(arg, "plugin", 6) == 0)
9302 {
9303 plugin = TRUE;
9304 arg = skipwhite(arg + 6);
9305 continue;
9306 }
9307 if (STRNCMP(arg, "indent", 6) == 0)
9308 {
9309 indent = TRUE;
9310 arg = skipwhite(arg + 6);
9311 continue;
9312 }
9313 break;
9314 }
9315 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9316 {
9317 if (*arg == 'o' || !filetype_detect)
9318 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009319 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 filetype_detect = TRUE;
9321 if (plugin)
9322 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009323 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 filetype_plugin = TRUE;
9325 }
9326 if (indent)
9327 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009328 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 filetype_indent = TRUE;
9330 }
9331 }
9332 if (*arg == 'd')
9333 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009334 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009335 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 }
9337 }
9338 else if (STRCMP(arg, "off") == 0)
9339 {
9340 if (plugin || indent)
9341 {
9342 if (plugin)
9343 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009344 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 filetype_plugin = FALSE;
9346 }
9347 if (indent)
9348 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009349 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 filetype_indent = FALSE;
9351 }
9352 }
9353 else
9354 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009355 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 filetype_detect = FALSE;
9357 }
9358 }
9359 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009360 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361}
9362
9363/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009364 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 */
9366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009367ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368{
9369 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009370 {
9371 char_u *arg = eap->arg;
9372
9373 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9374 arg += 9;
9375
9376 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9377 if (arg != eap->arg)
9378 did_filetype = FALSE;
9379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009383ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385#ifdef FEAT_DIGRAPHS
9386 if (*eap->arg != NUL)
9387 putdigraph(eap->arg);
9388 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009389 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390#else
mityu61065042021-07-19 20:07:21 +02009391 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392#endif
9393}
9394
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009395#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9396 void
9397set_no_hlsearch(int flag)
9398{
9399 no_hlsearch = flag;
9400# ifdef FEAT_EVAL
9401 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9402# endif
9403}
9404
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405/*
9406 * ":nohlsearch"
9407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009409ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009411 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009412 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414#endif
9415
9416#ifdef FEAT_CRYPT
9417/*
9418 * ":X": Get crypt key
9419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009421ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009423 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009424 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425}
9426#endif
9427
9428#ifdef FEAT_FOLDING
9429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009430ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431{
9432 if (foldManualAllowed(TRUE))
9433 foldCreate(eap->line1, eap->line2);
9434}
9435
9436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009437ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9440 eap->forceit, FALSE);
9441}
9442
9443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009444ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
9446 linenr_T lnum;
9447
Bram Moolenaar4facea32019-10-12 20:17:40 +02009448# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009449 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009450# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009451
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009452 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9454 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9455 ml_setmarked(lnum);
9456
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009457 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009459 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009460# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009461 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463}
9464#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009466#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009467/*
9468 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9469 * instead of a quickfix command.
9470 */
9471 int
9472is_loclist_cmd(int cmdidx)
9473{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009474 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009475 return FALSE;
9476 return cmdnames[cmdidx].cmd_name[0] == 'l';
9477}
9478#endif
9479
Bram Moolenaar4facea32019-10-12 20:17:40 +02009480#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009481 int
9482get_pressedreturn(void)
9483{
9484 return ex_pressedreturn;
9485}
9486
9487 void
9488set_pressedreturn(int val)
9489{
9490 ex_pressedreturn = val;
9491}
9492#endif