blob: 773a6bec9f7e02d172b2ffddeed92d522bd1ba63 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020071static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_cquit(exarg_T *eap);
91static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_close(exarg_T *eap);
93static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
94static void ex_only(exarg_T *eap);
95static void ex_resize(exarg_T *eap);
96static void ex_stag(exarg_T *eap);
97static void ex_tabclose(exarg_T *eap);
98static void ex_tabonly(exarg_T *eap);
99static void ex_tabnext(exarg_T *eap);
100static void ex_tabmove(exarg_T *eap);
101static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200102#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100103static void ex_pclose(exarg_T *eap);
104static void ex_ptag(exarg_T *eap);
105static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#else
107# define ex_pclose ex_ni
108# define ex_ptag ex_ni
109# define ex_pedit ex_ni
110#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100111static void ex_hide(exarg_T *eap);
112static void ex_stop(exarg_T *eap);
113static void ex_exit(exarg_T *eap);
114static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100116static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#else
118# define ex_goto ex_ni
119#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_shell(exarg_T *eap);
121static void ex_preserve(exarg_T *eap);
122static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_mode(exarg_T *eap);
124static void ex_wrongmodifier(exarg_T *eap);
125static void ex_find(exarg_T *eap);
126static void ex_open(exarg_T *eap);
127static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#ifndef FEAT_GUI
129# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100132#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#else
135# define ex_tearoff ex_ni
136#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100137#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
138 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_popup ex_ni
142#endif
143#ifndef FEAT_GUI_MSWIN
144# define ex_simalt ex_ni
145#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000146#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define gui_mch_find_dialog ex_ni
148# define gui_mch_replace_dialog ex_ni
149#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000150#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define ex_helpfind ex_ni
152#endif
153#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100154# define ex_cscope ex_ni
155# define ex_scscope ex_ni
156# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifndef FEAT_SYN_HL
159# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200160# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000161#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200162#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200163# define ex_syntime ex_ni
164#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000165#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000166# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000167# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000168# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000169# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000170# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000171#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200172#ifndef FEAT_PERSISTENT_UNDO
173# define ex_rundo ex_ni
174# define ex_wundo ex_ni
175#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176#ifndef FEAT_LUA
177# define ex_lua ex_script_ni
178# define ex_luado ex_ni
179# define ex_luafile ex_ni
180#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000181#ifndef FEAT_MZSCHEME
182# define ex_mzscheme ex_script_ni
183# define ex_mzfile ex_ni
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifndef FEAT_PERL
186# define ex_perl ex_script_ni
187# define ex_perldo ex_ni
188#endif
189#ifndef FEAT_PYTHON
190# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200191# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define ex_pyfile ex_ni
193#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200195# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200196# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define ex_py3file ex_ni
198#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100199#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
200# define ex_pyx ex_script_ni
201# define ex_pyxdo ex_ni
202# define ex_pyxfile ex_ni
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_TCL
205# define ex_tcl ex_script_ni
206# define ex_tcldo ex_ni
207# define ex_tclfile ex_ni
208#endif
209#ifndef FEAT_RUBY
210# define ex_ruby ex_script_ni
211# define ex_rubydo ex_ni
212# define ex_rubyfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_KEYMAP
215# define ex_loadkeymap ex_ni
216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_swapname(exarg_T *eap);
218static void ex_syncbind(exarg_T *eap);
219static void ex_read(exarg_T *eap);
220static void ex_pwd(exarg_T *eap);
221static void ex_equal(exarg_T *eap);
222static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_winpos ex_ni
229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_operators(exarg_T *eap);
231static void ex_put(exarg_T *eap);
232static void ex_copymove(exarg_T *eap);
233static void ex_submagic(exarg_T *eap);
234static void ex_join(exarg_T *eap);
235static void ex_at(exarg_T *eap);
236static void ex_bang(exarg_T *eap);
237static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_wundo(exarg_T *eap);
240static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redo(exarg_T *eap);
243static void ex_later(exarg_T *eap);
244static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100246static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_startinsert(exarg_T *eap);
250static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_checkpath(exarg_T *eap);
253static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#else
255# define ex_findpat ex_ni
256# define ex_checkpath ex_ni
257#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200258#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#else
261# define ex_psearch ex_ni
262#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_tag(exarg_T *eap);
264static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200266# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200267# define ex_break ex_ni
268# define ex_breakadd ex_ni
269# define ex_breakdel ex_ni
270# define ex_breaklist ex_ni
271# define ex_call ex_ni
272# define ex_catch ex_ni
273# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200274# define ex_continue ex_ni
275# define ex_debug ex_ni
276# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200278# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_echo ex_ni
281# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200283# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200284# define ex_endfunction ex_ni
285# define ex_endif ex_ni
286# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200288# define ex_eval ex_ni
289# define ex_execute ex_ni
Bram Moolenaar340c59e2021-04-24 20:37:03 +0200290# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200291# define ex_finally ex_ni
292# define ex_finish ex_ni
293# define ex_function ex_ni
294# define ex_if ex_ni
295# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200296# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200297# define ex_lockvar ex_ni
298# define ex_oldfiles ex_ni
299# define ex_options ex_ni
300# define ex_packadd ex_ni
301# define ex_packloadall ex_ni
302# define ex_return ex_ni
303# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# define ex_throw ex_ni
305# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000307# define ex_unlockvar ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200308# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309# define ex_import ex_ni
310# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200312#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313# define ex_loadview ex_ni
314#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200315#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316# define ex_viminfo ex_ni
317#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_filetype(exarg_T *eap);
320static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000322# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323# define ex_diffpatch ex_ni
324# define ex_diffgetput ex_ni
325# define ex_diffsplit ex_ni
326# define ex_diffthis ex_ni
327# define ex_diffupdate ex_ni
328#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100331static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#else
333# define ex_nohlsearch ex_ni
334# define ex_match ex_ni
335#endif
336#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#else
339# define ex_X ex_ni
340#endif
341#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_fold(exarg_T *eap);
343static void ex_foldopen(exarg_T *eap);
344static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#else
346# define ex_fold ex_ni
347# define ex_foldopen ex_ni
348# define ex_folddo ex_ni
349#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100350#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351# define ex_language ex_ni
352#endif
353#ifndef FEAT_SIGNS
354# define ex_sign ex_ni
355#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200359# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifndef FEAT_JUMPLIST
363# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200364# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365# define ex_changes ex_ni
366#endif
367
Bram Moolenaar05159a02005-02-26 23:04:13 +0000368#ifndef FEAT_PROFILE
369# define ex_profile ex_ni
370#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200371#ifndef FEAT_TERMINAL
372# define ex_terminal ex_ni
373#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200374#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
375# define ex_xrestore ex_ni
376#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100377#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200378# define ex_popupclear ex_ni
379#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381/*
382 * Declare cmdnames[].
383 */
384#define DO_DECLARE_EXCMD
385#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200386#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100387
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static char_u dollar_command[2] = {'$', 0};
389
390
391#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100392// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393typedef struct
394{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100395 char_u *line; // command line
396 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397} wcmd_T;
398
399/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000402 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000404struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100406 garray_T *lines_gap; // growarray with line info
407 int current_line; // last read line from growarray
408 int repeating; // TRUE when looping a second time
409 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200410 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 void *cookie;
412};
413
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200414static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100415static int store_loop_line(garray_T *gap, char_u *line);
416static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000417
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100418// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000419struct dbg_stuff
420{
421 int trylevel;
422 int force_abort;
423 except_T *caught_stack;
424 char_u *vv_exception;
425 char_u *vv_throwpoint;
426 int did_emsg;
427 int got_int;
428 int did_throw;
429 int need_rethrow;
430 int check_cstack;
431 except_T *current_exception;
432};
433
Bram Moolenaared203462004-06-16 11:19:22 +0000434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100435save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000436{
437 dsp->trylevel = trylevel; trylevel = 0;
438 dsp->force_abort = force_abort; force_abort = FALSE;
439 dsp->caught_stack = caught_stack; caught_stack = NULL;
440 dsp->vv_exception = v_exception(NULL);
441 dsp->vv_throwpoint = v_throwpoint(NULL);
442
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100443 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000444 dsp->did_emsg = did_emsg; did_emsg = FALSE;
445 dsp->got_int = got_int; got_int = FALSE;
446 dsp->did_throw = did_throw; did_throw = FALSE;
447 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
448 dsp->check_cstack = check_cstack; check_cstack = FALSE;
449 dsp->current_exception = current_exception; current_exception = NULL;
450}
451
452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100453restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000454{
455 suppress_errthrow = FALSE;
456 trylevel = dsp->trylevel;
457 force_abort = dsp->force_abort;
458 caught_stack = dsp->caught_stack;
459 (void)v_exception(dsp->vv_exception);
460 (void)v_throwpoint(dsp->vv_throwpoint);
461 did_emsg = dsp->did_emsg;
462 got_int = dsp->got_int;
463 did_throw = dsp->did_throw;
464 need_rethrow = dsp->need_rethrow;
465 check_cstack = dsp->check_cstack;
466 current_exception = dsp->current_exception;
467}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#endif
469
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470/*
471 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
472 * command is given.
473 */
474 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100475do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100476 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477{
478 int save_msg_scroll;
479 int prev_msg_row;
480 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100481 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482
483 if (improved)
484 exmode_active = EXMODE_VIM;
485 else
486 exmode_active = EXMODE_NORMAL;
487 State = NORMAL;
488
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100489 // When using ":global /pat/ visual" and then "Q" we return to continue
490 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000491 if (global_busy)
492 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
494 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100495 ++RedrawingDisabled; // don't redisplay the window
496 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100498 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 ++hold_gui_events;
500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
Bram Moolenaar32526b32019-01-19 17:43:09 +0100502 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 while (exmode_active)
504 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100505 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000506 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
507 {
508 exmode_active = FALSE;
509 break;
510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 msg_scroll = TRUE;
512 need_wait_return = FALSE;
513 ex_pressedreturn = FALSE;
514 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100515 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 prev_msg_row = msg_row;
517 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (improved)
519 {
520 cmdline_row = msg_row;
521 do_cmdline(NULL, getexline, NULL, 0);
522 }
523 else
524 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
525 lines_left = Rows - 1;
526
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100528 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100531 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 if (ex_pressedreturn)
535 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100536 // go up one line, to overwrite the ":<CR>" line, so the
537 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000538 msg_row = prev_msg_row;
539 if (prev_msg_row == Rows - 1)
540 msg_row--;
541 }
542 msg_col = 0;
543 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
544 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100547 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 {
549 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100550 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100552 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 }
555
556#ifdef FEAT_GUI
557 --hold_gui_events;
558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 --RedrawingDisabled;
560 --no_wait_return;
561 update_screen(CLEAR);
562 need_wait_return = FALSE;
563 msg_scroll = save_msg_scroll;
564}
565
566/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200567 * Print the executed command for when 'verbose' is set.
568 * When "lnum" is 0 only print the command.
569 */
570 static void
571msg_verbose_cmd(linenr_T lnum, char_u *cmd)
572{
573 ++no_wait_return;
574 verbose_enter_scroll();
575
576 if (lnum == 0)
577 smsg(_("Executing: %s"), cmd);
578 else
579 smsg(_("line %ld: %s"), (long)lnum, cmd);
580 if (msg_silent == 0)
581 msg_puts("\n"); // don't overwrite this
582
583 verbose_leave_scroll();
584 --no_wait_return;
585}
586
587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 * Execute a simple command line. Used for translated commands like "*".
589 */
590 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100591do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 return do_cmdline(cmd, NULL, NULL,
594 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
595}
596
597/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100598 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
599 * This allows for using a range without ":" in Vim9 script.
600 */
601 int
602do_cmd_argument(char_u *cmd)
603{
604 return do_cmdline(cmd, NULL, NULL,
605 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
606}
607
608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 * do_cmdline(): execute one Ex command line
610 *
611 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100612 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 * 2. Split up in parts separated with '|'.
614 *
615 * This function can be called recursively!
616 *
617 * flags:
618 * DOCMD_VERBOSE - The command will be included in the error message.
619 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100620 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 * DOCMD_KEYTYPED - Don't reset KeyTyped.
622 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
623 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
624 *
625 * return FAIL if cmdline could not be executed, OK otherwise
626 */
627 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100628do_cmdline(
629 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200630 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100631 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100632 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 char_u *next_cmdline; // next cmd to execute
635 char_u *cmdline_copy = NULL; // copy of cmd line
636 int used_getline = FALSE; // used "fgetline" to obtain command
637 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100639 int count = 0; // line number count
640 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 int retval = OK;
642#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100643 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100644 garray_T lines_ga; // keep lines for ":while"/":for"
645 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200646 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100647 char_u *fname = NULL; // function or script name
648 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
649 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
650 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200652 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200653 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200656 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000658 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000660 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100662# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663# define cmd_cookie cookie
664#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200666#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100667 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100669 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
670 // location for storing error messages to be converted to an exception.
671 // This ensures that the do_errthrow() call in do_one_cmd() does not
672 // combine the messages stored by an earlier invocation of do_one_cmd()
673 // with the command name of the later one. This would happen when
674 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 saved_msg_list = msg_list;
676 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677#endif
678
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100679 // It's possible to create an endless loop with ":execute", catch that
680 // here. The value of 200 allows nested function calls, ":source", etc.
681 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100682 if (call_depth >= 200
683#ifdef FEAT_EVAL
684 && call_depth >= p_mfd
685#endif
686 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100688 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100690 // When converting to an exception, we do not include the command name
691 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100692 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 msg_list = saved_msg_list;
694#endif
695 return FAIL;
696 }
697 ++call_depth;
698
699#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100700 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 cstack.cs_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
703
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100704 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100706 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100707 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000708 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 ++ex_nesting_level;
710
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100711 // Get the function or script name and the address where the next breakpoint
712 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000713 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
715 fname = func_name(real_cookie);
716 breakpoint = func_breakpoint(real_cookie);
717 dbg_tick = func_dbg_tick(real_cookie);
718 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100719 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100721 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 breakpoint = source_breakpoint(real_cookie);
723 dbg_tick = source_dbg_tick(real_cookie);
724 }
725
726 /*
727 * Initialize "force_abort" and "suppress_errthrow" at the top level.
728 */
729 if (!recursive)
730 {
731 force_abort = FALSE;
732 suppress_errthrow = FALSE;
733 }
734
735 /*
736 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000737 * exception handling (used when debugging). Otherwise clear it to avoid
738 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000740 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000741 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000742 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200743 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
745 initial_trylevel = trylevel;
746
747 /*
748 * "did_throw" will be set to TRUE when an exception is being thrown.
749 */
750 did_throw = FALSE;
751#endif
752 /*
753 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000754 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 * If force_abort is set, we cancel everything.
756 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100757#ifdef FEAT_EVAL
758 did_emsg_cumul += did_emsg;
759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 did_emsg = FALSE;
761
762 /*
763 * KeyTyped is only set when calling vgetc(). Reset it here when not
764 * calling vgetc() (sourced command lines).
765 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100766 if (!(flags & DOCMD_KEYTYPED)
767 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 KeyTyped = FALSE;
769
770 /*
771 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000772 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 * - for multiple commands on one line, separated with '|'
774 * - when repeating until there are no more lines (for ":source")
775 */
776 next_cmdline = cmdline;
777 do
778 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100780 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000781#endif
782
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100783 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (next_cmdline == NULL
785#ifdef FEAT_EVAL
786 && !force_abort
787 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000788 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789#endif
790 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100791 {
792#ifdef FEAT_EVAL
793 did_emsg_cumul += did_emsg;
794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
798 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000799 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100800 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 * 3. If a line is given: Make a copy, so we can mess with it.
802 */
803
804#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100805 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000806 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100808 // Each '|' separated command is stored separately in lines_ga, to
809 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100810 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100812 // Check if a function has returned or, unless it has an unclosed
813 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000817 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818 func_line_end(real_cookie);
819# endif
820 if (func_has_ended(real_cookie))
821 {
822 retval = FAIL;
823 break;
824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000827 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000829 script_line_end();
830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100833 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 retval = FAIL;
836 break;
837 }
838
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100839 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 if (breakpoint != NULL && dbg_tick != NULL
841 && *dbg_tick != debug_tick)
842 {
843 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100844 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100845 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 *dbg_tick = debug_tick;
847 }
848
849 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100850 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100852 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100854 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100856 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100857 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100859 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100860 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 *dbg_tick = debug_tick;
862 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000863# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000864 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000865 {
866 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100867 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000869 script_line_start();
870 }
871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#endif
874
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100875 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (next_cmdline == NULL)
877 {
878 /*
879 * Need to set msg_didout for the first line after an ":if",
880 * otherwise the ":if" will be overwritten.
881 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100882 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885#ifdef FEAT_EVAL
886 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
887#else
888 0
889#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100890 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
891 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100893 // Don't call wait_return for aborted command line. The NULL
894 // returned for the end of a sourced file or executed function
895 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (KeyTyped && !(flags & DOCMD_REPEAT))
897 need_wait_return = FALSE;
898 retval = FAIL;
899 break;
900 }
901 used_getline = TRUE;
902
903 /*
904 * Keep the first typed line. Clear it when more lines are typed.
905 */
906 if (flags & DOCMD_KEEPLINE)
907 {
908 vim_free(repeat_cmdline);
909 if (count == 0)
910 repeat_cmdline = vim_strsave(next_cmdline);
911 else
912 repeat_cmdline = NULL;
913 }
914 }
915
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100916 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 else if (cmdline_copy == NULL)
918 {
919 next_cmdline = vim_strsave(next_cmdline);
920 if (next_cmdline == NULL)
921 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200922 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 retval = FAIL;
924 break;
925 }
926 }
927 cmdline_copy = next_cmdline;
928
929#ifdef FEAT_EVAL
930 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200931 * Inside a while/for loop, and when the command looks like a ":while"
932 * or ":for", the line is stored, because we may need it later when
933 * looping.
934 *
935 * When there is a '|' and another command, it is stored separately,
936 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200938 *
939 * Pass a different "fgetline" function to do_one_cmd() below,
940 * that it stores lines in or reads them from "lines_ga". Makes it
941 * possible to define a function inside a while/for loop and handles
942 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200944 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200946 cmd_getline = get_loop_line;
947 cmd_cookie = (void *)&cmd_loop_cookie;
948 cmd_loop_cookie.lines_gap = &lines_ga;
949 cmd_loop_cookie.current_line = current_line;
950 cmd_loop_cookie.getline = fgetline;
951 cmd_loop_cookie.cookie = cookie;
952 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
953
954 // Save the current line when encountering it the first time.
955 if (current_line == lines_ga.ga_len
956 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
958 retval = FAIL;
959 break;
960 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200961 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200963 else
964 {
965 cmd_getline = fgetline;
966 cmd_cookie = cookie;
967 }
968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 did_endif = FALSE;
970#endif
971
972 if (count++ == 0)
973 {
974 /*
975 * All output from the commands is put below each other, without
976 * waiting for a return. Don't do this when executing commands
977 * from a script or when being called recursive (e.g. for ":e
978 * +command file").
979 */
980 if (!(flags & DOCMD_NOWAIT) && !recursive)
981 {
982 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100983 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100985 msg_scroll = TRUE; // put messages below each other
986 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 ++RedrawingDisabled;
988 did_inc = TRUE;
989 }
990 }
991
Bram Moolenaar823654b2020-05-29 23:03:09 +0200992 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100993 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995 /*
996 * 2. Execute one '|' separated command.
997 * do_one_cmd() will return NULL if there is no trailing '|'.
998 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
999 */
1000 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001001 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002#ifdef FEAT_EVAL
1003 &cstack,
1004#endif
1005 cmd_getline, cmd_cookie);
1006 --recursive;
1007
1008#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001009 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001010 // Use "current_line" from "cmd_loop_cookie", it may have been
1011 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001012 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013#endif
1014
1015 if (next_cmdline == NULL)
1016 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001017 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 /*
1020 * If the command was typed, remember it for the ':' register.
1021 * Do this AFTER executing the command to make :@: work.
1022 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 && new_last_cmdline != NULL)
1025 {
1026 vim_free(last_cmdline);
1027 last_cmdline = new_last_cmdline;
1028 new_last_cmdline = NULL;
1029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 }
1031 else
1032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001033 // need to copy the command after the '|' to cmdline_copy, for the
1034 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001035 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 next_cmdline = cmdline_copy;
1037 }
1038
1039
1040#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001041 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001043 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001045 {
1046 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001050 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
1052 ++current_line;
1053
1054 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001055 * An ":endwhile", ":endfor" and ":continue" is handled here.
1056 * If we were executing commands, jump back to the ":while" or
1057 * ":for".
1058 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001060 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001064 // Jump back to the matching ":while" or ":for". Be careful
1065 // not to use a cs_line[] from an entry that isn't a ":while"
1066 // or ":for": It would make "current_line" invalid and can
1067 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (!did_emsg && !got_int && !did_throw
1069 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 && (cstack.cs_flags[cstack.cs_idx]
1071 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 && cstack.cs_line[cstack.cs_idx] >= 0
1073 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1074 {
1075 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001076 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001078 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001080 // Check for the next breakpoint at or after the ":while"
1081 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (breakpoint != NULL)
1083 {
1084 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001085 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 fname,
1087 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1088 *dbg_tick = debug_tick;
1089 }
1090 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001091 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001093 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001095 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1096 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098 }
1099
1100 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001105 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001106 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 }
1108 }
1109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001110 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001111 if (breakpoint != NULL && has_watchexpr())
1112 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001113 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001114 *dbg_tick = debug_tick;
1115 }
1116
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 /*
1118 * When not inside any ":while" loop, clear remembered lines.
1119 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001120 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
1122 if (lines_ga.ga_len > 0)
1123 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001124 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1126 free_cmdlines(&lines_ga);
1127 }
1128 current_line = 0;
1129 }
1130
1131 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1133 * being restored at the ":endtry". Reset them here and set the
1134 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1135 * This includes the case where a missing ":endif", ":endwhile" or
1136 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 cstack.cs_lflags &= ~CSL_HAD_FINA;
1141 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1142 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 did_throw ? (void *)current_exception : NULL);
1144 did_emsg = got_int = did_throw = FALSE;
1145 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1146 }
1147
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001148 // Update global "trylevel" for recursive calls to do_cmdline() from
1149 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 trylevel = initial_trylevel + cstack.cs_trylevel;
1151
1152 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001153 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 * files) is aborted because of an error, an interrupt, or an uncaught
1155 * exception, cancel everything. If it is left normally, reset
1156 * force_abort to get the non-EH compatible abortion behavior for
1157 * the rest of the script.
1158 */
1159 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1160 force_abort = FALSE;
1161
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001162 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001164#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
1166 }
1167 /*
1168 * Continue executing command lines when:
1169 * - no CTRL-C typed, no aborting error, no exception thrown or try
1170 * conditionals need to be checked for executing finally clauses or
1171 * catching an interrupt exception
1172 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 * looping for ":source" command or function call.
1175 */
1176 while (!((got_int
1177#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001178 || (did_emsg && (force_abort || in_vim9script()))
1179 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#endif
1181 )
1182#ifdef FEAT_EVAL
1183 && cstack.cs_trylevel == 0
1184#endif
1185 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001186 && !(did_emsg
1187#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001188 // Keep going when inside try/catch, so that the error can be
1189 // deal with, except when it is a syntax error, it may cause
1190 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001191 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1192#endif
1193 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001194 && (getline_equal(fgetline, cookie, getexmodeline)
1195 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 && (next_cmdline != NULL
1197#ifdef FEAT_EVAL
1198 || cstack.cs_idx >= 0
1199#endif
1200 || (flags & DOCMD_REPEAT)));
1201
1202 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001203 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204#ifdef FEAT_EVAL
1205 free_cmdlines(&lines_ga);
1206 ga_clear(&lines_ga);
1207
1208 if (cstack.cs_idx >= 0)
1209 {
1210 /*
1211 * If a sourced file or executed function ran to its end, report the
1212 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001213 * In Vim9 script do not give a second error, executing aborts after
1214 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 */
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001216 if (!got_int && !did_throw && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001217 && ((getline_equal(fgetline, cookie, getsourceline)
1218 && !source_finished(fgetline, cookie))
1219 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 && !func_has_ended(real_cookie))))
1221 {
1222 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001225 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001227 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001229 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 }
1231
1232 /*
1233 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1234 * ":endtry" in a sourced file or executed function. If the try
1235 * conditional is in its finally clause, ignore anything pending.
1236 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001237 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 */
1239 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001240 {
1241 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1242
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001243 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001244 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001245 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1246 &cstack.cs_looplevel);
1247 }
1248 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 trylevel = initial_trylevel;
1250 }
1251
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001252 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1253 // lack was reported above and the error message is to be converted to an
1254 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001255 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 ? (char_u *)"endfunction" : (char_u *)NULL);
1257
1258 if (trylevel == 0)
1259 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001260 // Just in case did_throw got set but current_exception wasn't.
1261 if (current_exception == NULL)
1262 did_throw = FALSE;
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 /*
1265 * When an exception is being thrown out of the outermost try
1266 * conditional, discard the uncaught exception, disable the conversion
1267 * of interrupts or errors to exceptions, and ensure that no more
1268 * commands are executed.
1269 */
1270 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001271 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
1273 /*
1274 * On an interrupt or an aborting error not converted to an exception,
1275 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001276 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 * when force_abort is set and did_emsg unset in case of an interrupt
1278 * from a finally clause after an error.
1279 */
1280 else if (got_int || (did_emsg && force_abort))
1281 suppress_errthrow = TRUE;
1282 }
1283
1284 /*
1285 * The current cstack will be freed when do_cmdline() returns. An uncaught
1286 * exception will have to be rethrown in the previous cstack. If a function
1287 * has just returned or a script file was just finished and the previous
1288 * cstack belongs to the same function or, respectively, script file, it
1289 * will have to be checked for finally clauses to be executed due to the
1290 * ":return" or ":finish". This is done in do_one_cmd().
1291 */
1292 if (did_throw)
1293 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001294 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001296 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 && ex_nesting_level > func_level(real_cookie) + 1))
1298 {
1299 if (!did_throw)
1300 check_cstack = TRUE;
1301 }
1302 else
1303 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001304 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001305 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 --ex_nesting_level;
1307 /*
1308 * Go to debug mode when returning from a function in which we are
1309 * single-stepping.
1310 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001311 if ((getline_equal(fgetline, cookie, getsourceline)
1312 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001314 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 ? (char_u *)_("End of sourced file")
1316 : (char_u *)_("End of function"));
1317 }
1318
1319 /*
1320 * Restore the exception environment (done after returning from the
1321 * debugger).
1322 */
1323 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001324 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
1326 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001327
1328 // Cleanup if "cs_emsg_silent_list" remains.
1329 if (cstack.cs_emsg_silent_list != NULL)
1330 {
1331 eslist_T *elem, *temp;
1332
1333 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1334 {
1335 temp = elem->next;
1336 vim_free(elem);
1337 }
1338 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001339#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
1341 /*
1342 * If there was too much output to fit on the command line, ask the user to
1343 * hit return before redrawing the screen. With the ":global" command we do
1344 * this only once after the command is finished.
1345 */
1346 if (did_inc)
1347 {
1348 --RedrawingDisabled;
1349 --no_wait_return;
1350 msg_scroll = FALSE;
1351
1352 /*
1353 * When just finished an ":if"-":else" which was typed, no need to
1354 * wait for hit-return. Also for an error situation.
1355 */
1356 if (retval == FAIL
1357#ifdef FEAT_EVAL
1358 || (did_endif && KeyTyped && !did_emsg)
1359#endif
1360 )
1361 {
1362 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001363 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
1365 else if (need_wait_return)
1366 {
1367 /*
1368 * The msg_start() above clears msg_didout. The wait_return we do
1369 * here should not overwrite the command that may be shown before
1370 * doing that.
1371 */
1372 msg_didout |= msg_didout_before_start;
1373 wait_return(FALSE);
1374 }
1375 }
1376
Bram Moolenaar2a942252012-11-28 23:03:07 +01001377#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001378 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001379#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 /*
1381 * Reset if_level, in case a sourced script file contains more ":if" than
1382 * ":endif" (could be ":if x | foo | endif").
1383 */
1384 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001385#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001386
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 --call_depth;
1388 return retval;
1389}
1390
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001391#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001392/*
1393 * Handle when "did_throw" is set after executing commands.
1394 */
1395 void
1396handle_did_throw()
1397{
1398 char *p = NULL;
1399 msglist_T *messages = NULL;
1400
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 Moolenaarf5be8cd2020-07-17 20:36:00 +02002318 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 * Don't do this for ":read !cmd" and ":write !cmd".
2320 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002321 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 separate_nextcmd(&ea);
2323
2324 /*
2325 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002326 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2327 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002329 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002330 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002331 || ea.cmdidx == CMD_global
2332 || ea.cmdidx == CMD_vglobal
2333 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
2335 for (p = ea.arg; *p; ++p)
2336 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002337 // Remove one backslash before a newline, so that it's possible to
2338 // pass a newline to the shell and also a newline that is preceded
2339 // with a backslash. This makes it impossible to end a shell
2340 // command in a backslash, but that doesn't appear useful.
2341 // Halving the number of backslashes is incompatible with previous
2342 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002344 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 else if (*p == '\n')
2346 {
2347 ea.nextcmd = p + 1;
2348 *p = NUL;
2349 break;
2350 }
2351 }
2352 }
2353
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002354 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002355 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002357 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002358 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002360 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002361 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002362 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002364#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002365 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002366 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002368 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002369 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
2371#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002372 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2373 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002374 {
2375 ea.regname = *ea.arg++;
2376#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002377 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002378 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2379 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002380 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002381 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002382 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002383 }
2384#endif
2385 ea.arg = skipwhite(ea.arg);
2386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
2388
2389 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002390 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 * count, it's a buffer name.
2392 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002393 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002394 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002395 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
2397 n = getdigits(&ea.arg);
2398 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002399 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002401 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 goto doend;
2403 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002404 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
2406 ea.line2 = n;
2407 if (ea.addr_count == 0)
2408 ea.addr_count = 1;
2409 }
2410 else
2411 {
2412 ea.line1 = ea.line2;
2413 ea.line2 += n - 1;
2414 ++ea.addr_count;
2415 /*
2416 * Be vi compatible: no error message for out of range.
2417 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002418 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 ea.line2 = curbuf->b_ml.ml_line_count;
2420 }
2421 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002422
2423 /*
2424 * Check for flags: 'l', 'p' and '#'.
2425 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002426 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002427 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002428 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2429 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002431 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002432 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 goto doend;
2434 }
2435
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002436 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002438 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 goto doend;
2440 }
2441
2442#ifdef FEAT_EVAL
2443 /*
2444 * Skip the command when it's not going to be executed.
2445 * The commands like :if, :endif, etc. always need to be executed.
2446 * Also make an exception for commands that handle a trailing command
2447 * themselves.
2448 */
2449 if (ea.skip)
2450 {
2451 switch (ea.cmdidx)
2452 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002453 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 case CMD_while:
2455 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002456 case CMD_for:
2457 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_if:
2459 case CMD_elseif:
2460 case CMD_else:
2461 case CMD_endif:
2462 case CMD_try:
2463 case CMD_catch:
2464 case CMD_finally:
2465 case CMD_endtry:
2466 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002467 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 break;
2469
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002470 // Commands that handle '|' themselves. Check: A command should
2471 // either have the EX_TRLBAR flag, appear in this list or appear in
2472 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 case CMD_aboveleft:
2474 case CMD_and:
2475 case CMD_belowright:
2476 case CMD_botright:
2477 case CMD_browse:
2478 case CMD_call:
2479 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002480 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 case CMD_delfunction:
2482 case CMD_djump:
2483 case CMD_dlist:
2484 case CMD_dsearch:
2485 case CMD_dsplit:
2486 case CMD_echo:
2487 case CMD_echoerr:
2488 case CMD_echomsg:
2489 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002490 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002492 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002493 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 case CMD_help:
2495 case CMD_hide:
2496 case CMD_ijump:
2497 case CMD_ilist:
2498 case CMD_isearch:
2499 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002500 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 case CMD_keepjumps:
2502 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002503 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 case CMD_leftabove:
2505 case CMD_let:
2506 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002507 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002508 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002510 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002511 case CMD_noautocmd:
2512 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 case CMD_perl:
2514 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002515 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002516 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002517 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 case CMD_return:
2519 case CMD_rightbelow:
2520 case CMD_ruby:
2521 case CMD_silent:
2522 case CMD_smagic:
2523 case CMD_snomagic:
2524 case CMD_substitute:
2525 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002526 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 case CMD_tcl:
2528 case CMD_throw:
2529 case CMD_tilde:
2530 case CMD_topleft:
2531 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002532 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002533 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 case CMD_verbose:
2535 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002536 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 break;
2538
2539 default: goto doend;
2540 }
2541 }
2542#endif
2543
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002544 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
2546 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2547 goto doend;
2548 }
2549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 /*
2551 * Accept buffer name. Cannot be used at the same time with a buffer
2552 * number. Don't do this for a user command.
2553 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002554 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002555 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
2557 /*
2558 * :bdelete, :bwipeout and :bunload take several arguments, separated
2559 * by spaces: find next space (skipping over escaped characters).
2560 * The others take one argument: ignore trailing spaces.
2561 */
2562 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2563 || ea.cmdidx == CMD_bunload)
2564 p = skiptowhite_esc(ea.arg);
2565 else
2566 {
2567 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002568 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 --p;
2570 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002571 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002572 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002573 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 goto doend;
2575 ea.addr_count = 1;
2576 ea.arg = skipwhite(p);
2577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002579 // The :try command saves the emsg_silent flag, reset it here when
2580 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002581 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002582 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002583 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002584 if (emsg_silent < 0)
2585 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002586 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002587 }
2588
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002590 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
Bram Moolenaar958636c2014-10-21 20:01:58 +02002593 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
2595 /*
2596 * Execute a user-defined command.
2597 */
2598 do_ucmd(&ea);
2599 }
2600 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
2602 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002603 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 */
2605 ea.errmsg = NULL;
2606 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2607 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002608 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
2610
2611#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002613 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002614 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002615 && current_sctx.sc_sid > 0
2616 && ea.cmdidx != CMD_endif
2617 && (cstack->cs_idx < 0
2618 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002619 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 /*
2622 * If the command just executed called do_cmdline(), any throw or ":return"
2623 * or ":finish" encountered there must also check the cstack of the still
2624 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2625 * exception, or reanimate a returned function or finished script file and
2626 * return or finish it again.
2627 */
2628 if (need_rethrow)
2629 do_throw(cstack);
2630 else if (check_cstack)
2631 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002632 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002634 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 && current_func_returned())
2636 do_return(&ea, TRUE, FALSE, NULL);
2637 }
2638 need_rethrow = check_cstack = FALSE;
2639#endif
2640
2641doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002642 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002645 curwin->w_cursor.col = 0;
2646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
2648 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2649 {
2650 if (sourcing)
2651 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002652 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
2654 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002655 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002657 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659 emsg(errormsg);
2660 }
2661#ifdef FEAT_EVAL
2662 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002663 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2664 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002665
2666 if (did_set_expr_line)
2667 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668#endif
2669
Bram Moolenaare1004402020-10-24 20:49:43 +02002670 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002672 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002674 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 ea.nextcmd = NULL;
2676
2677#ifdef FEAT_EVAL
2678 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002679 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680#endif
2681
2682 return ea.nextcmd;
2683}
2684#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002685 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686#endif
2687
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002688static char ex_error_buf[MSG_BUF_LEN];
2689
2690/*
2691 * Return an error message with argument included.
2692 * Uses a static buffer, only the last error will be kept.
2693 * "msg" will be translated, caller should use N_().
2694 */
2695 char *
2696ex_errmsg(char *msg, char_u *arg)
2697{
2698 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2699 return ex_error_buf;
2700}
2701
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002703 * Check for an Ex command with optional tail.
2704 * If there is a match advance "pp" to the argument and return TRUE.
2705 * If "noparen" is TRUE do not recognize the command followed by "(".
2706 */
2707 static int
2708checkforcmd_opt(
2709 char_u **pp, // start of command
2710 char *cmd, // name of command
2711 int len, // required length
2712 int noparen)
2713{
2714 int i;
2715
2716 for (i = 0; cmd[i] != NUL; ++i)
2717 if (((char_u *)cmd)[i] != (*pp)[i])
2718 break;
2719 if (i >= len && !isalpha((*pp)[i])
2720 && (*pp)[i] != '_' && (!noparen || (*pp)[i] != '('))
2721 {
2722 *pp = skipwhite(*pp + i);
2723 return TRUE;
2724 }
2725 return FALSE;
2726}
2727
2728/*
2729 * Check for an Ex command with optional tail.
2730 * If there is a match advance "pp" to the argument and return TRUE.
2731 */
2732 int
2733checkforcmd(
2734 char_u **pp, // start of command
2735 char *cmd, // name of command
2736 int len) // required length
2737{
2738 return checkforcmd_opt(pp, cmd, len, FALSE);
2739}
2740
2741/*
2742 * Check for an Ex command with optional tail, not followed by "(".
2743 * If there is a match advance "pp" to the argument and return TRUE.
2744 */
2745 static int
2746checkforcmd_noparen(
2747 char_u **pp, // start of command
2748 char *cmd, // name of command
2749 int len) // required length
2750{
2751 return checkforcmd_opt(pp, cmd, len, TRUE);
2752}
2753
2754/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002755 * Parse and skip over command modifiers:
2756 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002757 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002758 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002759 * When "skip_only" is TRUE the global variables are not changed, except for
2760 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002761 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2762 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002763 * Call apply_cmdmod() to get the side effects of the modifiers:
2764 * - Increment "sandbox" for ":sandbox"
2765 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002766 * - set msg_silent for ":silent"
2767 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002768 * Return FAIL when the command is not to be executed.
2769 * May set "errormsg" to an error message.
2770 */
2771 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002772parse_command_modifiers(
2773 exarg_T *eap,
2774 char **errormsg,
2775 cmdmod_T *cmod,
2776 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002777{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002778 char_u *p;
2779 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002780
Bram Moolenaare1004402020-10-24 20:49:43 +02002781 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002782
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002783 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002784 for (;;)
2785 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002786 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002787 {
2788 if (*eap->cmd == ':')
2789 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002790 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002791 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002792
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002793 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002794 if (*eap->cmd == NUL && exmode_active
2795 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2796 || getline_equal(eap->getline, eap->cookie, getexline))
2797 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2798 {
2799 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002800 if (!skip_only)
2801 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002802 }
2803
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002804 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002805 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002806 return FAIL;
2807 if (*eap->cmd == NUL)
2808 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002809 if (!skip_only)
2810 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 return FAIL;
2812 }
2813
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002814 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002815
2816 // In Vim9 script a variable can shadow a command modifier:
2817 // verbose = 123
2818 // verbose += 123
2819 // silent! verbose = func()
2820 // verbose.member = 2
2821 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002822 // But not:
2823 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002824 if (in_vim9script())
2825 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002826 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002827
2828 for (s = p; ASCII_ISALPHA(*s); ++s)
2829 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002830 n = skipwhite(s);
2831 if (vim_strchr((char_u *)".=", *n) != NULL
2832 || *s == '['
2833 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002834 break;
2835 }
2836
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 switch (*p)
2838 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002839 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002840 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002842 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002843 continue;
2844
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002845 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002847 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002848 continue;
2849 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002850 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002851 {
2852#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002853 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002854#endif
2855 continue;
2856 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002857 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002858 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002859 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 continue;
2861
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002862 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002863 break;
2864#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002865 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002866#endif
2867 continue;
2868
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002869 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002871 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002872 continue;
2873 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002874 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002876 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 continue;
2878 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002879 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002881 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 continue;
2883 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002884 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002886 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002887 continue;
2888
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002889 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002890 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002891 char_u *reg_pat;
2892 char_u *nulp = NULL;
2893 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002894
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002895 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaareffed932018-08-14 13:38:17 +02002896 || *p == NUL || ends_excmd(*p))
2897 break;
2898 if (*p == '!')
2899 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002900 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002901 p = skipwhite(p + 1);
2902 if (*p == NUL || ends_excmd(*p))
2903 break;
2904 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002905#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002906 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002907 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002908 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002909#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002910 if (skip_only)
2911 p = skip_vimgrep_pat(p, NULL, NULL);
2912 else
2913 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002914 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2915 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002916 if (p == NULL || *p == NUL)
2917 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002918 if (!skip_only)
2919 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002920 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002922 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002923 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002924 // restore the character overwritten by NUL
2925 if (nulp != NULL)
2926 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002927 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002928 eap->cmd = p;
2929 continue;
2930 }
2931
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002932 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002933 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002934 || *p == NUL || ends_excmd(*p))
2935 break;
2936 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002937 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002938 continue;
2939
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002940 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002941 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002942 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002943 continue;
2944 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002945 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2946 {
2947 if (ends_excmd2(p, eap->cmd))
2948 {
2949 *errormsg =
2950 _(e_vim9cmd_must_be_followed_by_command);
2951 return FAIL;
2952 }
2953 cmod->cmod_flags |= CMOD_LEGACY;
2954 continue;
2955 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002956
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002957 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002959 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002960 continue;
2961
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002962 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002964 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002965 continue;
2966 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002967 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002968 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002969 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002970 continue;
2971
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002972 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02002973 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002974 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002975 continue;
2976
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002977 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002978 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002979 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002980 continue;
2981 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002982 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002983 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002984 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002985 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2986 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002987 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002988 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002989 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002990 }
2991 continue;
2992
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002993 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002995 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002996 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002997 long tabnr = get_address(eap, &eap->cmd,
2998 ADDR_TABS, eap->skip,
2999 skip_only, FALSE, 1);
3000 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003001 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003002 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003003 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003004 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3005 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003006 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003007 return FAIL;
3008 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003009 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003010 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003011 }
3012 eap->cmd = p;
3013 continue;
3014 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003015 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003016 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003017 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003018 continue;
3019
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003020 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003022 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003023 continue;
3024
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003025 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003026 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003027 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003028 continue;
3029 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003030 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003031 {
3032 if (ends_excmd2(p, eap->cmd))
3033 {
3034 *errormsg =
3035 _(e_vim9cmd_must_be_followed_by_command);
3036 return FAIL;
3037 }
3038 cmod->cmod_flags |= CMOD_VIM9CMD;
3039 continue;
3040 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003041 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003042 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003043 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003044 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003045 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003046 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003047 eap->cmd = p;
3048 continue;
3049 }
3050 break;
3051 }
3052
3053 return OK;
3054}
3055
3056/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003057 * Return TRUE if "cmod" has anything set.
3058 */
3059 int
3060has_cmdmod(cmdmod_T *cmod)
3061{
3062 return cmod->cmod_flags != 0
3063 || cmod->cmod_split != 0
3064 || cmod->cmod_verbose != 0
3065 || cmod->cmod_tab != 0
3066 || cmod->cmod_filter_regmatch.regprog != NULL;
3067}
3068
3069/*
3070 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3071 */
3072 int
3073cmdmod_error(void)
3074{
3075 if (in_vim9script() && has_cmdmod(&cmdmod))
3076 {
3077 emsg(_(e_misplaced_command_modifier));
3078 return TRUE;
3079 }
3080 return FALSE;
3081}
3082
3083/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003084 * Apply the command modifiers. Saves current state in "cmdmod", call
3085 * undo_cmdmod() later.
3086 */
3087 void
3088apply_cmdmod(cmdmod_T *cmod)
3089{
3090#ifdef HAVE_SANDBOX
3091 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3092 {
3093 ++sandbox;
3094 cmod->cmod_did_sandbox = TRUE;
3095 }
3096#endif
3097 if (cmod->cmod_verbose > 0)
3098 {
3099 if (cmod->cmod_verbose_save == 0)
3100 cmod->cmod_verbose_save = p_verbose + 1;
3101 p_verbose = cmod->cmod_verbose;
3102 }
3103
3104 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3105 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003106 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003107 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003108 cmod->cmod_save_msg_scroll = msg_scroll;
3109 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003110 if (cmod->cmod_flags & CMOD_SILENT)
3111 ++msg_silent;
3112 if (cmod->cmod_flags & CMOD_UNSILENT)
3113 msg_silent = 0;
3114
3115 if (cmod->cmod_flags & CMOD_ERRSILENT)
3116 {
3117 ++emsg_silent;
3118 ++cmod->cmod_did_esilent;
3119 }
3120
Bram Moolenaare1004402020-10-24 20:49:43 +02003121 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003122 {
3123 // Set 'eventignore' to "all".
3124 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003125 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003126 set_string_option_direct((char_u *)"ei", -1,
3127 (char_u *)"all", OPT_FREE, SID_NONE);
3128 }
3129}
3130
3131/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003132 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003133 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003134 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003135undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003136{
Bram Moolenaare1004402020-10-24 20:49:43 +02003137 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003138 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003139 p_verbose = cmod->cmod_verbose_save - 1;
3140 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003141 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003142
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003143#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003144 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003145 {
3146 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003147 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003148 }
3149#endif
3150
Bram Moolenaare1004402020-10-24 20:49:43 +02003151 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003152 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003153 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003154 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3155 OPT_FREE, SID_NONE);
3156 free_string_option(cmod->cmod_save_ei);
3157 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003158 }
3159
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003160 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003161
Bram Moolenaare1004402020-10-24 20:49:43 +02003162 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003163 {
3164 // messages could be enabled for a serious error, need to check if the
3165 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003166 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3167 msg_silent = cmod->cmod_save_msg_silent - 1;
3168 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003169 if (emsg_silent < 0)
3170 emsg_silent = 0;
3171 // Restore msg_scroll, it's set by file I/O commands, even when no
3172 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003173 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003174
3175 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3176 // somewhere in the line. Put it back in the first column.
3177 if (redirecting())
3178 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003179
Bram Moolenaare1004402020-10-24 20:49:43 +02003180 cmod->cmod_save_msg_silent = 0;
3181 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003182 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003183}
3184
3185/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003186 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003187 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003188 * Return FAIL and set "errormsg" or return OK.
3189 */
3190 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003191parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003192{
3193 int address_count = 1;
3194 linenr_T lnum;
3195
3196 // Repeat for all ',' or ';' separated addresses.
3197 for (;;)
3198 {
3199 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003200 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003201 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003202 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003203 eap->addr_count == 0, address_count++);
3204 if (eap->cmd == NULL) // error detected
3205 return FAIL;
3206 if (lnum == MAXLNUM)
3207 {
3208 if (*eap->cmd == '%') // '%' - all lines
3209 {
3210 ++eap->cmd;
3211 switch (eap->addr_type)
3212 {
3213 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003214 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003215 eap->line1 = 1;
3216 eap->line2 = curbuf->b_ml.ml_line_count;
3217 break;
3218 case ADDR_LOADED_BUFFERS:
3219 {
3220 buf_T *buf = firstbuf;
3221
3222 while (buf->b_next != NULL
3223 && buf->b_ml.ml_mfp == NULL)
3224 buf = buf->b_next;
3225 eap->line1 = buf->b_fnum;
3226 buf = lastbuf;
3227 while (buf->b_prev != NULL
3228 && buf->b_ml.ml_mfp == NULL)
3229 buf = buf->b_prev;
3230 eap->line2 = buf->b_fnum;
3231 break;
3232 }
3233 case ADDR_BUFFERS:
3234 eap->line1 = firstbuf->b_fnum;
3235 eap->line2 = lastbuf->b_fnum;
3236 break;
3237 case ADDR_WINDOWS:
3238 case ADDR_TABS:
3239 if (IS_USER_CMDIDX(eap->cmdidx))
3240 {
3241 eap->line1 = 1;
3242 eap->line2 = eap->addr_type == ADDR_WINDOWS
3243 ? LAST_WIN_NR : LAST_TAB_NR;
3244 }
3245 else
3246 {
3247 // there is no Vim command which uses '%' and
3248 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003249 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003250 return FAIL;
3251 }
3252 break;
3253 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003254 case ADDR_UNSIGNED:
3255 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003256 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003257 return FAIL;
3258 case ADDR_ARGUMENTS:
3259 if (ARGCOUNT == 0)
3260 eap->line1 = eap->line2 = 0;
3261 else
3262 {
3263 eap->line1 = 1;
3264 eap->line2 = ARGCOUNT;
3265 }
3266 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003267 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003268#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003269 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003270 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003271 if (eap->line2 == 0)
3272 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003273#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003274 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003275 case ADDR_NONE:
3276 // Will give an error later if a range is found.
3277 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003278 }
3279 ++eap->addr_count;
3280 }
3281 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3282 {
3283 pos_T *fp;
3284
3285 // '*' - visual area
3286 if (eap->addr_type != ADDR_LINES)
3287 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003288 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003289 return FAIL;
3290 }
3291
3292 ++eap->cmd;
3293 if (!eap->skip)
3294 {
3295 fp = getmark('<', FALSE);
3296 if (check_mark(fp) == FAIL)
3297 return FAIL;
3298 eap->line1 = fp->lnum;
3299 fp = getmark('>', FALSE);
3300 if (check_mark(fp) == FAIL)
3301 return FAIL;
3302 eap->line2 = fp->lnum;
3303 ++eap->addr_count;
3304 }
3305 }
3306 }
3307 else
3308 eap->line2 = lnum;
3309 eap->addr_count++;
3310
3311 if (*eap->cmd == ';')
3312 {
3313 if (!eap->skip)
3314 {
3315 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003316 // Don't leave the cursor on an illegal line or column, but do
3317 // accept zero as address, so 0;/PATTERN/ works correctly.
3318 if (eap->line2 > 0)
3319 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003320 }
3321 }
3322 else if (*eap->cmd != ',')
3323 break;
3324 ++eap->cmd;
3325 }
3326
3327 // One address given: set start and end lines.
3328 if (eap->addr_count == 1)
3329 {
3330 eap->line1 = eap->line2;
3331 // ... but only implicit: really no address given
3332 if (lnum == MAXLNUM)
3333 eap->addr_count = 0;
3334 }
3335 return OK;
3336}
3337
3338/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003339 * Append "cmd" to the error message in IObuff.
3340 * Takes care of limiting the length and handling 0xa0, which would be
3341 * invisible otherwise.
3342 */
3343 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003344append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003345{
3346 char_u *s = cmd;
3347 char_u *d;
3348
3349 STRCAT(IObuff, ": ");
3350 d = IObuff + STRLEN(IObuff);
3351 while (*s != NUL && d - IObuff < IOSIZE - 7)
3352 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003353 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003354 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003355 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003356 STRCPY(d, "<a0>");
3357 d += 4;
3358 }
3359 else
3360 MB_COPY_CHAR(s, d);
3361 }
3362 *d = NUL;
3363}
3364
3365/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003366 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3367 * the name. Otherwise just return "start".
3368 */
3369 char_u *
3370skip_option_env_lead(char_u *start)
3371{
3372 char_u *name = start;
3373
3374 if (*start == '&')
3375 {
3376 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3377 name += 3;
3378 else
3379 name += 1;
3380 }
3381 else if (*start == '$')
3382 name += 1;
3383 return name;
3384}
3385
3386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003388 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003389 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003390 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 *
3392 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3393 * assignment without "let". Sets eap->cmdidx to the command while returning
3394 * "eap->cmd".
3395 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 * Returns NULL for an ambiguous user command.
3397 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 char_u *
3399find_ex_command(
3400 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003401 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003402 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003403 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404{
3405 int len;
3406 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003407 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003408#ifndef FEAT_EVAL
3409 int vim9 = FALSE;
3410#else
3411 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 /*
3414 * Recognize a Vim9 script function/method call and assignment:
3415 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3416 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003417 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003418 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003420 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421
Bram Moolenaar83144542020-08-02 20:40:43 +02003422 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003423 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003425 int oplen;
3426 int heredoc;
3427 char_u *swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003429 if (
3430 // "(..." is an expression.
3431 // "funcname(" is always a function call.
3432 *p == '('
3433 || (p == eap->cmd
3434 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003435 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003436 *eap->cmd == '{'
3437 // "'string'->func()" is an expression.
3438 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003439 // '"string"->func()' is an expression.
3440 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003441 // "g:varname" is an expression.
3442 || eap->cmd[1] == ':'
3443 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003444 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003445 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003446 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003447 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3448 {
3449 // "{" by itself is the start of a block.
3450 eap->cmdidx = CMD_block;
3451 return eap->cmd + 1;
3452 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003453 eap->cmdidx = CMD_eval;
3454 return eap->cmd;
3455 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003456
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003457 if (p != eap->cmd && (
3458 // "varname[]" is an expression.
3459 *p == '['
3460 // "varname.key" is an expression.
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02003461 || (*p == '.' && (ASCII_ISALPHA(p[1])
3462 || p[1] == '_'))))
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003463 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003464 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003465
3466 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003467 // Skip over the whole thing, it can be:
3468 // name.member = val
3469 // name[a : b] = val
3470 // name[idx] = val
3471 // name[idx].member = val
3472 // etc.
3473 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003474 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003475 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003476 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003477 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003478 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003479 || (after[0] == '.' && after[1] == '.'
3480 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003481 eap->cmdidx = CMD_var;
3482 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003483 --emsg_silent;
3484 return eap->cmd;
3485 }
3486
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003487 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3488 // an assignment.
3489 // If there is no line break inside the "[...]" then "p" is
3490 // advanced to after the "]" by to_name_const_end(): check if a "="
3491 // follows.
3492 // If "[...]" has a line break "p" still points at the "[" and it
3493 // can't be an assignment.
3494 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003495 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003496 char_u *eq;
3497
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003498 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003499 if (p == eap->cmd && *p == '[')
3500 {
3501 int count = 0;
3502 int semicolon = FALSE;
3503
3504 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3505 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003506 eq = p;
3507 if (eq != NULL)
3508 {
3509 eq = skipwhite(eq);
3510 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3511 ++eq;
3512 }
3513 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003514 {
3515 eap->cmdidx = CMD_eval;
3516 return eap->cmd;
3517 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003518 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003519 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003520 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003521 return eap->cmd;
3522 }
3523 }
3524
3525 // Recognize an assignment if we recognize the variable name:
3526 // "g:var = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003527 // "var = expr" where "var" is a variable name or we are skipping
3528 // (variable declaration might have been skipped).
Bram Moolenaar83144542020-08-02 20:40:43 +02003529 if (*eap->cmd == '@')
3530 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003531 oplen = assignment_len(skipwhite(p), &heredoc);
3532 if (oplen > 0)
3533 {
3534 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3535 || *eap->cmd == '&'
3536 || *eap->cmd == '$'
3537 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003538 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003539 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003540 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003541 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003542 return eap->cmd;
3543 }
3544 }
3545
3546 // Recognize using a type for a w:, b:, t: or g: variable:
3547 // "w:varname: number = 123".
3548 if (eap->cmd[1] == ':' && *p == ':')
3549 {
3550 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003551 return eap->cmd;
3552 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003553 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003554
3555 // If it is an ID it might be a variable with an operator on the next
3556 // line, if the variable exists it can't be an Ex command.
3557 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003558 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003559 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3560 {
3561 eap->cmdidx = CMD_eval;
3562 return eap->cmd;
3563 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003564
3565 // Check for "++nr" and "--nr".
3566 if (p == eap->cmd && p[0] == p[1] && (*p == '+' || *p == '-'))
3567 {
3568 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3569 return eap->cmd + 2;
3570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 }
3572#endif
3573
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 /*
3575 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003576 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003577 * - The 'k' command can directly be followed by any character.
3578 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003580 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003582 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 */
3584 p = eap->cmd;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003585 if (!vim9 && *p == 'k')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 eap->cmdidx = CMD_k;
3588 ++p;
3589 }
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003590 else if (!vim9
3591 && p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003592 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3593 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 || p[1] == 'g'
3595 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3596 || p[1] == 'I'
3597 || (p[1] == 'r' && p[2] != 'e')))
3598 {
3599 eap->cmdidx = CMD_substitute;
3600 ++p;
3601 }
3602 else
3603 {
3604 while (ASCII_ISALPHA(*p))
3605 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003606 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003607 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003609 while (ASCII_ISALNUM(*p))
3610 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 }
3612 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3613 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003614 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 ++p;
3616 while (ASCII_ISALPHA(*p))
3617 ++p;
3618 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003619
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003620 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003621 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 ++p;
3623 len = (int)(p - eap->cmd);
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003624 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003625 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003626 // Check for ":dl", ":dell", etc. to ":deletel": that's
3627 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003628 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003629 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003630 break;
3631 if (i == len - 1)
3632 {
3633 --len;
3634 if (p[-1] == 'l')
3635 eap->flags |= EXFLAG_LIST;
3636 else
3637 eap->flags |= EXFLAG_PRINT;
3638 }
3639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003641 if (ASCII_ISLOWER(eap->cmd[0]))
3642 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003643 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003644 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003645
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003646 if (command_count != (int)CMD_SIZE)
3647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003648 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003649 getout(1);
3650 }
3651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003652 // Use a precomputed index for fast look-up in cmdnames[]
3653 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003654 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3655 if (ASCII_ISLOWER(c2))
3656 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3657 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003658 else if (ASCII_ISUPPER(eap->cmd[0]))
3659 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003661 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
3663 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3664 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3665 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3666 (size_t)len) == 0)
3667 {
3668#ifdef FEAT_EVAL
3669 if (full != NULL
3670 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3671 *full = TRUE;
3672#endif
3673 break;
3674 }
3675
Bram Moolenaar95388e32020-11-20 21:07:00 +01003676 // Not not recognize ":*" as the star command unless '*' is in
3677 // 'cpoptions'.
3678 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3679 p = eap->cmd;
3680
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003681 // Look for a user defined command as a last resort. Let ":Print" be
3682 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003683 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3684 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003686 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 while (ASCII_ISALNUM(*p))
3688 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003689 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003691 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 eap->cmdidx = CMD_SIZE;
3693 }
3694
Bram Moolenaar373863e2020-09-26 17:20:53 +02003695 // ":fina" means ":finally" for backwards compatibility.
3696 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3697 eap->cmdidx = CMD_finally;
3698
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003699#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003700 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003701 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003702 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003703 && (eap->cmdidx < 0 ||
3704 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003705 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003706 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3707
3708 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003709 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003710 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003711 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003712#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 return p;
3715}
3716
3717#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003718static struct cmdmod
3719{
3720 char *name;
3721 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003722 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003723} cmdmods[] = {
3724 {"aboveleft", 3, FALSE},
3725 {"belowright", 3, FALSE},
3726 {"botright", 2, FALSE},
3727 {"browse", 3, FALSE},
3728 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003729 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003730 {"hide", 3, FALSE},
3731 {"keepalt", 5, FALSE},
3732 {"keepjumps", 5, FALSE},
3733 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003734 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003735 {"leftabove", 5, FALSE},
3736 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003737 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003738 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003739 {"rightbelow", 6, FALSE},
3740 {"sandbox", 3, FALSE},
3741 {"silent", 3, FALSE},
3742 {"tab", 3, TRUE},
3743 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003744 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003745 {"verbose", 4, TRUE},
3746 {"vertical", 4, FALSE},
3747};
3748
3749/*
3750 * Return length of a command modifier (including optional count).
3751 * Return zero when it's not a modifier.
3752 */
3753 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003754modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003755{
3756 int i, j;
3757 char_u *p = cmd;
3758
3759 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003760 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003761 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003762 {
3763 for (j = 0; p[j] != NUL; ++j)
3764 if (p[j] != cmdmods[i].name[j])
3765 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003766 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003767 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003768 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003769 }
3770 return 0;
3771}
3772
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773/*
3774 * Return > 0 if an Ex command "name" exists.
3775 * Return 2 if there is an exact match.
3776 * Return 3 if there is an ambiguous match.
3777 */
3778 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003779cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780{
3781 exarg_T ea;
3782 int full = FALSE;
3783 int i;
3784 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003785 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003787 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003788 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 {
3790 for (j = 0; name[j] != NUL; ++j)
3791 if (name[j] != cmdmods[i].name[j])
3792 break;
3793 if (name[j] == NUL && j >= cmdmods[i].minlen)
3794 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3795 }
3796
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003797 // Check built-in commands and user defined commands.
3798 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003799 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003802 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003804 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3805 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003806 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003807 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3809}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003810
3811/*
3812 * "fullcommand" function
3813 */
3814 void
3815f_fullcommand(typval_T *argvars, typval_T *rettv)
3816{
3817 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003818 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003819 char_u *p;
3820
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003821 rettv->v_type = VAR_STRING;
3822 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003823
3824 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3825 return;
3826
3827 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003828 if (name == NULL)
3829 return;
3830
3831 while (*name != NUL && *name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003832 name++;
3833 name = skip_range(name, TRUE, NULL);
3834
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003835 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3836 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003837 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003838 p = find_ex_command(&ea, NULL, NULL, NULL);
3839 if (p == NULL || ea.cmdidx == CMD_SIZE)
3840 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003841 if (in_vim9script())
3842 {
3843 int res;
3844
3845 ++emsg_silent;
3846 res = not_in_vim9(&ea);
3847 --emsg_silent;
3848
3849 if (res == FAIL)
3850 return;
3851 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003852
3853 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
3854 ? get_user_commands(NULL, ea.useridx)
3855 : cmdnames[ea.cmdidx].cmd_name);
3856}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857#endif
3858
Bram Moolenaard0190392019-08-23 21:17:35 +02003859 cmdidx_T
3860excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
Bram Moolenaard0190392019-08-23 21:17:35 +02003862 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
Bram Moolenaard0190392019-08-23 21:17:35 +02003864 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3865 idx = (cmdidx_T)((int)idx + 1))
3866 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 break;
3868
Bram Moolenaard0190392019-08-23 21:17:35 +02003869 return idx;
3870}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
Bram Moolenaard0190392019-08-23 21:17:35 +02003872 long
3873excmd_get_argt(cmdidx_T idx)
3874{
3875 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876}
3877
3878/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003879 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 *
3881 * Backslashed delimiters after / or ? will be skipped, and commands will
3882 * not be expanded between /'s and ?'s or after "'".
3883 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003884 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 * Returns the "cmd" pointer advanced to beyond the range.
3886 */
3887 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003888skip_range(
3889 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003890 int skip_star, // skip "*" used for Visual range
3891 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003893 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003895 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003897 if (*cmd == '\\')
3898 {
3899 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3900 ++cmd;
3901 else
3902 break;
3903 }
3904 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
3906 if (*++cmd == NUL && ctx != NULL)
3907 *ctx = EXPAND_NOTHING;
3908 }
3909 else if (*cmd == '/' || *cmd == '?')
3910 {
3911 delim = *cmd++;
3912 while (*cmd != NUL && *cmd != delim)
3913 if (*cmd++ == '\\' && *cmd != NUL)
3914 ++cmd;
3915 if (*cmd == NUL && ctx != NULL)
3916 *ctx = EXPAND_NOTHING;
3917 }
3918 if (*cmd != NUL)
3919 ++cmd;
3920 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003921
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003922 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003923 while (*cmd == ':')
3924 cmd = skipwhite(cmd + 1);
3925
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003926 // Skip "*" used for Visual range.
3927 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3928 cmd = skipwhite(cmd + 1);
3929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 return cmd;
3931}
3932
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003933 static void
3934addr_error(cmd_addr_T addr_type)
3935{
3936 if (addr_type == ADDR_NONE)
3937 emsg(_(e_norange));
3938 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003939 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003940}
3941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003943 * Return the default address for an address type.
3944 */
3945 static linenr_T
3946default_address(exarg_T *eap)
3947{
3948 linenr_T lnum = 0;
3949
3950 switch (eap->addr_type)
3951 {
3952 case ADDR_LINES:
3953 case ADDR_OTHER:
3954 // Default is the cursor line number. Avoid using an invalid
3955 // line number though.
3956 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3957 lnum = curbuf->b_ml.ml_line_count;
3958 else
3959 lnum = curwin->w_cursor.lnum;
3960 break;
3961 case ADDR_WINDOWS:
3962 lnum = CURRENT_WIN_NR;
3963 break;
3964 case ADDR_ARGUMENTS:
3965 lnum = curwin->w_arg_idx + 1;
3966 if (lnum > ARGCOUNT)
3967 lnum = ARGCOUNT;
3968 break;
3969 case ADDR_LOADED_BUFFERS:
3970 case ADDR_BUFFERS:
3971 lnum = curbuf->b_fnum;
3972 break;
3973 case ADDR_TABS:
3974 lnum = CURRENT_TAB_NR;
3975 break;
3976 case ADDR_TABS_RELATIVE:
3977 case ADDR_UNSIGNED:
3978 lnum = 1;
3979 break;
3980 case ADDR_QUICKFIX:
3981#ifdef FEAT_QUICKFIX
3982 lnum = qf_get_cur_idx(eap);
3983#endif
3984 break;
3985 case ADDR_QUICKFIX_VALID:
3986#ifdef FEAT_QUICKFIX
3987 lnum = qf_get_cur_valid_idx(eap);
3988#endif
3989 break;
3990 case ADDR_NONE:
3991 // Will give an error later if a range is found.
3992 break;
3993 }
3994 return lnum;
3995}
3996
3997/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003998 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 *
4000 * Set ptr to the next character after the part that was interpreted.
4001 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004002 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 *
4004 * Return MAXLNUM when no Ex address was found.
4005 */
4006 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004007get_address(
4008 exarg_T *eap UNUSED,
4009 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004010 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004011 int skip, // only skip the address, don't use it
4012 int silent, // no errors or side effects
4013 int to_other_file, // flag: may jump to other file
4014 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015{
4016 int c;
4017 int i;
4018 long n;
4019 char_u *cmd;
4020 pos_T pos;
4021 pos_T *fp;
4022 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004023 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 cmd = skipwhite(*ptr);
4026 lnum = MAXLNUM;
4027 do
4028 {
4029 switch (*cmd)
4030 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004031 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004032 ++cmd;
4033 switch (addr_type)
4034 {
4035 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004036 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 lnum = curwin->w_cursor.lnum;
4038 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004039 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004040 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004041 break;
4042 case ADDR_ARGUMENTS:
4043 lnum = curwin->w_arg_idx + 1;
4044 break;
4045 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004046 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004047 lnum = curbuf->b_fnum;
4048 break;
4049 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004050 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004051 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004052 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004053 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004054 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004055 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004056 cmd = NULL;
4057 goto error;
4058 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004059 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004060#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004061 lnum = qf_get_cur_idx(eap);
4062#endif
4063 break;
4064 case ADDR_QUICKFIX_VALID:
4065#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004066 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004067#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004068 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004069 }
4070 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004072 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004073 ++cmd;
4074 switch (addr_type)
4075 {
4076 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004077 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 lnum = curbuf->b_ml.ml_line_count;
4079 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004080 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004081 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004082 break;
4083 case ADDR_ARGUMENTS:
4084 lnum = ARGCOUNT;
4085 break;
4086 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004087 buf = lastbuf;
4088 while (buf->b_ml.ml_mfp == NULL)
4089 {
4090 if (buf->b_prev == NULL)
4091 break;
4092 buf = buf->b_prev;
4093 }
4094 lnum = buf->b_fnum;
4095 break;
4096 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004097 lnum = lastbuf->b_fnum;
4098 break;
4099 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004100 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004101 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004102 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004103 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004104 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004105 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004106 cmd = NULL;
4107 goto error;
4108 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004109 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004110#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004111 lnum = qf_get_size(eap);
4112 if (lnum == 0)
4113 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004114#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004115 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004116 case ADDR_QUICKFIX_VALID:
4117#ifdef FEAT_QUICKFIX
4118 lnum = qf_get_valid_size(eap);
4119 if (lnum == 0)
4120 lnum = 1;
4121#endif
4122 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004123 }
4124 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004126 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004127 if (*++cmd == NUL)
4128 {
4129 cmd = NULL;
4130 goto error;
4131 }
4132 if (addr_type != ADDR_LINES)
4133 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004134 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004135 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004136 goto error;
4137 }
4138 if (skip)
4139 ++cmd;
4140 else
4141 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004142 // Only accept a mark in another file when it is
4143 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004144 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4145 ++cmd;
4146 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004147 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004148 lnum = curwin->w_cursor.lnum;
4149 else
4150 {
4151 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
4153 cmd = NULL;
4154 goto error;
4155 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004156 lnum = fp->lnum;
4157 }
4158 }
4159 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004162 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004163 c = *cmd++;
4164 if (addr_type != ADDR_LINES)
4165 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004166 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004167 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004168 goto error;
4169 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004170 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004171 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004172 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004173 if (*cmd == c)
4174 ++cmd;
4175 }
4176 else
4177 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004178 int flags;
4179
4180 pos = curwin->w_cursor; // save curwin->w_cursor
4181
4182 // When '/' or '?' follows another address, start from
4183 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004184 if (lnum != MAXLNUM)
4185 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004186
4187 // Start a forward search at the end of the line (unless
4188 // before the first line).
4189 // Start a backward search at the start of the line.
4190 // This makes sure we never match in the current
4191 // line, and can match anywhere in the
4192 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004193 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004194 curwin->w_cursor.col = MAXCOL;
4195 else
4196 curwin->w_cursor.col = 0;
4197 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004198 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004199 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004200 {
4201 curwin->w_cursor = pos;
4202 cmd = NULL;
4203 goto error;
4204 }
4205 lnum = curwin->w_cursor.lnum;
4206 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004207 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004208 cmd += searchcmdlen;
4209 }
4210 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004212 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004213 ++cmd;
4214 if (addr_type != ADDR_LINES)
4215 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004216 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004217 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004218 goto error;
4219 }
4220 if (*cmd == '&')
4221 i = RE_SUBST;
4222 else if (*cmd == '?' || *cmd == '/')
4223 i = RE_SEARCH;
4224 else
4225 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004226 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004227 cmd = NULL;
4228 goto error;
4229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004231 if (!skip)
4232 {
4233 /*
4234 * When search follows another address, start from
4235 * there.
4236 */
4237 if (lnum != MAXLNUM)
4238 pos.lnum = lnum;
4239 else
4240 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004242 /*
4243 * Start the search just like for the above
4244 * do_search().
4245 */
4246 if (*cmd != '?')
4247 pos.col = MAXCOL;
4248 else
4249 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004250 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004251 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004252 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004253 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004254 lnum = pos.lnum;
4255 else
4256 {
4257 cmd = NULL;
4258 goto error;
4259 }
4260 }
4261 ++cmd;
4262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004265 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004266 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268
4269 for (;;)
4270 {
4271 cmd = skipwhite(cmd);
4272 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4273 break;
4274
4275 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004276 {
4277 switch (addr_type)
4278 {
4279 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004280 case ADDR_OTHER:
4281 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004282 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004283 break;
4284 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004285 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004286 break;
4287 case ADDR_ARGUMENTS:
4288 lnum = curwin->w_arg_idx + 1;
4289 break;
4290 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004291 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004292 lnum = curbuf->b_fnum;
4293 break;
4294 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004295 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004296 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004297 case ADDR_TABS_RELATIVE:
4298 lnum = 1;
4299 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004300 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004301#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004302 lnum = qf_get_cur_idx(eap);
4303#endif
4304 break;
4305 case ADDR_QUICKFIX_VALID:
4306#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004307 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004308#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004309 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004310 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004311 case ADDR_UNSIGNED:
4312 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004313 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004314 }
4315 }
4316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004318 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 else
4320 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004321 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 n = 1;
4323 else
4324 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004325
4326 if (addr_type == ADDR_TABS_RELATIVE)
4327 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004328 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004329 cmd = NULL;
4330 goto error;
4331 }
4332 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004333 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004334 lnum = compute_buffer_local_count(
4335 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004337 {
4338#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004339 // Relative line addressing, need to adjust for folded lines
4340 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004341 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4342 && address_count >= 2)
4343 (void)hasFolding(lnum, NULL, &lnum);
4344#endif
4345 if (i == '-')
4346 lnum -= n;
4347 else
4348 lnum += n;
4349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
4351 } while (*cmd == '/' || *cmd == '?');
4352
4353error:
4354 *ptr = cmd;
4355 return lnum;
4356}
4357
4358/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004359 * Set eap->line1 and eap->line2 to the whole range.
4360 * Used for commands with the EX_DFLALL flag and no range given.
4361 */
4362 static void
4363address_default_all(exarg_T *eap)
4364{
4365 eap->line1 = 1;
4366 switch (eap->addr_type)
4367 {
4368 case ADDR_LINES:
4369 case ADDR_OTHER:
4370 eap->line2 = curbuf->b_ml.ml_line_count;
4371 break;
4372 case ADDR_LOADED_BUFFERS:
4373 {
4374 buf_T *buf = firstbuf;
4375
4376 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4377 buf = buf->b_next;
4378 eap->line1 = buf->b_fnum;
4379 buf = lastbuf;
4380 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4381 buf = buf->b_prev;
4382 eap->line2 = buf->b_fnum;
4383 }
4384 break;
4385 case ADDR_BUFFERS:
4386 eap->line1 = firstbuf->b_fnum;
4387 eap->line2 = lastbuf->b_fnum;
4388 break;
4389 case ADDR_WINDOWS:
4390 eap->line2 = LAST_WIN_NR;
4391 break;
4392 case ADDR_TABS:
4393 eap->line2 = LAST_TAB_NR;
4394 break;
4395 case ADDR_TABS_RELATIVE:
4396 eap->line2 = 1;
4397 break;
4398 case ADDR_ARGUMENTS:
4399 if (ARGCOUNT == 0)
4400 eap->line1 = eap->line2 = 0;
4401 else
4402 eap->line2 = ARGCOUNT;
4403 break;
4404 case ADDR_QUICKFIX_VALID:
4405#ifdef FEAT_QUICKFIX
4406 eap->line2 = qf_get_valid_size(eap);
4407 if (eap->line2 == 0)
4408 eap->line2 = 1;
4409#endif
4410 break;
4411 case ADDR_NONE:
4412 case ADDR_UNSIGNED:
4413 case ADDR_QUICKFIX:
4414 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4415 break;
4416 }
4417}
4418
4419
4420/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004421 * Get flags from an Ex command argument.
4422 */
4423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004424get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004425{
4426 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4427 {
4428 if (*eap->arg == 'l')
4429 eap->flags |= EXFLAG_LIST;
4430 else if (*eap->arg == 'p')
4431 eap->flags |= EXFLAG_PRINT;
4432 else
4433 eap->flags |= EXFLAG_NR;
4434 eap->arg = skipwhite(eap->arg + 1);
4435 }
4436}
4437
4438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 * Function called for command which is Not Implemented. NI!
4440 */
4441 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004442ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004445 eap->errmsg =
4446 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447}
4448
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004449#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450/*
4451 * Function called for script command which is Not Implemented. NI!
4452 * Skips over ":perl <<EOF" constructs.
4453 */
4454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004455ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456{
4457 if (!eap->skip)
4458 ex_ni(eap);
4459 else
4460 vim_free(script_get(eap, eap->arg));
4461}
4462#endif
4463
4464/*
4465 * Check range in Ex command for validity.
4466 * Return NULL when valid, error message when invalid.
4467 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004468 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004469invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004471 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004472
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 if ( eap->line1 < 0
4474 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004475 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004476 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004477
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004478 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004479 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004480 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004481 {
4482 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004483 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004484#ifdef FEAT_DIFF
4485 + (eap->cmdidx == CMD_diffget)
4486#endif
4487 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004488 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004489 break;
4490 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004491 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004492 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004493 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004494 break;
4495 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004496 // Only a boundary check, not whether the buffers actually
4497 // exist.
4498 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004499 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004500 break;
4501 case ADDR_LOADED_BUFFERS:
4502 buf = firstbuf;
4503 while (buf->b_ml.ml_mfp == NULL)
4504 {
4505 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004506 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004507 buf = buf->b_next;
4508 }
4509 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004510 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004511 buf = lastbuf;
4512 while (buf->b_ml.ml_mfp == NULL)
4513 {
4514 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004515 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004516 buf = buf->b_prev;
4517 }
4518 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004519 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004520 break;
4521 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004522 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004523 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004524 break;
4525 case ADDR_TABS:
4526 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004527 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004528 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004529 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004530 case ADDR_OTHER:
4531 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004532 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004533 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004534#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004535 // No error for value that is too big, will use the last entry.
4536 if (eap->line2 <= 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004537 return _(e_invalid_range);
Bram Moolenaare906c502015-09-09 21:10:39 +02004538#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004539 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004540 case ADDR_QUICKFIX_VALID:
4541#ifdef FEAT_QUICKFIX
4542 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4543 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004544 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004545#endif
4546 break;
4547 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004548 case ADDR_NONE:
4549 // Will give an error elsewhere.
4550 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004551 }
4552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 return NULL;
4554}
4555
4556/*
4557 * Correct the range for zero line number, if required.
4558 */
4559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004560correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004562 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
4564 if (eap->line1 == 0)
4565 eap->line1 = 1;
4566 if (eap->line2 == 0)
4567 eap->line2 = 1;
4568 }
4569}
4570
Bram Moolenaar748bf032005-02-02 23:04:36 +00004571#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004572/*
4573 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4574 * pattern. Otherwise return eap->arg.
4575 */
4576 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004577skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004578{
4579 char_u *p = eap->arg;
4580
Bram Moolenaara37420f2006-02-04 22:37:47 +00004581 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4582 || eap->cmdidx == CMD_vimgrepadd
4583 || eap->cmdidx == CMD_lvimgrepadd
4584 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004585 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004586 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004587 if (p == NULL)
4588 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004589 }
4590 return p;
4591}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004592
4593/*
4594 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4595 * in the command line, so that things like % get expanded.
4596 */
4597 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004598replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004599{
4600 char_u *new_cmdline;
4601 char_u *program;
4602 char_u *pos;
4603 char_u *ptr;
4604 int len;
4605 int i;
4606
4607 /*
4608 * Don't do it when ":vimgrep" is used for ":grep".
4609 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004610 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4611 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4612 || eap->cmdidx == CMD_grepadd
4613 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004614 && !grep_internal(eap->cmdidx))
4615 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004616 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4617 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004618 {
4619 if (*curbuf->b_p_gp == NUL)
4620 program = p_gp;
4621 else
4622 program = curbuf->b_p_gp;
4623 }
4624 else
4625 {
4626 if (*curbuf->b_p_mp == NUL)
4627 program = p_mp;
4628 else
4629 program = curbuf->b_p_mp;
4630 }
4631
4632 p = skipwhite(p);
4633
4634 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4635 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004636 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004637 i = 1;
4638 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4639 ++i;
4640 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004641 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004642 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004643 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004644 ptr = new_cmdline;
4645 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4646 {
4647 i = (int)(pos - program);
4648 STRNCPY(ptr, program, i);
4649 STRCPY(ptr += i, p);
4650 ptr += len;
4651 program = pos + 2;
4652 }
4653 STRCPY(ptr, program);
4654 }
4655 else
4656 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004657 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004658 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004659 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004660 STRCPY(new_cmdline, program);
4661 STRCAT(new_cmdline, " ");
4662 STRCAT(new_cmdline, p);
4663 }
4664 msg_make(p);
4665
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004666 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004667 vim_free(*cmdlinep);
4668 *cmdlinep = new_cmdline;
4669 p = new_cmdline;
4670 }
4671 return p;
4672}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004673#endif
4674
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675/*
4676 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004677 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 * Return FAIL for failure, OK otherwise.
4679 */
4680 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004681expand_filename(
4682 exarg_T *eap,
4683 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004684 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004686 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 char_u *repl;
4688 int srclen;
4689 char_u *p;
4690 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004691 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692
Bram Moolenaar748bf032005-02-02 23:04:36 +00004693#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004694 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004695 p = skip_grep_pat(eap);
4696#else
4697 p = eap->arg;
4698#endif
4699
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 /*
4701 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4702 * the file name contains a wildcard it should not cause expanding.
4703 * (it will be expanded anyway if there is a wildcard before replacing).
4704 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004705 has_wildcards = mch_has_wildcard(p);
4706 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004708#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004709 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004710 if (p[0] == '`' && p[1] == '=')
4711 {
4712 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004713 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004714 if (*p == '`')
4715 ++p;
4716 continue;
4717 }
4718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 /*
4720 * Quick check if this cannot be the start of a special string.
4721 * Also removes backslash before '%', '#' and '<'.
4722 */
4723 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4724 {
4725 ++p;
4726 continue;
4727 }
4728
4729 /*
4730 * Try to find a match at this position.
4731 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004732 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4733 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004734 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004736 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 {
4738 p += srclen;
4739 continue;
4740 }
4741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004742 // Wildcards won't be expanded below, the replacement is taken
4743 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004744 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4745 {
4746 char_u *l = repl;
4747
4748 repl = expand_env_save(repl);
4749 vim_free(l);
4750 }
4751
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004752 // Need to escape white space et al. with a backslash.
4753 // Don't do this for:
4754 // - replacement that already has been escaped: "##"
4755 // - shell commands (may have to use quotes instead).
4756 // - non-unix systems when there is a single argument (spaces don't
4757 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004759 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 && eap->cmdidx != CMD_grep
4762 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004763 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004764 && eap->cmdidx != CMD_lgrep
4765 && eap->cmdidx != CMD_lgrepadd
4766 && eap->cmdidx != CMD_lmake
4767 && eap->cmdidx != CMD_make
4768 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004770 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771#endif
4772 )
4773 {
4774 char_u *l;
4775#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004776 // Don't escape a backslash here, because rem_backslash() doesn't
4777 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 static char_u *nobslash = (char_u *)" \t\"|";
4779# define ESCAPE_CHARS nobslash
4780#else
4781# define ESCAPE_CHARS escape_chars
4782#endif
4783
4784 for (l = repl; *l; ++l)
4785 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4786 {
4787 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4788 if (l != NULL)
4789 {
4790 vim_free(repl);
4791 repl = l;
4792 }
4793 break;
4794 }
4795 }
4796
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004797 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004798 if ((eap->usefilter || eap->cmdidx == CMD_bang
4799 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004800 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 {
4802 char_u *l;
4803
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004804 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (l != NULL)
4806 {
4807 vim_free(repl);
4808 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 }
4810 }
4811
4812 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4813 vim_free(repl);
4814 if (p == NULL)
4815 return FAIL;
4816 }
4817
4818 /*
4819 * One file argument: Expand wildcards.
4820 * Don't do this with ":r !command" or ":w !command".
4821 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004822 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 {
4824 /*
4825 * May do this twice:
4826 * 1. Replace environment variables.
4827 * 2. Replace any other wildcards, remove backslashes.
4828 */
4829 for (n = 1; n <= 2; ++n)
4830 {
4831 if (n == 2)
4832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 /*
4834 * Halve the number of backslashes (this is Vi compatible).
4835 * For Unix and OS/2, when wildcards are expanded, this is
4836 * done by ExpandOne() below.
4837 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004838#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if (!has_wildcards)
4840#endif
4841 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 }
4843
4844 if (has_wildcards)
4845 {
4846 if (n == 1)
4847 {
4848 /*
4849 * First loop: May expand environment variables. This
4850 * can be done much faster with expand_env() than with
4851 * something else (e.g., calling a shell).
4852 * After expanding environment variables, check again
4853 * if there are still wildcards present.
4854 */
4855 if (vim_strchr(eap->arg, '$') != NULL
4856 || vim_strchr(eap->arg, '~') != NULL)
4857 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004858 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004859 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 has_wildcards = mch_has_wildcard(NameBuff);
4861 p = NameBuff;
4862 }
4863 else
4864 p = NULL;
4865 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004866 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
4868 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004869 int options = WILD_LIST_NOTFOUND
4870 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 ExpandInit(&xpc);
4873 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004874 if (p_wic)
4875 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004877 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 if (p == NULL)
4879 return FAIL;
4880 }
4881 if (p != NULL)
4882 {
4883 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4884 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004885 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 vim_free(p);
4887 }
4888 }
4889 }
4890 }
4891 return OK;
4892}
4893
4894/*
4895 * Replace part of the command line, keeping eap->cmd, eap->arg and
4896 * eap->nextcmd correct.
4897 * "src" points to the part that is to be replaced, of length "srclen".
4898 * "repl" is the replacement string.
4899 * Returns a pointer to the character after the replaced string.
4900 * Returns NULL for failure.
4901 */
4902 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004903repl_cmdline(
4904 exarg_T *eap,
4905 char_u *src,
4906 int srclen,
4907 char_u *repl,
4908 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909{
4910 int len;
4911 int i;
4912 char_u *new_cmdline;
4913
4914 /*
4915 * The new command line is build in new_cmdline[].
4916 * First allocate it.
4917 * Careful: a "+cmd" argument may have been NUL terminated.
4918 */
4919 len = (int)STRLEN(repl);
4920 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004921 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004922 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004923 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004924 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926 /*
4927 * Copy the stuff before the expanded part.
4928 * Copy the expanded stuff.
4929 * Copy what came after the expanded part.
4930 * Copy the next commands, if there are any.
4931 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004932 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004936 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004938 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004940 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
4942 i = (int)STRLEN(new_cmdline) + 1;
4943 STRCPY(new_cmdline + i, eap->nextcmd);
4944 eap->nextcmd = new_cmdline + i;
4945 }
4946 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4947 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4948 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4949 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4950 vim_free(*cmdlinep);
4951 *cmdlinep = new_cmdline;
4952
4953 return src;
4954}
4955
4956/*
4957 * Check for '|' to separate commands and '"' to start comments.
4958 */
4959 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004960separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
4962 char_u *p;
4963
Bram Moolenaar86b68352004-12-27 21:59:20 +00004964#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004965 p = skip_grep_pat(eap);
4966#else
4967 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004968#endif
4969
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004970 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
4972 if (*p == Ctrl_V)
4973 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004974 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004975 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004977 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004978 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004979 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 break;
4981 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004982
4983#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004984 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004985 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004986 {
4987 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004988 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004989 if (*p == NUL) // stop at NUL after CTRL-V
4990 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004991 }
4992#endif
4993
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004994 // Check for '"': start of comment or '|': next command
4995 // :@" and :*" do not start a comment!
4996 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004997 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004998#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004999 && !in_vim9script()
5000#endif
5001 && !(eap->argt & EX_NOTRLCOM)
5002 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5003 || p != eap->arg)
5004 && (eap->cmdidx != CMD_redir
5005 || p != eap->arg + 1 || p[-1] != '@'))
5006#ifdef FEAT_EVAL
5007 || (*p == '#'
5008 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005009 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005010 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 || *p == '|' || *p == '\n')
5013 {
5014 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005015 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 * AND 'b' is present in 'cpoptions'.
5017 */
5018 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005019 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005021 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 --p;
5023 }
5024 else
5025 {
5026 eap->nextcmd = check_nextcmd(p);
5027 *p = NUL;
5028 break;
5029 }
5030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005032
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005033 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 del_trailing_spaces(eap->arg);
5035}
5036
5037/*
5038 * get + command from ex argument
5039 */
5040 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005041getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042{
5043 char_u *arg = *argp;
5044 char_u *command = NULL;
5045
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005046 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
5048 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005049 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 command = dollar_command;
5051 else
5052 {
5053 command = arg;
5054 arg = skip_cmd_arg(command, TRUE);
5055 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005056 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005059 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 *argp = arg;
5061 }
5062 return command;
5063}
5064
5065/*
5066 * Find end of "+command" argument. Skip over "\ " and "\\".
5067 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005068 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005069skip_cmd_arg(
5070 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005071 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072{
5073 while (*p && !vim_isspace(*p))
5074 {
5075 if (*p == '\\' && p[1] != NUL)
5076 {
5077 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005078 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 else
5080 ++p;
5081 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005082 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
5084 return p;
5085}
5086
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005087 int
5088get_bad_opt(char_u *p, exarg_T *eap)
5089{
5090 if (STRICMP(p, "keep") == 0)
5091 eap->bad_char = BAD_KEEP;
5092 else if (STRICMP(p, "drop") == 0)
5093 eap->bad_char = BAD_DROP;
5094 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5095 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005096 else
5097 return FAIL;
5098 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005099}
5100
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101/*
5102 * Get "++opt=arg" argument.
5103 * Return FAIL or OK.
5104 */
5105 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005106getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107{
5108 char_u *arg = eap->arg + 2;
5109 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005110 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005113 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5115 {
5116 if (*arg == 'n')
5117 {
5118 arg += 2;
5119 eap->force_bin = FORCE_NOBIN;
5120 }
5121 else
5122 eap->force_bin = FORCE_BIN;
5123 if (!checkforcmd(&arg, "binary", 3))
5124 return FAIL;
5125 eap->arg = skipwhite(arg);
5126 return OK;
5127 }
5128
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005129 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005130 if (STRNCMP(arg, "edit", 4) == 0)
5131 {
5132 eap->read_edit = TRUE;
5133 eap->arg = skipwhite(arg + 4);
5134 return OK;
5135 }
5136
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 if (STRNCMP(arg, "ff", 2) == 0)
5138 {
5139 arg += 2;
5140 pp = &eap->force_ff;
5141 }
5142 else if (STRNCMP(arg, "fileformat", 10) == 0)
5143 {
5144 arg += 10;
5145 pp = &eap->force_ff;
5146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 else if (STRNCMP(arg, "enc", 3) == 0)
5148 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005149 if (STRNCMP(arg, "encoding", 8) == 0)
5150 arg += 8;
5151 else
5152 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 pp = &eap->force_enc;
5154 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005155 else if (STRNCMP(arg, "bad", 3) == 0)
5156 {
5157 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005158 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160
5161 if (pp == NULL || *arg != '=')
5162 return FAIL;
5163
5164 ++arg;
5165 *pp = (int)(arg - eap->cmd);
5166 arg = skip_cmd_arg(arg, FALSE);
5167 eap->arg = skipwhite(arg);
5168 *arg = NUL;
5169
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (pp == &eap->force_ff)
5171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5173 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005174 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005176 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005178 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5180 *p = TOLOWER_ASC(*p);
5181 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005182 else
5183 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005184 // Check ++bad= argument. Must be a single-byte character, "keep" or
5185 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005186 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005187 return FAIL;
5188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
5190 return OK;
5191}
5192
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005194ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195{
5196 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005197 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 * directory for security reasons.
5199 */
5200 if (secure)
5201 {
5202 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005203 eap->errmsg =
5204 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
5206 else if (eap->cmdidx == CMD_autocmd)
5207 do_autocmd(eap->arg, eap->forceit);
5208 else
5209 do_augroup(eap->arg, eap->forceit);
5210}
5211
5212/*
5213 * ":doautocmd": Apply the automatic commands to the current buffer.
5214 */
5215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005216ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005218 char_u *arg = eap->arg;
5219 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005220 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005221
Bram Moolenaar1610d052016-06-09 22:53:01 +02005222 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005223 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005224 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005225 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228/*
5229 * :[N]bunload[!] [N] [bufname] unload buffer
5230 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5231 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5232 */
5233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005234ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005236 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005237 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 eap->errmsg = do_bufdel(
5239 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5240 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5241 : DOBUF_UNLOAD, eap->arg,
5242 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5243}
5244
5245/*
5246 * :[N]buffer [N] to buffer N
5247 * :[N]sbuffer [N] to buffer N
5248 */
5249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005250ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005252 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005253 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005255 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 else
5257 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005258 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5260 else
5261 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005262 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005263 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 }
5265}
5266
5267/*
5268 * :[N]bmodified [N] to next mod. buffer
5269 * :[N]sbmodified [N] to next mod. buffer
5270 */
5271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005272ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273{
5274 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005275 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005276 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277}
5278
5279/*
5280 * :[N]bnext [N] to next buffer
5281 * :[N]sbnext [N] split and to next buffer
5282 */
5283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005284ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005286 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005287 return;
5288
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005290 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005291 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292}
5293
5294/*
5295 * :[N]bNext [N] to previous buffer
5296 * :[N]bprevious [N] to previous buffer
5297 * :[N]sbNext [N] split and to previous buffer
5298 * :[N]sbprevious [N] split and to previous buffer
5299 */
5300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005301ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005303 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005304 return;
5305
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (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 * :brewind to first buffer
5313 * :bfirst to first buffer
5314 * :sbrewind split and to first buffer
5315 * :sbfirst split and to first buffer
5316 */
5317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005318ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005320 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005321 return;
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005324 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005325 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326}
5327
5328/*
5329 * :blast to last buffer
5330 * :sblast split and to last buffer
5331 */
5332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005333ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005335 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005336 return;
5337
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005339 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005340 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342
Bram Moolenaar7a092242020-04-16 22:10:49 +02005343/*
5344 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005345 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005348ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005350 int comment_char = '"';
5351
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005352#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005353 if (in_vim9script())
5354 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005355#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005356 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005357}
5358
5359/*
5360 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5361 * to "cmd_start" or has a white space character before it.
5362 */
5363 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005364ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005365{
5366 int c = *cmd;
5367
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005368 if (c == NUL || c == '|' || c == '\n')
5369 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005370#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005371 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005372 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5373 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005374 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005375#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005376 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377}
5378
5379#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5380 || defined(PROTO)
5381/*
5382 * Return the next command, after the first '|' or '\n'.
5383 * Return NULL if not found.
5384 */
5385 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005386find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
5388 while (*p != '|' && *p != '\n')
5389 {
5390 if (*p == NUL)
5391 return NULL;
5392 ++p;
5393 }
5394 return (p + 1);
5395}
5396#endif
5397
5398/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005399 * Check if *p is a separator between Ex commands, skipping over white space.
5400 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 */
5402 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005403check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005405 char_u *s = skipwhite(p);
5406
5407 if (*s == '|' || *s == '\n')
5408 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 else
5410 return NULL;
5411}
5412
5413/*
5414 * - if there are more files to edit
5415 * - and this is the last window
5416 * - and forceit not used
5417 * - and not repeated twice on a row
5418 * return FAIL and give error message if 'message' TRUE
5419 * return OK otherwise
5420 */
5421 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005422check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005423 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005424 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425{
5426 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5427
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005428 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005429 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {
5431 if (message)
5432 {
5433#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005434 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5435 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005437 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005439 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5440 NGETTEXT("%d more file to edit. Quit anyway?",
5441 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5443 return OK;
5444 return FAIL;
5445 }
5446#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005447 semsg(NGETTEXT("E173: %d more file to edit",
5448 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005449 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 }
5451 return FAIL;
5452 }
5453 return OK;
5454}
5455
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456/*
5457 * Function given to ExpandGeneric() to obtain the list of command names.
5458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005460get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461{
5462 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 return cmdnames[idx].cmd_name;
5465}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005468ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469{
Bram Moolenaare6850792010-05-14 15:28:44 +02005470 if (*eap->arg == NUL)
5471 {
5472#ifdef FEAT_EVAL
5473 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5474 char_u *p = NULL;
5475
5476 if (expr != NULL)
5477 {
5478 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005479 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005480 --emsg_off;
5481 vim_free(expr);
5482 }
5483 if (p != NULL)
5484 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005485 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005486 vim_free(p);
5487 }
5488 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005489 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005490#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005491 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005492#endif
5493 }
5494 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005495 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005496
5497#ifdef FEAT_VTP
5498 else if (has_vtp_working())
5499 {
5500 // background color change requires clear + redraw
5501 update_screen(CLEAR);
5502 redrawcmd();
5503 }
5504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505}
5506
5507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005508ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509{
5510 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005511 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 do_highlight(eap->arg, eap->forceit, FALSE);
5513}
5514
5515
5516/*
5517 * Call this function if we thought we were going to exit, but we won't
5518 * (because of an error). May need to restore the terminal mode.
5519 */
5520 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005521not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
5523 exiting = FALSE;
5524 settmode(TMODE_RAW);
5525}
5526
Bram Moolenaar3552e742021-05-29 12:21:58 +02005527 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005528before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5529{
5530 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5531
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005532 // Bail out when autocommands closed the window.
5533 // Refuse to quit when the buffer in the last window is being closed (can
5534 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005535 if (!win_valid(wp)
5536 || curbuf_locked()
5537 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5538 return TRUE;
5539
5540 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5541 {
5542 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005543 // Refuse to quit when locked or when the window was closed or the
5544 // buffer in the last window is being closed (can only happen in
5545 // autocommands).
5546 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005547 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5548 return TRUE;
5549 }
5550
5551 return FALSE;
5552}
5553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005555 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005556 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005557 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005559 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005560ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005562 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005563
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564#ifdef FEAT_CMDWIN
5565 if (cmdwin_type != 0)
5566 {
5567 cmdwin_result = Ctrl_C;
5568 return;
5569 }
5570#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005571 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005572 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005573 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005574 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005575 return;
5576 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005577 if (eap->addr_count > 0)
5578 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005579 int wnr = eap->line2;
5580
5581 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5582 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005583 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005584 }
5585 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005586 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005588 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005589 if (curbuf_locked())
5590 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005591
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005592 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005593 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595
5596#ifdef FEAT_NETBEANS_INTG
5597 netbeansForcedQuit = eap->forceit;
5598#endif
5599
5600 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005601 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 */
5603 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5604 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005605 if ((!buf_hide(wp->w_buffer)
5606 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005607 | (eap->forceit ? CCGD_FORCEIT : 0)
5608 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005610 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 {
5612 not_exiting();
5613 }
5614 else
5615 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005616 // quit last window
5617 // Note: only_one_window() returns true, even so a help window is
5618 // still open. In that case only quit, if no address has been
5619 // specified. Example:
5620 // :h|wincmd w|1q - don't quit
5621 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005622 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005624 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005625#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005627#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005628 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005629 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
5631}
5632
5633/*
5634 * ":cquit".
5635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005637ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005639 // this does not always pass on the exit code to the Manx compiler. why?
5640 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641}
5642
5643/*
5644 * ":qall": try to quit all windows
5645 */
5646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005647ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648{
5649# ifdef FEAT_CMDWIN
5650 if (cmdwin_type != 0)
5651 {
5652 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005653 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 else
5655 cmdwin_result = K_XF2;
5656 return;
5657 }
5658# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005659
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005660 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005661 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005662 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005663 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005664 return;
5665 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005666
5667 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005668 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005669
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005671 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 getout(0);
5673 not_exiting();
5674}
5675
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676/*
5677 * ":close": close current window, unless it is the last one
5678 */
5679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005680ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005682 win_T *win;
5683 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005684#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005686 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005688#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005689 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005690 {
5691 if (eap->addr_count == 0)
5692 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005693 else
5694 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005695 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005696 {
5697 winnr++;
5698 if (winnr == eap->line2)
5699 break;
5700 }
5701 if (win == NULL)
5702 win = lastwin;
5703 ex_win_close(eap->forceit, win, NULL);
5704 }
5705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706}
5707
Bram Moolenaar4033c552017-09-16 20:54:51 +02005708#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005709/*
5710 * ":pclose": Close any preview window.
5711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005713ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005714{
5715 win_T *win;
5716
Bram Moolenaar79648732019-07-18 21:43:07 +02005717 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005718 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005719 if (win->w_p_pvw)
5720 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005721 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005722 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005723 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005724# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005725 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005726 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005727# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005728}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005729#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005730
Bram Moolenaarf740b292006-02-16 22:11:02 +00005731/*
5732 * Close window "win" and take care of handling closing the last window for a
5733 * modified buffer.
5734 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005736ex_win_close(
5737 int forceit,
5738 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005739 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 int need_hide;
5742 buf_T *buf = win->w_buffer;
5743
Bram Moolenaarcf844172020-06-26 19:44:06 +02005744 // Never close the autocommand window.
5745 if (win == aucmd_win)
5746 {
5747 emsg(_(e_autocmd_close));
5748 return;
5749 }
5750
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005752 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005754#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005755 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005757 bufref_T bufref;
5758
5759 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005761 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 return;
5763 need_hide = FALSE;
5764 }
5765 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005768 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 return;
5770 }
5771 }
5772
Bram Moolenaar4033c552017-09-16 20:54:51 +02005773#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005775#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005776
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005777 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005778 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005779 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005780 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005781 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782}
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005785 * Handle the argument for a tabpage related ex command.
5786 * Returns a tabpage number.
5787 * When an error is encountered then eap->errmsg is set.
5788 */
5789 static int
5790get_tabpage_arg(exarg_T *eap)
5791{
5792 int tab_number;
5793 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5794
5795 if (eap->arg && *eap->arg != NUL)
5796 {
5797 char_u *p = eap->arg;
5798 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005799 int relative = 0; // argument +N/-N means: go to N places to the
5800 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005801
5802 if (*p == '-')
5803 {
5804 relative = -1;
5805 p++;
5806 }
5807 else if (*p == '+')
5808 {
5809 relative = 1;
5810 p++;
5811 }
5812
5813 p_save = p;
5814 tab_number = getdigits(&p);
5815
5816 if (relative == 0)
5817 {
5818 if (STRCMP(p, "$") == 0)
5819 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005820 else if (STRCMP(p, "#") == 0)
5821 if (valid_tabpage(lastused_tabpage))
5822 tab_number = tabpage_index(lastused_tabpage);
5823 else
5824 {
5825 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5826 tab_number = 0;
5827 goto theend;
5828 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005829 else if (p == p_save || *p_save == '-' || *p != NUL
5830 || tab_number > LAST_TAB_NR)
5831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005832 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005833 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005834 goto theend;
5835 }
5836 }
5837 else
5838 {
5839 if (*p_save == NUL)
5840 tab_number = 1;
5841 else if (p == p_save || *p_save == '-' || *p != NUL
5842 || tab_number == 0)
5843 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005844 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005845 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005846 goto theend;
5847 }
5848 tab_number = tab_number * relative + tabpage_index(curtab);
5849 if (!unaccept_arg0 && relative == -1)
5850 --tab_number;
5851 }
5852 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005853 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005854 }
5855 else if (eap->addr_count > 0)
5856 {
5857 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005858 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005859 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005860 tab_number = 0;
5861 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005862 else
5863 {
5864 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005865 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005866 {
5867 --tab_number;
5868 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005869 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01005870 }
5871 }
5872 }
5873 else
5874 {
5875 switch (eap->cmdidx)
5876 {
5877 case CMD_tabnext:
5878 tab_number = tabpage_index(curtab) + 1;
5879 if (tab_number > LAST_TAB_NR)
5880 tab_number = 1;
5881 break;
5882 case CMD_tabmove:
5883 tab_number = LAST_TAB_NR;
5884 break;
5885 default:
5886 tab_number = tabpage_index(curtab);
5887 }
5888 }
5889
5890theend:
5891 return tab_number;
5892}
5893
5894/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005895 * ":tabclose": close current tab page, unless it is the last one.
5896 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 */
5898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005899ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005901 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005902 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005903
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005904# ifdef FEAT_CMDWIN
5905 if (cmdwin_type != 0)
5906 cmdwin_result = K_IGNORE;
5907 else
5908# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005909 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005910 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005911 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005913 tab_number = get_tabpage_arg(eap);
5914 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005915 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005916 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005917 if (tp == NULL)
5918 {
5919 beep_flush();
5920 return;
5921 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005922 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005923 {
5924 tabpage_close_other(tp, eap->forceit);
5925 return;
5926 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005927 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005928 tabpage_close(eap->forceit);
5929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005931}
5932
5933/*
5934 * ":tabonly": close all tab pages except the current one
5935 */
5936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005937ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005938{
5939 tabpage_T *tp;
5940 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005941 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005942
5943# ifdef FEAT_CMDWIN
5944 if (cmdwin_type != 0)
5945 cmdwin_result = K_IGNORE;
5946 else
5947# endif
5948 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005949 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005950 else
5951 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005952 tab_number = get_tabpage_arg(eap);
5953 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005954 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005955 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005956 // Repeat this up to a 1000 times, because autocommands may
5957 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005958 for (done = 0; done < 1000; ++done)
5959 {
5960 FOR_ALL_TABPAGES(tp)
5961 if (tp->tp_topframe != topframe)
5962 {
5963 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005964 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005965 if (valid_tabpage(tp))
5966 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005967 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005968 break;
5969 }
5970 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005971 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005972 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005973 }
5974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976
5977/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005978 * Close the current tab page.
5979 */
5980 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005981tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005982{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005983 // First close all the windows but the current one. If that worked then
5984 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005985 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005986 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005987 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005988 ex_win_close(forceit, curwin, NULL);
5989# ifdef FEAT_GUI
5990 need_mouse_correct = TRUE;
5991# endif
5992}
5993
5994/*
5995 * Close tab page "tp", which is not the current tab page.
5996 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005997 * Also takes care of the tab pages line disappearing when closing the
5998 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005999 */
6000 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006001tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006002{
6003 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006004 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006005 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006006
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006007 // Limit to 1000 windows, autocommands may add a window while we close
6008 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006009 while (++done < 1000)
6010 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006011 wp = tp->tp_firstwin;
6012 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006013
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006014 // Autocommands may delete the tab page under our fingers and we may
6015 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006016 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006017 break;
6018 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006019
Bram Moolenaar29323592016-07-24 22:04:11 +02006020 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006021
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006022 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006023 if (h != tabline_height())
6024 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006025}
6026
6027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 * ":only".
6029 */
6030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006031ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006033 win_T *wp;
6034 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035# ifdef FEAT_GUI
6036 need_mouse_correct = TRUE;
6037# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006038 if (eap->addr_count > 0)
6039 {
6040 wnr = eap->line2;
6041 for (wp = firstwin; --wnr > 0; )
6042 {
6043 if (wp->w_next == NULL)
6044 break;
6045 else
6046 wp = wp->w_next;
6047 }
6048 win_goto(wp);
6049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 close_others(TRUE, eap->forceit);
6051}
6052
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006054ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006056 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006057 if (!eap->skip)
6058 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006059#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006060 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006061#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006062 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006063 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006064 else
6065 {
6066 int winnr = 0;
6067 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006068
Bram Moolenaar2256c992016-11-15 21:17:07 +01006069 FOR_ALL_WINDOWS(win)
6070 {
6071 winnr++;
6072 if (winnr == eap->line2)
6073 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006074 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006075 if (win == NULL)
6076 win = lastwin;
6077 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 }
6080}
6081
6082/*
6083 * ":stop" and ":suspend": Suspend Vim.
6084 */
6085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006086ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087{
6088 /*
6089 * Disallow suspending for "rvim".
6090 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006091 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 {
6093 if (!eap->forceit)
6094 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006095 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 windgoto((int)Rows - 1, 0);
6097 out_char('\n');
6098 out_flush();
6099 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006100 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006102 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006104 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105#ifdef FEAT_TITLE
6106 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006107 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108#endif
6109 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006110 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006112 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006113 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 }
6115}
6116
6117/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006118 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 */
6120 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006121ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006123#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006124 if (not_in_vim9(eap) == FAIL)
6125 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127#ifdef FEAT_CMDWIN
6128 if (cmdwin_type != 0)
6129 {
6130 cmdwin_result = Ctrl_C;
6131 return;
6132 }
6133#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006134 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006135 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006136 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006137 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006138 return;
6139 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006140
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006142 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 */
6144 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6145 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006146
6147 // Write the buffer for ":wq" or when it was changed.
6148 // Trigger QuitPre and ExitPre.
6149 // Check if we can exit now, after autocommands have changed things.
6150 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6151 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006153 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 {
6155 not_exiting();
6156 }
6157 else
6158 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006159 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006161 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162# ifdef FEAT_GUI
6163 need_mouse_correct = TRUE;
6164# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006165 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006166 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 }
6168}
6169
6170/*
6171 * ":print", ":list", ":number".
6172 */
6173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006174ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006176 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006177 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006178 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006180 for ( ;!got_int; ui_breakcheck())
6181 {
6182 print_line(eap->line1,
6183 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6184 || (eap->flags & EXFLAG_NR)),
6185 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6186 if (++eap->line1 > eap->line2)
6187 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006188 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006189 }
6190 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006191 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006192 curwin->w_cursor.lnum = eap->line2;
6193 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 }
6195
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197}
6198
6199#ifdef FEAT_BYTEOFF
6200 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006201ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203 goto_byte(eap->line2);
6204}
6205#endif
6206
6207/*
6208 * ":shell".
6209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006211ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 do_shell(NULL, 0);
6214}
6215
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006216#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006218static int drop_busy = FALSE;
6219static int drop_filec;
6220static char_u **drop_filev = NULL;
6221static int drop_split;
6222static void (*drop_callback)(void *);
6223static void *drop_cookie;
6224
6225 static void
6226handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227{
6228 exarg_T ea;
6229 int save_msg_scroll = msg_scroll;
6230
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006231 // Setting the argument list may cause screen updates and being called
6232 // recursively. Avoid that by setting drop_busy.
6233 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006235 // Check whether the current buffer is changed. If so, we will need
6236 // to split the current window or data could be lost.
6237 // We don't need to check if the 'hidden' option is set, as in this
6238 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006239 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 {
6241 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006242 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 --emsg_off;
6244 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006245 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 if (win_split(0, 0) == FAIL)
6248 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006249 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006251 // When splitting the window, create a new alist. Otherwise the
6252 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 alist_unlink(curwin->w_alist);
6254 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 }
6256
6257 /*
6258 * Set up the new argument list.
6259 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006260 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261
6262 /*
6263 * Move to the first file.
6264 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006265 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006266 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 ea.cmd = (char_u *)"next";
6268 do_argfile(&ea, 0);
6269
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006270 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6271 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 need_start_insertmode = FALSE;
6273
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006274 // Restore msg_scroll, otherwise a following command may cause scrolling
6275 // unexpectedly. The screen will be redrawn by the caller, thus
6276 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006278
6279 if (drop_callback != NULL)
6280 drop_callback(drop_cookie);
6281
6282 drop_filev = NULL;
6283 drop_busy = FALSE;
6284}
6285
6286/*
6287 * Handle a file drop. The code is here because a drop is *nearly* like an
6288 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006289 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006290 * code is very similar to :args and hence needs access to a lot of the static
6291 * functions in this file.
6292 *
6293 * The "filev" list must have been allocated using alloc(), as should each item
6294 * in the list. This function takes over responsibility for freeing the "filev"
6295 * list.
6296 */
6297 void
6298handle_drop(
6299 int filec, // the number of files dropped
6300 char_u **filev, // the list of files dropped
6301 int split, // force splitting the window
6302 void (*callback)(void *), // to be called after setting the argument
6303 // list
6304 void *cookie) // argument for "callback" (allocated)
6305{
6306 // Cannot handle recursive drops, finish the pending one.
6307 if (drop_busy)
6308 {
6309 FreeWild(filec, filev);
6310 vim_free(cookie);
6311 return;
6312 }
6313
6314 // When calling handle_drop() more than once in a row we only use the last
6315 // one.
6316 if (drop_filev != NULL)
6317 {
6318 FreeWild(drop_filec, drop_filev);
6319 vim_free(drop_cookie);
6320 }
6321
6322 drop_filec = filec;
6323 drop_filev = filev;
6324 drop_split = split;
6325 drop_callback = callback;
6326 drop_cookie = cookie;
6327
6328 // Postpone this when:
6329 // - editing the command line
6330 // - not possible to change the current buffer
6331 // - updating the screen
6332 // As it may change buffers and window structures that are in use and cause
6333 // freed memory to be used.
6334 if (text_locked() || curbuf_locked() || updating_screen)
6335 return;
6336
6337 handle_drop_internal();
6338}
6339
6340/*
6341 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6342 * reset: Handle a postponed drop.
6343 */
6344 void
6345handle_any_postponed_drop(void)
6346{
6347 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006348 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006349 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350}
6351#endif
6352
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 * ":preserve".
6355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006357ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006359 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 ml_preserve(curbuf, TRUE);
6361}
6362
6363/*
6364 * ":recover".
6365 */
6366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006367ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006369 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006371 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6372 | CCGD_MULTWIN
6373 | (eap->forceit ? CCGD_FORCEIT : 0)
6374 | CCGD_EXCMD)
6375
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 && (*eap->arg == NUL
6377 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006378 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 recoverymode = FALSE;
6380}
6381
6382/*
6383 * Command modifier used in a wrong way.
6384 */
6385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006386ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006388 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389}
6390
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391/*
6392 * :sview [+command] file split window with new file, read-only
6393 * :split [[+command] file] split window with current or new file
6394 * :vsplit [[+command] file] split window vertically with current or new file
6395 * :new [[+command] file] split window with no or new file
6396 * :vnew [[+command] file] split vertically window with no or new file
6397 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006398 *
6399 * :tabedit open new Tab page with empty window
6400 * :tabedit [+command] file open new Tab page and edit "file"
6401 * :tabnew [[+command] file] just like :tabedit
6402 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 */
6404 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006405ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006407 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006408#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006410#endif
6411#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006412 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006413 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006414#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006415 int use_tab = eap->cmdidx == CMD_tabedit
6416 || eap->cmdidx == CMD_tabfind
6417 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006419 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006420 return;
6421
Bram Moolenaar4033c552017-09-16 20:54:51 +02006422#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425
Bram Moolenaar4033c552017-09-16 20:54:51 +02006426#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006427 // A ":split" in the quickfix window works like ":new". Don't want two
6428 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006429 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
6431 if (eap->cmdidx == CMD_split)
6432 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 if (eap->cmdidx == CMD_vsplit)
6434 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437
Bram Moolenaar4033c552017-09-16 20:54:51 +02006438#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006439 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 {
6441 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6442 FNAME_MESS, TRUE, curbuf->b_ffname);
6443 if (fname == NULL)
6444 goto theend;
6445 eap->arg = fname;
6446 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006447# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006448 else
6449# endif
6450#endif
6451#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006452 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 && eap->cmdidx != CMD_new)
6455 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006456 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006457# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006458 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006459# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006460 au_has_group((char_u *)"FileExplorer"))
6461 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006462 // No browsing supported but we do have the file explorer:
6463 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006464 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006465 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006466 }
6467 else
6468 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006469 fname = do_browse(0, (char_u *)(use_tab
6470 ? _("Edit File in new tab page")
6471 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006473 if (fname == NULL)
6474 goto theend;
6475 eap->arg = fname;
6476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006478 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006481 /*
6482 * Either open new tab page or split the window.
6483 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006484 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006485 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006486 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006487 : eap->addr_count == 0 ? 0
6488 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006489 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006490 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006491
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006492 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006493 if (curwin != old_curwin
6494 && win_valid(old_curwin)
6495 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006496 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006497 old_curwin->w_alt_fnum = curbuf->b_fnum;
6498 }
6499 }
6500 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6502 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006503 // Reset 'scrollbind' when editing another file, but keep it when
6504 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006505 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006506 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 else
6508 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 do_exedit(eap, old_curwin);
6510 }
6511
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006512# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006513 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006514# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006516# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517theend:
6518 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006519# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006521
6522/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006523 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006524 */
6525 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006526tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006527{
6528 exarg_T ea;
6529
Bram Moolenaara80faa82020-04-12 19:37:17 +02006530 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006531 ea.cmdidx = CMD_tabnew;
6532 ea.cmd = (char_u *)"tabn";
6533 ea.arg = (char_u *)"";
6534 ex_splitview(&ea);
6535}
6536
6537/*
6538 * :tabnext command
6539 */
6540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006541ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006542{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006543 int tab_number;
6544
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006545 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006546 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006547 switch (eap->cmdidx)
6548 {
6549 case CMD_tabfirst:
6550 case CMD_tabrewind:
6551 goto_tabpage(1);
6552 break;
6553 case CMD_tablast:
6554 goto_tabpage(9999);
6555 break;
6556 case CMD_tabprevious:
6557 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006558 if (eap->arg && *eap->arg != NUL)
6559 {
6560 char_u *p = eap->arg;
6561 char_u *p_save = p;
6562
6563 tab_number = getdigits(&p);
6564 if (p == p_save || *p_save == '-' || *p != NUL
6565 || tab_number == 0)
6566 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006567 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006568 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006569 return;
6570 }
6571 }
6572 else
6573 {
6574 if (eap->addr_count == 0)
6575 tab_number = 1;
6576 else
6577 {
6578 tab_number = eap->line2;
6579 if (tab_number < 1)
6580 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006581 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006582 return;
6583 }
6584 }
6585 }
6586 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006587 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006588 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006589 tab_number = get_tabpage_arg(eap);
6590 if (eap->errmsg == NULL)
6591 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006592 break;
6593 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006594}
6595
6596/*
6597 * :tabmove command
6598 */
6599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006600ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006601{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006602 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006603
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006604 tab_number = get_tabpage_arg(eap);
6605 if (eap->errmsg == NULL)
6606 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006607}
6608
6609/*
6610 * :tabs command: List tabs and their contents.
6611 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006613ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006614{
6615 tabpage_T *tp;
6616 win_T *wp;
6617 int tabcount = 1;
6618
6619 msg_start();
6620 msg_scroll = TRUE;
6621 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6622 {
6623 msg_putchar('\n');
6624 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006625 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006626 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006627 ui_breakcheck();
6628
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006629 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006630 wp = firstwin;
6631 else
6632 wp = tp->tp_firstwin;
6633 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6634 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006635 msg_putchar('\n');
6636 msg_putchar(wp == curwin ? '>' : ' ');
6637 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006638 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6639 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006640 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006641 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006642 else
6643 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6644 IObuff, IOSIZE, TRUE);
6645 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006646 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006647 ui_breakcheck();
6648 }
6649 }
6650}
6651
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652/*
6653 * ":mode": Set screen mode.
6654 * If no argument given, just get the screen size and redraw.
6655 */
6656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006657ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658{
6659 if (*eap->arg == NUL)
6660 shell_resized();
6661 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006662 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663}
6664
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665/*
6666 * ":resize".
6667 * set, increment or decrement current window height
6668 */
6669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006670ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 int n;
6673 win_T *wp = curwin;
6674
6675 if (eap->addr_count > 0)
6676 {
6677 n = eap->line2;
6678 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6679 ;
6680 }
6681
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006682# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006686 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 {
6688 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006689 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006690 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 n = 9999;
6692 win_setwidth_win((int)n, wp);
6693 }
6694 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 {
6696 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006697 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006698 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 n = 9999;
6700 win_setheight_win((int)n, wp);
6701 }
6702}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
6704/*
6705 * ":find [+command] <file>" command.
6706 */
6707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006708ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
6710#ifdef FEAT_SEARCHPATH
6711 char_u *fname;
6712 int count;
6713
6714 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6715 TRUE, curbuf->b_ffname);
6716 if (eap->addr_count > 0)
6717 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006718 // Repeat finding the file "count" times. This matters when it
6719 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 count = eap->line2;
6721 while (fname != NULL && --count > 0)
6722 {
6723 vim_free(fname);
6724 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6725 FALSE, curbuf->b_ffname);
6726 }
6727 }
6728
6729 if (fname != NULL)
6730 {
6731 eap->arg = fname;
6732#endif
6733 do_exedit(eap, NULL);
6734#ifdef FEAT_SEARCHPATH
6735 vim_free(fname);
6736 }
6737#endif
6738}
6739
6740/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006741 * ":open" simulation: for now just work like ":visual".
6742 */
6743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006744ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006745{
6746 regmatch_T regmatch;
6747 char_u *p;
6748
Bram Moolenaar65088802021-03-13 21:07:21 +01006749#ifdef FEAT_EVAL
6750 if (not_in_vim9(eap) == FAIL)
6751 return;
6752#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006753 curwin->w_cursor.lnum = eap->line2;
6754 beginline(BL_SOL | BL_FIX);
6755 if (*eap->arg == '/')
6756 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006757 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006758 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006759 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006760 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006761 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006762 if (regmatch.regprog != NULL)
6763 {
6764 regmatch.rm_ic = p_ic;
6765 p = ml_get_curline();
6766 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006767 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006768 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006769 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006770 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006771 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006772 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006773 eap->arg += STRLEN(eap->arg);
6774 }
6775 check_cursor();
6776
6777 eap->cmdidx = CMD_visual;
6778 do_exedit(eap, NULL);
6779}
6780
6781/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006782 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 */
6784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006785ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786{
6787 do_exedit(eap, NULL);
6788}
6789
6790/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006791 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006794do_exedit(
6795 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006796 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797{
6798 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006800 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006802 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6803 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006804 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 /*
6806 * ":vi" command ends Ex mode.
6807 */
6808 if (exmode_active && (eap->cmdidx == CMD_visual
6809 || eap->cmdidx == CMD_view))
6810 {
6811 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006812 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006814 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006815 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006816 if (global_busy)
6817 {
6818 int rd = RedrawingDisabled;
6819 int nwr = no_wait_return;
6820 int ms = msg_scroll;
6821#ifdef FEAT_GUI
6822 int he = hold_gui_events;
6823#endif
6824
6825 if (eap->nextcmd != NULL)
6826 {
6827 stuffReadbuff(eap->nextcmd);
6828 eap->nextcmd = NULL;
6829 }
6830
6831 if (exmode_was != EXMODE_VIM)
6832 settmode(TMODE_RAW);
6833 RedrawingDisabled = 0;
6834 no_wait_return = 0;
6835 need_wait_return = FALSE;
6836 msg_scroll = 0;
6837#ifdef FEAT_GUI
6838 hold_gui_events = 0;
6839#endif
6840 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006841 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006842
6843 main_loop(FALSE, TRUE);
6844
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006845 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006846 RedrawingDisabled = rd;
6847 no_wait_return = nwr;
6848 msg_scroll = ms;
6849#ifdef FEAT_GUI
6850 hold_gui_events = he;
6851#endif
6852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 }
6856
6857 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006858 || eap->cmdidx == CMD_tabnew
6859 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006860 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006862 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 setpcmark();
6864 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006865 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6866 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006868 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 || *eap->arg != NUL
6870#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006871 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872#endif
6873 )
6874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006875 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6876 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006877 if (*eap->arg != NUL && curbuf_locked())
6878 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006879
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 n = readonlymode;
6881 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6882 readonlymode = TRUE;
6883 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006884 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6885 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006886 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6887 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6889 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006890 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6892 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6893 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006894 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006896 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006897 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006898 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6899 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006900 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006902 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 if (old_curwin != NULL)
6904 {
6905 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006906 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006908#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006909 cleanup_T cs;
6910
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006911 // Reset the error/interrupt/exception state here so that
6912 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006913 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006914#endif
6915#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006917#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006918 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006919
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006920#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006921 // Restore the error/interrupt/exception state if not
6922 // discarded by a new aborting error, interrupt, or
6923 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006924 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 }
6927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 }
6929 else if (readonlymode && curbuf->b_nwindows == 1)
6930 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006931 // When editing an already visited buffer, 'readonly' won't be set
6932 // but the previous value is kept. With ":view" and ":sview" we
6933 // want the file to be readonly, except when another window is
6934 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 curbuf->b_p_ro = TRUE;
6936 }
6937 readonlymode = n;
6938 }
6939 else
6940 {
6941 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01006942 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943#ifdef FEAT_TITLE
6944 n = curwin->w_arg_idx_invalid;
6945#endif
6946 check_arg_idx(curwin);
6947#ifdef FEAT_TITLE
6948 if (n != curwin->w_arg_idx_invalid)
6949 maketitle();
6950#endif
6951 }
6952
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 /*
6954 * if ":split file" worked, set alternate file name in old window to new
6955 * file
6956 */
6957 if (old_curwin != NULL
6958 && *eap->arg != NUL
6959 && curwin != old_curwin
6960 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006961 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006962 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964
6965 ex_no_reprint = TRUE;
6966}
6967
6968#ifndef FEAT_GUI
6969/*
6970 * ":gui" and ":gvim" when there is no GUI.
6971 */
6972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006973ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006975 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976}
6977#endif
6978
Bram Moolenaar4f974752019-02-17 17:44:42 +01006979#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006981ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982{
6983 gui_make_tearoff(eap->arg);
6984}
6985#endif
6986
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006987#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6988 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006990ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006992# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6993 if (gui.in_use)
6994 gui_make_popup(eap->arg, eap->forceit);
6995# ifdef FEAT_TERM_POPUP_MENU
6996 else
6997# endif
6998# endif
6999# ifdef FEAT_TERM_POPUP_MENU
7000 pum_make_popup(eap->arg, eap->forceit);
7001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002}
7003#endif
7004
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007006ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007009 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007011 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012}
7013
7014/*
7015 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7016 * offset.
7017 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007020ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007023 win_T *save_curwin = curwin;
7024 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 long topline;
7026 long y;
7027 linenr_T old_linenr = curwin->w_cursor.lnum;
7028
7029 setpcmark();
7030
7031 /*
7032 * determine max topline
7033 */
7034 if (curwin->w_p_scb)
7035 {
7036 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007037 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 {
7039 if (wp->w_p_scb && wp->w_buffer)
7040 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007041 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 if (topline > y)
7043 topline = y;
7044 }
7045 }
7046 if (topline < 1)
7047 topline = 1;
7048 }
7049 else
7050 {
7051 topline = 1;
7052 }
7053
7054
7055 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007056 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007058 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {
7060 if (curwin->w_p_scb)
7061 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007062 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 y = topline - curwin->w_topline;
7064 if (y > 0)
7065 scrollup(y, TRUE);
7066 else
7067 scrolldown(-y, TRUE);
7068 curwin->w_scbind_pos = topline;
7069 redraw_later(VALID);
7070 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 }
7073 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007074 curwin = save_curwin;
7075 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 if (curwin->w_p_scb)
7077 {
7078 did_syncbind = TRUE;
7079 checkpcmark();
7080 if (old_linenr != curwin->w_cursor.lnum)
7081 {
7082 char_u ctrl_o[2];
7083
7084 ctrl_o[0] = Ctrl_O;
7085 ctrl_o[1] = 0;
7086 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7087 }
7088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089}
7090
7091
7092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007093ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007095 int i;
7096 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7097 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007099 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 do_bang(1, eap, FALSE, FALSE, TRUE);
7101 else
7102 {
7103 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7104 return;
7105
7106#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007107 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {
7109 char_u *browseFile;
7110
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007111 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 NULL, NULL, NULL, curbuf);
7113 if (browseFile != NULL)
7114 {
7115 i = readfile(browseFile, NULL,
7116 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7117 vim_free(browseFile);
7118 }
7119 else
7120 i = OK;
7121 }
7122 else
7123#endif
7124 if (*eap->arg == NUL)
7125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007126 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 return;
7128 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7129 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7130 }
7131 else
7132 {
7133 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7134 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7135 i = readfile(eap->arg, NULL,
7136 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7137
7138 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007139 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007141#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 if (!aborting())
7143#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007144 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 }
7146 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007147 {
7148 if (empty && exmode_active)
7149 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007150 // Delete the empty line that remains. Historically ex does
7151 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007152 if (eap->line2 == 0)
7153 lnum = curbuf->b_ml.ml_line_count;
7154 else
7155 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007156 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007157 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007158 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007159 if (curwin->w_cursor.lnum > 1
7160 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007161 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007162 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007163 }
7164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
7168}
7169
Bram Moolenaarea408852005-06-25 22:49:46 +00007170static char_u *prev_dir = NULL;
7171
7172#if defined(EXITFREE) || defined(PROTO)
7173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007174free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007175{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007176 VIM_CLEAR(prev_dir);
7177 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007178}
7179#endif
7180
Bram Moolenaarf4258302013-06-02 18:20:17 +02007181/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007182 * Get the previous directory for the given chdir scope.
7183 */
7184 static char_u *
7185get_prevdir(cdscope_T scope)
7186{
7187 if (scope == CDSCOPE_WINDOW)
7188 return curwin->w_prevdir;
7189 else if (scope == CDSCOPE_TABPAGE)
7190 return curtab->tp_prevdir;
7191 return prev_dir;
7192}
7193
7194/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007195 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007196 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7197 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007198 */
7199 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007200post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007201{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007202 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007203 // Clear tab local directory for both :cd and :tcd
7204 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007205 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007206 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007207 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007208 char_u *pdir = get_prevdir(scope);
7209
7210 // If still in the global directory, need to remember current
7211 // directory as the global directory.
7212 if (globaldir == NULL && pdir != NULL)
7213 globaldir = vim_strsave(pdir);
7214
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007215 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007216 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007217 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007218 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007219 curtab->tp_localdir = vim_strsave(NameBuff);
7220 else
7221 curwin->w_localdir = vim_strsave(NameBuff);
7222 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007223 }
7224 else
7225 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007226 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007227 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007228 }
7229
7230 shorten_fnames(TRUE);
7231}
7232
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007233/*
7234 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007235 * chdir() function.
7236 * scope == CDSCOPE_WINDOW: changes the window-local directory
7237 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7238 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007239 * Returns TRUE if the directory is successfully changed.
7240 */
7241 int
7242changedir_func(
7243 char_u *new_dir,
7244 int forceit,
7245 cdscope_T scope)
7246{
7247 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007248 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007249 int dir_differs;
7250 int retval = FALSE;
7251
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007252 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007253 return FALSE;
7254
7255 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7256 {
7257 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7258 return FALSE;
7259 }
7260
7261 // ":cd -": Change to previous directory
7262 if (STRCMP(new_dir, "-") == 0)
7263 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007264 pdir = get_prevdir(scope);
7265 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007266 {
7267 emsg(_("E186: No previous directory"));
7268 return FALSE;
7269 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007270 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007271 }
7272
Bram Moolenaar002bc792020-06-05 22:33:42 +02007273 // Free the previous directory
7274 tofree = get_prevdir(scope);
7275
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007276 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007277 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007278 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007279 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007280 pdir = NULL;
7281 if (scope == CDSCOPE_WINDOW)
7282 curwin->w_prevdir = pdir;
7283 else if (scope == CDSCOPE_TABPAGE)
7284 curtab->tp_prevdir = pdir;
7285 else
7286 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007287
7288#if defined(UNIX) || defined(VMS)
7289 // for UNIX ":cd" means: go to home directory
7290 if (*new_dir == NUL)
7291 {
7292 // use NameBuff for home directory name
7293# ifdef VMS
7294 char_u *p;
7295
7296 p = mch_getenv((char_u *)"SYS$LOGIN");
7297 if (p == NULL || *p == NUL) // empty is the same as not set
7298 NameBuff[0] = NUL;
7299 else
7300 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7301# else
7302 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7303# endif
7304 new_dir = NameBuff;
7305 }
7306#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007307 dir_differs = new_dir == NULL || pdir == NULL
7308 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007309 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7310 emsg(_(e_failed));
7311 else
7312 {
7313 char_u *acmd_fname;
7314
7315 post_chdir(scope);
7316
7317 if (dir_differs)
7318 {
7319 if (scope == CDSCOPE_WINDOW)
7320 acmd_fname = (char_u *)"window";
7321 else if (scope == CDSCOPE_TABPAGE)
7322 acmd_fname = (char_u *)"tabpage";
7323 else
7324 acmd_fname = (char_u *)"global";
7325 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7326 curbuf);
7327 }
7328 retval = TRUE;
7329 }
7330 vim_free(tofree);
7331
7332 return retval;
7333}
Bram Moolenaarea408852005-06-25 22:49:46 +00007334
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007336 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007338 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007339ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007341 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342
7343 new_dir = eap->arg;
7344#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007345 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 if (*new_dir == NUL)
7347 ex_pwd(NULL);
7348 else
7349#endif
7350 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007351 cdscope_T scope = CDSCOPE_GLOBAL;
7352
7353 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7354 scope = CDSCOPE_WINDOW;
7355 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7356 scope = CDSCOPE_TABPAGE;
7357
7358 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007359 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007360 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007361 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 ex_pwd(eap);
7363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 }
7365}
7366
7367/*
7368 * ":pwd".
7369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007371ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372{
7373 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7374 {
7375#ifdef BACKSLASH_IN_FILENAME
7376 slash_adjust(NameBuff);
7377#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007378 if (p_verbose > 0)
7379 {
7380 char *context = "global";
7381
7382 if (curwin->w_localdir != NULL)
7383 context = "window";
7384 else if (curtab->tp_localdir != NULL)
7385 context = "tabpage";
7386 smsg("[%s] %s", context, (char *)NameBuff);
7387 }
7388 else
7389 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 }
7391 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007392 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393}
7394
7395/*
7396 * ":=".
7397 */
7398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007399ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007401 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007402 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403}
7404
7405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007406ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007408 int n;
7409 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410
7411 if (cursor_valid())
7412 {
7413 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7414 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007415 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007417
7418 len = eap->line2;
7419 switch (*eap->arg)
7420 {
7421 case 'm': break;
7422 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007423 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007424 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007425
7426 // Hide the cursor if invoked with !
7427 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428}
7429
7430/*
7431 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007432 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 */
7434 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007435do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
Bram Moolenaar52953192019-08-16 10:27:13 +02007437 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007438 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007439# ifdef ELAPSED_FUNC
7440 elapsed_T start_tv;
7441
7442 // Remember at what time we started, so that we know how much longer we
7443 // should wait after waiting for a bit.
7444 ELAPSED_INIT(start_tv);
7445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007447 if (hide_cursor)
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007448 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007449 else
7450 cursor_on();
7451
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007452 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007453 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007455 wait_now = msec - done > 1000L ? 1000L : msec - done;
7456#ifdef FEAT_TIMERS
7457 {
7458 long due_time = check_due_timer();
7459
7460 if (due_time > 0 && due_time < wait_now)
7461 wait_now = due_time;
7462 }
7463#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007464#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007465 if (has_any_channel() && wait_now > 20L)
7466 wait_now = 20L;
7467#endif
7468#ifdef FEAT_SOUND
7469 if (has_any_sound_callback() && wait_now > 20L)
7470 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007471#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007472 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007473
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007474#ifdef FEAT_JOB_CHANNEL
7475 if (has_any_channel())
7476 ui_breakcheck_force(TRUE);
7477 else
7478#endif
7479 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007480#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007481 // Process the netbeans and clientserver messages that may have been
7482 // received in the call to ui_breakcheck() when the GUI is in use. This
7483 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007484 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007485#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007486
7487# ifdef ELAPSED_FUNC
7488 // actual time passed
7489 done = ELAPSED_FUNC(start_tv);
7490# else
7491 // guestimate time passed (will actually be more)
7492 done += wait_now;
7493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007495
7496 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7497 // input buffer, otherwise a following call to input() fails.
7498 if (got_int)
7499 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007500
7501 if (hide_cursor)
7502 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503}
7504
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505/*
7506 * ":winsize" command (obsolete).
7507 */
7508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007509ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510{
7511 int w, h;
7512 char_u *arg = eap->arg;
7513 char_u *p;
7514
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007515 if (!isdigit(*arg))
7516 {
7517 semsg(_(e_invarg2), arg);
7518 return;
7519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 w = getdigits(&arg);
7521 arg = skipwhite(arg);
7522 p = arg;
7523 h = getdigits(&arg);
7524 if (*p != NUL && *arg == NUL)
7525 set_shellsize(w, h, TRUE);
7526 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007527 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528}
7529
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007531ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532{
7533 int xchar = NUL;
7534 char_u *p;
7535
7536 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7537 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007538 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 if (eap->arg[1] == NUL)
7540 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007541 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 return;
7543 }
7544 xchar = eap->arg[1];
7545 p = eap->arg + 2;
7546 }
7547 else
7548 p = eap->arg + 1;
7549
7550 eap->nextcmd = check_nextcmd(p);
7551 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007552 if (*p != NUL && *p != (
7553#ifdef FEAT_EVAL
7554 in_vim9script() ? '#' :
7555#endif
7556 '"')
7557 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007558 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007559 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007561 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007562 postponed_split_flags = cmdmod.cmod_split;
7563 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7565 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007566 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 }
7568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569
Bram Moolenaar843ee412004-06-30 16:16:41 +00007570#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571/*
7572 * ":winpos".
7573 */
7574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007575ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576{
7577 int x, y;
7578 char_u *arg = eap->arg;
7579 char_u *p;
7580
7581 if (*arg == NUL)
7582 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007583# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007584# ifdef VIMDLL
7585 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7586 mch_get_winpos(&x, &y) != FAIL)
7587# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007589# else
7590 if (mch_get_winpos(&x, &y) != FAIL)
7591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 {
7593 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007594 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596 else
7597# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007598 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 }
7600 else
7601 {
7602 x = getdigits(&arg);
7603 arg = skipwhite(arg);
7604 p = arg;
7605 y = getdigits(&arg);
7606 if (*p == NUL || *arg != NUL)
7607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007608 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 return;
7610 }
7611# ifdef FEAT_GUI
7612 if (gui.in_use)
7613 gui_mch_set_winpos(x, y);
7614 else if (gui.starting)
7615 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007616 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 gui_win_x = x;
7618 gui_win_y = y;
7619 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007620# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 else
7622# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007623# endif
7624# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007625 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626# endif
7627# ifdef HAVE_TGETENT
7628 if (*T_CWP)
7629 term_set_winpos(x, y);
7630# endif
7631 }
7632}
7633#endif
7634
7635/*
7636 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7637 */
7638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007639ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
7641 oparg_T oa;
7642
7643 clear_oparg(&oa);
7644 oa.regname = eap->regname;
7645 oa.start.lnum = eap->line1;
7646 oa.end.lnum = eap->line2;
7647 oa.line_count = eap->line2 - eap->line1 + 1;
7648 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007650 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
7652 setpcmark();
7653 curwin->w_cursor.lnum = eap->line1;
7654 beginline(BL_SOL | BL_FIX);
7655 }
7656
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007657 if (VIsual_active)
7658 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007659
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 switch (eap->cmdidx)
7661 {
7662 case CMD_delete:
7663 oa.op_type = OP_DELETE;
7664 op_delete(&oa);
7665 break;
7666
7667 case CMD_yank:
7668 oa.op_type = OP_YANK;
7669 (void)op_yank(&oa, FALSE, TRUE);
7670 break;
7671
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007672 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007673 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007675 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7676#else
7677 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007679 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 oa.op_type = OP_RSHIFT;
7681 else
7682 oa.op_type = OP_LSHIFT;
7683 op_shift(&oa, FALSE, eap->amount);
7684 break;
7685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007687 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688}
7689
7690/*
7691 * ":put".
7692 */
7693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007694ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007696 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 if (eap->line2 == 0)
7698 {
7699 eap->line2 = 1;
7700 eap->forceit = TRUE;
7701 }
7702 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007703 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007704 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705}
7706
7707/*
7708 * Handle ":copy" and ":move".
7709 */
7710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007711ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 long n;
7714
Bram Moolenaar491799b2020-08-01 19:23:43 +02007715#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007716 if (not_in_vim9(eap) == FAIL)
7717 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007718#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007719 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007720 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {
7722 eap->nextcmd = NULL;
7723 return;
7724 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007725 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726
7727 /*
7728 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7729 */
7730 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7731 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007732 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 return;
7734 }
7735
7736 if (eap->cmdidx == CMD_move)
7737 {
7738 if (do_move(eap->line1, eap->line2, n) == FAIL)
7739 return;
7740 }
7741 else
7742 ex_copy(eap->line1, eap->line2, n);
7743 u_clearline();
7744 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007745 ex_may_print(eap);
7746}
7747
7748/*
7749 * Print the current line if flags were given to the Ex command.
7750 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007751 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007752ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007753{
7754 if (eap->flags != 0)
7755 {
7756 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7757 (eap->flags & EXFLAG_LIST));
7758 ex_no_reprint = TRUE;
7759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760}
7761
7762/*
7763 * ":smagic" and ":snomagic".
7764 */
7765 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007766ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007768 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007770 magic_overruled = eap->cmdidx == CMD_smagic
7771 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007772 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007773 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774}
7775
7776/*
7777 * ":join".
7778 */
7779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007780ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
7782 curwin->w_cursor.lnum = eap->line1;
7783 if (eap->line1 == eap->line2)
7784 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007785 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 return;
7787 if (eap->line2 == curbuf->b_ml.ml_line_count)
7788 {
7789 beep_flush();
7790 return;
7791 }
7792 ++eap->line2;
7793 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007794 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007796 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797}
7798
7799/*
7800 * ":[addr]@r" or ":[addr]*r": execute register
7801 */
7802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007803ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
7805 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007806 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807
7808 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007809 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810
7811#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007812 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813#endif
7814
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007815 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 c = *eap->arg;
7817 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7818 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007819 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007820 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7821 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007822 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 else
7826 {
7827 int save_efr = exec_from_reg;
7828
7829 exec_from_reg = TRUE;
7830
7831 /*
7832 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007833 * Continue until the stuff buffer is empty and all added characters
7834 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007836 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7838
7839 exec_from_reg = save_efr;
7840 }
7841}
7842
7843/*
7844 * ":!".
7845 */
7846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007847ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848{
7849 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7850}
7851
7852/*
7853 * ":undo".
7854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007856ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007858 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007859 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007860 else
7861 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862}
7863
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007864#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007866ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007867{
7868 char_u hash[UNDO_HASH_SIZE];
7869
7870 u_compute_hash(hash);
7871 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7872}
7873
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007875ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007876{
7877 char_u hash[UNDO_HASH_SIZE];
7878
7879 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007880 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007881}
7882#endif
7883
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884/*
7885 * ":redo".
7886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007888ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
7890 u_redo(1);
7891}
7892
7893/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007894 * ":earlier" and ":later".
7895 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007896 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007897ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007898{
7899 long count = 0;
7900 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007901 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007902 char_u *p = eap->arg;
7903
7904 if (*p == NUL)
7905 count = 1;
7906 else if (isdigit(*p))
7907 {
7908 count = getdigits(&p);
7909 switch (*p)
7910 {
7911 case 's': ++p; sec = TRUE; break;
7912 case 'm': ++p; sec = TRUE; count *= 60; break;
7913 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007914 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7915 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007916 }
7917 }
7918
7919 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007920 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007921 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007922 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7923 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007924}
7925
7926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 * ":redir": start/stop redirection.
7928 */
7929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007930ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
7932 char *mode;
7933 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007934 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935
Bram Moolenaarba768492016-07-08 15:32:54 +02007936#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007937 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007938 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007939 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007940 return;
7941 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007942#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007943
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 if (STRICMP(eap->arg, "END") == 0)
7945 close_redir();
7946 else
7947 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007948 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007950 ++arg;
7951 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007953 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 mode = "a";
7955 }
7956 else
7957 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007958 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959
7960 close_redir();
7961
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007962 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007963 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 if (fname == NULL)
7965 return;
7966#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007967 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {
7969 char_u *browseFile;
7970
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007971 browseFile = do_browse(BROWSE_SAVE,
7972 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007973 fname, NULL, NULL,
7974 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007976 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 vim_free(fname);
7978 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007979 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 }
7981#endif
7982
7983 redir_fd = open_exfile(fname, eap->forceit, mode);
7984 vim_free(fname);
7985 }
7986#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007987 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007989 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007991 ++arg;
7992 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007994 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007995 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007997 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007999 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008000 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008001 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008004 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008005 if (*arg == '>')
8006 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008007 // Make register empty when not using @A-@Z and the
8008 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008009 if (*arg == NUL && !isupper(redir_reg))
8010 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008012 }
8013 if (*arg != NUL)
8014 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008015 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008016 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008017 }
8018 }
8019 else if (*arg == '=' && arg[1] == '>')
8020 {
8021 int append;
8022
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008023 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008024 close_redir();
8025 arg += 2;
8026
8027 if (*arg == '>')
8028 {
8029 ++arg;
8030 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 }
8032 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008033 append = FALSE;
8034
8035 if (var_redir_start(skipwhite(arg), append) == OK)
8036 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 }
8038#endif
8039
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008040 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008043 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008045
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008046 // Make sure redirection is not off. Can happen for cmdline completion
8047 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008048 if (redir_fd != NULL
8049#ifdef FEAT_EVAL
8050 || redir_reg || redir_vname
8051#endif
8052 )
8053 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054}
8055
8056/*
8057 * ":redraw": force redraw
8058 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008059 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008060ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
8062 int r = RedrawingDisabled;
8063 int p = p_lz;
8064
8065 RedrawingDisabled = 0;
8066 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008067 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008069 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070#ifdef FEAT_TITLE
8071 if (need_maketitle)
8072 maketitle();
8073#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008074#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8075# ifdef VIMDLL
8076 if (!gui.in_use)
8077# endif
8078 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 RedrawingDisabled = r;
8081 p_lz = p;
8082
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008083 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 msg_didout = FALSE;
8085 msg_col = 0;
8086
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008087 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008088 need_wait_return = FALSE;
8089
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 out_flush();
8091}
8092
8093/*
8094 * ":redrawstatus": force redraw of status line(s)
8095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008097ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 int r = RedrawingDisabled;
8100 int p = p_lz;
8101
8102 RedrawingDisabled = 0;
8103 p_lz = FALSE;
8104 if (eap->forceit)
8105 status_redraw_all();
8106 else
8107 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008108 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 RedrawingDisabled = r;
8110 p_lz = p;
8111 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112}
8113
Bram Moolenaare12bab32019-01-08 22:02:56 +01008114/*
8115 * ":redrawtabline": force redraw of the tabline
8116 */
8117 static void
8118ex_redrawtabline(exarg_T *eap UNUSED)
8119{
8120 int r = RedrawingDisabled;
8121 int p = p_lz;
8122
8123 RedrawingDisabled = 0;
8124 p_lz = FALSE;
8125
8126 draw_tabline();
8127
8128 RedrawingDisabled = r;
8129 p_lz = p;
8130 out_flush();
8131}
8132
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008134close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135{
8136 if (redir_fd != NULL)
8137 {
8138 fclose(redir_fd);
8139 redir_fd = NULL;
8140 }
8141#ifdef FEAT_EVAL
8142 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008143 if (redir_vname)
8144 {
8145 var_redir_stop();
8146 redir_vname = 0;
8147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148#endif
8149}
8150
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008151#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008152 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008153vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008154{
8155 if (vim_mkdir(name, prot) != 0)
8156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008157 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008158 return FAIL;
8159 }
8160 return OK;
8161}
8162#endif
8163
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164/*
8165 * Open a file for writing for an Ex command, with some checks.
8166 * Return file descriptor, or NULL on failure.
8167 */
8168 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008169open_exfile(
8170 char_u *fname,
8171 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008172 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173{
8174 FILE *fd;
8175
8176#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008177 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 if (mch_isdir(fname))
8179 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008180 semsg(_(e_src_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 return NULL;
8182 }
8183#endif
8184 if (!forceit && *mode != 'a' && vim_fexists(fname))
8185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008186 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 return NULL;
8188 }
8189
8190 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008191 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192
8193 return fd;
8194}
8195
8196/*
8197 * ":mark" and ":k".
8198 */
8199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008200ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201{
8202 pos_T pos;
8203
Bram Moolenaar10b94212021-02-19 21:42:57 +01008204#ifdef FEAT_EVAL
8205 if (not_in_vim9(eap) == FAIL)
8206 return;
8207#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008208 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008209 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008210 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008211 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 else
8213 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008214 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 curwin->w_cursor.lnum = eap->line2;
8216 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008217 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008218 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008219 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 }
8221}
8222
8223/*
8224 * Update w_topline, w_leftcol and the cursor position.
8225 */
8226 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008227update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008229 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 update_topline();
8231 if (!curwin->w_p_wrap)
8232 validate_cursor();
8233 update_curswant();
8234}
8235
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008237 * Save the current State and go to Normal mode.
8238 * Return TRUE if the typeahead could be saved.
8239 */
8240 int
8241save_current_state(save_state_T *sst)
8242{
8243 sst->save_msg_scroll = msg_scroll;
8244 sst->save_restart_edit = restart_edit;
8245 sst->save_msg_didout = msg_didout;
8246 sst->save_State = State;
8247 sst->save_insertmode = p_im;
8248 sst->save_finish_op = finish_op;
8249 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008250 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008251
Bram Moolenaar4324d872020-12-01 20:12:24 +01008252 msg_scroll = FALSE; // no msg scrolling in Normal mode
8253 restart_edit = 0; // don't go to Insert mode
8254 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008255
Bram Moolenaara452b802020-12-01 21:47:59 +01008256 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008257 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008258
8259 /*
8260 * Save the current typeahead. This is required to allow using ":normal"
8261 * from an event handler and makes sure we don't hang when the argument
8262 * ends with half a command.
8263 */
8264 save_typeahead(&sst->tabuf);
8265 return sst->tabuf.typebuf_valid;
8266}
8267
8268 void
8269restore_current_state(save_state_T *sst)
8270{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008271 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008272 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008273
8274 msg_scroll = sst->save_msg_scroll;
8275 restart_edit = sst->save_restart_edit;
8276 p_im = sst->save_insertmode;
8277 finish_op = sst->save_finish_op;
8278 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008279 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008280 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008281 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008282
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008283 // Restore the state (needed when called from a function executed for
8284 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008285 State = sst->save_State;
8286#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008287 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008288#endif
8289}
8290
8291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 * ":normal[!] {commands}": Execute normal mode commands.
8293 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008294 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008295ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008297 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 char_u *arg = NULL;
8299 int l;
8300 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008302 if (ex_normal_lock > 0)
8303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008304 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008305 return;
8306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 if (ex_normal_busy >= p_mmd)
8308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008309 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 return;
8311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 /*
8314 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8315 * this for the K_SPECIAL leading byte, otherwise special keys will not
8316 * work.
8317 */
8318 if (has_mbyte)
8319 {
8320 int len = 0;
8321
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008322 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 for (p = eap->arg; *p != NUL; ++p)
8324 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008325#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008326 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008328#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008329 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008330 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008331#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 )
8335 len += 2;
8336 }
8337 if (len > 0)
8338 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008339 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 if (arg != NULL)
8341 {
8342 len = 0;
8343 for (p = eap->arg; *p != NUL; ++p)
8344 {
8345 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008346#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 if (*p == CSI)
8348 {
8349 arg[len++] = KS_EXTRA;
8350 arg[len++] = (int)KE_CSI;
8351 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008352#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008353 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {
8355 arg[len++] = *++p;
8356 if (*p == K_SPECIAL)
8357 {
8358 arg[len++] = KS_SPECIAL;
8359 arg[len++] = KE_FILLER;
8360 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008361#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 else if (*p == CSI)
8363 {
8364 arg[len++] = KS_EXTRA;
8365 arg[len++] = (int)KE_CSI;
8366 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 }
8369 arg[len] = NUL;
8370 }
8371 }
8372 }
8373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008375 ++ex_normal_busy;
8376 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {
8378 /*
8379 * Repeat the :normal command for each line in the range. When no
8380 * range given, execute it just once, without positioning the cursor
8381 * first.
8382 */
8383 do
8384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 if (eap->addr_count != 0)
8386 {
8387 curwin->w_cursor.lnum = eap->line1++;
8388 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008389 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 }
8391
Bram Moolenaar13505972019-01-24 15:04:48 +01008392 exec_normal_cmd(arg != NULL
8393 ? arg
8394 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 }
8396 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8397 }
8398
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008399 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 update_topline_cursor();
8401
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008402 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008404 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008405#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008406 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008407#endif
8408
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410}
8411
8412/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008413 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 */
8415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008416ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008418 if (eap->forceit)
8419 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008420 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008421 if (!curwin->w_cursor.lnum)
8422 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008423 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008424 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008425#ifdef FEAT_TERMINAL
8426 // Ignore this when running in an active terminal.
8427 if (term_job_running(curbuf->b_term))
8428 return;
8429#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008430
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008431 // Ignore the command when already in Insert mode. Inserting an
8432 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008433 if (State & INSERT)
8434 return;
8435
Bram Moolenaar64969662005-12-14 21:59:55 +00008436 if (eap->cmdidx == CMD_startinsert)
8437 restart_edit = 'a';
8438 else if (eap->cmdidx == CMD_startreplace)
8439 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008441 restart_edit = 'V';
8442
8443 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008445 if (eap->cmdidx == CMD_startinsert)
8446 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008447 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008449
8450 if (VIsual_active)
8451 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452}
8453
8454/*
8455 * ":stopinsert"
8456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008458ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459{
8460 restart_edit = 0;
8461 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008462 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008465/*
8466 * Execute normal mode command "cmd".
8467 * "remap" can be REMAP_NONE or REMAP_YES.
8468 */
8469 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008470exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008471{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008472 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008473 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008474 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008475}
Bram Moolenaar25281632016-01-21 23:32:32 +01008476
Bram Moolenaar25281632016-01-21 23:32:32 +01008477/*
8478 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008479 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008480 */
8481 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008482exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008483{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008484 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008485 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008486
Bram Moolenaar905dd902019-04-07 14:21:47 +02008487 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8488 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008489 clear_oparg(&oa);
8490 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008491 while ((!stuff_empty()
8492 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008493 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008494 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008495 {
8496 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008497#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008498 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008499 && oa.op_type == OP_NOP && oa.regname == NUL
8500 && !VIsual_active)
8501 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008502 // If terminal_loop() returns OK we got a key that is handled
8503 // in Normal model. With FAIL we first need to position the
8504 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008505 if (terminal_loop(TRUE) == OK)
8506 normal_cmd(&oa, TRUE);
8507 }
8508 else
8509#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008510 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008511 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008512 }
8513}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008514
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515#ifdef FEAT_FIND_ID
8516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008517ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518{
8519 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8520 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8521 (linenr_T)1, (linenr_T)MAXLNUM);
8522}
8523
Bram Moolenaar4033c552017-09-16 20:54:51 +02008524#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525/*
8526 * ":psearch"
8527 */
8528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008529ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 g_do_tagpreview = p_pvh;
8532 ex_findpat(eap);
8533 g_do_tagpreview = 0;
8534}
8535#endif
8536
8537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008538ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539{
8540 int whole = TRUE;
8541 long n;
8542 char_u *p;
8543 int action;
8544
8545 switch (cmdnames[eap->cmdidx].cmd_name[2])
8546 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008547 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8549 action = ACTION_GOTO;
8550 else
8551 action = ACTION_SHOW;
8552 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008553 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 action = ACTION_SHOW_ALL;
8555 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008556 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 action = ACTION_GOTO;
8558 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008559 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 action = ACTION_SPLIT;
8561 break;
8562 }
8563
8564 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008565 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 {
8567 n = getdigits(&eap->arg);
8568 eap->arg = skipwhite(eap->arg);
8569 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008570 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 {
8572 whole = FALSE;
8573 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008574 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 if (*p)
8576 {
8577 *p++ = NUL;
8578 p = skipwhite(p);
8579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008580 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008581 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008582 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 else
8584 eap->nextcmd = check_nextcmd(p);
8585 }
8586 }
8587 if (!eap->skip)
8588 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8589 whole, !eap->forceit,
8590 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8591 n, action, eap->line1, eap->line2);
8592}
8593#endif
8594
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595
Bram Moolenaar4033c552017-09-16 20:54:51 +02008596#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597/*
8598 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8599 */
8600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008601ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008603 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8605}
8606
8607/*
8608 * ":pedit"
8609 */
8610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008611ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612{
8613 win_T *curwin_save = curwin;
8614
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008615 if (ERROR_IF_ANY_POPUP_WINDOW)
8616 return;
8617
Bram Moolenaar026587b2019-08-17 15:08:00 +02008618 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008620 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008621
Bram Moolenaar026587b2019-08-17 15:08:00 +02008622 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008624
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 if (curwin != curwin_save && win_valid(curwin_save))
8626 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008627 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 validate_cursor();
8629 redraw_later(VALID);
8630 win_enter(curwin_save, TRUE);
8631 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008632# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008633 else if (WIN_IS_POPUP(curwin))
8634 {
8635 // can't keep focus in popup window
8636 win_enter(firstwin, TRUE);
8637 }
8638# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 g_do_tagpreview = 0;
8640}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642
8643/*
8644 * ":stag", ":stselect" and ":stjump".
8645 */
8646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008647ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648{
8649 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008650 postponed_split_flags = cmdmod.cmod_split;
8651 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8653 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008654 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656
8657/*
8658 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8659 */
8660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008661ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662{
8663 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8664}
8665
8666 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008667ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668{
8669 int cmd;
8670
8671 switch (name[1])
8672 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008673 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008675 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008677 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008679 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008681 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008683 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008685 case 'f': // ":tfirst"
8686 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008688 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008690 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008692 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008694 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 return;
8696 }
8697#endif
8698 cmd = DT_TAG;
8699 break;
8700 }
8701
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008702 if (name[0] == 'l')
8703 {
8704#ifndef FEAT_QUICKFIX
8705 ex_ni(eap);
8706 return;
8707#else
8708 cmd = DT_LTAG;
8709#endif
8710 }
8711
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8713 eap->forceit, TRUE);
8714}
8715
8716/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008717 * Check "str" for starting with a special cmdline variable.
8718 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8719 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8720 */
8721 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008722find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008723{
8724 int len;
8725 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008726 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008727 "%",
8728#define SPEC_PERC 0
8729 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008730#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008731 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008732#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008733 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008734#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008735 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008736#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008737 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008738#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008739 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008740#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008741 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008742#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008743 "<stack>", // call stack
8744#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008745 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008746#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008747 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008748#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008749 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008750#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008751 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008752#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008753 "<SID>", // script ID: <SNR>123_
8754#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008755#ifdef FEAT_CLIENTSERVER
8756 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008757# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008758#endif
8759 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008760
K.Takataeeec2542021-06-02 13:28:16 +02008761 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008762 {
8763 len = (int)STRLEN(spec_str[i]);
8764 if (STRNCMP(src, spec_str[i], len) == 0)
8765 {
8766 *usedlen = len;
8767 return i;
8768 }
8769 }
8770 return -1;
8771}
8772
8773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 * Evaluate cmdline variables.
8775 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008776 * change "%" to curbuf->b_ffname
8777 * "#" to curwin->w_alt_fnum
8778 * "%%" to curwin->w_alt_fnum in Vim9 script
8779 * "<cword>" to word under the cursor
8780 * "<cWORD>" to WORD under the cursor
8781 * "<cexpr>" to C-expression under the cursor
8782 * "<cfile>" to path name under the cursor
8783 * "<sfile>" to sourced file name
8784 * "<stack>" to call stack
8785 * "<slnum>" to sourced file line number
8786 * "<afile>" to file name for autocommand
8787 * "<abuf>" to buffer number for autocommand
8788 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 *
8790 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8791 * "" for error without a message) and NULL is returned.
8792 * Returns an allocated string if a valid match was found.
8793 * Returns NULL if no match was found. "usedlen" then still contains the
8794 * number of characters to skip.
8795 */
8796 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008797eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008798 char_u *src, // pointer into commandline
8799 char_u *srcstart, // beginning of valid memory for src
8800 int *usedlen, // characters after src that are used
8801 linenr_T *lnump, // line number for :e command, or NULL
8802 char **errormsg, // pointer to error message
8803 int *escaped) // return value has escaped white space (can
8804 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
8806 int i;
8807 char_u *s;
8808 char_u *result;
8809 char_u *resultbuf = NULL;
8810 int resultlen;
8811 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008812 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008814 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817
8818 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008819 if (escaped != NULL)
8820 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821
8822 /*
8823 * Check if there is something to do.
8824 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008825 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008826 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 {
8828 *usedlen = 1;
8829 return NULL;
8830 }
8831
8832 /*
8833 * Skip when preceded with a backslash "\%" and "\#".
8834 * Note: In "\\%" the % is also not recognized!
8835 */
8836 if (src > srcstart && src[-1] == '\\')
8837 {
8838 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008839 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 return NULL;
8841 }
8842
8843 /*
8844 * word or WORD under cursor
8845 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008846 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8847 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008849 resultlen = find_ident_under_cursor(&result,
8850 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8851 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8852 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 if (resultlen == 0)
8854 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008855 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 return NULL;
8857 }
8858 }
8859
8860 /*
8861 * '#': Alternate file name
8862 * '%': Current file name
8863 * File name under the cursor
8864 * File name for autocommand
8865 * and following modifiers
8866 */
8867 else
8868 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008869 int off = 0;
8870
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 switch (spec_idx)
8872 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008873 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008874#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008875 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008878 // '%': current file
8879 if (curbuf->b_fname == NULL)
8880 {
8881 result = (char_u *)"";
8882 valid = 0; // Must have ":p:h" to be valid
8883 }
8884 else
8885 {
8886 result = curbuf->b_fname;
8887 tilde_file = STRCMP(result, "~") == 0;
8888 }
8889 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008891#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008892 // "%%" alternate file
8893 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008894#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008895 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008896 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008897 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008899 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 result = arg_all();
8901 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008902 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008903 if (escaped != NULL)
8904 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 break;
8907 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008908 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008909 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008910 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008912 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008913 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008914 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008915 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008917 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008919 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00008920 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008921 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008922 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008923 return NULL;
8924 }
8925#ifdef FEAT_EVAL
8926 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8927 (long)i);
8928 if (result == NULL)
8929 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008930 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008931 return NULL;
8932 }
8933#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008934 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 }
8938 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008939 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008940 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
8941 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008942 buf = buflist_findnr(i);
8943 if (buf == NULL)
8944 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008945 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008946 return NULL;
8947 }
8948 if (lnump != NULL)
8949 *lnump = ECMD_LAST;
8950 if (buf->b_fname == NULL)
8951 {
8952 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008953 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008954 }
8955 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008956 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008957 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008958 tilde_file = STRCMP(result, "~") == 0;
8959 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 break;
8962
8963#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008964 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008965 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 if (result == NULL)
8967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008968 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 return NULL;
8970 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008971 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 break;
8973#endif
8974
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008975 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008977 if (result != NULL && !autocmd_fname_full)
8978 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008979 // Still need to turn the fname into a full path. It is
8980 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008981 autocmd_fname_full = TRUE;
8982 result = FullName_save(autocmd_fname, FALSE);
8983 vim_free(autocmd_fname);
8984 autocmd_fname = result;
8985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 if (result == NULL)
8987 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008988 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 return NULL;
8990 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008991 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 break;
8993
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008994 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 if (autocmd_bufnr <= 0)
8996 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008997 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 return NULL;
8999 }
9000 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9001 result = strbuf;
9002 break;
9003
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009004 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 result = autocmd_match;
9006 if (result == NULL)
9007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009008 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 return NULL;
9010 }
9011 break;
9012
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009013 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02009014 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02009015 result = estack_sfile(spec_idx == SPEC_SFILE
9016 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 if (result == NULL)
9018 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02009019 *errormsg = spec_idx == SPEC_SFILE
9020 ? _("E498: no :source file name to substitute for \"<sfile>\"")
9021 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 return NULL;
9023 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009024 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009026
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009027 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009028 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009029 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009030 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009031 return NULL;
9032 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009033 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009034 result = strbuf;
9035 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009036
9037#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009038 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009039 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009041 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009042 return NULL;
9043 }
9044 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009045 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009046 result = strbuf;
9047 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009048
Bram Moolenaar90944302020-08-01 20:45:11 +02009049 case SPEC_SID:
9050 if (current_sctx.sc_sid <= 0)
9051 {
9052 *errormsg = _(e_usingsid);
9053 return NULL;
9054 }
9055 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9056 result = strbuf;
9057 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009058#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009059
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009060#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009061 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009062 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9063 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 result = strbuf;
9065 break;
9066#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009067
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009068 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009069 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009070 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 }
9072
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009073 resultlen = (int)STRLEN(result); // length of new string
9074 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 {
9076 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 resultlen = (int)(s - result);
9079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 else if (!skip_mod)
9081 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009082 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 &resultlen);
9084 if (result == NULL)
9085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009086 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 return NULL;
9088 }
9089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 }
9091
9092 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9093 {
9094 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009095 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009096 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009098 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 result = NULL;
9100 }
9101 else
9102 result = vim_strnsave(result, resultlen);
9103 vim_free(resultbuf);
9104 return result;
9105}
9106
9107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 * Expand the <sfile> string in "arg".
9109 *
9110 * Returns an allocated string, or NULL for any error.
9111 */
9112 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009113expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009115 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 int len;
9117 char_u *result;
9118 char_u *newres;
9119 char_u *repl;
9120 int srclen;
9121 char_u *p;
9122
9123 result = vim_strsave(arg);
9124 if (result == NULL)
9125 return NULL;
9126
9127 for (p = result; *p; )
9128 {
9129 if (STRNCMP(p, "<sfile>", 7) != 0)
9130 ++p;
9131 else
9132 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009133 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009134 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 if (errormsg != NULL)
9136 {
9137 if (*errormsg)
9138 emsg(errormsg);
9139 vim_free(result);
9140 return NULL;
9141 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009142 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 {
9144 p += srclen;
9145 continue;
9146 }
9147 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9148 newres = alloc(len);
9149 if (newres == NULL)
9150 {
9151 vim_free(repl);
9152 vim_free(result);
9153 return NULL;
9154 }
9155 mch_memmove(newres, result, (size_t)(p - result));
9156 STRCPY(newres + (p - result), repl);
9157 len = (int)STRLEN(newres);
9158 STRCAT(newres, p + srclen);
9159 vim_free(repl);
9160 vim_free(result);
9161 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009162 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 }
9164 }
9165
9166 return result;
9167}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009170/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009171 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009172 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009175dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 if (fname == NULL)
9178 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009179 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180}
9181#endif
9182
9183/*
9184 * ":behave {mswin,xterm}"
9185 */
9186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009187ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188{
9189 if (STRCMP(eap->arg, "mswin") == 0)
9190 {
9191 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9192 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9193 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9194 set_option_value((char_u *)"keymodel", 0L,
9195 (char_u *)"startsel,stopsel", 0);
9196 }
9197 else if (STRCMP(eap->arg, "xterm") == 0)
9198 {
9199 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9200 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9201 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9202 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9203 }
9204 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009205 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206}
9207
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208static int filetype_detect = FALSE;
9209static int filetype_plugin = FALSE;
9210static int filetype_indent = FALSE;
9211
9212/*
9213 * ":filetype [plugin] [indent] {on,off,detect}"
9214 * on: Load the filetype.vim file to install autocommands for file types.
9215 * off: Load the ftoff.vim file to remove all autocommands for file types.
9216 * plugin on: load filetype.vim and ftplugin.vim
9217 * plugin off: load ftplugof.vim
9218 * indent on: load filetype.vim and indent.vim
9219 * indent off: load indoff.vim
9220 */
9221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009222ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224 char_u *arg = eap->arg;
9225 int plugin = FALSE;
9226 int indent = FALSE;
9227
9228 if (*eap->arg == NUL)
9229 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009230 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009231 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 filetype_detect ? "ON" : "OFF",
9233 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9234 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9235 return;
9236 }
9237
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009238 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 for (;;)
9240 {
9241 if (STRNCMP(arg, "plugin", 6) == 0)
9242 {
9243 plugin = TRUE;
9244 arg = skipwhite(arg + 6);
9245 continue;
9246 }
9247 if (STRNCMP(arg, "indent", 6) == 0)
9248 {
9249 indent = TRUE;
9250 arg = skipwhite(arg + 6);
9251 continue;
9252 }
9253 break;
9254 }
9255 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9256 {
9257 if (*arg == 'o' || !filetype_detect)
9258 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009259 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 filetype_detect = TRUE;
9261 if (plugin)
9262 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009263 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 filetype_plugin = TRUE;
9265 }
9266 if (indent)
9267 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009268 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 filetype_indent = TRUE;
9270 }
9271 }
9272 if (*arg == 'd')
9273 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009274 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009275 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 }
9277 }
9278 else if (STRCMP(arg, "off") == 0)
9279 {
9280 if (plugin || indent)
9281 {
9282 if (plugin)
9283 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009284 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 filetype_plugin = FALSE;
9286 }
9287 if (indent)
9288 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009289 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 filetype_indent = FALSE;
9291 }
9292 }
9293 else
9294 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009295 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 filetype_detect = FALSE;
9297 }
9298 }
9299 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009300 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301}
9302
9303/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009304 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 */
9306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009307ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308{
9309 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009310 {
9311 char_u *arg = eap->arg;
9312
9313 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9314 arg += 9;
9315
9316 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9317 if (arg != eap->arg)
9318 did_filetype = FALSE;
9319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009323ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
9325#ifdef FEAT_DIGRAPHS
9326 if (*eap->arg != NUL)
9327 putdigraph(eap->arg);
9328 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009329 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330#else
mityu61065042021-07-19 20:07:21 +02009331 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332#endif
9333}
9334
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009335#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9336 void
9337set_no_hlsearch(int flag)
9338{
9339 no_hlsearch = flag;
9340# ifdef FEAT_EVAL
9341 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9342# endif
9343}
9344
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345/*
9346 * ":nohlsearch"
9347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009349ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009351 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009352 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354#endif
9355
9356#ifdef FEAT_CRYPT
9357/*
9358 * ":X": Get crypt key
9359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009361ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009363 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009364 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365}
9366#endif
9367
9368#ifdef FEAT_FOLDING
9369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009370ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371{
9372 if (foldManualAllowed(TRUE))
9373 foldCreate(eap->line1, eap->line2);
9374}
9375
9376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009377ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
9379 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9380 eap->forceit, FALSE);
9381}
9382
9383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009384ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386 linenr_T lnum;
9387
Bram Moolenaar4facea32019-10-12 20:17:40 +02009388# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009389 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009390# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009391
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009392 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9394 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9395 ml_setmarked(lnum);
9396
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009397 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009399 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009400# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009401 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403}
9404#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009406#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009407/*
9408 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9409 * instead of a quickfix command.
9410 */
9411 int
9412is_loclist_cmd(int cmdidx)
9413{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009414 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009415 return FALSE;
9416 return cmdnames[cmdidx].cmd_name[0] == 'l';
9417}
9418#endif
9419
Bram Moolenaar4facea32019-10-12 20:17:40 +02009420#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009421 int
9422get_pressedreturn(void)
9423{
9424 return ex_pressedreturn;
9425}
9426
9427 void
9428set_pressedreturn(int val)
9429{
9430 ex_pressedreturn = val;
9431}
9432#endif